From a20d601f8e605ba62ffc74c8de94cc5a892c9d5b Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Sun, 26 Jun 2011 20:54:21 +0000 Subject: [PATCH] [MRM-1345] update use of Nexus indexer to use Maven indexer remove the per-lookup component refactor test to use only NexusIndexer facade git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1139940 13f79535-47bb-0310-9956-ffa450edef68 --- .../archiva-base/archiva-common/pom.xml | 4 +- .../common/utils/ArchivaNexusIndexerUtil.java | 8 +- .../archiva-lucene-consumers/pom.xml | 4 +- .../archiva-base/archiva-indexer/pom.xml | 4 +- .../indexer/search/NexusRepositorySearch.java | 35 ++--- .../resources/META-INF/plexus/components.xml | 18 ++- .../search/NexusRepositorySearchTest.java | 121 ++++++++++++++---- .../archiva-scheduler-indexing/pom.xml | 4 +- pom.xml | 8 +- 9 files changed, 138 insertions(+), 68 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-common/pom.xml b/archiva-modules/archiva-base/archiva-common/pom.xml index 32ea5a0f1..158e11984 100644 --- a/archiva-modules/archiva-base/archiva-common/pom.xml +++ b/archiva-modules/archiva-base/archiva-common/pom.xml @@ -57,8 +57,8 @@ compile - org.sonatype.nexus - nexus-indexer + org.apache.maven.indexer + indexer-core javax.inject diff --git a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/ArchivaNexusIndexerUtil.java b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/ArchivaNexusIndexerUtil.java index ebac81159..98be33efd 100644 --- a/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/ArchivaNexusIndexerUtil.java +++ b/archiva-modules/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/ArchivaNexusIndexerUtil.java @@ -22,10 +22,10 @@ package org.apache.maven.archiva.common.utils; import java.util.Arrays; import java.util.List; -import org.sonatype.nexus.index.context.IndexCreator; -import org.sonatype.nexus.index.creator.JarFileContentsIndexCreator; -import org.sonatype.nexus.index.creator.MavenPluginArtifactInfoIndexCreator; -import org.sonatype.nexus.index.creator.MinimalArtifactInfoIndexCreator; +import org.apache.maven.index.context.IndexCreator; +import org.apache.maven.index.creator.JarFileContentsIndexCreator; +import org.apache.maven.index.creator.MavenPluginArtifactInfoIndexCreator; +import org.apache.maven.index.creator.MinimalArtifactInfoIndexCreator; /** * ArchivaNexusIndexerUtil diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/pom.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/pom.xml index 1cf58da2c..059f715bc 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/pom.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/pom.xml @@ -63,8 +63,8 @@ commons-io - org.sonatype.nexus - nexus-indexer + org.apache.maven.indexer + indexer-core javax.inject diff --git a/archiva-modules/archiva-base/archiva-indexer/pom.xml b/archiva-modules/archiva-base/archiva-indexer/pom.xml index 4a421f63d..61f449ee3 100644 --- a/archiva-modules/archiva-base/archiva-indexer/pom.xml +++ b/archiva-modules/archiva-base/archiva-indexer/pom.xml @@ -63,8 +63,8 @@ commons-io - org.sonatype.nexus - nexus-indexer + org.apache.maven.indexer + indexer-core org.sonatype.sisu diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java index 2c72edc82..641c635a1 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/java/org/apache/archiva/indexer/search/NexusRepositorySearch.java @@ -28,14 +28,17 @@ import org.apache.maven.archiva.common.utils.ArchivaNexusIndexerUtil; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.index.ArtifactInfo; +import org.apache.maven.index.Field; +import org.apache.maven.index.FlatSearchRequest; +import org.apache.maven.index.FlatSearchResponse; +import org.apache.maven.index.MAVEN; +import org.apache.maven.index.NexusIndexer; +import org.apache.maven.index.context.IndexingContext; +import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException; +import org.apache.maven.index.expr.StringSearchExpression; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -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.context.IndexingContext; -import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; @@ -120,27 +123,27 @@ public class NexusRepositorySearch BooleanQuery q = new BooleanQuery(); if ( searchFields.getGroupId() != null && !"".equals( searchFields.getGroupId() ) ) { - q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, searchFields.getGroupId() ), Occur.MUST ); + q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( searchFields.getGroupId() ) ), Occur.MUST ); } if ( searchFields.getArtifactId() != null && !"".equals( searchFields.getArtifactId() ) ) { - q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, searchFields.getArtifactId() ), Occur.MUST ); + q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( searchFields.getArtifactId() ) ), Occur.MUST ); } if ( searchFields.getVersion() != null && !"".equals( searchFields.getVersion() ) ) { - q.add( indexer.constructQuery( ArtifactInfo.VERSION, searchFields.getVersion() ), Occur.MUST ); + q.add( indexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( searchFields.getVersion() ) ), Occur.MUST ); } if ( searchFields.getPackaging() != null && !"".equals( searchFields.getPackaging() ) ) { - q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, searchFields.getPackaging() ), Occur.MUST ); + q.add( indexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( searchFields.getPackaging() ) ), Occur.MUST ); } if ( searchFields.getClassName() != null && !"".equals( searchFields.getClassName() ) ) { - q.add( indexer.constructQuery( ArtifactInfo.NAMES, searchFields.getClassName() ), Occur.MUST ); + q.add( indexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( searchFields.getClassName( ) ) ), Occur.MUST ); } if ( q.getClauses() == null || q.getClauses().length <= 0 ) @@ -194,11 +197,11 @@ public class NexusRepositorySearch private void constructQuery( String term, BooleanQuery q ) { - q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, term ), Occur.SHOULD ); - q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, term ), Occur.SHOULD ); - q.add( indexer.constructQuery( ArtifactInfo.VERSION, term ), Occur.SHOULD ); - q.add( indexer.constructQuery( ArtifactInfo.PACKAGING, term ), Occur.SHOULD ); - q.add( indexer.constructQuery( ArtifactInfo.NAMES, term ), Occur.SHOULD ); + q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( term ) ), Occur.SHOULD ); + q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( term ) ), Occur.SHOULD ); + q.add( indexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( term ) ), Occur.SHOULD ); + q.add( indexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( term ) ), Occur.SHOULD ); + q.add( indexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( term ) ), Occur.SHOULD ); } diff --git a/archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/plexus/components.xml b/archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/plexus/components.xml index 8e03700cc..a6d123f83 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/plexus/components.xml +++ b/archiva-modules/archiva-base/archiva-indexer/src/main/resources/META-INF/plexus/components.xml @@ -17,39 +17,37 @@ ~ specific language governing permissions and limitations ~ under the License. --> - + diff --git a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java index 3b1a83da3..88c1da5d1 100644 --- a/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java +++ b/archiva-modules/archiva-base/archiva-indexer/src/test/java/org/apache/archiva/indexer/search/NexusRepositorySearchTest.java @@ -27,19 +27,19 @@ import org.apache.maven.archiva.common.utils.FileUtil; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.index.ArtifactContext; +import org.apache.maven.index.ArtifactContextProducer; +import org.apache.maven.index.IndexerEngine; +import org.apache.maven.index.NexusIndexer; +import org.apache.maven.index.artifact.IllegalArtifactCoordinateException; +import org.apache.maven.index.context.DefaultIndexingContext; +import org.apache.maven.index.context.IndexingContext; +import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException; import org.easymock.MockControl; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.sonatype.nexus.artifact.IllegalArtifactCoordinateException; -import org.sonatype.nexus.index.ArtifactContext; -import org.sonatype.nexus.index.ArtifactContextProducer; -import org.sonatype.nexus.index.IndexerEngine; -import org.sonatype.nexus.index.NexusIndexer; -import org.sonatype.nexus.index.context.DefaultIndexingContext; -import org.sonatype.nexus.index.context.IndexingContext; -import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -59,11 +59,9 @@ public class NexusRepositorySearchTest private ArchivaConfiguration archivaConfig; - private NexusIndexer indexer; + //private DefaultIndexingContext context; - private IndexingContext context; - - private IndexerEngine indexerEngine; + //private IndexerEngine indexerEngine; private ArtifactContextProducer artifactContextProducer; @@ -71,20 +69,40 @@ public class NexusRepositorySearchTest private Configuration config; - private final static String TEST_REPO_1 = "nexus-search-test-repo"; + private static String TEST_REPO_1 = "nexus-search-test-repo"; - private final static String TEST_REPO_2 = "nexus-search-test-repo-2"; + private static String TEST_REPO_2 = "nexus-search-test-repo-2"; + + private static int TEST_NUMBER = 0; @Inject PlexusSisuBridge plexusSisuBridge; + NexusIndexer nexusIndexer; + @Before public void setUp() throws Exception { super.setUp(); - indexer = plexusSisuBridge.lookup( NexusIndexer.class ); + //to prevent failure during obtain lock change name + + System.gc(); + + TEST_NUMBER++; + + //TEST_REPO_1 = TEST_REPO_1 + TEST_NUMBER; + + //TEST_REPO_2 = TEST_REPO_2 + TEST_NUMBER; + + FileUtils.deleteDirectory( + new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ) ); + assertFalse( new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ).exists() ); + + FileUtils.deleteDirectory( + new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) ); + assertFalse( new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() ); archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class ); @@ -92,7 +110,9 @@ public class NexusRepositorySearchTest search = new NexusRepositorySearch( plexusSisuBridge, archivaConfig ); - indexerEngine = plexusSisuBridge.lookup( IndexerEngine.class ); + //indexerEngine = plexusSisuBridge.lookup( IndexerEngine.class ); + + nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class ); artifactContextProducer = plexusSisuBridge.lookup( ArtifactContextProducer.class ); @@ -155,10 +175,30 @@ public class NexusRepositorySearchTest public void tearDown() throws Exception { - FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ) ); + /* + + if ( context != null ) + { + context.unlock(); + context.unlockExclusively(); + context.close( true ); + } + */ + + for (IndexingContext indexingContext : nexusIndexer.getIndexingContexts().values()) + { + //indexingContext.close( true ); + nexusIndexer.removeIndexingContext( indexingContext, true ); + } + + + + FileUtils.deleteDirectory( + new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ) ); assertFalse( new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_1 + "/.indexer" ).exists() ); - FileUtils.deleteDirectory( new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) ); + FileUtils.deleteDirectory( + new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ) ); assertFalse( new File( FileUtil.getBasedir(), "/target/test-classes/" + TEST_REPO_2 + "/.indexer" ).exists() ); super.tearDown(); @@ -167,28 +207,57 @@ public class NexusRepositorySearchTest private void createIndex( String repository, List filesToBeIndexed ) throws IOException, UnsupportedExistingLuceneIndexException, IllegalArtifactCoordinateException { + + File indexerDirectory = new File( FileUtil.getBasedir(), "/target/test-classes/" + repository + "/.indexer" ); + + if ( indexerDirectory.exists() ) + { + FileUtils.deleteDirectory( indexerDirectory ); + } + + assertFalse( indexerDirectory.exists() ); + + File lockFile = + new File( FileUtil.getBasedir(), "/target/test-classes/" + repository + "/.indexer/write.lock" ); + if ( lockFile.exists() ) + { + lockFile.delete(); + } + + //IndexWriter.unlock( FSDirectory.open( lockFile.getParentFile()) ); + + assertFalse( lockFile.exists() ); + + /* context = new DefaultIndexingContext( repository, repository, new File( FileUtil.getBasedir(), "/target/test-classes/" + repository ), new File( FileUtil.getBasedir(), "/target/test-classes/" + repository + "/.indexer" ), null, null, ArchivaNexusIndexerUtil.FULL_INDEX, false ); - //indexer.addIndexingContext( repository, repository, new File( getBasedir(), "/target/test-classes/" + - // repository ), new File( getBasedir(), "/target/test-classes/" + repository + "/.indexer" ), null, null, - // NexusIndexer.FULL_INDEX ); context.setSearchable( true ); + */ - //indexerEngine.beginIndexing( context ); + File repo = new File( FileUtil.getBasedir(), "/target/test-classes/" + repository ); + File indexDirectory = new File( FileUtil.getBasedir(), "/target/test-classes/" + repository + "/.indexer" ); + //String id, String repositoryId, File repository, File indexDirectory, + // String repositoryUrl, String indexUpdateUrl, + // List indexers + IndexingContext context = nexusIndexer.addIndexingContext( repository, repository, repo, indexDirectory, + repo.toURI().toURL().toExternalForm(), + indexDirectory.toURI().toURL().toString(), ArchivaNexusIndexerUtil.FULL_INDEX ); + + List artifactContexts = new ArrayList( filesToBeIndexed.size() ); for ( File artifactFile : filesToBeIndexed ) { ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile ); - indexerEngine.index( context, ac ); + artifactContexts.add( ac ); } - context.close( false ); - //indexerEngine.endIndexing( context ); - //indexer.removeIndexingContext( context, false ); + nexusIndexer.addArtifactsToIndex( artifactContexts, context ); + nexusIndexer.scan( context ); + //context.close( false ); assertTrue( new File( FileUtil.getBasedir(), "/target/test-classes/" + repository + "/.indexer" ).exists() ); } diff --git a/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/pom.xml b/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/pom.xml index 4c3dbabf7..647efa5db 100644 --- a/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/pom.xml +++ b/archiva-modules/archiva-scheduler/archiva-scheduler-indexing/pom.xml @@ -19,8 +19,8 @@ archiva-configuration - org.sonatype.nexus - nexus-indexer + org.apache.maven.indexer + indexer-core javax.inject diff --git a/pom.xml b/pom.xml index a6ec7aad8..d5f0d5f66 100644 --- a/pom.xml +++ b/pom.xml @@ -74,7 +74,7 @@ 2.2.5 UTF-8 true - 2.4.1 + 3.0.3 @@ -235,9 +235,9 @@ 1.4-SNAPSHOT - org.sonatype.nexus - nexus-indexer - 3.0.1 + org.apache.maven.indexer + indexer-core + 4.1.1 org.codehaus.plexus