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.maven.archiva.configuration.ArchivaConfiguration;
30 import org.apache.maven.archiva.configuration.ConfigurationNames;
31 import org.apache.maven.archiva.configuration.FileTypes;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
34 import org.apache.maven.archiva.consumers.ConsumerException;
35 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
36 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
37 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
38 import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
39 import org.apache.maven.archiva.scheduled.tasks.TaskCreator;
40 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
41 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
42 import org.codehaus.plexus.registry.Registry;
43 import org.codehaus.plexus.registry.RegistryListener;
44 import org.codehaus.plexus.taskqueue.TaskQueueException;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.sonatype.nexus.index.context.IndexingContext;
48 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
51 * Consumer for indexing the repository to provide search and IDE integration features.
53 public class NexusIndexerConsumer
54 extends AbstractMonitoredConsumer
55 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
57 private static final Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class );
59 private ArchivaConfiguration configuration;
61 private FileTypes filetypes;
63 private ManagedDefaultRepositoryContent repositoryContent;
65 private File managedRepository;
67 private ArchivaTaskScheduler scheduler;
69 private IndexingContext context;
71 private List<String> includes = new ArrayList<String>();
73 public NexusIndexerConsumer( ArchivaTaskScheduler scheduler, 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 = TaskCreator.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 TaskCreator.createIndexingTask( repositoryContent.getRepository(), artifactFile,
124 ArtifactIndexingTask.Action.ADD, context );
127 log.debug( "Queueing indexing task + '" + task + "' to add or update the artifact in the index." );
128 scheduler.queueIndexingTask( task );
130 catch ( TaskQueueException e )
132 throw new ConsumerException( e.getMessage(), e );
136 public void completeScan()
138 ArtifactIndexingTask task =
139 TaskCreator.createIndexingTask( repositoryContent.getRepository(), null,
140 ArtifactIndexingTask.Action.FINISH, context );
143 log.debug( "Queueing indexing task + '" + task + "' to finish indexing." );
144 scheduler.queueIndexingTask( 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()