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.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
29 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
30 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
31 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
32 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
33 import org.apache.archiva.repository.scanner.RepositoryScanner;
34 import org.apache.archiva.repository.scanner.RepositoryScannerException;
35 import org.apache.commons.lang.StringUtils;
36 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
37 import org.apache.archiva.redback.components.taskqueue.Task;
38 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutionException;
39 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutor;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Service;
44 import javax.annotation.PostConstruct;
45 import javax.inject.Inject;
46 import java.util.Date;
49 * ArchivaRepositoryScanningTaskExecutor
53 @Service( "taskExecutor#repository-scanning" )
54 public class ArchivaRepositoryScanningTaskExecutor
55 implements TaskExecutor
57 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
63 private ManagedRepositoryAdmin managedRepositoryAdmin;
66 * The repository scanner component.
69 private RepositoryScanner repoScanner;
75 private RepositoryContentConsumers consumers;
83 private RepositoryStatisticsManager repositoryStatisticsManager;
86 * TODO: may be different implementations
89 private RepositorySessionFactory repositorySessionFactory;
92 public void initialize()
93 throws InitializationException
95 log.info( "Initialized {}", this.getClass().getName() );
98 @SuppressWarnings( "unchecked" )
99 public void executeTask( Task task )
100 throws TaskExecutionException
104 // TODO: replace this whole class with the prescribed content scanning service/action
105 // - scan repository for artifacts that do not have corresponding metadata or have been updated and
106 // send events for each
107 // - scan metadata for artifacts that have been removed and send events for each
108 // - scan metadata for missing plugin data
109 // - store information so that it can restart upon failure (publish event on the server recovery
110 // queue, remove it on successful completion)
114 RepositoryTask repoTask = (RepositoryTask) task;
116 String repoId = repoTask.getRepositoryId();
117 if ( StringUtils.isBlank( repoId ) )
119 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
122 ManagedRepository arepo = managedRepositoryAdmin.getManagedRepository( repoId );
124 // execute consumers on resource file if set
125 if ( repoTask.getResourceFile() != null )
127 log.debug( "Executing task from queue with job name: {}", repoTask );
128 consumers.executeConsumers( arepo, repoTask.getResourceFile(), repoTask.isUpdateRelatedArtifacts() );
132 log.info( "Executing task from queue with job name: {}", repoTask );
134 // otherwise, execute consumers on whole repository
137 throw new TaskExecutionException(
138 "Unable to execute RepositoryTask with invalid repository id: " + repoId );
141 long sinceWhen = RepositoryScanner.FRESH_SCAN;
142 long previousFileCount = 0;
144 RepositorySession repositorySession = repositorySessionFactory.createSession();
145 MetadataRepository metadataRepository = repositorySession.getRepository();
148 if ( !repoTask.isScanAll() )
150 RepositoryStatistics previousStats =
151 repositoryStatisticsManager.getLastStatistics( metadataRepository, repoId );
152 if ( previousStats != null )
154 sinceWhen = previousStats.getScanStartTime().getTime();
155 previousFileCount = previousStats.getTotalFileCount();
159 RepositoryScanStatistics stats;
162 stats = repoScanner.scan( arepo, sinceWhen );
164 catch ( RepositoryScannerException e )
166 throw new TaskExecutionException( "Repository error when executing repository job.", e );
169 log.info( "Finished first scan: {}", stats.toDump( arepo ) );
171 // further statistics will be populated by the following method
172 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
174 log.info( "Gathering repository statistics" );
176 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId,
177 stats.getWhenGathered(), endTime,
178 stats.getTotalFileCount(),
179 stats.getTotalFileCount() - previousFileCount );
180 repositorySession.save();
182 catch ( MetadataRepositoryException e )
184 throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
188 repositorySession.close();
191 // log.info( "Scanning for removed repository content" );
193 // metadataRepository.findAllProjects();
194 // FIXME: do something
196 log.info( "Finished repository task: {}", repoTask );
201 catch ( RepositoryAdminException e )
203 log.error( e.getMessage(), e );
204 throw new TaskExecutionException( e.getMessage(), e );
208 public Task getCurrentTaskInExecution()
213 public RepositoryScanner getRepoScanner()
218 public void setRepoScanner( RepositoryScanner repoScanner )
220 this.repoScanner = repoScanner;
223 public RepositoryContentConsumers getConsumers()
228 public void setConsumers( RepositoryContentConsumers consumers )
230 this.consumers = consumers;
233 public RepositorySessionFactory getRepositorySessionFactory()
235 return repositorySessionFactory;
238 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
240 this.repositorySessionFactory = repositorySessionFactory;
243 public RepositoryStatisticsManager getRepositoryStatisticsManager()
245 return repositoryStatisticsManager;
248 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
250 this.repositoryStatisticsManager = repositoryStatisticsManager;
253 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
255 return managedRepositoryAdmin;
258 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
260 this.managedRepositoryAdmin = managedRepositoryAdmin;