From 3cb6dd196aca8138498b2384382a35d71fbc9cf9 Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Wed, 19 Aug 2009 10:30:53 +0000 Subject: [PATCH] [MRM-1155] m2eclipse - getting index from archiva 1.2 failed o optimize index before packaging o added test case git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@805736 13f79535-47bb-0310-9956-ffa450edef68 --- .../ArchivaIndexingTaskExecutor.java | 21 +++-- .../ArchivaIndexingTaskExecutorTest.java | 85 +++++++++++++++++++ 2 files changed, 99 insertions(+), 7 deletions(-) diff --git a/archiva-modules/archiva-scheduled/src/main/java/org/apache/maven/archiva/scheduled/executors/ArchivaIndexingTaskExecutor.java b/archiva-modules/archiva-scheduled/src/main/java/org/apache/maven/archiva/scheduled/executors/ArchivaIndexingTaskExecutor.java index 31d0a9f96..c805c3f2c 100644 --- a/archiva-modules/archiva-scheduled/src/main/java/org/apache/maven/archiva/scheduled/executors/ArchivaIndexingTaskExecutor.java +++ b/archiva-modules/archiva-scheduled/src/main/java/org/apache/maven/archiva/scheduled/executors/ArchivaIndexingTaskExecutor.java @@ -47,13 +47,12 @@ import org.sonatype.nexus.index.packer.IndexPacker; import org.sonatype.nexus.index.packer.IndexPackingRequest; /** - * ArchivaIndexingTaskExecutor - * - * Executes all indexing tasks. Adding, updating and removing artifacts from the index are all performed by - * this executor. Add and update artifact in index tasks are added in the indexing task queue by the NexusIndexerConsumer while - * remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer. + * ArchivaIndexingTaskExecutor Executes all indexing tasks. Adding, updating and removing artifacts from the index are + * all performed by this executor. Add and update artifact in index tasks are added in the indexing task queue by the + * NexusIndexerConsumer while remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer. * * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor" role-hint="indexing" + * instantiation-strategy="singleton" */ public class ArchivaIndexingTaskExecutor implements TaskExecutor, Initializable @@ -135,31 +134,38 @@ public class ArchivaIndexingTaskExecutor { log.debug( "Adding artifact '" + ac.getArtifactInfo() + "' to index.." ); indexerEngine.index( context, ac ); + context.optimize(); } else { log.debug( "Updating artifact '" + ac.getArtifactInfo() + "' in index.." ); indexerEngine.update( context, ac ); + context.optimize(); } } else { - log.debug( "removing artifact '" + ac.getArtifactInfo() + "' from index.." ); + log.debug( "Removing artifact '" + ac.getArtifactInfo() + "' from index.." ); indexerEngine.remove( context, ac ); + context.optimize(); } final File indexLocation = new File( managedRepository, ".index" ); IndexPackingRequest request = new IndexPackingRequest( context, indexLocation ); indexPacker.packIndex( request ); + + log.debug( "Index file packaged at '" + indexLocation.getPath() + "'." ); } } catch ( IOException e ) { + log.error( "Error occurred while executing indexing task '" + indexingTask.getName() + "'" ); throw new TaskExecutionException( "Error occurred while executing indexing task '" + indexingTask.getName() + "'" ); } catch ( UnsupportedExistingLuceneIndexException e ) { + log.error( "Unsupported Lucene index format: " + e.getMessage() ); throw new TaskExecutionException( "Unsupported Lucene index format: " + e.getMessage() ); } finally @@ -167,11 +173,12 @@ public class ArchivaIndexingTaskExecutor if( context != null ) { try - { + { context.close( false ); } catch ( IOException e ) { + log.error( "Error occurred while closing context: " + e.getMessage() ); throw new TaskExecutionException( "Error occurred while closing context: " + e.getMessage() ); } } diff --git a/archiva-modules/archiva-scheduled/src/test/java/org/apache/maven/archiva/scheduled/executors/ArchivaIndexingTaskExecutorTest.java b/archiva-modules/archiva-scheduled/src/test/java/org/apache/maven/archiva/scheduled/executors/ArchivaIndexingTaskExecutorTest.java index 135909980..9c97364e5 100644 --- a/archiva-modules/archiva-scheduled/src/test/java/org/apache/maven/archiva/scheduled/executors/ArchivaIndexingTaskExecutorTest.java +++ b/archiva-modules/archiva-scheduled/src/test/java/org/apache/maven/archiva/scheduled/executors/ArchivaIndexingTaskExecutorTest.java @@ -19,8 +19,16 @@ package org.apache.maven.archiva.scheduled.executors; * under the License. */ +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.Set; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; import org.apache.commons.io.FileUtils; import org.apache.lucene.search.BooleanQuery; @@ -234,4 +242,81 @@ public class ArchivaIndexingTaskExecutorTest assertEquals( 0, topDocs.totalHits ); } + public void testPackagedIndex() + throws Exception + { + File artifactFile = + new File( repositoryConfig.getLocation(), + "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); + + ArtifactIndexingTask task = + TaskCreator.createIndexingTask( repositoryConfig.getId(), artifactFile, ArtifactIndexingTask.ADD ); + + archivaConfigControl.expectAndReturn( archivaConfiguration.getConfiguration(), configuration ); + + archivaConfigControl.replay(); + + indexingExecutor.executeTask( task ); + + archivaConfigControl.verify(); + + assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() ); + assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() ); + + // unpack .zip index + File destDir = new File( repositoryConfig.getLocation(), ".index/tmp" ); + unzipIndex( new File( repositoryConfig.getLocation(), ".index" ).getPath(), destDir.getPath() ); + + BooleanQuery q = new BooleanQuery(); + q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD ); + q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD ); + + IndexingContext context = + indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(), + new File( repositoryConfig.getLocation() ), destDir, null, null, + NexusIndexer.FULL_INDEX ); + context.setSearchable( true ); + + FlatSearchRequest request = new FlatSearchRequest( q ); + FlatSearchResponse response = indexer.searchFlat( request ); + + assertEquals( 1, response.getTotalHits() ); + + Set 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 ); + } + + private void unzipIndex( String indexDir, String destDir ) + throws FileNotFoundException, IOException + { + final int buff = 2048; + + new File( destDir ).mkdirs(); + + BufferedOutputStream out = null; + FileInputStream fin = new FileInputStream( new File( indexDir, "nexus-maven-repository-index.zip" ) ); + ZipInputStream in = new ZipInputStream( new BufferedInputStream( fin ) ); + ZipEntry entry; + + while ( ( entry = in.getNextEntry() ) != null ) + { + int count; + byte data[] = new byte[buff]; + FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) ); + out = new BufferedOutputStream( fout, buff ); + + while ( ( count = in.read( data, 0, buff ) ) != -1 ) + { + out.write( data, 0, count ); + } + out.flush(); + out.close(); + } + + in.close(); + } } -- 2.39.5