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;
43 import java.util.Date;
46 * ArchivaRepositoryScanningTaskExecutor
49 * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
50 * role-hint="repository-scanning"
52 public class ArchivaRepositoryScanningTaskExecutor
53 implements TaskExecutor, Initializable
55 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
60 private ArchivaConfiguration archivaConfiguration;
63 * The repository scanner component.
67 private RepositoryScanner repoScanner;
72 private RepositoryContentConsumers consumers;
79 private RepositoryStatisticsManager repositoryStatisticsManager;
82 * TODO: may be different implementations
86 private RepositorySessionFactory repositorySessionFactory;
88 public void initialize()
89 throws InitializationException
91 log.info( "Initialized " + this.getClass().getName() );
94 @SuppressWarnings( "unchecked" )
95 public void executeTask( Task task )
96 throws TaskExecutionException
99 // TODO: replace this whole class with the prescribed content scanning service/action
100 // - scan repository for artifacts that do not have corresponding metadata or have been updated and
101 // send events for each
102 // - scan metadata for artifacts that have been removed and send events for each
103 // - scan metadata for missing plugin data
104 // - store information so that it can restart upon failure (publish event on the server recovery
105 // queue, remove it on successful completion)
109 RepositoryTask repoTask = (RepositoryTask) task;
111 String repoId = repoTask.getRepositoryId();
112 if ( StringUtils.isBlank( repoId ) )
114 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
117 ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById(
120 // execute consumers on resource file if set
121 if ( repoTask.getResourceFile() != null )
123 log.debug( "Executing task from queue with job name: " + repoTask );
124 consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
128 log.info( "Executing task from queue with job name: " + repoTask );
130 // otherwise, execute consumers on whole repository
133 throw new TaskExecutionException(
134 "Unable to execute RepositoryTask with invalid repository id: " + repoId );
137 long sinceWhen = RepositoryScanner.FRESH_SCAN;
138 long previousFileCount = 0;
140 RepositorySession repositorySession = repositorySessionFactory.createSession();
141 MetadataRepository metadataRepository = repositorySession.getRepository();
144 if ( !repoTask.isScanAll() )
146 RepositoryStatistics previousStats = repositoryStatisticsManager.getLastStatistics(
147 metadataRepository, repoId );
148 if ( previousStats != null )
150 sinceWhen = previousStats.getScanStartTime().getTime();
151 previousFileCount = previousStats.getTotalFileCount();
155 RepositoryScanStatistics stats;
158 stats = repoScanner.scan( arepo, sinceWhen );
160 catch ( RepositoryScannerException e )
162 throw new TaskExecutionException( "Repository error when executing repository job.", e );
165 log.info( "Finished first scan: " + stats.toDump( arepo ) );
167 // further statistics will be populated by the following method
168 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
169 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId, stats.getWhenGathered(),
170 endTime, stats.getTotalFileCount(),
171 stats.getTotalFileCount() - previousFileCount );
172 repositorySession.save();
174 catch ( MetadataRepositoryException e )
176 throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
180 repositorySession.close();
183 // log.info( "Scanning for removed repository content" );
185 // metadataRepository.findAllProjects();
186 // FIXME: do something
188 log.info( "Finished repository task: " + repoTask );
194 public Task getCurrentTaskInExecution()