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.managed.ManagedRepositoryAdmin;
24 import org.apache.archiva.metadata.repository.MetadataRepository;
25 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
26 import org.apache.archiva.metadata.repository.RepositorySession;
27 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
28 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatistics;
29 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatisticsManager;
30 import org.apache.archiva.redback.components.taskqueue.Task;
31 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutionException;
32 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutor;
33 import org.apache.archiva.repository.ManagedRepository;
34 import org.apache.archiva.repository.RepositoryRegistry;
35 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
36 import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
37 import org.apache.archiva.repository.scanner.RepositoryScanner;
38 import org.apache.archiva.repository.scanner.RepositoryScannerException;
39 import org.apache.archiva.scheduler.repository.model.RepositoryTask;
40 import org.apache.commons.lang3.StringUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.stereotype.Service;
45 import javax.annotation.PostConstruct;
46 import javax.inject.Inject;
47 import java.util.Date;
50 * ArchivaRepositoryScanningTaskExecutor
54 @Service( "taskExecutor#repository-scanning" )
55 public class ArchivaRepositoryScanningTaskExecutor
56 implements TaskExecutor<RepositoryTask>
58 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
61 RepositoryRegistry repositoryRegistry;
64 private ManagedRepositoryAdmin managedRepositoryAdmin;
67 private RepositoryScanner repoScanner;
70 private RepositoryContentConsumers consumers;
75 private RepositoryStatisticsManager repositoryStatisticsManager;
78 * FIXME: this could be multiple implementations and needs to be configured.
81 private RepositorySessionFactory repositorySessionFactory;
84 public void initialize()
86 log.info( "Initialized {}", this.getClass().getName() );
89 @SuppressWarnings( "unchecked" )
91 public void executeTask( RepositoryTask task )
92 throws TaskExecutionException
96 // TODO: replace this whole class with the prescribed content scanning service/action
97 // - scan repository for artifacts that do not have corresponding metadata or have been updated and
98 // send events for each
99 // - scan metadata for artifacts that have been removed and send events for each
100 // - scan metadata for missing plugin data
101 // - store information so that it can restart upon failure (publish event on the server recovery
102 // queue, remove it on successful completion)
106 String repoId = task.getRepositoryId();
107 if ( StringUtils.isBlank( repoId ) )
109 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
112 ManagedRepository arepo = repositoryRegistry.getManagedRepository( repoId );
114 // execute consumers on resource file if set
115 if ( task.getResourceFile() != null )
117 log.debug( "Executing task from queue with job name: {}", task );
118 if (task.getResourceFile().isFileBased())
120 consumers.executeConsumers( arepo, task.getResourceFile( ).getFilePath(), task.isUpdateRelatedArtifacts( ) );
125 log.info( "Executing task from queue with job name: {}", task );
127 // otherwise, execute consumers on whole repository
130 throw new TaskExecutionException(
131 "Unable to execute RepositoryTask with invalid repository id: " + repoId );
134 long sinceWhen = RepositoryScanner.FRESH_SCAN;
135 long previousFileCount = 0;
137 RepositorySession repositorySession = repositorySessionFactory.createSession();
138 MetadataRepository metadataRepository = repositorySession.getRepository();
141 if ( !task.isScanAll() )
143 RepositoryStatistics previousStats =
144 repositoryStatisticsManager.getLastStatistics( repoId );
145 if ( previousStats != null )
147 sinceWhen = previousStats.getScanStartTime().getTime();
148 previousFileCount = previousStats.getTotalFileCount();
152 RepositoryScanStatistics stats;
155 stats = repoScanner.scan( arepo, sinceWhen );
157 catch ( RepositoryScannerException e )
159 throw new TaskExecutionException( "Repository error when executing repository job.", e );
162 log.info( "Finished first scan: {}", stats.toDump( arepo ) );
164 // further statistics will be populated by the following method
165 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
167 log.info( "Gathering repository statistics" );
169 repositoryStatisticsManager.addStatisticsAfterScan( repoId,
170 stats.getWhenGathered(), endTime,
171 stats.getTotalFileCount(),
172 stats.getTotalFileCount() - previousFileCount );
173 repositorySession.save();
175 catch ( MetadataRepositoryException e )
177 throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
179 catch ( org.apache.archiva.metadata.repository.MetadataSessionException e )
181 e.printStackTrace( );
185 repositorySession.close();
188 // log.info( "Scanning for removed repository content" );
190 // metadataRepository.findAllProjects();
191 // FIXME: do something
193 log.info( "Finished repository task: {}", task );
198 catch ( RepositoryAdminException e )
200 log.error( e.getMessage(), e );
201 throw new TaskExecutionException( e.getMessage(), e );
203 catch ( MetadataRepositoryException e )
205 e.printStackTrace( );
209 public Task getCurrentTaskInExecution()
214 public RepositoryScanner getRepoScanner()
219 public void setRepoScanner( RepositoryScanner repoScanner )
221 this.repoScanner = repoScanner;
224 public RepositoryContentConsumers getConsumers()
229 public void setConsumers( RepositoryContentConsumers consumers )
231 this.consumers = consumers;
234 public RepositorySessionFactory getRepositorySessionFactory()
236 return repositorySessionFactory;
239 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
241 this.repositorySessionFactory = repositorySessionFactory;
244 public RepositoryStatisticsManager getRepositoryStatisticsManager()
246 return repositoryStatisticsManager;
249 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
251 this.repositoryStatisticsManager = repositoryStatisticsManager;
254 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
256 return managedRepositoryAdmin;
259 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
261 this.managedRepositoryAdmin = managedRepositoryAdmin;