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.lang.StringUtils;
41 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.stereotype.Service;
46 import javax.annotation.PostConstruct;
47 import javax.inject.Inject;
48 import java.util.Date;
51 * ArchivaRepositoryScanningTaskExecutor
55 @Service( "taskExecutor#repository-scanning" )
56 public class ArchivaRepositoryScanningTaskExecutor
57 implements TaskExecutor<RepositoryTask>
59 private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
62 RepositoryRegistry repositoryRegistry;
65 private ManagedRepositoryAdmin managedRepositoryAdmin;
68 private RepositoryScanner repoScanner;
71 private RepositoryContentConsumers consumers;
76 private RepositoryStatisticsManager repositoryStatisticsManager;
79 * FIXME: this could be multiple implementations and needs to be configured.
82 private RepositorySessionFactory repositorySessionFactory;
85 public void initialize()
86 throws InitializationException
88 log.info( "Initialized {}", this.getClass().getName() );
91 @SuppressWarnings( "unchecked" )
93 public void executeTask( RepositoryTask task )
94 throws TaskExecutionException
98 // TODO: replace this whole class with the prescribed content scanning service/action
99 // - scan repository for artifacts that do not have corresponding metadata or have been updated and
100 // send events for each
101 // - scan metadata for artifacts that have been removed and send events for each
102 // - scan metadata for missing plugin data
103 // - store information so that it can restart upon failure (publish event on the server recovery
104 // queue, remove it on successful completion)
108 String repoId = task.getRepositoryId();
109 if ( StringUtils.isBlank( repoId ) )
111 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
114 ManagedRepository arepo = repositoryRegistry.getManagedRepository( repoId );
116 // execute consumers on resource file if set
117 if ( task.getResourceFile() != null )
119 log.debug( "Executing task from queue with job name: {}", task );
120 consumers.executeConsumers( arepo, task.getResourceFile(), task.isUpdateRelatedArtifacts() );
124 log.info( "Executing task from queue with job name: {}", task );
126 // otherwise, execute consumers on whole repository
129 throw new TaskExecutionException(
130 "Unable to execute RepositoryTask with invalid repository id: " + repoId );
133 long sinceWhen = RepositoryScanner.FRESH_SCAN;
134 long previousFileCount = 0;
136 RepositorySession repositorySession = repositorySessionFactory.createSession();
137 MetadataRepository metadataRepository = repositorySession.getRepository();
140 if ( !task.isScanAll() )
142 RepositoryStatistics previousStats =
143 repositoryStatisticsManager.getLastStatistics( metadataRepository, repoId );
144 if ( previousStats != null )
146 sinceWhen = previousStats.getScanStartTime().getTime();
147 previousFileCount = previousStats.getTotalFileCount();
151 RepositoryScanStatistics stats;
154 stats = repoScanner.scan( arepo, sinceWhen );
156 catch ( RepositoryScannerException e )
158 throw new TaskExecutionException( "Repository error when executing repository job.", e );
161 log.info( "Finished first scan: {}", stats.toDump( arepo ) );
163 // further statistics will be populated by the following method
164 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
166 log.info( "Gathering repository statistics" );
168 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId,
169 stats.getWhenGathered(), endTime,
170 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: {}", task );
193 catch ( RepositoryAdminException e )
195 log.error( e.getMessage(), e );
196 throw new TaskExecutionException( e.getMessage(), e );
200 public Task getCurrentTaskInExecution()
205 public RepositoryScanner getRepoScanner()
210 public void setRepoScanner( RepositoryScanner repoScanner )
212 this.repoScanner = repoScanner;
215 public RepositoryContentConsumers getConsumers()
220 public void setConsumers( RepositoryContentConsumers consumers )
222 this.consumers = consumers;
225 public RepositorySessionFactory getRepositorySessionFactory()
227 return repositorySessionFactory;
230 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
232 this.repositorySessionFactory = repositorySessionFactory;
235 public RepositoryStatisticsManager getRepositoryStatisticsManager()
237 return repositoryStatisticsManager;
240 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
242 this.repositoryStatisticsManager = repositoryStatisticsManager;
245 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
247 return managedRepositoryAdmin;
250 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
252 this.managedRepositoryAdmin = managedRepositoryAdmin;