]> source.dussan.org Git - archiva.git/commitdiff
prevent NPE on initial scan at repository creation
authorOlivier Lamy <olamy@apache.org>
Mon, 16 Jan 2012 22:57:03 +0000 (22:57 +0000)
committerOlivier Lamy <olamy@apache.org>
Mon, 16 Jan 2012 22:57:03 +0000 (22:57 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1232213 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java
archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java

index 070e7b11c539994e81ac3fe3d326230c1f020fbb..9cbff9bd1f50626442a1ef71528f671c25aea916 100644 (file)
@@ -72,7 +72,7 @@ public class NexusIndexerConsumer
 
     private ArchivaTaskScheduler<ArtifactIndexingTask> scheduler;
 
-    private IndexingContext context;
+    private IndexingContext indexingContext;
 
     private NexusIndexer nexusIndexer;
 
@@ -124,7 +124,7 @@ public class NexusIndexerConsumer
         try
         {
             log.info( "Creating indexing context for repo : {}", repository.getId() );
-            context = managedRepositoryAdmin.createIndexContext( repository );
+            indexingContext = managedRepositoryAdmin.createIndexContext( repository );
         }
         catch ( RepositoryAdminException e )
         {
@@ -152,7 +152,7 @@ public class NexusIndexerConsumer
         File artifactFile = new File( managedRepository, path );
 
         ArtifactIndexingTask task =
-            new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, context );
+            new ArtifactIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.ADD, getIndexingContext() );
         try
         {
             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!
             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
             task.setOnlyUpdate( true );
             try
@@ -194,6 +195,19 @@ public class NexusIndexerConsumer
 
     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 =
             new ArtifactIndexingTask( repository, null, ArtifactIndexingTask.Action.FINISH, context );
         try
@@ -205,7 +219,6 @@ public class NexusIndexerConsumer
         {
             log.error( "Error queueing task: " + task + ": " + e.getMessage(), e );
         }
-        context = null;
     }
 
     public void completeScan( boolean executeOnEntireRepo )
@@ -260,4 +273,23 @@ public class NexusIndexerConsumer
     {
         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;
+    }
 }
index 495fc82125e3a0c23179cd16c312149a29de6e0a..d78d6413698a0a5e9011794c270ee9c68bf58c85 100644 (file)
@@ -576,12 +576,17 @@ public class DefaultManagedRepositoryAdmin
                 indexDirectory.mkdirs();
             }
 
-            context =
-                indexer.addIndexingContext( repository.getId(), repository.getId(), managedRepository, indexDirectory,
-                                            managedRepository.toURI().toURL().toExternalForm(),
-                                            indexDirectory.toURI().toURL().toString(), indexCreators );
+            context = indexer.getIndexingContexts().get( repository.getId() );
 
-            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;
         }
         catch ( MalformedURLException e )