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
{
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
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() );
}
}
* 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;
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();
+ }
}