You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArchivaRepositoryScanningTaskExecutor.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package org.apache.archiva.scheduler.repository;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.admin.model.RepositoryAdminException;
  21. import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
  22. import org.apache.archiva.metadata.repository.MetadataRepository;
  23. import org.apache.archiva.metadata.repository.MetadataRepositoryException;
  24. import org.apache.archiva.metadata.repository.RepositorySession;
  25. import org.apache.archiva.metadata.repository.RepositorySessionFactory;
  26. import org.apache.archiva.metadata.repository.stats.model.RepositoryStatistics;
  27. import org.apache.archiva.metadata.repository.stats.model.RepositoryStatisticsManager;
  28. import org.apache.archiva.redback.components.taskqueue.Task;
  29. import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutionException;
  30. import org.apache.archiva.redback.components.taskqueue.execution.TaskExecutor;
  31. import org.apache.archiva.repository.ManagedRepository;
  32. import org.apache.archiva.repository.RepositoryRegistry;
  33. import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
  34. import org.apache.archiva.repository.scanner.RepositoryScanStatistics;
  35. import org.apache.archiva.repository.scanner.RepositoryScanner;
  36. import org.apache.archiva.repository.scanner.RepositoryScannerException;
  37. import org.apache.archiva.scheduler.repository.model.RepositoryTask;
  38. import org.apache.commons.lang.StringUtils;
  39. import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
  40. import org.slf4j.Logger;
  41. import org.slf4j.LoggerFactory;
  42. import org.springframework.stereotype.Service;
  43. import javax.annotation.PostConstruct;
  44. import javax.inject.Inject;
  45. import java.util.Date;
  46. /**
  47. * ArchivaRepositoryScanningTaskExecutor
  48. *
  49. *
  50. */
  51. @Service( "taskExecutor#repository-scanning" )
  52. public class ArchivaRepositoryScanningTaskExecutor
  53. implements TaskExecutor<RepositoryTask>
  54. {
  55. private Logger log = LoggerFactory.getLogger( ArchivaRepositoryScanningTaskExecutor.class );
  56. @Inject
  57. RepositoryRegistry repositoryRegistry;
  58. @Inject
  59. private ManagedRepositoryAdmin managedRepositoryAdmin;
  60. @Inject
  61. private RepositoryScanner repoScanner;
  62. @Inject
  63. private RepositoryContentConsumers consumers;
  64. private Task task;
  65. @Inject
  66. private RepositoryStatisticsManager repositoryStatisticsManager;
  67. /**
  68. * FIXME: this could be multiple implementations and needs to be configured.
  69. */
  70. @Inject
  71. private RepositorySessionFactory repositorySessionFactory;
  72. @PostConstruct
  73. public void initialize()
  74. throws InitializationException
  75. {
  76. log.info( "Initialized {}", this.getClass().getName() );
  77. }
  78. @SuppressWarnings( "unchecked" )
  79. @Override
  80. public void executeTask( RepositoryTask task )
  81. throws TaskExecutionException
  82. {
  83. try
  84. {
  85. // TODO: replace this whole class with the prescribed content scanning service/action
  86. // - scan repository for artifacts that do not have corresponding metadata or have been updated and
  87. // send events for each
  88. // - scan metadata for artifacts that have been removed and send events for each
  89. // - scan metadata for missing plugin data
  90. // - store information so that it can restart upon failure (publish event on the server recovery
  91. // queue, remove it on successful completion)
  92. this.task = task;
  93. String repoId = task.getRepositoryId();
  94. if ( StringUtils.isBlank( repoId ) )
  95. {
  96. throw new TaskExecutionException( "Unable to execute RepositoryTask with blank repository Id." );
  97. }
  98. ManagedRepository arepo = repositoryRegistry.getManagedRepository( repoId );
  99. // execute consumers on resource file if set
  100. if ( task.getResourceFile() != null )
  101. {
  102. log.debug( "Executing task from queue with job name: {}", task );
  103. consumers.executeConsumers( arepo, task.getResourceFile(), task.isUpdateRelatedArtifacts() );
  104. }
  105. else
  106. {
  107. log.info( "Executing task from queue with job name: {}", task );
  108. // otherwise, execute consumers on whole repository
  109. if ( arepo == null )
  110. {
  111. throw new TaskExecutionException(
  112. "Unable to execute RepositoryTask with invalid repository id: " + repoId );
  113. }
  114. long sinceWhen = RepositoryScanner.FRESH_SCAN;
  115. long previousFileCount = 0;
  116. RepositorySession repositorySession = repositorySessionFactory.createSession();
  117. MetadataRepository metadataRepository = repositorySession.getRepository();
  118. try
  119. {
  120. if ( !task.isScanAll() )
  121. {
  122. RepositoryStatistics previousStats =
  123. repositoryStatisticsManager.getLastStatistics( metadataRepository, repoId );
  124. if ( previousStats != null )
  125. {
  126. sinceWhen = previousStats.getScanStartTime().getTime();
  127. previousFileCount = previousStats.getTotalFileCount();
  128. }
  129. }
  130. RepositoryScanStatistics stats;
  131. try
  132. {
  133. stats = repoScanner.scan( arepo, sinceWhen );
  134. }
  135. catch ( RepositoryScannerException e )
  136. {
  137. throw new TaskExecutionException( "Repository error when executing repository job.", e );
  138. }
  139. log.info( "Finished first scan: {}", stats.toDump( arepo ) );
  140. // further statistics will be populated by the following method
  141. Date endTime = new Date( stats.getWhenGathered().getTime() + stats.getDuration() );
  142. log.info( "Gathering repository statistics" );
  143. repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId,
  144. stats.getWhenGathered(), endTime,
  145. stats.getTotalFileCount(),
  146. stats.getTotalFileCount() - previousFileCount );
  147. repositorySession.save();
  148. }
  149. catch ( MetadataRepositoryException e )
  150. {
  151. throw new TaskExecutionException( "Unable to store updated statistics: " + e.getMessage(), e );
  152. }
  153. finally
  154. {
  155. repositorySession.close();
  156. }
  157. // log.info( "Scanning for removed repository content" );
  158. // metadataRepository.findAllProjects();
  159. // FIXME: do something
  160. log.info( "Finished repository task: {}", task );
  161. this.task = null;
  162. }
  163. }
  164. catch ( RepositoryAdminException e )
  165. {
  166. log.error( e.getMessage(), e );
  167. throw new TaskExecutionException( e.getMessage(), e );
  168. }
  169. }
  170. public Task getCurrentTaskInExecution()
  171. {
  172. return task;
  173. }
  174. public RepositoryScanner getRepoScanner()
  175. {
  176. return repoScanner;
  177. }
  178. public void setRepoScanner( RepositoryScanner repoScanner )
  179. {
  180. this.repoScanner = repoScanner;
  181. }
  182. public RepositoryContentConsumers getConsumers()
  183. {
  184. return consumers;
  185. }
  186. public void setConsumers( RepositoryContentConsumers consumers )
  187. {
  188. this.consumers = consumers;
  189. }
  190. public RepositorySessionFactory getRepositorySessionFactory()
  191. {
  192. return repositorySessionFactory;
  193. }
  194. public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
  195. {
  196. this.repositorySessionFactory = repositorySessionFactory;
  197. }
  198. public RepositoryStatisticsManager getRepositoryStatisticsManager()
  199. {
  200. return repositoryStatisticsManager;
  201. }
  202. public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
  203. {
  204. this.repositoryStatisticsManager = repositoryStatisticsManager;
  205. }
  206. public ManagedRepositoryAdmin getManagedRepositoryAdmin()
  207. {
  208. return managedRepositoryAdmin;
  209. }
  210. public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin )
  211. {
  212. this.managedRepositoryAdmin = managedRepositoryAdmin;
  213. }
  214. }