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;
44 import javax.annotation.PostConstruct;
46 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.Date;
50 import java.util.List;
53 * Consumer for indexing the repository to provide search and IDE integration features.
55 public class NexusIndexerConsumer
56 extends AbstractMonitoredConsumer
57 implements KnownRepositoryContentConsumer, RegistryListener
59 private Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class );
61 private ArchivaConfiguration configuration;
63 private FileTypes filetypes;
65 private File managedRepository;
67 private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
69 private IndexingContext context;
71 private NexusIndexer nexusIndexer;
73 private List<String> includes = new ArrayList<String>();
75 private ManagedRepository repository;
77 private List<? extends IndexCreator> allIndexCreators;
79 public NexusIndexerConsumer( ArchivaTaskScheduler<ArtifactIndexingTask> scheduler,
80 ArchivaConfiguration configuration, FileTypes filetypes,
81 PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
82 throws PlexusSisuBridgeException
84 this.configuration = configuration;
85 this.filetypes = filetypes;
86 this.scheduler = scheduler;
87 this.nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
88 this.allIndexCreators = mavenIndexerUtils.getAllIndexCreators();
91 public String getDescription()
93 return "Indexes the repository to provide search and IDE integration features";
98 return "index-content";
101 public boolean isPermanent()
106 public void beginScan( ManagedRepository repository, Date whenGathered )
107 throws ConsumerException
109 this.repository = repository;
110 managedRepository = new File( repository.getLocation() );
114 log.info( "Creating indexing context for repo : {}", repository.getId() );
115 context = ArtifactIndexingTask.createContext( repository, nexusIndexer, allIndexCreators );
117 catch ( IOException e )
119 throw new ConsumerException( e.getMessage(), e );
121 catch ( UnsupportedExistingLuceneIndexException e )
123 throw new ConsumerException( e.getMessage(), e );
127 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
128 throws ConsumerException
130 if ( executeOnEntireRepo )
132 beginScan( repository, whenGathered );
136 this.repository = repository;
137 managedRepository = new File( repository.getLocation() );
141 public void processFile( String path )
142 throws ConsumerException
144 File artifactFile = new File( managedRepository, path );
146 ArtifactIndexingTask task =
147 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, context );
150 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
151 scheduler.queueTask( task );
153 catch ( TaskQueueException e )
155 throw new ConsumerException( e.getMessage(), e );
159 public void processFile( String path, boolean executeOnEntireRepo )
162 if ( executeOnEntireRepo )
168 File artifactFile = new File( managedRepository, path );
170 // specify in indexing task that this is not a repo scan request!
171 ArtifactIndexingTask task =
172 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, context, 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 );
187 public void completeScan()
189 ArtifactIndexingTask task =
190 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
193 log.debug( "Queueing indexing task '{}' to finish indexing.", task );
194 scheduler.queueTask( task );
196 catch ( TaskQueueException e )
198 log.error( "Error queueing task: " + task + ": " + e.getMessage(), e );
203 public void completeScan( boolean executeOnEntireRepo )
205 if ( executeOnEntireRepo )
210 // else, do nothing as the context will be closed when indexing task is executed if not a repo scan request!
213 public List<String> getExcludes()
215 return Collections.emptyList();
218 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
220 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
226 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
231 private void initIncludes()
235 includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) );
237 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
241 public void initialize()
243 configuration.addChangeListener( this );
248 public List<String> getIncludes()