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.model.RepositoryStatistics;
30 import org.apache.archiva.metadata.repository.stats.model.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.archiva.scheduler.repository.model.RepositoryTask;
36 import org.apache.commons.lang.StringUtils;
37 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
38 import org.apache.archiva.redback.components.taskqueue.Task;
39 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutionException;
40 import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutor;
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 private ManagedRepositoryAdmin managedRepositoryAdmin;
64 private RepositoryScanner repoScanner;
67 private RepositoryContentConsumers consumers;
72 private RepositoryStatisticsManager repositoryStatisticsManager;
75 * FIXME: this could be multiple implementations and needs to be configured.
78 private RepositorySessionFactory repositorySessionFactory;
81 public void initialize()
82 throws InitializationException
84 log.info( "Initialized {}", this.getClass().getName() );
87 @SuppressWarnings( "unchecked" )
89 public void executeTask( RepositoryTask task )
90 throws TaskExecutionException
94 // TODO: replace this whole class with the prescribed content scanning service/action
95 // - scan repository for artifacts that do not have corresponding metadata or have been updated and
96 // send events for each
97 // - scan metadata for artifacts that have been removed and send events for each
98 // - scan metadata for missing plugin data
99 // - store information so that it can restart upon failure (publish event on the server recovery
100 // queue, remove it on successful completion)
104 String repoId = task.getRepositoryId();
105 if ( StringUtils.isBlank( repoId ) )
107 throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
110 ManagedRepository arepo = managedRepositoryAdmin.getManagedRepository( repoId );
112 // execute consumers on resource file if set
113 if ( task.getResourceFile() != null )
115 log.debug( "Executing task from queue with job name: {}", task );
116 consumers.executeConsumers( arepo, task.getResourceFile().toPath(), task.isUpdateRelatedArtifacts() );
120 log.info( "Executing task from queue with job name: {}", task );
122 // otherwise, execute consumers on whole repository
125 throw new TaskExecutionException(
126 "Unable to execute RepositoryTask with invalid repository id: " + repoId );
129 long sinceWhen = RepositoryScanner.FRESH_SCAN;
130 long previousFileCount = 0;
132 RepositorySession repositorySession = repositorySessionFactory.createSession();
133 MetadataRepository metadataRepository = repositorySession.getRepository();
136 if ( !task.isScanAll() )
138 RepositoryStatistics previousStats =
139 repositoryStatisticsManager.getLastStatistics( metadataRepository, repoId );
140 if ( previousStats != null )
142 sinceWhen = previousStats.getScanStartTime().getTime();
143 previousFileCount = previousStats.getTotalFileCount();
147 RepositoryScanStatistics stats;
150 stats = repoScanner.scan( arepo, sinceWhen );
152 catch ( RepositoryScannerException e )
154 throw new TaskExecutionException( "Repository error when executing repository job.", e );
157 log.info( "Finished first scan: {}", stats.toDump( arepo ) );
159 // further statistics will be populated by the following method
160 Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
162 log.info( "Gathering repository statistics" );
164 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId,
165 stats.getWhenGathered(), endTime,
166 stats.getTotalFileCount(),
167 stats.getTotalFileCount() - previousFileCount );
168 repositorySession.save();
170 catch ( MetadataRepositoryException e )
172 throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
176 repositorySession.close();
179 // log.info( "Scanning for removed repository content" );
181 // metadataRepository.findAllProjects();
182 // FIXME: do something
184 log.info( "Finished repository task: {}", task );
189 catch ( RepositoryAdminException e )
191 log.error( e.getMessage(), e );
192 throw new TaskExecutionException( e.getMessage(), e );
196 public Task getCurrentTaskInExecution()
201 public RepositoryScanner getRepoScanner()
206 public void setRepoScanner( RepositoryScanner repoScanner )
208 this.repoScanner = repoScanner;
211 public RepositoryContentConsumers getConsumers()
216 public void setConsumers( RepositoryContentConsumers consumers )
218 this.consumers = consumers;
221 public RepositorySessionFactory getRepositorySessionFactory()
223 return repositorySessionFactory;
226 public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
228 this.repositorySessionFactory = repositorySessionFactory;
231 public RepositoryStatisticsManager getRepositoryStatisticsManager()
233 return repositoryStatisticsManager;
236 public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
238 this.repositoryStatisticsManager = repositoryStatisticsManager;
241 public ManagedRepositoryAdmin getManagedRepositoryAdmin()
243 return managedRepositoryAdmin;
246 public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
248 this.managedRepositoryAdmin = managedRepositoryAdmin;