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 java.util.Date;
24 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
25 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
26 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
27 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
28 import org.apache.archiva.repository.scanner.RepositoryScanner;
29 import org.apache.archiva.repository.scanner.RepositoryScannerException;
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
34 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
35 import org.codehaus.plexus.taskqueue.Task;
36 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
37 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * ArchivaRepositoryScanningTaskExecutor
45 * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor"
46 * role-hint="repository-scanning"
48 public class ArchivaRepositoryScanningTaskExecutor
49 implements TaskExecutor, Initializable
51 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
56 private ArchivaConfiguration archivaConfiguration;
59 * The repository scanner component.
63 private RepositoryScanner repoScanner;
68 private RepositoryContentConsumers consumers;
75 private RepositoryStatisticsManager repositoryStatisticsManager;
77 public void initialize()
78 throws InitializationException
80 log.info( "Initialized " + this.getClass().getName() );
83 @SuppressWarnings("unchecked")
84 public void executeTask( Task task )
85 throws TaskExecutionException
88 // TODO: replace this whole class with the prescribed content scanning service/action
89 // - scan repository for artifacts that do not have corresponding metadata or have been updated and
90 // send events for each
91 // - scan metadata for artifacts that have been removed and send events for each
92 // - scan metadata for missing plugin data
93 // - store information so that it can restart upon failure (publish event on the server recovery
94 // queue, remove it on successful completion)
98 RepositoryTask repoTask = (RepositoryTask) task;
100 String repoId = repoTask.getRepositoryId();
101 if ( StringUtils.isBlank( repoId ) )
103 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
106 ManagedRepositoryConfiguration arepo =
107 archivaConfiguration.getConfiguration().findManagedRepositoryById( repoId );
109 // execute consumers on resource file if set
110 if ( repoTask.getResourceFile() != null )
112 log.debug( "Executing task from queue with job name: " + repoTask );
113 consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
117 log.info( "Executing task from queue with job name: " + repoTask );
119 // otherwise, execute consumers on whole repository
124 throw new TaskExecutionException(
125 "Unable to execute RepositoryTask with invalid repository id: " + repoId );
128 long sinceWhen = RepositoryScanner.FRESH_SCAN;
129 long previousFileCount = 0;
131 if ( !repoTask.isScanAll() )
133 RepositoryStatistics previousStats = repositoryStatisticsManager.getLastStatistics( repoId );
134 if ( previousStats != null )
136 sinceWhen = previousStats.getScanStartTime().getTime();
137 previousFileCount = previousStats.getTotalFileCount();
141 RepositoryScanStatistics stats = repoScanner.scan( arepo, sinceWhen );
143 log.info( "Finished first scan: " + stats.toDump( arepo ) );
145 RepositoryStatistics repositoryStatistics = new RepositoryStatistics();
146 repositoryStatistics.setScanStartTime( stats.getWhenGathered() );
147 repositoryStatistics.setScanEndTime(
148 new Date( stats.getWhenGathered().getTime() + stats.getDuration() ) );
149 repositoryStatistics.setTotalFileCount( stats.getTotalFileCount() );
150 repositoryStatistics.setNewFileCount( stats.getTotalFileCount() - previousFileCount );
151 // further statistics will be populated by the following method
152 repositoryStatisticsManager.addStatisticsAfterScan( repoId, repositoryStatistics );
154 // log.info( "Scanning for removed repository content" );
156 // metadataRepository.findAllProjects();
157 // FIXME: do something
159 log.info( "Finished repository task: " + repoTask );
163 catch ( RepositoryScannerException e )
165 throw new TaskExecutionException( "Repository error when executing repository job.", e );
170 public Task getCurrentTaskInExecution()