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
96 private RepositorySessionFactory repositorySessionFactory;
99 public void initialize()
100 throws InitializationException
102 log.info( "Initialized {}", this.getClass().getName() );
105 @SuppressWarnings( "unchecked" )
106 public void executeTask( Task task )
107 throws TaskExecutionException
110 // TODO: replace this whole class with the prescribed content scanning service/action
111 // - scan repository for artifacts that do not have corresponding metadata or have been updated and
112 // send events for each
113 // - scan metadata for artifacts that have been removed and send events for each
114 // - scan metadata for missing plugin data
115 // - store information so that it can restart upon failure (publish event on the server recovery
116 // queue, remove it on successful completion)
120 RepositoryTask repoTask = (RepositoryTask) task;
122 String repoId = repoTask.getRepositoryId();
123 if ( StringUtils.isBlank( repoId ) )
125 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
128 ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById(
131 // execute consumers on resource file if set
132 if ( repoTask.getResourceFile() != null )
134 log.debug( "Executing task from queue with job name: {}", repoTask );
135 consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
139 log.info( "Executing task from queue with job name: {}", repoTask );
141 // otherwise, execute consumers on whole repository
144 throw new TaskExecutionException(
145 "Unable to execute RepositoryTask with invalid repository id: " + repoId );
148 long sinceWhen = RepositoryScanner.FRESH_SCAN;
149 long previousFileCount = 0;
151 RepositorySession repositorySession = repositorySessionFactory.createSession();
152 MetadataRepository metadataRepository = repositorySession.getRepository();
155 if ( !repoTask.isScanAll() )
157 RepositoryStatistics previousStats = repositoryStatisticsManager.getLastStatistics(
158 metadataRepository, repoId );
159 if ( previousStats != null )
161 sinceWhen = previousStats.getScanStartTime().getTime();
162 previousFileCount = previousStats.getTotalFileCount();
166 RepositoryScanStatistics stats;
169 stats = repoScanner.scan( arepo, sinceWhen );
171 catch ( RepositoryScannerException e )
173 throw new TaskExecutionException( "Repository error when executing repository job.", e );
176 log.info( "Finished first scan: " + stats.toDump( arepo ) );
178 // further statistics will be populated by the following method
179 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
181 log.info( "Gathering repository statistics" );
183 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId, stats.getWhenGathered(),
184 endTime, stats.getTotalFileCount(),
185 stats.getTotalFileCount() - previousFileCount );
186 repositorySession.save();
188 catch ( MetadataRepositoryException e )
190 throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
194 repositorySession.close();
197 // log.info( "Scanning for removed repository content" );
199 // metadataRepository.findAllProjects();
200 // FIXME: do something
202 log.info( "Finished repository task: {}", repoTask );
208 public Task getCurrentTaskInExecution()
213 public ArchivaConfiguration getArchivaConfiguration()
215 return archivaConfiguration;
218 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
220 this.archivaConfiguration = archivaConfiguration;
223 public RepositoryScanner getRepoScanner()
228 public void setRepoScanner( RepositoryScanner repoScanner )
230 this.repoScanner = repoScanner;
233 public RepositoryContentConsumers getConsumers()
238 public void setConsumers( RepositoryContentConsumers consumers )
240 this.consumers = consumers;
243 public RepositorySessionFactory getRepositorySessionFactory()
245 return repositorySessionFactory;
248 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
250 this.repositorySessionFactory = repositorySessionFactory;
253 public RepositoryStatisticsManager getRepositoryStatisticsManager()
255 return repositoryStatisticsManager;
258 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
260 this.repositoryStatisticsManager = repositoryStatisticsManager;