Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

NexusIndexerConsumer.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package org.apache.archiva.consumers.lucene;
  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.common.utils.PathUtil;
  23. import org.apache.archiva.configuration.ArchivaConfiguration;
  24. import org.apache.archiva.configuration.ConfigurationNames;
  25. import org.apache.archiva.configuration.FileTypes;
  26. import org.apache.archiva.consumers.AbstractMonitoredConsumer;
  27. import org.apache.archiva.consumers.ConsumerException;
  28. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  29. import org.apache.archiva.redback.components.registry.Registry;
  30. import org.apache.archiva.redback.components.registry.RegistryListener;
  31. import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
  32. import org.apache.archiva.repository.ManagedRepository;
  33. import org.apache.archiva.repository.RepositoryRegistry;
  34. import org.apache.archiva.scheduler.ArchivaTaskScheduler;
  35. import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
  36. import org.apache.maven.index.NexusIndexer;
  37. import org.apache.maven.index.context.IndexCreator;
  38. import org.apache.maven.index.context.IndexingContext;
  39. import org.slf4j.Logger;
  40. import org.slf4j.LoggerFactory;
  41. import org.springframework.context.annotation.Scope;
  42. import org.springframework.stereotype.Service;
  43. import javax.annotation.PostConstruct;
  44. import javax.inject.Inject;
  45. import javax.inject.Named;
  46. import java.nio.file.Path;
  47. import java.nio.file.Paths;
  48. import java.util.ArrayList;
  49. import java.util.Collections;
  50. import java.util.Date;
  51. import java.util.List;
  52. /**
  53. * Consumer for indexing the repository to provide search and IDE integration features.
  54. */
  55. @Service( "knownRepositoryContentConsumer#index-content" )
  56. @Scope( "prototype" )
  57. public class NexusIndexerConsumer
  58. extends AbstractMonitoredConsumer
  59. implements KnownRepositoryContentConsumer, RegistryListener
  60. {
  61. @Inject
  62. private RepositoryRegistry repositoryRegistry;
  63. private Logger log = LoggerFactory.getLogger( getClass() );
  64. private ArchivaConfiguration configuration;
  65. private FileTypes filetypes;
  66. private Path managedRepository;
  67. private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
  68. private IndexingContext indexingContext;
  69. private NexusIndexer nexusIndexer;
  70. private List<String> includes = new ArrayList<>( 0 );
  71. private ManagedRepository repository;
  72. private List<? extends IndexCreator> allIndexCreators;
  73. private ManagedRepositoryAdmin managedRepositoryAdmin;
  74. @Inject
  75. public NexusIndexerConsumer(
  76. @Named( value = "archivaTaskScheduler#indexing" ) ArchivaTaskScheduler<ArtifactIndexingTask> scheduler,
  77. @Named( value = "archivaConfiguration" ) ArchivaConfiguration configuration, FileTypes filetypes,
  78. List<IndexCreator> indexCreators, ManagedRepositoryAdmin managedRepositoryAdmin, NexusIndexer nexusIndexer )
  79. {
  80. this.configuration = configuration;
  81. this.filetypes = filetypes;
  82. this.scheduler = scheduler;
  83. this.nexusIndexer = nexusIndexer;
  84. this.allIndexCreators = indexCreators;
  85. this.managedRepositoryAdmin = managedRepositoryAdmin;
  86. }
  87. @Override
  88. public String getDescription()
  89. {
  90. return "Indexes the repository to provide search and IDE integration features";
  91. }
  92. @Override
  93. public String getId()
  94. {
  95. return "index-content";
  96. }
  97. @Override
  98. public void beginScan( ManagedRepository repository, Date whenGathered )
  99. throws ConsumerException
  100. {
  101. this.repository = repository;
  102. managedRepository = PathUtil.getPathFromUri( repository.getLocation() );
  103. try
  104. {
  105. log.info( "Creating indexing context for repo : {}", repository.getId() );
  106. indexingContext = managedRepositoryAdmin.createIndexContext( repository );
  107. }
  108. catch ( RepositoryAdminException e )
  109. {
  110. throw new ConsumerException( e.getMessage(), e );
  111. }
  112. }
  113. @Override
  114. public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
  115. throws ConsumerException
  116. {
  117. if ( executeOnEntireRepo )
  118. {
  119. beginScan( repository, whenGathered );
  120. }
  121. else
  122. {
  123. this.repository = repository;
  124. managedRepository = Paths.get( repository.getLocation() );
  125. }
  126. }
  127. @Override
  128. public void processFile( String path )
  129. throws ConsumerException
  130. {
  131. Path artifactFile = managedRepository.resolve(path);
  132. ArtifactIndexingTask task =
  133. new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, getIndexingContext() );
  134. try
  135. {
  136. log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
  137. scheduler.queueTask( task );
  138. }
  139. catch ( TaskQueueException e )
  140. {
  141. throw new ConsumerException( e.getMessage(), e );
  142. }
  143. }
  144. @Override
  145. public void processFile( String path, boolean executeOnEntireRepo )
  146. throws Exception
  147. {
  148. if ( executeOnEntireRepo )
  149. {
  150. processFile( path );
  151. }
  152. else
  153. {
  154. Path artifactFile = managedRepository.resolve(path);
  155. // specify in indexing task that this is not a repo scan request!
  156. ArtifactIndexingTask task =
  157. new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD,
  158. getIndexingContext(), false );
  159. // only update index we don't need to scan the full repo here
  160. task.setOnlyUpdate( true );
  161. try
  162. {
  163. log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
  164. scheduler.queueTask( task );
  165. }
  166. catch ( TaskQueueException e )
  167. {
  168. throw new ConsumerException( e.getMessage(), e );
  169. }
  170. }
  171. }
  172. @Override
  173. public void completeScan()
  174. {
  175. IndexingContext context = this.indexingContext;
  176. if ( context == null )
  177. {
  178. try
  179. {
  180. context = getIndexingContext();
  181. }
  182. catch ( ConsumerException e )
  183. {
  184. log.warn( "failed to get an IndexingContext:{}", e.getMessage() );
  185. return;
  186. }
  187. }
  188. ArtifactIndexingTask task =
  189. new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
  190. try
  191. {
  192. log.debug( "Queueing indexing task '{}' to finish indexing.", task );
  193. scheduler.queueTask( task );
  194. }
  195. catch ( TaskQueueException e )
  196. {
  197. log.error( "Error queueing task: {}: {}", task, e.getMessage(), e );
  198. }
  199. }
  200. @Override
  201. public void completeScan( boolean executeOnEntireRepo )
  202. {
  203. if ( executeOnEntireRepo )
  204. {
  205. completeScan();
  206. }
  207. // else, do nothing as the context will be closed when indexing task is executed if not a repo scan request!
  208. }
  209. @Override
  210. public List<String> getExcludes()
  211. {
  212. return Collections.emptyList();
  213. }
  214. @Override
  215. public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  216. {
  217. if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
  218. {
  219. initIncludes();
  220. }
  221. }
  222. @Override
  223. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  224. {
  225. /* do nothing */
  226. }
  227. private void initIncludes()
  228. {
  229. List<String> indexable = filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT );
  230. List<String> artifacts = filetypes.getFileTypePatterns( FileTypes.ARTIFACTS );
  231. includes = new ArrayList<>( indexable.size() + artifacts.size() );
  232. includes.addAll( indexable );
  233. includes.addAll( artifacts );
  234. }
  235. @PostConstruct
  236. public void initialize()
  237. {
  238. configuration.addChangeListener( this );
  239. initIncludes();
  240. }
  241. @Override
  242. public List<String> getIncludes()
  243. {
  244. return includes;
  245. }
  246. private IndexingContext getIndexingContext()
  247. throws ConsumerException
  248. {
  249. if ( this.indexingContext == null )
  250. {
  251. try
  252. {
  253. indexingContext = managedRepositoryAdmin.createIndexContext( repository );
  254. }
  255. catch ( RepositoryAdminException e )
  256. {
  257. throw new ConsumerException( e.getMessage(), e );
  258. }
  259. }
  260. return indexingContext;
  261. }
  262. }