From 8ce5cde6b45e307e50321b20d9337ff5e5df7119 Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Thu, 16 Aug 2012 00:39:48 -0500
Subject: [PATCH] Fixed exceptions during shutdown.

The shutdown() methods in several classes have been fixed to allow
more graceful shutdown and clean restart. There are two types of
object attributes that need to be handled differently.

Attributes that are initialized by the constructor should not be
nulled during shutdown because they won't be reinitialized during
restart. If they require a cleanup (e.g. emptying collections,
closing LDAP connections) it's not necessary to check for null
before calling the cleanup method because they're never null.

For attributes that are initialized during init(), it may not be
necessary to do a cleanup or null the attribute since they might
still be used by other threads and they will be reinitialized
during restart so the old objects will be garbage collected. If
they do need a cleanup they should be checked for null because
they might still be null due to init() failure or initialization
conditionals.

If the attributes are initialized conditionally, the logic has been
modified to ensure the attributes are either initialized or set to
null.

Ticket #247
---
 .../src/com/netscape/ca/CertificateAuthority.java  | 23 ++++++++++++----------
 .../cms/authentication/DirBasedAuthentication.java |  1 -
 .../netscape/cms/authorization/DirAclAuthz.java    |  3 +--
 .../cmscore/authentication/AuthSubsystem.java      |  3 ---
 .../authentication/PasswdUserDBAuthentication.java |  3 +--
 .../cmscore/authorization/AuthzSubsystem.java      |  7 ++-----
 .../cmscore/cert/CrossCertPairSubsystem.java       |  3 ---
 .../src/com/netscape/cmscore/dbs/DBRegistry.java   |  4 ----
 .../src/com/netscape/cmscore/dbs/DBSubsystem.java  |  1 -
 .../netscape/cmscore/ldap/PublisherProcessor.java  | 10 ++++++----
 .../src/com/netscape/cmscore/logging/LogQueue.java |  1 -
 .../netscape/cmscore/profile/ProfileSubsystem.java |  4 +---
 .../netscape/cmscore/registry/PluginRegistry.java  |  1 -
 .../com/netscape/cmscore/usrgrp/UGSubsystem.java   |  1 -
 .../src/com/netscape/kra/KeyRecoveryAuthority.java | 13 +++++++++---
 15 files changed, 34 insertions(+), 44 deletions(-)

diff --git a/base/ca/src/com/netscape/ca/CertificateAuthority.java b/base/ca/src/com/netscape/ca/CertificateAuthority.java
index f66192cf566144ed1db43bccd579c4e3d3ac787e..f8f3d7a9b2be96935df1b2ca68dce1ddd496ce8d 100644
--- a/base/ca/src/com/netscape/ca/CertificateAuthority.java
+++ b/base/ca/src/com/netscape/ca/CertificateAuthority.java
@@ -527,20 +527,19 @@ public class CertificateAuthority implements ICertificateAuthority, ICertAuthori
             CRLIssuingPoint point = (CRLIssuingPoint) enums.nextElement();
             point.shutdown();
         }
+        mCRLIssuePoints.clear();
 
         if (mMasterCRLIssuePoint != null) {
             mMasterCRLIssuePoint.shutdown();
         }
 
-        mSigningUnit = null;
-        mOCSPSigningUnit = null;
-        mCRLSigningUnit = null;
         if (mCertRepot != null) {
             mCertRepot.shutdown();
-            mCertRepot = null;
         }
