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.MetadataRepositoryException;
23 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
24 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
25 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
26 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
27 import org.apache.archiva.repository.scanner.RepositoryScanner;
28 import org.apache.archiva.repository.scanner.RepositoryScannerException;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
33 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
34 import org.codehaus.plexus.taskqueue.Task;
35 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
36 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import java.util.Date;
43 * ArchivaRepositoryScanningTaskExecutor
46 * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
47 * role-hint="repository-scanning"
49 public class ArchivaRepositoryScanningTaskExecutor
50 implements TaskExecutor, Initializable
52 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
57 private ArchivaConfiguration archivaConfiguration;
60 * The repository scanner component.
64 private RepositoryScanner repoScanner;
69 private RepositoryContentConsumers consumers;
76 private RepositoryStatisticsManager repositoryStatisticsManager;
78 public void initialize()
79 throws InitializationException
81 log.info( "Initialized " + this.getClass().getName() );
84 @SuppressWarnings( "unchecked" )
85 public void executeTask( Task task )
86 throws TaskExecutionException
89 // TODO: replace this whole class with the prescribed content scanning service/action
90 // - scan repository for artifacts that do not have corresponding metadata or have been updated and
91 // send events for each
92 // - scan metadata for artifacts that have been removed and send events for each
93 // - scan metadata for missing plugin data
94 // - store information so that it can restart upon failure (publish event on the server recovery
95 // queue, remove it on successful completion)
99 RepositoryTask repoTask = (RepositoryTask) task;
101 String repoId = repoTask.getRepositoryId();
102 if ( StringUtils.isBlank( repoId ) )
104 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
107 ManagedRepositoryConfiguration arepo = archivaConfiguration.getConfiguration().findManagedRepositoryById(
110 // execute consumers on resource file if set
111 if ( repoTask.getResourceFile() != null )
113 log.debug( "Executing task from queue with job name: " + repoTask );
114 consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
118 log.info( "Executing task from queue with job name: " + repoTask );
120 // otherwise, execute consumers on whole repository
123 throw new TaskExecutionException(
124 "Unable to execute RepositoryTask with invalid repository id: " + repoId );
127 long sinceWhen = RepositoryScanner.FRESH_SCAN;
128 long previousFileCount = 0;
130 if ( !repoTask.isScanAll() )
132 RepositoryStatistics previousStats;
135 previousStats = repositoryStatisticsManager.getLastStatistics( repoId );
137 catch ( MetadataRepositoryException e )
139 throw new TaskExecutionException( "Unable to get previous statistics: " + e.getMessage(), e );
141 if ( previousStats != null )
143 sinceWhen = previousStats.getScanStartTime().getTime();
144 previousFileCount = previousStats.getTotalFileCount();
148 RepositoryScanStatistics stats;
151 stats = repoScanner.scan( arepo, sinceWhen );
153 catch ( RepositoryScannerException e )
155 throw new TaskExecutionException( "Repository error when executing repository job.", e );
158 log.info( "Finished first scan: " + stats.toDump( arepo ) );
160 // further statistics will be populated by the following method
161 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
164 repositoryStatisticsManager.addStatisticsAfterScan( repoId, stats.getWhenGathered(), endTime,
165 stats.getTotalFileCount(),
166 stats.getTotalFileCount() - previousFileCount );
168 catch ( MetadataRepositoryException e )
170 throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
173 // log.info( "Scanning for removed repository content" );
175 // metadataRepository.findAllProjects();
176 // FIXME: do something
178 log.info( "Finished repository task: " + repoTask );
184 public Task getCurrentTaskInExecution()