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;
28 import java.util.zip.ZipException;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
32 import org.apache.maven.archiva.consumers.ConsumerException;
33 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
34 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.sonatype.nexus.index.ArtifactContext;
38 import org.sonatype.nexus.index.ArtifactContextProducer;
39 import org.sonatype.nexus.index.DefaultArtifactContextProducer;
40 import org.sonatype.nexus.index.NexusIndexer;
41 import org.sonatype.nexus.index.context.DefaultIndexingContext;
42 import org.sonatype.nexus.index.context.IndexingContext;
43 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
44 import org.sonatype.nexus.index.IndexerEngine;
45 import org.sonatype.nexus.index.packer.IndexPacker;
46 import org.sonatype.nexus.index.packer.IndexPackingRequest;
49 * Consumer for indexing the repository to provide search and IDE integration features.
51 public class NexusIndexerConsumer
52 extends AbstractMonitoredConsumer
53 implements KnownRepositoryContentConsumer
55 private static final Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class );
57 private ArtifactContextProducer artifactContextProducer;
59 private IndexPacker indexPacker;
61 private ManagedDefaultRepositoryContent repositoryContent;
63 private IndexingContext context;
65 private File managedRepository;
67 private IndexerEngine indexerEngine;
69 //private IndexingContextMap indexingContextMap;
71 public NexusIndexerConsumer( IndexPacker indexPacker, IndexerEngine indexerEngine )
73 this.indexPacker = indexPacker;
74 this.indexerEngine = indexerEngine;
75 this.artifactContextProducer = new DefaultArtifactContextProducer();
78 /* public NexusIndexerConsumer( IndexPacker indexPacker, IndexerEngine indexerEngine, IndexingContextMap indexingContextMap )
80 this.indexPacker = indexPacker;
81 this.indexerEngine = indexerEngine;
82 this.indexingContextMap = indexingContextMap;
83 this.artifactContextProducer = new DefaultArtifactContextProducer();
86 public String getDescription()
88 return "Indexes the repository to provide search and IDE integration features";
93 return "index-content";
96 public boolean isPermanent()
101 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
102 throws ConsumerException
104 //synchronized( context )
106 log.debug( "Begin indexing of repository '" + repository.getId() + "'..");
108 managedRepository = new File( repository.getLocation() );
109 String indexDir = repository.getIndexDir();
111 File indexDirectory = null;
112 if( indexDir != null && !"".equals( indexDir ) )
114 indexDirectory = new File( repository.getIndexDir() );
118 indexDirectory = new File( managedRepository, ".indexer" );
121 repositoryContent = new ManagedDefaultRepositoryContent();
122 repositoryContent.setRepository( repository );
127 new DefaultIndexingContext( repository.getId(), repository.getId(), managedRepository,
128 indexDirectory, null, null, NexusIndexer.FULL_INDEX, false );
130 //context = indexingContextMap.addIndexingContext( repository.getId(), repository.getId(), managedRepository,
131 // indexDirectory, null, null, NexusIndexer.FULL_INDEX, false );
133 context.setSearchable( repository.isScanned() );
135 //indexerEngine.beginIndexing( context );
137 catch ( UnsupportedExistingLuceneIndexException e )
139 throw new ConsumerException( "Could not create index at " + indexDirectory.getAbsoluteFile(), e );
141 catch ( IOException e )
143 throw new ConsumerException( "Could not create index at " + indexDirectory.getAbsoluteFile(), e );
148 public void processFile( String path )
149 throws ConsumerException
151 synchronized ( indexerEngine )
153 if ( context == null )
155 // didn't start correctly, so skip
159 File artifactFile = new File( managedRepository, path );
160 ArtifactContext artifactContext = artifactContextProducer.getArtifactContext( context, artifactFile );
162 if ( artifactContext != null )
166 indexerEngine.index( context, artifactContext );
168 catch ( ZipException e )
171 log.info( e.getMessage() );
173 catch ( IOException e )
175 throw new ConsumerException( e.getMessage(), e );
181 public void completeScan()
183 //synchronized( context )
185 log.debug( "End indexing of repository '" + context.getRepositoryId() + "'..");
187 final File indexLocation = new File( managedRepository, ".index" );
190 //indexerEngine.endIndexing( context );
192 IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
193 indexPacker.packIndex( request );
195 //indexingContextMap.removeIndexingContext( context.getId() );
197 context.close( false );
199 catch ( IOException e )
201 log.error( "Could not pack index" + indexLocation.getAbsolutePath(), e );
206 public List<String> getExcludes()
208 return new ArrayList<String>();
211 public List<String> getIncludes()
213 return Arrays.asList( "**/*" );