public void executeTask( Task task )
throws TaskExecutionException
{
synchronized ( indexerEngine )
{
ArtifactIndexingTask indexingTask = (ArtifactIndexingTask) task;
ManagedRepositoryConfiguration repository = indexingTask.getRepository();
IndexingContext context = indexingTask.getContext();
if ( ArtifactIndexingTask.Action.FINISH.equals( indexingTask.getAction() )
&& indexingTask.isExecuteOnEntireRepo() )
{
log.debug( "Finishing indexing task on repo: " + repository.getId() );
finishIndexingTask( indexingTask, repository, context );
}
else
{
// create context if not a repo scan request
if( !indexingTask.isExecuteOnEntireRepo() )
{
try
{
log.debug( "Creating indexing context on resource: " + indexingTask.getResourceFile().getPath() );
context = TaskCreator.createContext( repository );
}
catch( IOException e )
{
log.error( "Error occurred while creating context: " + e.getMessage() );
throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage() );
}
catch( UnsupportedExistingLuceneIndexException e )
{
log.error( "Error occurred while creating context: " + e.getMessage() );
throw new TaskExecutionException( "Error occurred while creating context: " + e.getMessage() );
}
}
if ( context == null || context.getIndexDirectory() == null )
{
throw new TaskExecutionException( "Trying to index an artifact but the context is already closed" );
}
try
{
File artifactFile = indexingTask.getResourceFile();
ArtifactContext ac = artifactContextProducer.getArtifactContext( context, artifactFile );
if ( ac != null )
{
if ( indexingTask.getAction().equals( ArtifactIndexingTask.Action.ADD ) )
{
IndexSearcher s = context.getIndexSearcher();
String uinfo = ac.getArtifactInfo().getUinfo();
TopDocs d = s.search( new TermQuery( new Term( ArtifactInfo.UINFO, uinfo ) ), 1 );
if ( d.totalHits == 0 )
{
log.debug( "Adding artifact '" + ac.getArtifactInfo() + "' to index.." );
indexerEngine.index( context, ac );
context.getIndexWriter().commit();
}
else
{
log.debug( "Updating artifact '" + ac.getArtifactInfo() + "' in index.." );
indexerEngine.update( context, ac );
context.getIndexWriter().commit();
}
// close the context if not a repo scan request
if( !indexingTask.isExecuteOnEntireRepo() )
{
log.debug( "Finishing indexing task on resource file : " + indexingTask.getResourceFile().getPath() );
finishIndexingTask( indexingTask, repository, context );
}
}
else
{