[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
This commit is contained in:
Maria Odea B. Ching 2009-08-19 10:30:53 +00:00
parent f4cc3f7208
commit 3cb6dd196a
2 changed files with 99 additions and 7 deletions

View File

@ -47,13 +47,12 @@ import org.sonatype.nexus.index.packer.IndexPacker;
import org.sonatype.nexus.index.packer.IndexPackingRequest; import org.sonatype.nexus.index.packer.IndexPackingRequest;
/** /**
* ArchivaIndexingTaskExecutor * 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
* Executes all indexing tasks. Adding, updating and removing artifacts from the index are all performed by * NexusIndexerConsumer while remove artifact from index tasks are added by the LuceneCleanupRemoveIndexedConsumer.
* 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" * @plexus.component role="org.codehaus.plexus.taskqueue.execution.TaskExecutor" role-hint="indexing"
* instantiation-strategy="singleton"
*/ */
public class ArchivaIndexingTaskExecutor public class ArchivaIndexingTaskExecutor
implements TaskExecutor, Initializable implements TaskExecutor, Initializable
@ -135,31 +134,38 @@ public class ArchivaIndexingTaskExecutor
{ {
log.debug( "Adding artifact '" + ac.getArtifactInfo() + "' to index.." ); log.debug( "Adding artifact '" + ac.getArtifactInfo() + "' to index.." );
indexerEngine.index( context, ac ); indexerEngine.index( context, ac );
context.optimize();
} }
else else
{ {
log.debug( "Updating artifact '" + ac.getArtifactInfo() + "' in index.." ); log.debug( "Updating artifact '" + ac.getArtifactInfo() + "' in index.." );
indexerEngine.update( context, ac ); indexerEngine.update( context, ac );
context.optimize();
} }
} }
else else
{ {
log.debug( "removing artifact '" + ac.getArtifactInfo() + "' from index.." ); log.debug( "Removing artifact '" + ac.getArtifactInfo() + "' from index.." );
indexerEngine.remove( context, ac ); indexerEngine.remove( context, ac );
context.optimize();
} }
final File indexLocation = new File( managedRepository, ".index" ); final File indexLocation = new File( managedRepository, ".index" );
IndexPackingRequest request = new IndexPackingRequest( context, indexLocation ); IndexPackingRequest request = new IndexPackingRequest( context, indexLocation );
indexPacker.packIndex( request ); indexPacker.packIndex( request );
log.debug( "Index file packaged at '" + indexLocation.getPath() + "'." );
} }
} }
catch ( IOException e ) catch ( IOException e )
{ {
log.error( "Error occurred while executing indexing task '" + indexingTask.getName() + "'" );
throw new TaskExecutionException( "Error occurred while executing indexing task '" + throw new TaskExecutionException( "Error occurred while executing indexing task '" +
indexingTask.getName() + "'" ); indexingTask.getName() + "'" );
} }
catch ( UnsupportedExistingLuceneIndexException e ) catch ( UnsupportedExistingLuceneIndexException e )
{ {
log.error( "Unsupported Lucene index format: " + e.getMessage() );
throw new TaskExecutionException( "Unsupported Lucene index format: " + e.getMessage() ); throw new TaskExecutionException( "Unsupported Lucene index format: " + e.getMessage() );
} }
finally finally
@ -167,11 +173,12 @@ public class ArchivaIndexingTaskExecutor
if( context != null ) if( context != null )
{ {
try try
{ {
context.close( false ); context.close( false );
} }
catch ( IOException e ) catch ( IOException e )
{ {
log.error( "Error occurred while closing context: " + e.getMessage() );
throw new TaskExecutionException( "Error occurred while closing context: " + e.getMessage() ); throw new TaskExecutionException( "Error occurred while closing context: " + e.getMessage() );
} }
} }

View File

@ -19,8 +19,16 @@ package org.apache.maven.archiva.scheduled.executors;
* under the License. * under the License.
*/ */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File; 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.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
@ -234,4 +242,81 @@ public class ArchivaIndexingTaskExecutorTest
assertEquals( 0, topDocs.totalHits ); 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<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 );
}
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();
}
} }