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.common.plexusbridge.MavenIndexerUtils;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
27 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
28 import org.apache.archiva.configuration.ArchivaConfiguration;
29 import org.apache.archiva.configuration.ConfigurationNames;
30 import org.apache.archiva.configuration.FileTypes;
31 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
32 import org.apache.archiva.consumers.ConsumerException;
33 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
34 import org.apache.archiva.redback.components.registry.Registry;
35 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
36 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
37 import org.apache.maven.index.NexusIndexer;
38 import org.apache.maven.index.context.IndexCreator;
39 import org.apache.maven.index.context.IndexingContext;
40 import org.apache.archiva.redback.components.registry.RegistryListener;
41 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.context.annotation.Scope;
45 import org.springframework.stereotype.Service;
47 import javax.annotation.PostConstruct;
48 import javax.inject.Inject;
49 import javax.inject.Named;
51 import java.util.ArrayList;
52 import java.util.Collections;
53 import java.util.Date;
54 import java.util.List;
57 * Consumer for indexing the repository to provide search and IDE integration features.
59 @Service( "knownRepositoryContentConsumer#index-content" )
61 public class NexusIndexerConsumer
62 extends AbstractMonitoredConsumer
63 implements KnownRepositoryContentConsumer, RegistryListener
65 private Logger log = LoggerFactory.getLogger( getClass() );
67 private ArchivaConfiguration configuration;
69 private FileTypes filetypes;
71 private File managedRepository;
73 private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
75 private IndexingContext indexingContext;
77 private NexusIndexer nexusIndexer;
79 private List<String> includes = new ArrayList<>( 0 );
81 private ManagedRepository repository;
83 private List<? extends IndexCreator> allIndexCreators;
85 private ManagedRepositoryAdmin managedRepositoryAdmin;
88 public NexusIndexerConsumer(
89 @Named( value = "archivaTaskScheduler#indexing" ) ArchivaTaskScheduler<ArtifactIndexingTask> scheduler,
90 @Named( value = "archivaConfiguration" ) ArchivaConfiguration configuration, FileTypes filetypes,
91 PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils,
92 ManagedRepositoryAdmin managedRepositoryAdmin )
93 throws PlexusSisuBridgeException
95 this.configuration = configuration;
96 this.filetypes = filetypes;
97 this.scheduler = scheduler;
98 this.nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
99 this.allIndexCreators = mavenIndexerUtils.getAllIndexCreators();
100 this.managedRepositoryAdmin = managedRepositoryAdmin;
104 public String getDescription()
106 return "Indexes the repository to provide search and IDE integration features";
110 public String getId()
112 return "index-content";
116 public void beginScan( ManagedRepository repository, Date whenGathered )
117 throws ConsumerException
119 this.repository = repository;
120 managedRepository = new File( repository.getLocation() );
124 log.info( "Creating indexing context for repo : {}", repository.getId() );
125 indexingContext = managedRepositoryAdmin.createIndexContext( repository );
127 catch ( RepositoryAdminException e )
129 throw new ConsumerException( e.getMessage(), e );
134 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
135 throws ConsumerException
137 if ( executeOnEntireRepo )
139 beginScan( repository, whenGathered );
143 this.repository = repository;
144 managedRepository = new File( repository.getLocation() );
149 public void processFile( String path )
150 throws ConsumerException
152 File artifactFile = new File( managedRepository, path );
154 ArtifactIndexingTask task =
155 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, getIndexingContext() );
158 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
159 scheduler.queueTask( task );
161 catch ( TaskQueueException e )
163 throw new ConsumerException( e.getMessage(), e );
168 public void processFile( String path, boolean executeOnEntireRepo )
171 if ( executeOnEntireRepo )
177 File artifactFile = new File( managedRepository, path );
179 // specify in indexing task that this is not a repo scan request!
180 ArtifactIndexingTask task =
181 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD,
182 getIndexingContext(), false );
183 // only update index we don't need to scan the full repo here
184 task.setOnlyUpdate( true );
187 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
188 scheduler.queueTask( task );
190 catch ( TaskQueueException e )
192 throw new ConsumerException( e.getMessage(), e );
198 public void completeScan()
200 IndexingContext context = this.indexingContext;
201 if ( context == null )
205 context = getIndexingContext();
207 catch ( ConsumerException e )
209 log.warn( "failed to get an IndexingContext:{}", e.getMessage() );
213 ArtifactIndexingTask task =
214 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
217 log.debug( "Queueing indexing task '{}' to finish indexing.", task );
218 scheduler.queueTask( task );
220 catch ( TaskQueueException e )
222 log.error( "Error queueing task: " + task + ": " + e.getMessage(), e );
227 public void completeScan( boolean executeOnEntireRepo )
229 if ( executeOnEntireRepo )
234 // else, do nothing as the context will be closed when indexing task is executed if not a repo scan request!
238 public List<String> getExcludes()
240 return Collections.emptyList();
244 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
246 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
253 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
258 private void initIncludes()
260 List<String> indexable = filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT );
261 List<String> artifacts = filetypes.getFileTypePatterns( FileTypes.ARTIFACTS );
263 includes = new ArrayList<>( indexable.size() + artifacts.size() );
265 includes.addAll( indexable );
267 includes.addAll( artifacts );
271 public void initialize()
273 configuration.addChangeListener( this );
279 public List<String> getIncludes()
285 private IndexingContext getIndexingContext()
286 throws ConsumerException
289 if ( this.indexingContext == null )
293 indexingContext = managedRepositoryAdmin.createIndexContext( repository );
295 catch ( RepositoryAdminException e )
297 throw new ConsumerException( e.getMessage(), e );
300 return indexingContext;