-        mCRLRepot = null;
-        mPublisherProcessor.shutdown();
+
+        if (mPublisherProcessor != null) {
+            mPublisherProcessor.shutdown();
+        }
     }
 
     /**
@@ -1695,12 +1694,12 @@ public class CertificateAuthority implements ICertificateAuthority, ICertAuthori
         }
 
         // a Master/full crl must exist.
+        CRLIssuingPoint masterCRLIssuePoint = null;
 
         while (issuePointIdEnum.hasMoreElements()) {
             String issuePointId = issuePointIdEnum.nextElement();
 
-            CMS.debug(
-                    "initializing crl issue point " + issuePointId);
+            CMS.debug("initializing crl issue point " + issuePointId);
             IConfigStore issuePointConfig = null;
             String issuePointClassName = null;
             Class<CRLIssuingPoint> issuePointClass = null;
@@ -1713,9 +1712,11 @@ public class CertificateAuthority implements ICertificateAuthority, ICertAuthori
                 issuePoint = issuePointClass.newInstance();
                 issuePoint.init(this, issuePointId, issuePointConfig);
                 mCRLIssuePoints.put(issuePointId, issuePoint);
-                if (mMasterCRLIssuePoint == null &&
+
+                if (masterCRLIssuePoint == null &&
                         issuePointId.equals(PROP_MASTER_CRL))
-                    mMasterCRLIssuePoint = issuePoint;
+                    masterCRLIssuePoint = issuePoint;
+
             } catch (ClassNotFoundException e) {
                 throw new ECAException(
                         CMS.getUserMessage("CMS_CA_CRL_ISSUING_POINT_INIT_FAILED",
@@ -1731,6 +1732,8 @@ public class CertificateAuthority implements ICertificateAuthority, ICertAuthori
             }
         }
 
+        mMasterCRLIssuePoint = masterCRLIssuePoint;
+
         /*
          if (mMasterCRLIssuePoint == null) {
          log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_CA_CA_NO_FULL_CRL", PROP_MASTER_CRL));
diff --git a/base/common/src/com/netscape/cms/authentication/DirBasedAuthentication.java b/base/common/src/com/netscape/cms/authentication/DirBasedAuthentication.java
index f529047188d1dd6a2bba67bd4a495a713ef608b7..f2d09df9e410ac817e2a90d4f82bbd4c488a8ab2 100644
--- a/base/common/src/com/netscape/cms/authentication/DirBasedAuthentication.java
+++ b/base/common/src/com/netscape/cms/authentication/DirBasedAuthentication.java
@@ -435,7 +435,6 @@ public abstract class DirBasedAuthentication
         try {
             if (mConnFactory != null) {
                 mConnFactory.reset();
-                mConnFactory = null;
             }
         } catch (ELdapException e) {
             // ignore
diff --git a/base/common/src/com/netscape/cms/authorization/DirAclAuthz.java b/base/common/src/com/netscape/cms/authorization/DirAclAuthz.java
index bf4ca7f5aa54f6d2ad7bdf139847253c83cdd703..133fe759e0d47e7aa935fb13374e33d5499bc4e7 100644
--- a/base/common/src/com/netscape/cms/authorization/DirAclAuthz.java
+++ b/base/common/src/com/netscape/cms/authorization/DirAclAuthz.java
@@ -343,8 +343,7 @@ public class DirAclAuthz extends AAclAuthz
         }
 
         try {
-            mLdapConnFactory.reset();
-            mLdapConnFactory = null;
+            if (mLdapConnFactory != null) mLdapConnFactory.reset();
         } catch (ELdapException e) {
             log(ILogger.LL_FAILURE, CMS.getLogMessage("AUTHZ_EVALUATOR_LDAP_ERROR", e.toString()));
         }
diff --git a/base/common/src/com/netscape/cmscore/authentication/AuthSubsystem.java b/base/common/src/com/netscape/cmscore/authentication/AuthSubsystem.java
index c5b09a7d7e956deb56f58e3713ba502dc6656544..549ce01f97fde8fb0ea3da9fb194451a43fdc128 100644
--- a/base/common/src/com/netscape/cmscore/authentication/AuthSubsystem.java
+++ b/base/common/src/com/netscape/cmscore/authentication/AuthSubsystem.java
@@ -466,11 +466,8 @@ public class AuthSubsystem implements IAuthSubsystem {
 
             mgr.shutdown();
         }
-
         mAuthMgrPlugins.clear();
-        mAuthMgrPlugins = null;
         mAuthMgrInsts.clear();
-        mAuthMgrInsts = null;
     }
 
     public Hashtable<String, AuthMgrPlugin> getPlugins() {
diff --git a/base/common/src/com/netscape/cmscore/authentication/PasswdUserDBAuthentication.java b/base/common/src/com/netscape/cmscore/authentication/PasswdUserDBAuthentication.java
index fa8696c1d6a25b1ea86a65d229d1f99efab713e8..449a196d443f27fb7e947f157776b55586e6bcaa 100644
--- a/base/common/src/com/netscape/cmscore/authentication/PasswdUserDBAuthentication.java
+++ b/base/common/src/com/netscape/cmscore/authentication/PasswdUserDBAuthentication.java
@@ -246,8 +246,7 @@ public class PasswdUserDBAuthentication implements IAuthManager {
     public void shutdown() {
         try {
             // disconnect all outstanding connections in the factory
-            mConnFactory.reset();
-            mConnFactory = null;
+            if (mConnFactory != null) mConnFactory.reset();
         } catch (ELdapException e) {
             log(ILogger.LL_FAILURE, e.toString());
         }
diff --git a/base/common/src/com/netscape/cmscore/authorization/AuthzSubsystem.java b/base/common/src/com/netscape/cmscore/authorization/AuthzSubsystem.java
index 2c092f91c14dd5da976d63c6b8749f93bba86949..a6019730a954418ffb5fa37b96dc1a5a77cbcf85 100644
--- a/base/common/src/com/netscape/cmscore/authorization/AuthzSubsystem.java
+++ b/base/common/src/com/netscape/cmscore/authorization/AuthzSubsystem.java
@@ -414,9 +414,8 @@ public class AuthzSubsystem implements IAuthzSubsystem {
      * <P>
      */
     public void shutdown() {
-        for (Enumeration<String> e = mAuthzMgrInsts.keys(); e.hasMoreElements();) {
-
-            IAuthzManager mgr = get(e.nextElement());
+        for (AuthzManagerProxy proxy : mAuthzMgrInsts.values()) {
+            IAuthzManager mgr = proxy.getAuthzManager();
 
             //String infoMsg =
             //        "Shutting down authz manager instance " + mgr.getName();
@@ -426,8 +425,6 @@ public class AuthzSubsystem implements IAuthzSubsystem {
         }
         mAuthzMgrPlugins.clear();
         mAuthzMgrInsts.clear();
-        mAuthzMgrPlugins = null;
-        mAuthzMgrInsts = null;
     }
 
     public Hashtable<String, AuthzMgrPlugin> getPlugins() {
diff --git a/base/common/src/com/netscape/cmscore/cert/CrossCertPairSubsystem.java b/base/common/src/com/netscape/cmscore/cert/CrossCertPairSubsystem.java
index 4db850d2ea4cde91710501454ef4abf8e221b693..b0feca8c28d3efd13c166a88b394ea5845895ad9 100644
--- a/base/common/src/com/netscape/cmscore/cert/CrossCertPairSubsystem.java
+++ b/base/common/src/com/netscape/cmscore/cert/CrossCertPairSubsystem.java
@@ -480,8 +480,6 @@ public class CrossCertPairSubsystem implements ICrossCertPairSubsystem {
      * Stops this system.
      */
     public synchronized void shutdown() {
-        mCa = null;
-        mPublisherProcessor = null;
         if (mLdapConnFactory != null) {
             try {
                 mLdapConnFactory.reset();
@@ -489,7 +487,6 @@ public class CrossCertPairSubsystem implements ICrossCertPairSubsystem {
                 CMS.debug("CrossCertPairSubsystem shutdown exception: " + e.toString());
             }
         }
-        mLdapConnFactory = null;
     }
 
     /*
diff --git a/base/common/src/com/netscape/cmscore/dbs/DBRegistry.java b/base/common/src/com/netscape/cmscore/dbs/DBRegistry.java
index 2b6a63a62f81ad09f71eadfebceedcae766b5ad3..653850e02d8a8641bf4420f6198357ce67ef24d2 100644
--- a/base/common/src/com/netscape/cmscore/dbs/DBRegistry.java
+++ b/base/common/src/com/netscape/cmscore/dbs/DBRegistry.java
@@ -114,12 +114,8 @@ public class DBRegistry implements IDBRegistry, ISubsystem {
      */
     public void shutdown() {
         mOCclassNames.clear();
-        mOCclassNames = null;
         mOCldapNames.clear();
-        mOCldapNames = null;
         mAttrufNames.clear();
-        mAttrufNames = null;
-        mConverter = null;
     }
 
     /**
diff --git a/base/common/src/com/netscape/cmscore/dbs/DBSubsystem.java b/base/common/src/com/netscape/cmscore/dbs/DBSubsystem.java
index c0bb627c225c2eba55590f435c3ca0f264e97fb4..0824cc9a7b2c4bd737d0afa9cd7623c0b47ba5bd 100644
--- a/base/common/src/com/netscape/cmscore/dbs/DBSubsystem.java
+++ b/base/common/src/com/netscape/cmscore/dbs/DBSubsystem.java
@@ -833,7 +833,6 @@ public class DBSubsystem implements IDBSubsystem {
         try {
             if (mLdapConnFactory != null) {
                 mLdapConnFactory.reset();
-                mLdapConnFactory = null;
             }
         } catch (ELdapException e) {
 
diff --git a/base/common/src/com/netscape/cmscore/ldap/PublisherProcessor.java b/base/common/src/com/netscape/cmscore/ldap/PublisherProcessor.java
index 50ed7c3c27b1369169fec6403d218aa0fe3c935d..5df29c10c0af15656bd249f5da4f627a23608904 100644
--- a/base/common/src/com/netscape/cmscore/ldap/PublisherProcessor.java
+++ b/base/common/src/com/netscape/cmscore/ldap/PublisherProcessor.java
@@ -408,10 +408,11 @@ public class PublisherProcessor implements
             CMS.debug("No LdapPublishing enabled");
         }
 
+        LdapRequestListener listener = null;
         if (mConfig.getBoolean(PROP_ENABLE, false)) {
-            mLdapRequestListener = new LdapRequestListener();
-            mLdapRequestListener.init(this, mLdapConfig);
-            mAuthority.registerRequestListener(mLdapRequestListener);
+            listener = new LdapRequestListener();
+            listener.init(this, mLdapConfig);
+            mAuthority.registerRequestListener(listener);
             IConfigStore queueConfig = mConfig.getSubStore(PROP_QUEUE_PUBLISH_SUBSTORE);
             if (queueConfig != null) {
                 boolean isPublishingQueueEnabled = queueConfig.getBoolean("enable", false);
@@ -431,6 +432,7 @@ public class PublisherProcessor implements
                                                 savePublishingStatus);
             }
         }
+        mLdapRequestListener = listener;
     }
 
     public void shutdown() {
@@ -439,7 +441,7 @@ public class PublisherProcessor implements
             if (mLdapConnModule != null) {
                 mLdapConnModule.getLdapConnFactory().reset();
             }
-            if (mLdapRequestListener != null) {
+            if (mAuthority != null && mLdapRequestListener != null) {
                 //mLdapRequestListener.shutdown();
                 mAuthority.removeRequestListener(mLdapRequestListener);
             }
diff --git a/base/common/src/com/netscape/cmscore/logging/LogQueue.java b/base/common/src/com/netscape/cmscore/logging/LogQueue.java
index 2a63fe478848a89a315768b55422457ed5d6590c..32af1bf2c733f3495afc9b71b7b17d6d4bed25c0 100644
--- a/base/common/src/com/netscape/cmscore/logging/LogQueue.java
+++ b/base/common/src/com/netscape/cmscore/logging/LogQueue.java
@@ -66,7 +66,6 @@ public class LogQueue implements ILogQueue {
         for (int i = 0; i < mListeners.size(); i++) {
             mListeners.elementAt(i).shutdown();
         }
-        mListeners = null;
     }
 
     /**
diff --git a/base/common/src/com/netscape/cmscore/profile/ProfileSubsystem.java b/base/common/src/com/netscape/cmscore/profile/ProfileSubsystem.java
index 1ec3c009ae1eb55bd27c887caf5a5aa634c09c47..27e72352ef22c742b5ea09a180d440d58452dd49 100644
--- a/base/common/src/com/netscape/cmscore/profile/ProfileSubsystem.java
+++ b/base/common/src/com/netscape/cmscore/profile/ProfileSubsystem.java
@@ -216,11 +216,9 @@ public class ProfileSubsystem implements IProfileSubsystem {
      * <P>
      */
     public void shutdown() {
-        mProfileIds.removeAllElements();
+        mProfileIds.clear();
         mProfiles.clear();
-        mProfiles = null;
         mProfileClassIds.clear();
-        mProfileClassIds = null;
     }
 
     /**
diff --git a/base/common/src/com/netscape/cmscore/registry/PluginRegistry.java b/base/common/src/com/netscape/cmscore/registry/PluginRegistry.java
index 1c014622279ce908b19de28a5e6ebd1eebd03713..4e3593411001793e86c3c79762eedf5705ea0a44 100644
--- a/base/common/src/com/netscape/cmscore/registry/PluginRegistry.java
+++ b/base/common/src/com/netscape/cmscore/registry/PluginRegistry.java
@@ -246,7 +246,6 @@ public class PluginRegistry implements IPluginRegistry {
      */
     public void shutdown() {
         mTypes.clear();
-        mTypes = null;
     }
 
     /**
diff --git a/base/common/src/com/netscape/cmscore/usrgrp/UGSubsystem.java b/base/common/src/com/netscape/cmscore/usrgrp/UGSubsystem.java
index 259173078ec92c2b67c1641a0d2c9b4819bd3683..1a29dcf0c5f4e43d53cf3f789ce41f33de922aaf 100644
--- a/base/common/src/com/netscape/cmscore/usrgrp/UGSubsystem.java
+++ b/base/common/src/com/netscape/cmscore/usrgrp/UGSubsystem.java
@@ -152,7 +152,6 @@ public final class UGSubsystem implements IUGSubsystem {
         try {
             if (mLdapConnFactory != null) {
                 mLdapConnFactory.reset();
-                mLdapConnFactory = null;
             }
         } catch (ELdapException e) {
             log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_USRGRP_LDAP_SHUT", e.toString()));
diff --git a/base/kra/src/com/netscape/kra/KeyRecoveryAuthority.java b/base/kra/src/com/netscape/kra/KeyRecoveryAuthority.java
index 4ba69ef9c7c5f0e9198becc5a40618c5fd03cefb..54cf2a0c649386054df63c534e8b2fe43513ecab 100644
--- a/base/kra/src/com/netscape/kra/KeyRecoveryAuthority.java
+++ b/base/kra/src/com/netscape/kra/KeyRecoveryAuthority.java
@@ -456,14 +456,21 @@ public class KeyRecoveryAuthority implements IAuthority, IKeyService, IKeyRecove
         if (!mInitialized)
             return;
 
-        mTransportKeyUnit.shutdown();
-        mStorageKeyUnit.shutdown();
+        if (mTransportKeyUnit != null) {
+            mTransportKeyUnit.shutdown();
+        }
+
+        if (mStorageKeyUnit != null) {
+            mStorageKeyUnit.shutdown();
+        }
+
         if (mKeyDB != null) {
             mKeyDB.shutdown();
-            mKeyDB = null;
         }
+
         getLogger().log(ILogger.EV_SYSTEM, ILogger.S_KRA,
                 ILogger.LL_INFO, mName.toString() + " is stopped");
+
         mInitialized = false;
     }
 
-- 
1.7.11.2

