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.plexusbridge.MavenIndexerUtils;
23 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
25 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
26 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.ConfigurationNames;
29 import org.apache.maven.archiva.configuration.FileTypes;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
32 import org.apache.maven.archiva.consumers.ConsumerException;
33 import org.apache.maven.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.personality.plexus.lifecycle.phase.Initializable;
39 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
40 import org.codehaus.plexus.registry.Registry;
41 import org.codehaus.plexus.registry.RegistryListener;
42 import org.codehaus.plexus.taskqueue.TaskQueueException;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 import java.io.IOException;
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 public class NexusIndexerConsumer
57 extends AbstractMonitoredConsumer
58 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
60 private Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class );
62 private ArchivaConfiguration configuration;
64 private FileTypes filetypes;
66 private File managedRepository;
68 private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
70 private IndexingContext context;
72 private NexusIndexer nexusIndexer;
74 private List<String> includes = new ArrayList<String>();
76 private ManagedRepositoryConfiguration repository;
78 private List<? extends IndexCreator> allIndexCreators;
80 public NexusIndexerConsumer( ArchivaTaskScheduler<ArtifactIndexingTask> scheduler,
81 ArchivaConfiguration configuration, FileTypes filetypes,
82 PlexusSisuBridge plexusSisuBridge, MavenIndexerUtils mavenIndexerUtils )
83 throws PlexusSisuBridgeException
85 this.configuration = configuration;
86 this.filetypes = filetypes;
87 this.scheduler = scheduler;
88 this.nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
89 this.allIndexCreators = mavenIndexerUtils.getAllIndexCreators();
92 public String getDescription()
94 return "Indexes the repository to provide search and IDE integration features";
99 return "index-content";
102 public boolean isPermanent()
107 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
108 throws ConsumerException
110 this.repository = repository;
111 managedRepository = new File( repository.getLocation() );
115 log.info( "Creating indexing context for repo : {}", repository.getId() );
116 context = ArtifactIndexingTask.createContext( repository, nexusIndexer, allIndexCreators );
118 catch ( IOException e )
120 throw new ConsumerException( e.getMessage(), e );
122 catch ( UnsupportedExistingLuceneIndexException e )
124 throw new ConsumerException( e.getMessage(), e );
128 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
129 throws ConsumerException
131 if ( executeOnEntireRepo )
133 beginScan( repository, whenGathered );
137 this.repository = repository;
138 managedRepository = new File( repository.getLocation() );
142 public void processFile( String path )
143 throws ConsumerException
145 File artifactFile = new File( managedRepository, path );
147 ArtifactIndexingTask task =
148 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, context );
151 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
152 scheduler.queueTask( task );
154 catch ( TaskQueueException e )
156 throw new ConsumerException( e.getMessage(), e );
160 public void processFile( String path, boolean executeOnEntireRepo )
163 if ( executeOnEntireRepo )
169 File artifactFile = new File( managedRepository, path );
171 // specify in indexing task that this is not a repo scan request!
172 ArtifactIndexingTask task =
173 new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, context, false );
174 // only update index we don't need to scan the full repo here
175 task.setOnlyUpdate( true );
178 log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
179 scheduler.queueTask( task );
181 catch ( TaskQueueException e )
183 throw new ConsumerException( e.getMessage(), e );
188 public void completeScan()
190 ArtifactIndexingTask task =
191 new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
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!
214 public List<String> getExcludes()
216 return Collections.emptyList();
219 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
221 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
227 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
232 private void initIncludes()
236 includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) );
238 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
241 public void initialize()
242 throws InitializationException
244 configuration.addChangeListener( this );
249 public List<String> getIncludes()