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.beans.ManagedRepository;
23 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
26 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
27 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
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.maven.index.NexusIndexer;
35 import org.apache.maven.index.context.IndexCreator;
36 import org.apache.maven.index.context.IndexingContext;
37 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
38 import org.codehaus.plexus.registry.Registry;
39 import org.codehaus.plexus.registry.RegistryListener;
40 import org.codehaus.plexus.taskqueue.TaskQueueException;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.context.annotation.Scope;
44 import org.springframework.stereotype.Service;
46 import javax.annotation.PostConstruct;
48 import java.io.IOException;
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.Date;
52 import java.util.List;
55 * Consumer for indexing the repository to provide search and IDE integration features.
57 @Service("knownRepositoryContentConsumer#index-content")
59 public class NexusIndexerConsumer
60 extends AbstractMonitoredConsumer
61 implements KnownRepositoryContentConsumer, RegistryListener
63 private Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class );
65 private ArchivaConfiguration configuration;
67 private FileTypes filetypes;
69 private File managedRepository;
71 private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
73 private IndexingContext context;
75 private NexusIndexer nexusIndexer;
77 private List<String> includes = new ArrayList<String>();
79 private ManagedRepository repository;
81 private List<? extends IndexCreator> allIndexCreators;
83 public NexusIndexerConsumer( ArchivaTaskScheduler<ArtifactIndexingTask> scheduler,
84 ArchivaConfiguration configuration, FileTypes filetypes,
85 PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
86 throws PlexusSisuBridgeException
88 this.configuration = configuration;
89 this.filetypes = filetypes;
90 this.scheduler = scheduler;
91 this.nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
92 this.allIndexCreators = mavenIndexerUtils.getAllIndexCreators();
95 public String getDescription()
97 return "Indexes the repository to provide search and IDE integration features";
100 public String getId()
102 return "index-content";
105 public boolean isPermanent()
110 public void beginScan( ManagedRepository repository, Date whenGathered )
111 throws ConsumerException
113 this.repository = repository;
114 managedRepository = new File( repository.getLocation() );
118 log.info( "Creating indexing context for repo : {}", repository.getId() );
119 context = ArtifactIndexingTask.createContext( repository, nexusIndexer, allIndexCreators );
121 catch ( IOException e )
123 throw new ConsumerException( e.getMessage(), e );
125 catch ( UnsupportedExistingLuceneIndexException e )
127 throw new ConsumerException( e.getMessage(), e );
131 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
132 throws ConsumerException
134 if ( executeOnEntireRepo )
136 beginScan( repository, whenGathered );
140 this.repository = repository;
141 managedRepository = new File( repository.getLocation() );
145 public void processFile( String path )
146 throws ConsumerException
148 File artifactFile = new File( managedRepository, path );
150 ArtifactIndexingTask task =
151 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, context );
154 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
155 scheduler.queueTask( task );
157 catch ( TaskQueueException e )
159 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, context, false );
177 // only update index we don't need to scan the full repo here
178 task.setOnlyUpdate( true );
181 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
182 scheduler.queueTask( task );
184 catch ( TaskQueueException e )
186 throw new ConsumerException( e.getMessage(), e );
191 public void completeScan()
193 ArtifactIndexingTask task =
194 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
197 log.debug( "Queueing indexing task '{}' to finish indexing.", task );
198 scheduler.queueTask( task );
200 catch ( TaskQueueException e )
202 log.error( "Error queueing task: " + task + ": " + e.getMessage(), e );
207 public void completeScan( boolean executeOnEntireRepo )
209 if ( executeOnEntireRepo )
214 // else, do nothing as the context will be closed when indexing task is executed if not a repo scan request!
217 public List<String> getExcludes()
219 return Collections.emptyList();
222 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
224 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
230 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
235 private void initIncludes()
239 includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) );
241 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
245 public void initialize()
247 configuration.addChangeListener( this );
252 public List<String> getIncludes()