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 MavenIndexerUtils mavenIndexerUtils,
92 ManagedRepositoryAdmin managedRepositoryAdmin, NexusIndexer nexusIndexer )
94 this.configuration = configuration;
95 this.filetypes = filetypes;
96 this.scheduler = scheduler;
97 this.nexusIndexer = nexusIndexer;
98 this.allIndexCreators = mavenIndexerUtils.getAllIndexCreators();
99 this.managedRepositoryAdmin = managedRepositoryAdmin;
103 public String getDescription()
105 return "Indexes the repository to provide search and IDE integration features";
109 public String getId()
111 return "index-content";
115 public void beginScan( ManagedRepository repository, Date whenGathered )
116 throws ConsumerException
118 this.repository = repository;
119 managedRepository = new File( repository.getLocation() );
123 log.info( "Creating indexing context for repo : {}", repository.getId() );
124 indexingContext = managedRepositoryAdmin.createIndexContext( repository );
126 catch ( RepositoryAdminException e )
128 throw new ConsumerException( e.getMessage(), e );
133 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
134 throws ConsumerException
136 if ( executeOnEntireRepo )
138 beginScan( repository, whenGathered );
142 this.repository = repository;
143 managedRepository = new File( repository.getLocation() );
148 public void processFile( String path )
149 throws ConsumerException
151 File artifactFile = new File( managedRepository, path );
153 ArtifactIndexingTask task =
154 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, getIndexingContext() );
157 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
158 scheduler.queueTask( task );
160 catch ( TaskQueueException e )
162 throw new ConsumerException( e.getMessage(), e );
167 public void processFile( String path, boolean executeOnEntireRepo )
170 if ( executeOnEntireRepo )
176 File artifactFile = new File( managedRepository, path );
178 // specify in indexing task that this is not a repo scan request!
179 ArtifactIndexingTask task =
180 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD,
181 getIndexingContext(), false );
182 // only update index we don't need to scan the full repo here
183 task.setOnlyUpdate( true );
186 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
187 scheduler.queueTask( task );
189 catch ( TaskQueueException e )
191 throw new ConsumerException( e.getMessage(), e );
197 public void completeScan()
199 IndexingContext context = this.indexingContext;
200 if ( context == null )
204 context = getIndexingContext();
206 catch ( ConsumerException e )
208 log.warn( "failed to get an IndexingContext:{}", e.getMessage() );
212 ArtifactIndexingTask task =
213 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
216 log.debug( "Queueing indexing task '{}' to finish indexing.", task );
217 scheduler.queueTask( task );
219 catch ( TaskQueueException e )
221 log.error( "Error queueing task: " + task + ": " + e.getMessage(), e );
226 public void completeScan( boolean executeOnEntireRepo )
228 if ( executeOnEntireRepo )
233 // else, do nothing as the context will be closed when indexing task is executed if not a repo scan request!
237 public List<String> getExcludes()
239 return Collections.emptyList();
243 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
245 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
252 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
257 private void initIncludes()
259 List<String> indexable = filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT );
260 List<String> artifacts = filetypes.getFileTypePatterns( FileTypes.ARTIFACTS );
262 includes = new ArrayList<>( indexable.size() + artifacts.size() );
264 includes.addAll( indexable );
266 includes.addAll( artifacts );
270 public void initialize()
272 configuration.addChangeListener( this );
278 public List<String> getIncludes()
284 private IndexingContext getIndexingContext()
285 throws ConsumerException
288 if ( this.indexingContext == null )
292 indexingContext = managedRepositoryAdmin.createIndexContext( repository );
294 catch ( RepositoryAdminException e )
296 throw new ConsumerException( e.getMessage(), e );
299 return indexingContext;