From f952f0056c34809def49e1d4f82cd89f26c5a83f 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. Null pointers are checked to avoid exception.
It's not necessary to empty container objects since they might still
be used by other threads and will be garbage collected eventually.

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

diff --git a/base/ca/src/com/netscape/ca/CertificateAuthority.java b/base/ca/src/com/netscape/ca/CertificateAuthority.java
index f66192cf566144ed1db43bccd579c4e3d3ac787e..c7ae856f8a913e5a4141187810ab1485293c4f01 100644
--- a/base/ca/src/com/netscape/ca/CertificateAuthority.java
+++ b/base/ca/src/com/netscape/ca/CertificateAuthority.java
@@ -532,15 +532,13 @@ public class CertificateAuthority implements ICertificateAuthority, ICertAuthori
             mMasterCRLIssuePoint.shutdown();
         }
 
-        mSigningUnit = null;
-        mOCSPSigningUnit = null;
-        mCRLSigningUnit = null;
         if (mCertRepot != null) {
             mCertRepot.shutdown();
-            mCertRepot = null;
         }
-        mCRLRepot = null;
-        mPublisherProcessor.shutdown();
+
+        if (mPublisherProcessor != null) {
+            mPublisherProcessor.shutdown();
+        }
     }
 
     /**
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..9039b752e094f7bd8491b478c192b0e8c1b5ec13 100644
--- a/base/common/src/com/netscape/cmscore/authentication/AuthSubsystem.java
+++ b/base/common/src/com/netscape/cmscore/authentication/AuthSubsystem.java
@@ -458,19 +458,16 @@ public class AuthSubsystem implements IAuthSubsystem {
      * <P>
      */
     public void shutdown() {
-        for (AuthManagerProxy proxy : mAuthMgrInsts.values()) {
+        if (mAuthMgrInsts != null) {
+            for (AuthManagerProxy proxy : mAuthMgrInsts.values()) {
 
-            IAuthManager mgr = proxy.getAuthManager();
+                IAuthManager mgr = proxy.getAuthManager();
 
-            log(ILogger.LL_INFO, CMS.getLogMessage("CMSCORE_AUTH_INSTANCE_SHUTDOWN", mgr.getName()));
+                log(ILogger.LL_INFO, CMS.getLogMessage("CMSCORE_AUTH_INSTANCE_SHUTDOWN", mgr.getName()));
 
-            mgr.shutdown();
+                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..711df455ecd3c5958892f0af08a0dc2ee2e46ca0 100644
--- a/base/common/src/com/netscape/cmscore/authorization/AuthzSubsystem.java
+++ b/base/common/src/com/netscape/cmscore/authorization/AuthzSubsystem.java
@@ -414,20 +414,17 @@ public class AuthzSubsystem implements IAuthzSubsystem {
      * <P>
      */
     public void shutdown() {
-        for (Enumeration<String> e = mAuthzMgrInsts.keys(); e.hasMoreElements();) {
+        if (mAuthzMgrInsts != null) {
+            for (AuthzManagerProxy proxy : mAuthzMgrInsts.values()) {
+                IAuthzManager mgr = proxy.getAuthzManager();
 
-            IAuthzManager mgr = get(e.nextElement());
-
-            //String infoMsg =
-            //        "Shutting down authz manager instance " + mgr.getName();
-            //log(ILogger.LL_INFO, infoMsg);
-            if (mgr != null)
-                mgr.shutdown();
-        }
-        mAuthzMgrPlugins.clear();
-        mAuthzMgrInsts.clear();
-        mAuthzMgrPlugins = null;
-        mAuthzMgrInsts = null;
+                //String infoMsg =
+                //        "Shutting down authz manager instance " + mgr.getName();
+                //log(ILogger.LL_INFO, infoMsg);
+                if (mgr != null)
+                    mgr.shutdown();
+            }
+    	}
     }
 
     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..c8bff91d9e3ae6a34b4b3fa8d5e800220e3cd615 100644
--- a/base/common/src/com/netscape/cmscore/dbs/DBRegistry.java
+++ b/base/common/src/com/netscape/cmscore/dbs/DBRegistry.java
@@ -113,13 +113,6 @@ public class DBRegistry implements IDBRegistry, ISubsystem {
      * Shutdowns this subsystem gracefully.
      */
     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..f09ae6bdbe1e57b776a026181d9fbb9c8a1e2edc 100644
--- a/base/common/src/com/netscape/cmscore/ldap/PublisherProcessor.java
+++ b/base/common/src/com/netscape/cmscore/ldap/PublisherProcessor.java
@@ -439,7 +439,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/logging/LogSubsystem.java b/base/common/src/com/netscape/cmscore/logging/LogSubsystem.java
index aa57146685535df9afc573fb499a1c6f56a0fd36..feb4685572bda581cdc2992e180dd0d1746457b7 100644
--- a/base/common/src/com/netscape/cmscore/logging/LogSubsystem.java
+++ b/base/common/src/com/netscape/cmscore/logging/LogSubsystem.java
@@ -171,7 +171,7 @@ public class LogSubsystem implements ILogSubsystem {
      * <P>
      */
     public void shutdown() {
-        mLogQueue.shutdown();
+        if (mLogQueue != null) mLogQueue.shutdown();
     }
 
     /**
diff --git a/base/common/src/com/netscape/cmscore/profile/ProfileSubsystem.java b/base/common/src/com/netscape/cmscore/profile/ProfileSubsystem.java
index 1ec3c009ae1eb55bd27c887caf5a5aa634c09c47..1ed1fbc88f75706ea30fc437e2b4bbc0f8e70413 100644
--- a/base/common/src/com/netscape/cmscore/profile/ProfileSubsystem.java
+++ b/base/common/src/com/netscape/cmscore/profile/ProfileSubsystem.java
@@ -216,11 +216,6 @@ public class ProfileSubsystem implements IProfileSubsystem {
      * <P>
      */
     public void shutdown() {
-        mProfileIds.removeAllElements();
-        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..fef7bbb7be2a95221083a4b4af62cb191e58dc85 100644
--- a/base/common/src/com/netscape/cmscore/registry/PluginRegistry.java
+++ b/base/common/src/com/netscape/cmscore/registry/PluginRegistry.java
@@ -245,8 +245,6 @@ public class PluginRegistry implements IPluginRegistry {
      * <P>
      */
     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

