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 org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
30 import org.apache.maven.archiva.consumers.ConsumerException;
31 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
32 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.sonatype.nexus.index.ArtifactContext;
36 import org.sonatype.nexus.index.ArtifactContextProducer;
37 import org.sonatype.nexus.index.DefaultArtifactContextProducer;
38 import org.sonatype.nexus.index.NexusIndexer;
39 import org.sonatype.nexus.index.context.IndexingContext;
40 import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException;
41 import org.sonatype.nexus.index.creator.IndexerEngine;
42 import org.sonatype.nexus.index.packer.IndexPacker;
45 * Consumer for indexing the repository to provide search and IDE integration features.
47 public class NexusIndexerConsumer
48 extends AbstractMonitoredConsumer
49 implements KnownRepositoryContentConsumer
51 private static final Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class );
53 private final NexusIndexer indexer;
55 private final ArtifactContextProducer artifactContextProducer;
57 private final IndexPacker indexPacker;
59 private ManagedRepositoryConfiguration repository;
61 private ManagedDefaultRepositoryContent repositoryContent;
63 private IndexingContext context;
65 private File managedRepository;
67 private IndexerEngine indexerEngine;
69 public NexusIndexerConsumer( NexusIndexer indexer, IndexPacker indexPacker, IndexerEngine indexerEngine )
71 this.indexer = indexer;
72 this.indexPacker = indexPacker;
73 this.indexerEngine = indexerEngine;
74 this.artifactContextProducer = new DefaultArtifactContextProducer();
77 public String getDescription()
79 return "Indexes the repository to provide search and IDE integration features";
84 return "index-content";
87 public boolean isPermanent()
92 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
93 throws ConsumerException
95 this.repository = repository;
96 managedRepository = new File( repository.getLocation() );
97 File indexDirectory = new File( managedRepository, ".indexer" );
99 repositoryContent = new ManagedDefaultRepositoryContent();
100 repositoryContent.setRepository( repository );
102 synchronized ( indexer )
107 indexer.addIndexingContext( repository.getId(), repository.getId(), managedRepository,
108 indexDirectory, null, null, NexusIndexer.FULL_INDEX );
109 context.setSearchable( repository.isScanned() );
111 indexerEngine.beginIndexing( context );
113 catch ( UnsupportedExistingLuceneIndexException e )
115 log.error( "Could not create index at " + indexDirectory.getAbsoluteFile(), e );
117 catch ( IOException e )
119 log.error( "Could not create index at " + indexDirectory.getAbsoluteFile(), e );
124 public void processFile( String path )
125 throws ConsumerException
127 File artifactFile = new File( managedRepository, path );
129 ArtifactContext artifactContext = artifactContextProducer.getArtifactContext( context, artifactFile );
130 if ( artifactContext != null )
134 indexer.artifactDiscovered( artifactContext, context );
136 indexerEngine.index( context, artifactContext );
138 catch ( IOException e )
140 throw new ConsumerException( e.getMessage(), e );
145 public void completeScan()
147 final File indexLocation = new File( managedRepository, ".index" );
150 indexPacker.packIndex( context, indexLocation );
151 indexerEngine.endIndexing( context );
153 catch ( IOException e )
155 log.error( "Could not pack index" + indexLocation.getAbsolutePath(), e );
159 public List<String> getExcludes()
161 return new ArrayList<String>();
164 public List<String> getIncludes()
166 return Arrays.asList( "**/*" );