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;
48 import java.util.ArrayList;
49 import java.util.Collections;
50 import java.util.Date;
51 import java.util.List;
54 * Consumer for indexing the repository to provide search and IDE integration features.
56 @Service( "knownRepositoryContentConsumer#index-content" )
58 public class NexusIndexerConsumer
59 extends AbstractMonitoredConsumer
60 implements KnownRepositoryContentConsumer, RegistryListener
62 private Logger log = LoggerFactory.getLogger( getClass() );
64 private ArchivaConfiguration configuration;
66 private FileTypes filetypes;
68 private File managedRepository;
70 private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
72 private IndexingContext indexingContext;
74 private NexusIndexer nexusIndexer;
76 private List<String> includes = new ArrayList<>( 0 );
78 private ManagedRepository repository;
80 private List<? extends IndexCreator> allIndexCreators;
82 private ManagedRepositoryAdmin managedRepositoryAdmin;
85 public NexusIndexerConsumer(
86 @Named( value = "archivaTaskScheduler#indexing" ) ArchivaTaskScheduler<ArtifactIndexingTask> scheduler,
87 @Named( value = "archivaConfiguration" ) ArchivaConfiguration configuration, FileTypes filetypes,
88 List<IndexCreator> indexCreators, ManagedRepositoryAdmin managedRepositoryAdmin, NexusIndexer nexusIndexer )
90 this.configuration = configuration;
91 this.filetypes = filetypes;
92 this.scheduler = scheduler;
93 this.nexusIndexer = nexusIndexer;
94 this.allIndexCreators = indexCreators;
95 this.managedRepositoryAdmin = managedRepositoryAdmin;
99 public String getDescription()
101 return "Indexes the repository to provide search and IDE integration features";
105 public String getId()
107 return "index-content";
111 public void beginScan( ManagedRepository repository, Date whenGathered )
112 throws ConsumerException
114 this.repository = repository;
115 managedRepository = new File( repository.getLocation() );
119 log.info( "Creating indexing context for repo : {}", repository.getId() );
120 indexingContext = managedRepositoryAdmin.createIndexContext( repository );
122 catch ( RepositoryAdminException e )
124 throw new ConsumerException( e.getMessage(), e );
129 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
130 throws ConsumerException
132 if ( executeOnEntireRepo )
134 beginScan( repository, whenGathered );
138 this.repository = repository;
139 managedRepository = new File( repository.getLocation() );
144 public void processFile( String path )
145 throws ConsumerException
147 File artifactFile = new File( managedRepository, path );
149 ArtifactIndexingTask task =
150 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, getIndexingContext() );
153 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
154 scheduler.queueTask( task );
156 catch ( TaskQueueException e )
158 throw new ConsumerException( e.getMessage(), e );
163 public void processFile( String path, boolean executeOnEntireRepo )
166 if ( executeOnEntireRepo )
172 File artifactFile = new File( managedRepository, path );
174 // specify in indexing task that this is not a repo scan request!
175 ArtifactIndexingTask task =
176 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD,
177 getIndexingContext(), false );
178 // only update index we don't need to scan the full repo here
179 task.setOnlyUpdate( true );
182 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
183 scheduler.queueTask( task );
185 catch ( TaskQueueException e )
187 throw new ConsumerException( e.getMessage(), e );
193 public void completeScan()
195 IndexingContext context = this.indexingContext;
196 if ( context == null )
200 context = getIndexingContext();
202 catch ( ConsumerException e )
204 log.warn( "failed to get an IndexingContext:{}", e.getMessage() );
208 ArtifactIndexingTask task =
209 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
212 log.debug( "Queueing indexing task '{}' to finish indexing.", task );
213 scheduler.queueTask( task );
215 catch ( TaskQueueException e )
217 log.error( "Error queueing task: {}: {}", task, e.getMessage(), e );
222 public void completeScan( boolean executeOnEntireRepo )
224 if ( executeOnEntireRepo )
229 // else, do nothing as the context will be closed when indexing task is executed if not a repo scan request!
233 public List<String> getExcludes()
235 return Collections.emptyList();
239 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
241 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
248 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
253 private void initIncludes()
255 List<String> indexable = filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT );
256 List<String> artifacts = filetypes.getFileTypePatterns( FileTypes.ARTIFACTS );
258 includes = new ArrayList<>( indexable.size() + artifacts.size() );
260 includes.addAll( indexable );
262 includes.addAll( artifacts );
266 public void initialize()
268 configuration.addChangeListener( this );
274 public List<String> getIncludes()
280 private IndexingContext getIndexingContext()
281 throws ConsumerException
284 if ( this.indexingContext == null )
288 indexingContext = managedRepositoryAdmin.createIndexContext( repository );
290 catch ( RepositoryAdminException e )
292 throw new ConsumerException( e.getMessage(), e );
295 return indexingContext;