]> source.dussan.org Git - archiva.git/commitdiff
correctly close index on shutdown
authorOlivier Lamy <olamy@apache.org>
Wed, 2 Nov 2011 23:19:46 +0000 (23:19 +0000)
committerOlivier Lamy <olamy@apache.org>
Wed, 2 Nov 2011 23:19:46 +0000 (23:19 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1196856 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java
archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/remote/DefaultRemoteRepositoryAdmin.java

index f1ad9c598147b79b122c639b7a0a423b9f0f0954..9bb5cfd2d575a184d0f0ce0b667bd5ab96d4d1ff 100644 (file)
@@ -55,6 +55,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Named;
 import java.io.File;
@@ -104,10 +105,24 @@ public class DefaultManagedRepositoryAdmin
     @Inject
     protected RoleManager roleManager;
 
+    // fields
+    List<? extends IndexCreator> indexCreators;
+
+    NexusIndexer indexer;
+
     @PostConstruct
     private void initialize()
         throws RepositoryAdminException
     {
+        try
+        {
+            indexCreators = mavenIndexerUtils.getAllIndexCreators();
+            indexer = plexusSisuBridge.lookup( NexusIndexer.class );
+        }
+        catch ( PlexusSisuBridgeException e )
+        {
+            throw new RepositoryAdminException( e.getMessage(), e );
+        }
         // initialize index context on start
         for ( ManagedRepository managedRepository : getManagedRepositories() )
         {
@@ -115,6 +130,28 @@ public class DefaultManagedRepositoryAdmin
         }
     }
 
+    @PreDestroy
+    private void shutdown()
+        throws RepositoryAdminException
+    {
+        try
+        {
+            // close index on shutdown
+            for ( ManagedRepository managedRepository : getManagedRepositories() )
+            {
+                IndexingContext context = indexer.getIndexingContexts().get( managedRepository.getId() );
+                if ( context != null )
+                {
+                    indexer.removeIndexingContext( context, false );
+                }
+            }
+        }
+        catch ( IOException e )
+        {
+            throw new RepositoryAdminException( e.getMessage(), e );
+        }
+    }
+
     public List<ManagedRepository> getManagedRepositories()
         throws RepositoryAdminException
     {
@@ -523,8 +560,6 @@ public class DefaultManagedRepositoryAdmin
     {
         try
         {
-            List<? extends IndexCreator> indexCreators = mavenIndexerUtils.getAllIndexCreators();
-            NexusIndexer indexer = plexusSisuBridge.lookup( NexusIndexer.class );
 
             IndexingContext context = indexer.getIndexingContexts().get( repository.getId() );
 
@@ -567,10 +602,6 @@ public class DefaultManagedRepositoryAdmin
         {
             throw new RepositoryAdminException( e.getMessage(), e );
         }
-        catch ( PlexusSisuBridgeException e )
-        {
-            throw new RepositoryAdminException( e.getMessage(), e );
-        }
         catch ( UnsupportedExistingLuceneIndexException e )
         {
             throw new RepositoryAdminException( e.getMessage(), e );
index 4c703c5cc9605968c2f759f4cd438a174729f6c0..17871f098425b8b5ab8f48d5b0d652ca75e1c833 100644 (file)
@@ -38,6 +38,7 @@ import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import java.io.File;
 import java.io.IOException;
@@ -63,16 +64,52 @@ public class DefaultRemoteRepositoryAdmin
     @Inject
     private MavenIndexerUtils mavenIndexerUtils;
 
+    // fields
+    List<? extends IndexCreator> indexCreators;
+
+    NexusIndexer indexer;
+
     @PostConstruct
     private void initialize()
         throws RepositoryAdminException
     {
+        try
+        {
+            indexCreators = mavenIndexerUtils.getAllIndexCreators();
+            indexer = plexusSisuBridge.lookup( NexusIndexer.class );
+        }
+        catch ( PlexusSisuBridgeException e )
+        {
+            throw new RepositoryAdminException( e.getMessage(), e );
+        }
         for ( RemoteRepository remoteRepository : getRemoteRepositories() )
         {
             createIndexContext( remoteRepository );
         }
     }
 
+    @PreDestroy
+    private void shutdown()
+        throws RepositoryAdminException
+    {
+        try
+        {
+            // close index on shutdown
+            for ( RemoteRepository remoteRepository : getRemoteRepositories() )
+            {
+                IndexingContext context = indexer.getIndexingContexts().get( remoteRepository.getId() );
+                if ( context != null )
+                {
+                    indexer.removeIndexingContext( context, false );
+                }
+            }
+        }
+        catch ( IOException e )
+        {
+            throw new RepositoryAdminException( e.getMessage(), e );
+        }
+    }
+
 
     public List<RemoteRepository> getRemoteRepositories()
         throws RepositoryAdminException
@@ -220,11 +257,8 @@ public class DefaultRemoteRepositoryAdmin
             // FIXME get this from ArchivaAdministration
             String appServerBase = System.getProperty( "appserver.base" );
 
-            List<? extends IndexCreator> indexCreators = mavenIndexerUtils.getAllIndexCreators();
-            NexusIndexer nexusIndexer = plexusSisuBridge.lookup( NexusIndexer.class );
-
             String contextKey = "remote-" + remoteRepository.getId();
-            IndexingContext indexingContext = nexusIndexer.getIndexingContexts().get( contextKey );
+            IndexingContext indexingContext = indexer.getIndexingContexts().get( contextKey );
             if ( indexingContext != null )
             {
                 return indexingContext;
@@ -240,10 +274,9 @@ public class DefaultRemoteRepositoryAdmin
             {
                 indexDirectory.mkdirs();
             }
-            return nexusIndexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory,
-                                                    remoteRepository.getUrl(),
-                                                    calculateIndexRemoteUrl( remoteRepository ),
-                                                    mavenIndexerUtils.getAllIndexCreators() );
+            return indexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory,
+                                               remoteRepository.getUrl(), calculateIndexRemoteUrl( remoteRepository ),
+                                               mavenIndexerUtils.getAllIndexCreators() );
         }
         catch ( MalformedURLException e )
         {
@@ -253,10 +286,6 @@ public class DefaultRemoteRepositoryAdmin
         {
             throw new RepositoryAdminException( e.getMessage(), e );
         }
-        catch ( PlexusSisuBridgeException e )
-        {
-            throw new RepositoryAdminException( e.getMessage(), e );
-        }
         catch ( UnsupportedExistingLuceneIndexException e )
         {
             throw new RepositoryAdminException( e.getMessage(), e );