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 NexusIndexer indexer;
62 private ArtifactContextProducer artifactContextProducer;
64 private 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( 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 );
137 // should we add a check here if the contents of the document still exist in the file system
138 // for cases when there is already an existing index & the contents of that index doesn't exist
139 // in the file system & in the database?
147 indexerEngine.beginIndexing( context );
149 catch ( UnsupportedExistingLuceneIndexException e )
151 throw new ConsumerException( "Could not create index at " + indexDirectory.getAbsoluteFile(), e );
153 catch ( IOException e )
155 throw new ConsumerException( "Could not create index at " + indexDirectory.getAbsoluteFile(), e );
160 public void processFile( String path )
161 throws ConsumerException
163 if ( context == null )
165 // didn't start correctly, so skip
169 File artifactFile = new File( managedRepository, path );
170 ArtifactContext artifactContext = artifactContextProducer.getArtifactContext( context, artifactFile );
172 if ( artifactContext != null )
176 ArtifactInfo ai = artifactContext.getArtifactInfo();
177 String uinfo = AbstractIndexCreator.getGAV(
178 ai.groupId, ai.artifactId, ai.version, ai.classifier, ai.packaging );
180 // already indexed so update!
181 if ( uinfos.contains( uinfo ) )
183 indexerEngine.update( context, artifactContext );
187 indexerEngine.index( context, artifactContext );
190 catch ( IOException e )
192 throw new ConsumerException( e.getMessage(), e );
197 public void completeScan()
199 final File indexLocation = new File( managedRepository, ".index" );
202 indexerEngine.endIndexing( context );
203 indexPacker.packIndex( context, indexLocation );
204 indexer.removeIndexingContext( context, false );
207 catch ( IOException e )
209 log.error( "Could not pack index" + indexLocation.getAbsolutePath(), e );
213 public List<String> getExcludes()
215 return new ArrayList<String>();
218 public List<String> getIncludes()
220 return Arrays.asList( "**/*" );