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.Arrays;
26 import java.util.Date;
27 import java.util.List;
29 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
31 import org.apache.maven.archiva.consumers.ConsumerException;
32 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
33 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
34 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
35 import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
36 import org.apache.maven.archiva.scheduled.tasks.TaskCreator;
37 import org.codehaus.plexus.taskqueue.TaskQueueException;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.sonatype.nexus.index.context.IndexingContext;
41 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
44 * Consumer for indexing the repository to provide search and IDE integration features.
46 public class NexusIndexerConsumer
47 extends AbstractMonitoredConsumer
48 implements KnownRepositoryContentConsumer
50 private static final Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class );
52 private ManagedDefaultRepositoryContent repositoryContent;
54 private File managedRepository;
56 private ArchivaTaskScheduler scheduler;
58 private IndexingContext context;
60 public NexusIndexerConsumer( ArchivaTaskScheduler scheduler )
62 this.scheduler = scheduler;
65 public String getDescription()
67 return "Indexes the repository to provide search and IDE integration features";
72 return "index-content";
75 public boolean isPermanent()
80 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
81 throws ConsumerException
83 managedRepository = new File( repository.getLocation() );
85 repositoryContent = new ManagedDefaultRepositoryContent();
86 repositoryContent.setRepository( repository );
90 context = TaskCreator.createContext( repository );
92 catch ( IOException e )
94 throw new ConsumerException( e.getMessage(), e );
96 catch ( UnsupportedExistingLuceneIndexException e )
98 throw new ConsumerException( e.getMessage(), e );
102 public void processFile( String path )
103 throws ConsumerException
105 File artifactFile = new File( managedRepository, path );
107 ArtifactIndexingTask task =
108 TaskCreator.createIndexingTask( repositoryContent.getRepository(), artifactFile,
109 ArtifactIndexingTask.Action.ADD, context );
112 log.debug( "Queueing indexing task + '" + task + "' to add or update the artifact in the index." );
113 scheduler.queueIndexingTask( task );
115 catch ( TaskQueueException e )
117 throw new ConsumerException( e.getMessage(), e );
121 public void completeScan()
123 ArtifactIndexingTask task =
124 TaskCreator.createIndexingTask( repositoryContent.getRepository(), null,
125 ArtifactIndexingTask.Action.FINISH, context );
128 log.debug( "Queueing indexing task + '" + task + "' to finish indexing." );
129 scheduler.queueIndexingTask( task );
131 catch ( TaskQueueException e )
133 log.error( "Error queueing task: " + task + ": " + e.getMessage(), e );
137 public List<String> getExcludes()
139 return new ArrayList<String>();
142 public List<String> getIncludes()
144 return Arrays.asList( "**/*" );