public void executeTask( Task task )
throws TaskExecutionException
{
synchronized( indexerEngine )
{
ArtifactIndexingTask indexingTask = ( ArtifactIndexingTask ) task;
ManagedRepositoryConfiguration repository =
archivaConfiguration.getConfiguration().findManagedRepositoryById( indexingTask.getRepositoryId() );
String indexDir = repository.getIndexDir();
File managedRepository = new File( repository.getLocation() );
File indexDirectory = null;
if( indexDir != null && !"".equals( indexDir ) )
{
indexDirectory = new File( repository.getIndexDir() );
}
else
{
indexDirectory = new File( managedRepository, ".indexer" );
}
IndexingContext context = null;
try
{
context =
new DefaultIndexingContext( repository.getId(), repository.getId(), managedRepository,
indexDirectory, null, null, NexusIndexer.FULL_INDEX, false );
context.setSearchable( repository.isScanned() );
File artifactFile = indexingTask.getResourceFile();
ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
if( ac != null )
{
if( indexingTask.getAction().equals( ArtifactIndexingTask.ADD ) )
{
boolean add = true;
IndexReader r = context.getIndexReader();
for ( int i = 0; i < r.numDocs(); i++ )
{
if ( !r.isDeleted( i ) )
{
Document d = r.document( i );
String uinfo = d.get( ArtifactInfo.UINFO );
if( ac.getArtifactInfo().getUinfo().equals( uinfo ) )
{
add = false;
break;
}
}
}
if( add )
{
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.." );
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() );