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 7.5KB

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