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.

NexusIndexerConsumer.java 9.5KB

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