1 package org.apache.archiva.scheduler.repository;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
29 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
30 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
31 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
32 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
33 import org.apache.archiva.repository.scanner.RepositoryScanner;
34 import org.apache.archiva.repository.scanner.RepositoryScannerException;
35 import org.apache.archiva.scheduler.repository.model.RepositoryTask;
36 import org.apache.commons.lang.StringUtils;
37 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
38 import org.apache.archiva.redback.components.taskqueue.Task;
39 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutionException;
40 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutor;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.stereotype.Service;
45 import javax.annotation.PostConstruct;
46 import javax.inject.Inject;
47 import java.util.Date;
50 * ArchivaRepositoryScanningTaskExecutor
54 @Service( "taskExecutor#repository-scanning" )
55 public class ArchivaRepositoryScanningTaskExecutor
56 implements TaskExecutor
58 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
64 private ManagedRepositoryAdmin managedRepositoryAdmin;
67 * The repository scanner component.
70 private RepositoryScanner repoScanner;
76 private RepositoryContentConsumers consumers;
84 private RepositoryStatisticsManager repositoryStatisticsManager;
87 * FIXME: this could be multiple implementations and needs to be configured.
90 private RepositorySessionFactory repositorySessionFactory;
93 public void initialize()
94 throws InitializationException
96 log.info( "Initialized {}", this.getClass().getName() );
99 @SuppressWarnings( "unchecked" )
100 public void executeTask( Task task )
101 throws TaskExecutionException
105 // TODO: replace this whole class with the prescribed content scanning service/action
106 // - scan repository for artifacts that do not have corresponding metadata or have been updated and
107 // send events for each
108 // - scan metadata for artifacts that have been removed and send events for each
109 // - scan metadata for missing plugin data
110 // - store information so that it can restart upon failure (publish event on the server recovery
111 // queue, remove it on successful completion)
115 RepositoryTask repoTask = (RepositoryTask) task;
117 String repoId = repoTask.getRepositoryId();
118 if ( StringUtils.isBlank( repoId ) )
120 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
123 ManagedRepository arepo = managedRepositoryAdmin.getManagedRepository( repoId );
125 // execute consumers on resource file if set
126 if ( repoTask.getResourceFile() != null )
128 log.debug( "Executing task from queue with job name: {}", repoTask );
129 consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
133 log.info( "Executing task from queue with job name: {}", repoTask );
135 // otherwise, execute consumers on whole repository
138 throw new TaskExecutionException(
139 "Unable to execute RepositoryTask with invalid repository id: " + repoId );
142 long sinceWhen = RepositoryScanner.FRESH_SCAN;
143 long previousFileCount = 0;
145 RepositorySession repositorySession = repositorySessionFactory.createSession();
146 MetadataRepository metadataRepository = repositorySession.getRepository();
149 if ( !repoTask.isScanAll() )
151 RepositoryStatistics previousStats =
152 repositoryStatisticsManager.getLastStatistics( metadataRepository, repoId );
153 if ( previousStats != null )
155 sinceWhen = previousStats.getScanStartTime().getTime();
156 previousFileCount = previousStats.getTotalFileCount();
160 RepositoryScanStatistics stats;
163 stats = repoScanner.scan( arepo, sinceWhen );
165 catch ( RepositoryScannerException e )
167 throw new TaskExecutionException( "Repository error when executing repository job.", e );
170 log.info( "Finished first scan: {}", stats.toDump( arepo ) );
172 // further statistics will be populated by the following method
173 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
175 log.info( "Gathering repository statistics" );
177 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId,
178 stats.getWhenGathered(), endTime,
179 stats.getTotalFileCount(),
180 stats.getTotalFileCount() - previousFileCount );
181 repositorySession.save();
183 catch ( MetadataRepositoryException e )
185 throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
189 repositorySession.close();
192 // log.info( "Scanning for removed repository content" );
194 // metadataRepository.findAllProjects();
195 // FIXME: do something
197 log.info( "Finished repository task: {}", repoTask );
202 catch ( RepositoryAdminException e )
204 log.error( e.getMessage(), e );
205 throw new TaskExecutionException( e.getMessage(), e );
209 public Task getCurrentTaskInExecution()
214 public RepositoryScanner getRepoScanner()
219 public void setRepoScanner( RepositoryScanner repoScanner )
221 this.repoScanner = repoScanner;
224 public RepositoryContentConsumers getConsumers()
229 public void setConsumers( RepositoryContentConsumers consumers )
231 this.consumers = consumers;
234 public RepositorySessionFactory getRepositorySessionFactory()
236 return repositorySessionFactory;
239 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
241 this.repositorySessionFactory = repositorySessionFactory;
244 public RepositoryStatisticsManager getRepositoryStatisticsManager()
246 return repositoryStatisticsManager;
249 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
251 this.repositoryStatisticsManager = repositoryStatisticsManager;
254 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
256 return managedRepositoryAdmin;
259 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
261 this.managedRepositoryAdmin = managedRepositoryAdmin;