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.metadata.repository.MetadataRepository;
23 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
24 import org.apache.archiva.metadata.repository.RepositorySession;
25 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
26 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
27 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
28 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
29 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
30 import org.apache.archiva.repository.scanner.RepositoryScanner;
31 import org.apache.archiva.repository.scanner.RepositoryScannerException;
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
34 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
36 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
37 import org.codehaus.plexus.taskqueue.Task;
38 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
39 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.scheduling.TaskScheduler;
43 import org.springframework.stereotype.Service;
45 import javax.annotation.PostConstruct;
46 import javax.inject.Inject;
47 import javax.inject.Named;
48 import java.util.Date;
51 * ArchivaRepositoryScanningTaskExecutor
55 @Service("taskExecutor#repository-scanning")
56 public class ArchivaRepositoryScanningTaskExecutor
57 implements TaskExecutor, Initializable
59 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
65 @Named(value="archivaConfiguration#default")
66 private ArchivaConfiguration archivaConfiguration;
69 * The repository scanner component.
74 private RepositoryScanner repoScanner;
80 private RepositoryContentConsumers consumers;
88 private RepositoryStatisticsManager repositoryStatisticsManager;
91 * TODO: may be different implementations
95 private RepositorySessionFactory repositorySessionFactory;
98 public void initialize()
99 throws InitializationException
101 log.info( "Initialized {}", this.getClass().getName() );
104 @SuppressWarnings( "unchecked" )
105 public void executeTask( Task task )
106 throws TaskExecutionException
109 // TODO: replace this whole class with the prescribed content scanning service/action
110 // - scan repository for artifacts that do not have corresponding metadata or have been updated and
111 // send events for each
112 // - scan metadata for artifacts that have been removed and send events for each
113 // - scan metadata for missing plugin data
114 // - store information so that it can restart upon failure (publish event on the server recovery
115 // queue, remove it on successful completion)
119 RepositoryTask repoTask = (RepositoryTask) task;
121 String repoId = repoTask.getRepositoryId();
122 if ( StringUtils.isBlank( repoId ) )
124 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
127 ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById(
130 // execute consumers on resource file if set
131 if ( repoTask.getResourceFile() != null )
133 log.debug( "Executing task from queue with job name: {}", repoTask );
134 consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
138 log.info( "Executing task from queue with job name: {}", repoTask );
140 // otherwise, execute consumers on whole repository
143 throw new TaskExecutionException(
144 "Unable to execute RepositoryTask with invalid repository id: " + repoId );
147 long sinceWhen = RepositoryScanner.FRESH_SCAN;
148 long previousFileCount = 0;
150 RepositorySession repositorySession = repositorySessionFactory.createSession();
151 MetadataRepository metadataRepository = repositorySession.getRepository();
154 if ( !repoTask.isScanAll() )
156 RepositoryStatistics previousStats = repositoryStatisticsManager.getLastStatistics(
157 metadataRepository, repoId );
158 if ( previousStats != null )
160 sinceWhen = previousStats.getScanStartTime().getTime();
161 previousFileCount = previousStats.getTotalFileCount();
165 RepositoryScanStatistics stats;
168 stats = repoScanner.scan( arepo, sinceWhen );
170 catch ( RepositoryScannerException e )
172 throw new TaskExecutionException( "Repository error when executing repository job.", e );
175 log.info( "Finished first scan: " + stats.toDump( arepo ) );
177 // further statistics will be populated by the following method
178 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
180 log.info( "Gathering repository statistics" );
182 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId, stats.getWhenGathered(),
183 endTime, stats.getTotalFileCount(),
184 stats.getTotalFileCount() - previousFileCount );
185 repositorySession.save();
187 catch ( MetadataRepositoryException e )
189 throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
193 repositorySession.close();
196 // log.info( "Scanning for removed repository content" );
198 // metadataRepository.findAllProjects();
199 // FIXME: do something
201 log.info( "Finished repository task: {}", repoTask );
207 public Task getCurrentTaskInExecution()
212 public ArchivaConfiguration getArchivaConfiguration()
214 return archivaConfiguration;
217 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
219 this.archivaConfiguration = archivaConfiguration;
222 public RepositoryScanner getRepoScanner()
227 public void setRepoScanner( RepositoryScanner repoScanner )
229 this.repoScanner = repoScanner;
232 public RepositoryContentConsumers getConsumers()
237 public void setConsumers( RepositoryContentConsumers consumers )
239 this.consumers = consumers;
242 public RepositorySessionFactory getRepositorySessionFactory()
244 return repositorySessionFactory;
247 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
249 this.repositorySessionFactory = repositorySessionFactory;
252 public RepositoryStatisticsManager getRepositoryStatisticsManager()
254 return repositoryStatisticsManager;
257 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
259 this.repositoryStatisticsManager = repositoryStatisticsManager;