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.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.codehaus.plexus.registry.Registry;
40 import org.codehaus.plexus.registry.RegistryListener;
41 import org.codehaus.plexus.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 context;
77 private NexusIndexer nexusIndexer;
79 private List<String> includes = new ArrayList<String>();
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, PlexusSisuBridge plexusSisuBridge,
91 MavenIndexerUtils mavenIndexerUtils, ManagedRepositoryAdmin managedRepositoryAdmin )
92 throws PlexusSisuBridgeException
94 this.configuration = configuration;
95 this.filetypes = filetypes;
96 this.scheduler = scheduler;
97 this.nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
98 this.allIndexCreators = mavenIndexerUtils.getAllIndexCreators();
99 this.managedRepositoryAdmin = managedRepositoryAdmin;
102 public String getDescription()
104 return "Indexes the repository to provide search and IDE integration features";
107 public String getId()
109 return "index-content";
112 public boolean isPermanent()
117 public void beginScan( ManagedRepository repository, Date whenGathered )
118 throws ConsumerException
120 this.repository = repository;
121 managedRepository = new File( repository.getLocation() );
125 log.info( "Creating indexing context for repo : {}", repository.getId() );
126 context = managedRepositoryAdmin.createIndexContext( repository );
128 catch ( RepositoryAdminException e )
130 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() );
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, context );
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 );
166 public void processFile( String path, boolean executeOnEntireRepo )
169 if ( executeOnEntireRepo )
175 File artifactFile = new File( managedRepository, path );
177 // specify in indexing task that this is not a repo scan request!
178 ArtifactIndexingTask task =
179 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, context, false );
180 // only update index we don't need to scan the full repo here
181 task.setOnlyUpdate( true );
184 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
185 scheduler.queueTask( task );
187 catch ( TaskQueueException e )
189 throw new ConsumerException( e.getMessage(), e );
194 public void completeScan()
196 ArtifactIndexingTask task =
197 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
200 log.debug( "Queueing indexing task '{}' to finish indexing.", task );
201 scheduler.queueTask( task );
203 catch ( TaskQueueException e )
205 log.error( "Error queueing task: " + task + ": " + e.getMessage(), e );
210 public void completeScan( boolean executeOnEntireRepo )
212 if ( executeOnEntireRepo )
217 // else, do nothing as the context will be closed when indexing task is executed if not a repo scan request!
220 public List<String> getExcludes()
222 return Collections.emptyList();
225 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
227 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
233 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
238 private void initIncludes()
242 includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) );
244 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
248 public void initialize()
250 configuration.addChangeListener( this );
255 public List<String> getIncludes()