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
54 * plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
55 * role-hint="repository-scanning"
57 @Service("taskExecutor#repository-scanning")
58 public class ArchivaRepositoryScanningTaskExecutor
59 implements TaskExecutor, Initializable
61 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
67 @Named(value="archivaConfiguration#default")
68 private ArchivaConfiguration archivaConfiguration;
71 * The repository scanner component.
76 private RepositoryScanner repoScanner;
82 private RepositoryContentConsumers consumers;
90 private RepositoryStatisticsManager repositoryStatisticsManager;
93 * TODO: may be different implementations
98 private RepositorySessionFactory repositorySessionFactory;
101 public void initialize()
102 throws InitializationException
104 log.info( "Initialized {}", this.getClass().getName() );
107 @SuppressWarnings( "unchecked" )
108 public void executeTask( Task task )
109 throws TaskExecutionException
112 // TODO: replace this whole class with the prescribed content scanning service/action
113 // - scan repository for artifacts that do not have corresponding metadata or have been updated and
114 // send events for each
115 // - scan metadata for artifacts that have been removed and send events for each
116 // - scan metadata for missing plugin data
117 // - store information so that it can restart upon failure (publish event on the server recovery
118 // queue, remove it on successful completion)
122 RepositoryTask repoTask = (RepositoryTask) task;
124 String repoId = repoTask.getRepositoryId();
125 if ( StringUtils.isBlank( repoId ) )
127 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
130 ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById(
133 // execute consumers on resource file if set
134 if ( repoTask.getResourceFile() != null )
136 log.debug( "Executing task from queue with job name: {}", repoTask );
137 consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
141 log.info( "Executing task from queue with job name: {}", repoTask );
143 // otherwise, execute consumers on whole repository
146 throw new TaskExecutionException(
147 "Unable to execute RepositoryTask with invalid repository id: " + repoId );
150 long sinceWhen = RepositoryScanner.FRESH_SCAN;
151 long previousFileCount = 0;
153 RepositorySession repositorySession = repositorySessionFactory.createSession();
154 MetadataRepository metadataRepository = repositorySession.getRepository();
157 if ( !repoTask.isScanAll() )
159 RepositoryStatistics previousStats = repositoryStatisticsManager.getLastStatistics(
160 metadataRepository, repoId );
161 if ( previousStats != null )
163 sinceWhen = previousStats.getScanStartTime().getTime();
164 previousFileCount = previousStats.getTotalFileCount();
168 RepositoryScanStatistics stats;
171 stats = repoScanner.scan( arepo, sinceWhen );
173 catch ( RepositoryScannerException e )
175 throw new TaskExecutionException( "Repository error when executing repository job.", e );
178 log.info( "Finished first scan: " + stats.toDump( arepo ) );
180 // further statistics will be populated by the following method
181 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
183 log.info( "Gathering repository statistics" );
185 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId, stats.getWhenGathered(),
186 endTime, stats.getTotalFileCount(),
187 stats.getTotalFileCount() - previousFileCount );
188 repositorySession.save();
190 catch ( MetadataRepositoryException e )
192 throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
196 repositorySession.close();
199 // log.info( "Scanning for removed repository content" );
201 // metadataRepository.findAllProjects();
202 // FIXME: do something
204 log.info( "Finished repository task: {}", repoTask );
210 public Task getCurrentTaskInExecution()
215 public ArchivaConfiguration getArchivaConfiguration()
217 return archivaConfiguration;
220 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
222 this.archivaConfiguration = archivaConfiguration;
225 public RepositoryScanner getRepoScanner()
230 public void setRepoScanner( RepositoryScanner repoScanner )
232 this.repoScanner = repoScanner;
235 public RepositoryContentConsumers getConsumers()
240 public void setConsumers( RepositoryContentConsumers consumers )
242 this.consumers = consumers;
245 public RepositorySessionFactory getRepositorySessionFactory()
247 return repositorySessionFactory;
250 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
252 this.repositorySessionFactory = repositorySessionFactory;
255 public RepositoryStatisticsManager getRepositoryStatisticsManager()
257 return repositoryStatisticsManager;
260 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
262 this.repositoryStatisticsManager = repositoryStatisticsManager;