prevent NPE on initial scan at repository creation

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1232213 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-01-16 22:57:03 +00:00
parent 9cc9c4d3d6
commit b15679f6a9
2 changed files with 47 additions and 10 deletions

View File

@ -72,7 +72,7 @@ public class NexusIndexerConsumer
private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler; private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
private IndexingContext context; private IndexingContext indexingContext;
private NexusIndexer nexusIndexer; private NexusIndexer nexusIndexer;
@ -124,7 +124,7 @@ public class NexusIndexerConsumer
try try
{ {
log.info( "Creating indexing context for repo : {}", repository.getId() ); log.info( "Creating indexing context for repo : {}", repository.getId() );
context = managedRepositoryAdmin.createIndexContext( repository ); indexingContext = managedRepositoryAdmin.createIndexContext( repository );
} }
catch ( RepositoryAdminException e ) catch ( RepositoryAdminException e )
{ {
@ -152,7 +152,7 @@ public class NexusIndexerConsumer
File artifactFile = new File( managedRepository, path ); File artifactFile = new File( managedRepository, path );
ArtifactIndexingTask task = ArtifactIndexingTask task =
new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, context ); new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, getIndexingContext() );
try try
{ {
log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task ); log.debug( "Queueing indexing task '{}' to add or update the artifact in the index.", task );
@ -177,7 +177,8 @@ public class NexusIndexerConsumer
// specify in indexing task that this is not a repo scan request! // specify in indexing task that this is not a repo scan request!
ArtifactIndexingTask task = ArtifactIndexingTask task =
new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, context, false ); new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD,
getIndexingContext(), false );
// only update index we don't need to scan the full repo here // only update index we don't need to scan the full repo here
task.setOnlyUpdate( true ); task.setOnlyUpdate( true );
try try
@ -194,6 +195,19 @@ public class NexusIndexerConsumer
public void completeScan() public void completeScan()
{ {
IndexingContext context = this.indexingContext;
if ( context == null )
{
try
{
context = getIndexingContext();
}
catch ( ConsumerException e )
{
log.warn( "failed to get an IndexingContext:{}", e.getMessage() );
return;
}
}
ArtifactIndexingTask task = ArtifactIndexingTask task =
new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context ); new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
try try
@ -205,7 +219,6 @@ public class NexusIndexerConsumer
{ {
log.error( "Error queueing task: " + task + ": " + e.getMessage(), e ); log.error( "Error queueing task: " + task + ": " + e.getMessage(), e );
} }
context = null;
} }
public void completeScan( boolean executeOnEntireRepo ) public void completeScan( boolean executeOnEntireRepo )
@ -260,4 +273,23 @@ public class NexusIndexerConsumer
{ {
return includes; return includes;
} }
private IndexingContext getIndexingContext()
throws ConsumerException
{
if ( this.indexingContext == null )
{
try
{
indexingContext = managedRepositoryAdmin.createIndexContext( repository );
}
catch ( RepositoryAdminException e )
{
throw new ConsumerException( e.getMessage(), e );
}
}
return indexingContext;
}
} }

View File

@ -576,12 +576,17 @@ public class DefaultManagedRepositoryAdmin
indexDirectory.mkdirs(); indexDirectory.mkdirs();
} }
context = context = indexer.getIndexingContexts().get( repository.getId() );
indexer.addIndexingContext( repository.getId(), repository.getId(), managedRepository, indexDirectory,
managedRepository.toURI().toURL().toExternalForm(),
indexDirectory.toURI().toURL().toString(), indexCreators );
context.setSearchable( repository.isScanned() ); if ( context == null )
{
context = indexer.addIndexingContext( repository.getId(), repository.getId(), managedRepository,
indexDirectory,
managedRepository.toURI().toURL().toExternalForm(),
indexDirectory.toURI().toURL().toString(), indexCreators );
context.setSearchable( repository.isScanned() );
}
return context; return context;
} }
catch ( MalformedURLException e ) catch ( MalformedURLException e )