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.common.utils.PathUtil;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.ConfigurationNames;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
27 import org.apache.archiva.consumers.ConsumerException;
28 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.apache.archiva.indexer.UnsupportedBaseContextException;
30 import org.apache.archiva.components.registry.Registry;
31 import org.apache.archiva.components.registry.RegistryListener;
32 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
33 import org.apache.archiva.repository.ManagedRepository;
34 import org.apache.archiva.repository.RepositoryType;
35 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
36 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
37 import org.apache.maven.index.context.IndexingContext;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.context.annotation.Scope;
41 import org.springframework.stereotype.Service;
43 import javax.annotation.PostConstruct;
44 import javax.inject.Inject;
45 import javax.inject.Named;
46 import java.nio.file.Path;
47 import java.nio.file.Paths;
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
64 private Logger log = LoggerFactory.getLogger( getClass() );
66 private ArchivaConfiguration configuration;
68 private FileTypes filetypes;
70 private Path managedRepository;
72 private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
74 private IndexingContext indexingContext;
76 private List<String> includes = new ArrayList<>( 0 );
78 private ManagedRepository repository;
81 public NexusIndexerConsumer(
82 @Named( value = "archivaTaskScheduler#indexing" ) ArchivaTaskScheduler<ArtifactIndexingTask> scheduler,
83 @Named( value = "archivaConfiguration" ) ArchivaConfiguration configuration, FileTypes filetypes)
85 this.configuration = configuration;
86 this.filetypes = filetypes;
87 this.scheduler = scheduler;
91 public String getDescription()
93 return "Indexes the repository to provide search and IDE integration features";
99 return "index-content";
103 public void beginScan( ManagedRepository repository, Date whenGathered )
104 throws ConsumerException
106 this.repository = repository;
107 managedRepository = PathUtil.getPathFromUri( repository.getLocation() );
111 log.info( "Creating indexing context for repo : {}", repository.getId() );
112 if (repository.getType()== RepositoryType.MAVEN) {
113 indexingContext = repository.getIndexingContext().getBaseContext(IndexingContext.class);
115 indexingContext= null;
117 } catch (UnsupportedBaseContextException e) {
118 log.error("Bad repository type. Not nexus indexer compatible.");
119 throw new ConsumerException("Bad repository type "+repository.getType());
124 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
125 throws ConsumerException
127 if ( executeOnEntireRepo )
129 beginScan( repository, whenGathered );
133 this.repository = repository;
134 managedRepository = Paths.get( repository.getLocation() );
139 public void processFile( String path )
140 throws ConsumerException
142 Path artifactFile = managedRepository.resolve(path);
144 ArtifactIndexingTask task =
145 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, repository.getIndexingContext() );
148 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
149 scheduler.queueTask( task );
151 catch ( TaskQueueException e )
153 throw new ConsumerException( e.getMessage(), e );
158 public void processFile( String path, boolean executeOnEntireRepo )
161 if ( executeOnEntireRepo )
167 Path artifactFile = managedRepository.resolve(path);
169 // specify in indexing task that this is not a repo scan request!
170 ArtifactIndexingTask task =
171 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD,
172 repository.getIndexingContext(), false );
173 // only update index we don't need to scan the full repo here
174 task.setOnlyUpdate( true );
177 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
178 scheduler.queueTask( task );
180 catch ( TaskQueueException e )
182 throw new ConsumerException( e.getMessage(), e );
188 public void completeScan()
190 ArtifactIndexingTask task =
191 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, repository.getIndexingContext());
194 log.debug( "Queueing indexing task '{}' to finish indexing.", task );
195 scheduler.queueTask( task );
197 catch ( TaskQueueException e )
199 log.error( "Error queueing task: {}: {}", task, e.getMessage(), e );
204 public void completeScan( boolean executeOnEntireRepo )
206 if ( executeOnEntireRepo )
211 // else, do nothing as the context will be closed when indexing task is executed if not a repo scan request!
215 public List<String> getExcludes()
217 return Collections.emptyList();
221 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
223 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
230 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
235 private void initIncludes()
237 List<String> indexable = filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT );
238 List<String> artifacts = filetypes.getFileTypePatterns( FileTypes.ARTIFACTS );
240 includes = new ArrayList<>( indexable.size() + artifacts.size() );
242 includes.addAll( indexable );
244 includes.addAll( artifacts );
248 public void initialize()
250 configuration.addChangeListener( this );
256 public List<String> getIncludes()
262 private IndexingContext getIndexingContext()
263 throws ConsumerException
266 if ( this.indexingContext == null )
270 indexingContext = repository.getIndexingContext().getBaseContext(IndexingContext.class);
271 } catch (UnsupportedBaseContextException e) {
272 log.error("Bad repository type. Not nexus indexer compatible. "+repository.getType());
273 throw new ConsumerException("Bad repository type "+repository.getType());
276 return indexingContext;