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.HashSet;
28 import java.util.List;
31 import org.apache.lucene.document.Document;
32 import org.apache.lucene.index.IndexReader;
33 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
35 import org.apache.maven.archiva.consumers.ConsumerException;
36 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
37 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.sonatype.nexus.index.ArtifactContext;
41 import org.sonatype.nexus.index.ArtifactContextProducer;
42 import org.sonatype.nexus.index.ArtifactInfo;
43 import org.sonatype.nexus.index.DefaultArtifactContextProducer;
44 import org.sonatype.nexus.index.NexusIndexer;
45 import org.sonatype.nexus.index.context.IndexingContext;
46 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
47 import org.sonatype.nexus.index.creator.AbstractIndexCreator;
48 import org.sonatype.nexus.index.creator.IndexerEngine;
49 import org.sonatype.nexus.index.packer.IndexPacker;
52 * Consumer for indexing the repository to provide search and IDE integration features.
54 public class NexusIndexerConsumer
55 extends AbstractMonitoredConsumer
56 implements KnownRepositoryContentConsumer
58 private static final Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class );
60 private final NexusIndexer indexer;
62 private final ArtifactContextProducer artifactContextProducer;
64 private final IndexPacker indexPacker;
66 private ManagedDefaultRepositoryContent repositoryContent;
68 private IndexingContext context;
70 private File managedRepository;
72 private IndexerEngine indexerEngine;
74 private Set<String> uinfos;
76 public NexusIndexerConsumer( NexusIndexer indexer, IndexPacker indexPacker, IndexerEngine indexerEngine )
78 this.indexer = indexer;
79 this.indexPacker = indexPacker;
80 this.indexerEngine = indexerEngine;
81 this.artifactContextProducer = new DefaultArtifactContextProducer();
84 public String getDescription()
86 return "Indexes the repository to provide search and IDE integration features";
91 return "index-content";
94 public boolean isPermanent()
99 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
100 throws ConsumerException
102 managedRepository = new File( repository.getLocation() );
103 String indexDir = repository.getIndexDir();
105 File indexDirectory = null;
106 if( indexDir != null && !"".equals( indexDir ) )
108 indexDirectory = new File( managedRepository, repository.getIndexDir() );
112 indexDirectory = new File( managedRepository, ".indexer" );
115 repositoryContent = new ManagedDefaultRepositoryContent();
116 repositoryContent.setRepository( repository );
117 uinfos = new HashSet<String>();
119 synchronized ( indexer )
124 indexer.addIndexingContext( repository.getId(), repository.getId(), managedRepository,
125 indexDirectory, null, null, NexusIndexer.FULL_INDEX );
126 context.setSearchable( repository.isScanned() );
128 // read index to get all the artifacts already indexed
129 IndexReader r = context.getIndexReader();
130 for ( int i = 0; i < r.numDocs(); i++ )
132 if ( !r.isDeleted( i ) )
134 Document d = r.document( i );
135 String uinfo = d.get( ArtifactInfo.UINFO );
144 indexerEngine.beginIndexing( context );
146 catch ( UnsupportedExistingLuceneIndexException e )
148 log.error( "Could not create index at " + indexDirectory.getAbsoluteFile(), e );
150 catch ( IOException e )
152 log.error( "Could not create index at " + indexDirectory.getAbsoluteFile(), e );
157 public void processFile( String path )
158 throws ConsumerException
160 File artifactFile = new File( managedRepository, path );
161 ArtifactContext artifactContext = artifactContextProducer.getArtifactContext( context, artifactFile );
163 if ( artifactContext != null )
167 ArtifactInfo ai = artifactContext.getArtifactInfo();
168 String uinfo = AbstractIndexCreator.getGAV(
169 ai.groupId, ai.artifactId, ai.version, ai.classifier, ai.packaging );
171 // already indexed so update!
172 if ( uinfos.contains( uinfo ) )
174 indexerEngine.update( context, artifactContext );
178 indexerEngine.index( context, artifactContext );
181 catch ( IOException e )
183 throw new ConsumerException( e.getMessage(), e );
188 public void completeScan()
190 final File indexLocation = new File( managedRepository, ".index" );
193 indexerEngine.endIndexing( context );
194 indexPacker.packIndex( context, indexLocation );
195 indexer.removeIndexingContext( context, false );
198 catch ( IOException e )
200 log.error( "Could not pack index" + indexLocation.getAbsolutePath(), e );
204 public List<String> getExcludes()
206 return new ArrayList<String>();
209 public List<String> getIncludes()
211 return Arrays.asList( "**/*" );