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.

RepositoryContentConsumers.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package org.apache.archiva.repository.scanner;
  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.admin.ArchivaAdministration;
  22. import org.apache.archiva.admin.model.beans.ManagedRepository;
  23. import org.apache.archiva.common.utils.BaseFile;
  24. import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
  25. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  26. import org.apache.archiva.consumers.functors.ConsumerWantsFilePredicate;
  27. import org.apache.archiva.repository.scanner.functors.ConsumerProcessFileClosure;
  28. import org.apache.archiva.repository.scanner.functors.TriggerBeginScanClosure;
  29. import org.apache.archiva.repository.scanner.functors.TriggerScanCompletedClosure;
  30. import org.apache.commons.collections.Closure;
  31. import org.apache.commons.collections.CollectionUtils;
  32. import org.apache.commons.collections.functors.IfClosure;
  33. import org.springframework.beans.BeansException;
  34. import org.springframework.context.ApplicationContext;
  35. import org.springframework.context.ApplicationContextAware;
  36. import org.springframework.stereotype.Service;
  37. import javax.inject.Inject;
  38. import java.io.File;
  39. import java.util.ArrayList;
  40. import java.util.Date;
  41. import java.util.HashMap;
  42. import java.util.List;
  43. import java.util.Map;
  44. /**
  45. * RepositoryContentConsumerUtil
  46. */
  47. @Service ("repositoryContentConsumers")
  48. public class RepositoryContentConsumers
  49. implements ApplicationContextAware
  50. {
  51. @Inject
  52. private ApplicationContext applicationContext;
  53. private ArchivaAdministration archivaAdministration;
  54. private List<KnownRepositoryContentConsumer> selectedKnownConsumers;
  55. private List<InvalidRepositoryContentConsumer> selectedInvalidConsumers;
  56. @Inject
  57. public RepositoryContentConsumers( ArchivaAdministration archivaAdministration )
  58. {
  59. this.archivaAdministration = archivaAdministration;
  60. }
  61. public void setApplicationContext( ApplicationContext applicationContext )
  62. throws BeansException
  63. {
  64. this.applicationContext = applicationContext;
  65. }
  66. /**
  67. * <p>
  68. * Get the list of Ids associated with those {@link KnownRepositoryContentConsumer} that have
  69. * been selected in the configuration to execute.
  70. * </p>
  71. * <p>
  72. * NOTE: This list can be larger and contain entries that might not exist or be available
  73. * in the classpath, or as a component.
  74. * </p>
  75. *
  76. * @return the list of consumer ids that have been selected by the configuration.
  77. */
  78. public List<String> getSelectedKnownConsumerIds()
  79. throws RepositoryAdminException
  80. {
  81. return archivaAdministration.getKnownContentConsumers();
  82. }
  83. /**
  84. * <p>
  85. * Get the list of Ids associated with those {@link InvalidRepositoryContentConsumer} that have
  86. * been selected in the configuration to execute.
  87. * </p>
  88. * <p>
  89. * NOTE: This list can be larger and contain entries that might not exist or be available
  90. * in the classpath, or as a component.
  91. * </p>
  92. *
  93. * @return the list of consumer ids that have been selected by the configuration.
  94. */
  95. public List<String> getSelectedInvalidConsumerIds()
  96. throws RepositoryAdminException
  97. {
  98. return archivaAdministration.getInvalidContentConsumers();
  99. }
  100. /**
  101. * Get the map of {@link String} ids to {@link KnownRepositoryContentConsumer} implementations,
  102. * for those consumers that have been selected according to the active configuration.
  103. *
  104. * @return the map of String ids to {@link KnownRepositoryContentConsumer} objects.
  105. */
  106. public Map<String, KnownRepositoryContentConsumer> getSelectedKnownConsumersMap()
  107. throws RepositoryAdminException
  108. {
  109. Map<String, KnownRepositoryContentConsumer> consumerMap = new HashMap<String, KnownRepositoryContentConsumer>();
  110. for ( KnownRepositoryContentConsumer consumer : getSelectedKnownConsumers() )
  111. {
  112. consumerMap.put( consumer.getId(), consumer );
  113. }
  114. return consumerMap;
  115. }
  116. /**
  117. * Get the map of {@link String} ids to {@link InvalidRepositoryContentConsumer} implementations,
  118. * for those consumers that have been selected according to the active configuration.
  119. *
  120. * @return the map of String ids to {@link InvalidRepositoryContentConsumer} objects.
  121. */
  122. public Map<String, InvalidRepositoryContentConsumer> getSelectedInvalidConsumersMap()
  123. throws RepositoryAdminException
  124. {
  125. Map<String, InvalidRepositoryContentConsumer> consumerMap =
  126. new HashMap<String, InvalidRepositoryContentConsumer>();
  127. for ( InvalidRepositoryContentConsumer consumer : getSelectedInvalidConsumers() )
  128. {
  129. consumerMap.put( consumer.getId(), consumer );
  130. }
  131. return consumerMap;
  132. }
  133. /**
  134. * Get the list of {@link KnownRepositoryContentConsumer} objects that are
  135. * selected according to the active configuration.
  136. *
  137. * @return the list of {@link KnownRepositoryContentConsumer} that have been selected
  138. * by the active configuration.
  139. */
  140. public synchronized List<KnownRepositoryContentConsumer> getSelectedKnownConsumers()
  141. throws RepositoryAdminException
  142. {
  143. // FIXME only for testing
  144. if ( selectedKnownConsumers != null )
  145. {
  146. return selectedKnownConsumers;
  147. }
  148. List<KnownRepositoryContentConsumer> ret = new ArrayList<>();
  149. List<String> knownSelected = getSelectedKnownConsumerIds();
  150. for ( KnownRepositoryContentConsumer consumer : getAvailableKnownConsumers() )
  151. {
  152. if ( knownSelected.contains( consumer.getId() ) || consumer.isPermanent() )
  153. {
  154. ret.add( consumer );
  155. }
  156. }
  157. return ret;
  158. }
  159. /**
  160. * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
  161. * selected according to the active configuration.
  162. *
  163. * @return the list of {@link InvalidRepositoryContentConsumer} that have been selected
  164. * by the active configuration.
  165. */
  166. public synchronized List<InvalidRepositoryContentConsumer> getSelectedInvalidConsumers()
  167. throws RepositoryAdminException
  168. {
  169. // FIXME only for testing
  170. if ( selectedInvalidConsumers != null )
  171. {
  172. return selectedInvalidConsumers;
  173. }
  174. List<InvalidRepositoryContentConsumer> ret = new ArrayList<>();
  175. List<String> invalidSelected = getSelectedInvalidConsumerIds();
  176. for ( InvalidRepositoryContentConsumer consumer : getAvailableInvalidConsumers() )
  177. {
  178. if ( invalidSelected.contains( consumer.getId() ) || consumer.isPermanent() )
  179. {
  180. ret.add( consumer );
  181. }
  182. }
  183. return ret;
  184. }
  185. /**
  186. * Get the list of {@link KnownRepositoryContentConsumer} objects that are
  187. * available and present in the classpath and as components in the IoC.
  188. *
  189. * @return the list of all available {@link KnownRepositoryContentConsumer} present in the classpath
  190. * and as a component in the IoC.
  191. */
  192. public List<KnownRepositoryContentConsumer> getAvailableKnownConsumers()
  193. {
  194. return new ArrayList<>(
  195. applicationContext.getBeansOfType( KnownRepositoryContentConsumer.class ).values() );
  196. }
  197. /**
  198. * Get the list of {@link InvalidRepositoryContentConsumer} objects that are
  199. * available and present in the classpath and as components in the IoC.
  200. *
  201. * @return the list of all available {@link InvalidRepositoryContentConsumer} present in the classpath
  202. * and as a component in the IoC.
  203. */
  204. public List<InvalidRepositoryContentConsumer> getAvailableInvalidConsumers()
  205. {
  206. return new ArrayList<>(
  207. applicationContext.getBeansOfType( InvalidRepositoryContentConsumer.class ).values() );
  208. }
  209. /**
  210. * A convienence method to execute all of the active selected consumers for a
  211. * particular arbitrary file.
  212. * NOTE: Make sure that there is no repository scanning task executing before invoking this so as to prevent
  213. * the index writer/reader of the current index-content consumer executing from getting closed. For an example,
  214. * see ArchivaDavResource#executeConsumers( File ).
  215. *
  216. * @param repository the repository configuration to use.
  217. * @param localFile the local file to execute the consumers against.
  218. * @param updateRelatedArtifacts TODO
  219. */
  220. public void executeConsumers( ManagedRepository repository, File localFile, boolean updateRelatedArtifacts )
  221. throws RepositoryAdminException
  222. {
  223. // Run the repository consumers
  224. try
  225. {
  226. Closure triggerBeginScan = new TriggerBeginScanClosure( repository, getStartTime(), false );
  227. List<KnownRepositoryContentConsumer> selectedKnownConsumers = getSelectedKnownConsumers();
  228. // MRM-1212/MRM-1197
  229. // - do not create missing/fix invalid checksums and update metadata when deploying from webdav since these are uploaded by maven
  230. if ( !updateRelatedArtifacts )
  231. {
  232. List<KnownRepositoryContentConsumer> clone = new ArrayList<>();
  233. clone.addAll( selectedKnownConsumers );
  234. for ( KnownRepositoryContentConsumer consumer : clone )
  235. {
  236. if ( consumer.getId().equals( "create-missing-checksums" ) || consumer.getId().equals(
  237. "metadata-updater" ) )
  238. {
  239. selectedKnownConsumers.remove( consumer );
  240. }
  241. }
  242. }
  243. List<InvalidRepositoryContentConsumer> selectedInvalidConsumers = getSelectedInvalidConsumers();
  244. CollectionUtils.forAllDo( selectedKnownConsumers, triggerBeginScan );
  245. CollectionUtils.forAllDo( selectedInvalidConsumers, triggerBeginScan );
  246. // yuck. In case you can't read this, it says
  247. // "process the file if the consumer has it in the includes list, and not in the excludes list"
  248. BaseFile baseFile = new BaseFile( repository.getLocation(), localFile );
  249. ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate( repository );
  250. predicate.setBasefile( baseFile );
  251. predicate.setCaseSensitive( false );
  252. ConsumerProcessFileClosure closure = new ConsumerProcessFileClosure();
  253. closure.setBasefile( baseFile );
  254. closure.setExecuteOnEntireRepo( false );
  255. Closure processIfWanted = IfClosure.getInstance( predicate, closure );
  256. CollectionUtils.forAllDo( selectedKnownConsumers, processIfWanted );
  257. if ( predicate.getWantedFileCount() <= 0 )
  258. {
  259. // Nothing known processed this file. It is invalid!
  260. CollectionUtils.forAllDo( selectedInvalidConsumers, closure );
  261. }
  262. TriggerScanCompletedClosure scanCompletedClosure = new TriggerScanCompletedClosure( repository, false );
  263. CollectionUtils.forAllDo( selectedKnownConsumers, scanCompletedClosure );
  264. }
  265. finally
  266. {
  267. /* TODO: This is never called by the repository scanner instance, so not calling here either - but it probably should be?
  268. CollectionUtils.forAllDo( availableKnownConsumers, triggerCompleteScan );
  269. CollectionUtils.forAllDo( availableInvalidConsumers, triggerCompleteScan );
  270. */
  271. }
  272. }
  273. public void setSelectedKnownConsumers( List<KnownRepositoryContentConsumer> selectedKnownConsumers )
  274. {
  275. this.selectedKnownConsumers = selectedKnownConsumers;
  276. }
  277. public void setSelectedInvalidConsumers( List<InvalidRepositoryContentConsumer> selectedInvalidConsumers )
  278. {
  279. this.selectedInvalidConsumers = selectedInvalidConsumers;
  280. }
  281. protected Date getStartTime()
  282. {
  283. return new Date( System.currentTimeMillis() );
  284. }
  285. public void setArchivaAdministration( ArchivaAdministration archivaAdministration )
  286. {
  287. this.archivaAdministration = archivaAdministration;
  288. }
  289. }