o re-packaged NexusIndexerConsumer and index discovered artifact using Nexus' IndexerEngine o added test for NexusIndexerConsumer git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/archiva-nexus-indexer@736595 13f79535-47bb-0310-9956-ffa450edef68archiva-nexus-indexer
@@ -1,4 +1,4 @@ | |||
package org.apache.maven.archiva.consumers.lucene; | |||
package org.apache.archiva.consumers.lucene; | |||
/* | |||
* Licensed to the Apache Software Foundation (ASF) under one | |||
@@ -30,7 +30,6 @@ import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer; | |||
import org.apache.maven.archiva.consumers.ConsumerException; | |||
import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; | |||
import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent; | |||
import org.apache.maven.archiva.repository.layout.LayoutException; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
import org.sonatype.nexus.index.ArtifactContext; | |||
@@ -39,13 +38,17 @@ import org.sonatype.nexus.index.DefaultArtifactContextProducer; | |||
import org.sonatype.nexus.index.NexusIndexer; | |||
import org.sonatype.nexus.index.context.IndexingContext; | |||
import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; | |||
import org.sonatype.nexus.index.creator.IndexerEngine; | |||
import org.sonatype.nexus.index.packer.IndexPacker; | |||
public class NexusIndexerConsumer | |||
/** | |||
* Consumer for indexing the repository to provide search and IDE integration features. | |||
*/ | |||
public class NexusIndexerConsumer | |||
extends AbstractMonitoredConsumer | |||
implements KnownRepositoryContentConsumer | |||
{ | |||
private static final Logger log = LoggerFactory.getLogger(NexusIndexerConsumer.class); | |||
private static final Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class ); | |||
private final NexusIndexer indexer; | |||
@@ -60,11 +63,14 @@ public class NexusIndexerConsumer | |||
private IndexingContext context; | |||
private File managedRepository; | |||
private IndexerEngine indexerEngine; | |||
public NexusIndexerConsumer(NexusIndexer indexer, IndexPacker indexPacker) | |||
public NexusIndexerConsumer( NexusIndexer indexer, IndexPacker indexPacker, IndexerEngine indexerEngine ) | |||
{ | |||
this.indexer = indexer; | |||
this.indexPacker = indexPacker; | |||
this.indexerEngine = indexerEngine; | |||
this.artifactContextProducer = new DefaultArtifactContextProducer(); | |||
} | |||
@@ -83,44 +89,70 @@ public class NexusIndexerConsumer | |||
return false; | |||
} | |||
public void beginScan(ManagedRepositoryConfiguration repository, Date whenGathered) | |||
public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered ) | |||
throws ConsumerException | |||
{ | |||
this.repository = repository; | |||
managedRepository = new File(repository.getLocation()); | |||
File indexDirectory = new File(managedRepository, ".indexer"); | |||
managedRepository = new File( repository.getLocation() ); | |||
File indexDirectory = new File( managedRepository, ".indexer" ); | |||
repositoryContent = new ManagedDefaultRepositoryContent(); | |||
repositoryContent.setRepository(repository); | |||
repositoryContent.setRepository( repository ); | |||
synchronized (indexer) | |||
synchronized ( indexer ) | |||
{ | |||
try | |||
{ | |||
context = indexer.addIndexingContext(repository.getId(), repository.getId(), managedRepository, indexDirectory, null, null, NexusIndexer.FULL_INDEX); | |||
context.setSearchable(repository.isScanned()); | |||
context = | |||
indexer.addIndexingContext( repository.getId(), repository.getId(), managedRepository, | |||
indexDirectory, null, null, NexusIndexer.FULL_INDEX ); | |||
context.setSearchable( repository.isScanned() ); | |||
indexerEngine.beginIndexing( context ); | |||
} | |||
catch ( UnsupportedExistingLuceneIndexException e ) | |||
{ | |||
log.error( "Could not create index at " + indexDirectory.getAbsoluteFile(), e ); | |||
} | |||
catch (UnsupportedExistingLuceneIndexException e) | |||
catch ( IOException e ) | |||
{ | |||
log.error( "Could not create index at " + indexDirectory.getAbsoluteFile(), e ); | |||
} | |||
} | |||
} | |||
public void processFile( String path ) | |||
throws ConsumerException | |||
{ | |||
File artifactFile = new File( managedRepository, path ); | |||
ArtifactContext artifactContext = artifactContextProducer.getArtifactContext( context, artifactFile ); | |||
if ( artifactContext != null ) | |||
{ | |||
try | |||
{ | |||
log.error("Could not create index at " + indexDirectory.getAbsoluteFile(), e); | |||
indexer.artifactDiscovered( artifactContext, context ); | |||
indexerEngine.index( context, artifactContext ); | |||
} | |||
catch (IOException e) | |||
catch ( IOException e ) | |||
{ | |||
log.error("Could not create index at " + indexDirectory.getAbsoluteFile(), e); | |||
throw new ConsumerException( e.getMessage(), e ); | |||
} | |||
} | |||
} | |||
public void completeScan() | |||
{ | |||
final File indexLocation = new File(managedRepository, ".index"); | |||
final File indexLocation = new File( managedRepository, ".index" ); | |||
try | |||
{ | |||
indexPacker.packIndex(context, indexLocation); | |||
indexPacker.packIndex( context, indexLocation ); | |||
indexerEngine.endIndexing( context ); | |||
} | |||
catch (IOException e) | |||
catch ( IOException e ) | |||
{ | |||
log.error("Could not pack index" + indexLocation.getAbsolutePath(), e ); | |||
log.error( "Could not pack index" + indexLocation.getAbsolutePath(), e ); | |||
} | |||
} | |||
@@ -131,25 +163,6 @@ public class NexusIndexerConsumer | |||
public List<String> getIncludes() | |||
{ | |||
return Arrays.asList("**/*"); | |||
} | |||
public void processFile(String path) | |||
throws ConsumerException | |||
{ | |||
File artifactFile = new File(managedRepository, path); | |||
ArtifactContext artifactContext = artifactContextProducer.getArtifactContext(context, artifactFile); | |||
if (artifactContext != null) | |||
{ | |||
try | |||
{ | |||
indexer.artifactDiscovered(artifactContext, context); | |||
} | |||
catch (IOException e) | |||
{ | |||
throw new ConsumerException(e.getMessage(), e); | |||
} | |||
} | |||
return Arrays.asList( "**/*" ); | |||
} | |||
} |
@@ -3,8 +3,9 @@ | |||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> | |||
<bean id="indexerConsumer" class="org.apache.maven.archiva.consumers.lucene.NexusIndexerConsumer"> | |||
<bean id="indexerConsumer" class="org.apache.archiva.consumers.lucene.NexusIndexerConsumer"> | |||
<constructor-arg ref="nexusIndexer"/> | |||
<constructor-arg ref="indexPacker"/> | |||
<constructor-arg ref="indexerEngine"/> | |||
</bean> | |||
</beans> |
@@ -0,0 +1,140 @@ | |||
package org.apache.archiva.consumers.lucene; | |||
/* | |||
* Licensed to the Apache Software Foundation (ASF) under one | |||
* or more contributor license agreements. See the NOTICE file | |||
* distributed with this work for additional information | |||
* regarding copyright ownership. The ASF licenses this file | |||
* to you under the Apache License, Version 2.0 (the | |||
* "License"); you may not use this file except in compliance | |||
* with the License. You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, | |||
* software distributed under the License is distributed on an | |||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |||
* KIND, either express or implied. See the License for the | |||
* specific language governing permissions and limitations | |||
* under the License. | |||
*/ | |||
import java.io.File; | |||
import java.util.Calendar; | |||
import java.util.Date; | |||
import java.util.Set; | |||
import org.apache.commons.io.FileUtils; | |||
import org.apache.lucene.search.BooleanQuery; | |||
import org.apache.lucene.search.BooleanClause.Occur; | |||
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; | |||
import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; | |||
import org.codehaus.plexus.spring.PlexusInSpringTestCase; | |||
import org.sonatype.nexus.index.ArtifactInfo; | |||
import org.sonatype.nexus.index.FlatSearchRequest; | |||
import org.sonatype.nexus.index.FlatSearchResponse; | |||
import org.sonatype.nexus.index.NexusIndexer; | |||
import org.sonatype.nexus.index.creator.IndexerEngine; | |||
import org.sonatype.nexus.index.packer.IndexPacker; | |||
public class NexusIndexerConsumerTest | |||
extends PlexusInSpringTestCase | |||
{ | |||
private KnownRepositoryContentConsumer nexusIndexerConsumer; | |||
private ManagedRepositoryConfiguration repositoryConfig; | |||
private NexusIndexer nexusIndexer; | |||
private IndexPacker indexPacker; | |||
private IndexerEngine indexerEngine; | |||
@Override | |||
protected void setUp() | |||
throws Exception | |||
{ | |||
super.setUp(); | |||
nexusIndexer = ( NexusIndexer ) lookup( NexusIndexer.class ); | |||
indexPacker = ( IndexPacker ) lookup( IndexPacker.class ); | |||
indexerEngine = ( IndexerEngine ) lookup( IndexerEngine.class ); | |||
nexusIndexerConsumer = new NexusIndexerConsumer( nexusIndexer, indexPacker, indexerEngine ); | |||
repositoryConfig = new ManagedRepositoryConfiguration(); | |||
repositoryConfig.setId( "test-repo" ); | |||
repositoryConfig.setLocation( getBasedir() + "/target/test-classes/test-repo" ); | |||
repositoryConfig.setLayout( "default" ); | |||
repositoryConfig.setName( "Test Repository" ); | |||
repositoryConfig.setScanned( true ); | |||
repositoryConfig.setSnapshots( false ); | |||
repositoryConfig.setReleases( true ); | |||
} | |||
@Override | |||
protected void tearDown() | |||
throws Exception | |||
{ | |||
// delete created index in the repository | |||
File indexDir = new File( repositoryConfig.getLocation(), ".indexer" ); | |||
FileUtils.deleteDirectory( indexDir ); | |||
assertFalse( indexDir.exists() ); | |||
indexDir = new File( repositoryConfig.getLocation(), ".index" ); | |||
FileUtils.deleteDirectory( indexDir ); | |||
assertFalse( indexDir.exists() ); | |||
super.tearDown(); | |||
} | |||
public void testIndexerIndexArtifact() | |||
throws Exception | |||
{ | |||
// begin scan | |||
Date now = Calendar.getInstance().getTime(); | |||
nexusIndexerConsumer.beginScan( repositoryConfig, now ); | |||
// process file | |||
nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); | |||
// end scan | |||
nexusIndexerConsumer.completeScan(); | |||
// search! | |||
BooleanQuery q = new BooleanQuery(); | |||
q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD ); | |||
q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD ); | |||
FlatSearchRequest request = new FlatSearchRequest( q ); | |||
FlatSearchResponse response = nexusIndexer.searchFlat( request ); | |||
assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() ); | |||
assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() ); | |||
assertEquals( 1, response.getTotalHits() ); | |||
Set<ArtifactInfo> results = response.getResults(); | |||
ArtifactInfo artifactInfo = (ArtifactInfo) results.iterator().next(); | |||
assertEquals( "org.apache.archiva", artifactInfo.groupId ); | |||
assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId ); | |||
assertEquals( "test-repo", artifactInfo.repository ); | |||
} | |||
/*public void testIndexerIndexPom() | |||
throws Exception | |||
{ | |||
// begin scan | |||
Date now = Calendar.getInstance().getTime(); | |||
nexusIndexerConsumer.beginScan( repositoryConfig, now ); | |||
// process file | |||
//nexusIndexerConsumer.processFile( ) | |||
// end scan | |||
// search! | |||
}*/ | |||
} |