1 package org.apache.archiva.consumers.lucene;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.ConfigurationNames;
27 import org.apache.archiva.configuration.FileTypes;
28 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
29 import org.apache.archiva.consumers.ConsumerException;
30 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
31 import org.apache.archiva.redback.components.registry.Registry;
32 import org.apache.archiva.redback.components.registry.RegistryListener;
33 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
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;
44 import javax.annotation.PostConstruct;
45 import javax.inject.Inject;
46 import javax.inject.Named;
47 import java.nio.file.Path;
48 import java.nio.file.Paths;
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.Date;
52 import java.util.List;
55 * Consumer for indexing the repository to provide search and IDE integration features.
57 @Service( "knownRepositoryContentConsumer#index-content" )
59 public class NexusIndexerConsumer
60 extends AbstractMonitoredConsumer
61 implements KnownRepositoryContentConsumer, RegistryListener
63 private Logger log = LoggerFactory.getLogger( getClass() );
65 private ArchivaConfiguration configuration;
67 private FileTypes filetypes;
69 private Path managedRepository;
71 private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
73 private IndexingContext indexingContext;
75 private NexusIndexer nexusIndexer;
77 private List<String> includes = new ArrayList<>( 0 );
79 private ManagedRepository repository;
81 private List<? extends IndexCreator> allIndexCreators;
83 private ManagedRepositoryAdmin managedRepositoryAdmin;
86 public NexusIndexerConsumer(
87 @Named( value = "archivaTaskScheduler#indexing" ) ArchivaTaskScheduler<ArtifactIndexingTask> scheduler,
88 @Named( value = "archivaConfiguration" ) ArchivaConfiguration configuration, FileTypes filetypes,
89 List<IndexCreator> indexCreators, ManagedRepositoryAdmin managedRepositoryAdmin, NexusIndexer nexusIndexer )
91 this.configuration = configuration;
92 this.filetypes = filetypes;
93 this.scheduler = scheduler;
94 this.nexusIndexer = nexusIndexer;
95 this.allIndexCreators = indexCreators;
96 this.managedRepositoryAdmin = managedRepositoryAdmin;
100 public String getDescription()
102 return "Indexes the repository to provide search and IDE integration features";
106 public String getId()
108 return "index-content";
112 public void beginScan( ManagedRepository repository, Date whenGathered )
113 throws ConsumerException
115 this.repository = repository;
116 managedRepository = Paths.get( repository.getLocation() );
120 log.info( "Creating indexing context for repo : {}", repository.getId() );
121 indexingContext = managedRepositoryAdmin.createIndexContext( repository );
123 catch ( RepositoryAdminException e )
125 throw new ConsumerException( e.getMessage(), e );
130 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
131 throws ConsumerException
133 if ( executeOnEntireRepo )
135 beginScan( repository, whenGathered );
139 this.repository = repository;
140 managedRepository = Paths.get( repository.getLocation() );
145 public void processFile( String path )
146 throws ConsumerException
148 Path artifactFile = managedRepository.resolve(path);
150 ArtifactIndexingTask task =
151 new ArtifactIndexingTask( repository, artifactFile.toFile(), ArtifactIndexingTask.Action.ADD, getIndexingContext() );
154 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
155 scheduler.queueTask( task );
157 catch ( TaskQueueException e )
159 throw new ConsumerException( e.getMessage(), e );
164 public void processFile( String path, boolean executeOnEntireRepo )
167 if ( executeOnEntireRepo )
173 Path artifactFile = managedRepository.resolve(path);
175 // specify in indexing task that this is not a repo scan request!
176 ArtifactIndexingTask task =
177 new ArtifactIndexingTask( repository, artifactFile.toFile(), ArtifactIndexingTask.Action.ADD,
178 getIndexingContext(), false );
179 // only update index we don't need to scan the full repo here
180 task.setOnlyUpdate( true );
183 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
184 scheduler.queueTask( task );
186 catch ( TaskQueueException e )
188 throw new ConsumerException( e.getMessage(), e );
194 public void completeScan()
196 IndexingContext context = this.indexingContext;
197 if ( context == null )
201 context = getIndexingContext();
203 catch ( ConsumerException e )
205 log.warn( "failed to get an IndexingContext:{}", e.getMessage() );
209 ArtifactIndexingTask task =
210 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
213 log.debug( "Queueing indexing task '{}' to finish indexing.", task );
214 scheduler.queueTask( task );
216 catch ( TaskQueueException e )
218 log.error( "Error queueing task: {}: {}", task, e.getMessage(), e );
223 public void completeScan( boolean executeOnEntireRepo )
225 if ( executeOnEntireRepo )
230 // else, do nothing as the context will be closed when indexing task is executed if not a repo scan request!
234 public List<String> getExcludes()
236 return Collections.emptyList();
240 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
242 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
249 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
254 private void initIncludes()
256 List<String> indexable = filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT );
257 List<String> artifacts = filetypes.getFileTypePatterns( FileTypes.ARTIFACTS );
259 includes = new ArrayList<>( indexable.size() + artifacts.size() );
261 includes.addAll( indexable );
263 includes.addAll( artifacts );
267 public void initialize()
269 configuration.addChangeListener( this );
275 public List<String> getIncludes()
281 private IndexingContext getIndexingContext()
282 throws ConsumerException
285 if ( this.indexingContext == null )
289 indexingContext = managedRepositoryAdmin.createIndexContext( repository );
291 catch ( RepositoryAdminException e )
293 throw new ConsumerException( e.getMessage(), e );
296 return indexingContext;