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
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.List;
29 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
30 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.ConfigurationNames;
33 import org.apache.maven.archiva.configuration.FileTypes;
34 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
36 import org.apache.maven.archiva.consumers.ConsumerException;
37 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
38 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
39 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
40 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
41 import org.codehaus.plexus.registry.Registry;
42 import org.codehaus.plexus.registry.RegistryListener;
43 import org.codehaus.plexus.taskqueue.TaskQueueException;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.sonatype.nexus.index.context.IndexingContext;
47 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
50 * Consumer for indexing the repository to provide search and IDE integration features.
52 public class NexusIndexerConsumer
53 extends AbstractMonitoredConsumer
54 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
56 private static final Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class );
58 private ArchivaConfiguration configuration;
60 private FileTypes filetypes;
62 private ManagedDefaultRepositoryContent repositoryContent;
64 private File managedRepository;
66 private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
68 private IndexingContext context;
70 private List<String> includes = new ArrayList<String>();
72 public NexusIndexerConsumer( ArchivaTaskScheduler<ArtifactIndexingTask> scheduler,
73 ArchivaConfiguration configuration, FileTypes filetypes )
75 this.configuration = configuration;
76 this.filetypes = filetypes;
77 this.scheduler = scheduler;
80 public String getDescription()
82 return "Indexes the repository to provide search and IDE integration features";
87 return "index-content";
90 public boolean isPermanent()
95 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
96 throws ConsumerException
98 managedRepository = new File( repository.getLocation() );
100 repositoryContent = new ManagedDefaultRepositoryContent();
101 repositoryContent.setRepository( repository );
105 context = ArtifactIndexingTask.createContext( repository );
107 catch ( IOException e )
109 throw new ConsumerException( e.getMessage(), e );
111 catch ( UnsupportedExistingLuceneIndexException e )
113 throw new ConsumerException( e.getMessage(), e );
117 public void processFile( String path )
118 throws ConsumerException
120 File artifactFile = new File( managedRepository, path );
122 ArtifactIndexingTask task =
123 new ArtifactIndexingTask( repositoryContent.getRepository(), artifactFile, ArtifactIndexingTask.Action.ADD,
127 log.debug( "Queueing indexing task + '" + task + "' to add or update the artifact in the index." );
128 scheduler.queueTask( task );
130 catch ( TaskQueueException e )
132 throw new ConsumerException( e.getMessage(), e );
136 public void completeScan()
138 ArtifactIndexingTask task =
139 new ArtifactIndexingTask( repositoryContent.getRepository(), null, ArtifactIndexingTask.Action.FINISH,
143 log.debug( "Queueing indexing task + '" + task + "' to finish indexing." );
144 scheduler.queueTask( task );
146 catch ( TaskQueueException e )
148 log.error( "Error queueing task: " + task + ": " + e.getMessage(), e );
153 public List<String> getExcludes()
155 return Collections.emptyList();
158 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
160 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
166 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
171 private void initIncludes()
175 includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) );
177 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
180 public void initialize()
181 throws InitializationException
183 configuration.addChangeListener( this );
188 public List<String> getIncludes()