[Pki-devel] [PATCH] 542 Added support for exception chains in EBaseException.

Endi Sukma Dewata edewata at redhat.com
Tue Jan 20 20:52:00 UTC 2015


The EBaseException has been modified to provide constructors that
can be used to chain exceptions. This way the root cause of the
exception can be traced back to help troubleshooting.

Some codes have been modified to utilize the proper exception
chaining.

https://fedorahosted.org/pki/ticket/915


-- 
Endi S. Dewata
-------------- next part --------------
From 6db8b7999007f25b87cd81182222a537fe0c2edc Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata at redhat.com>
Date: Tue, 20 Jan 2015 09:25:32 -0500
Subject: [PATCH] Added support for exception chains in EBaseException.

The EBaseException has been modified to provide constructors that
can be used to chain exceptions. This way the root cause of the
exception can be traced back to help troubleshooting.

Some codes have been modified to utilize the proper exception
chaining.

https://fedorahosted.org/pki/ticket/915
---
 base/common/src/com/netscape/certsrv/apps/CMS.java | 14 ++---
 .../com/netscape/certsrv/base/EBaseException.java  | 60 +++++++++++++++++-----
 .../dogtagpki/server/rest/SystemConfigService.java | 18 +++----
 .../cmscore/authentication/AuthSubsystem.java      | 20 +++++---
 .../ChallengePhraseAuthentication.java             |  3 +-
 5 files changed, 76 insertions(+), 39 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/apps/CMS.java b/base/common/src/com/netscape/certsrv/apps/CMS.java
index 63c1a2cbde2ffdb0ce116c664ab373aeba91f5a3..8b4bac2c0985637ceab6d55bf3d2b9a00b848412 100644
--- a/base/common/src/com/netscape/certsrv/apps/CMS.java
+++ b/base/common/src/com/netscape/certsrv/apps/CMS.java
@@ -17,8 +17,6 @@
 // --- END COPYRIGHT BLOCK ---
 package com.netscape.certsrv.apps;
 
-import java.io.ByteArrayOutputStream;
-import java.io.PrintStream;
 import java.math.BigInteger;
 import java.security.NoSuchAlgorithmException;
 import java.security.cert.Certificate;
@@ -1586,21 +1584,19 @@ public final class CMS {
             CMS.startup();
 
         } catch (EBaseException e) { // catch everything here purposely
-            CMS.debug("CMS:Caught EBaseException");
             CMS.debug(e);
 
             // Raidzilla Bug #57592:  Always print error message to stdout.
-            System.out.println(e.toString());
+            System.out.println(e);
 
             shutdown();
             throw e;
+
         } catch (Exception e) { // catch everything here purposely
-            ByteArrayOutputStream bos = new ByteArrayOutputStream();
-            PrintStream ps = new PrintStream(bos);
-
-            e.printStackTrace(ps);
+            CMS.debug(e);
             System.out.println(Constants.SERVER_SHUTDOWN_MESSAGE);
-            throw new EBaseException(bos.toString());
+
+            throw new EBaseException(e);
             // cms.shutdown();
         }
     }
diff --git a/base/common/src/com/netscape/certsrv/base/EBaseException.java b/base/common/src/com/netscape/certsrv/base/EBaseException.java
index 140a6acbcfc71b291ff059b09c8ec7eb3c6199fb..78d9a6d2d68083a39a91e4ae135bff3541406dad 100644
--- a/base/common/src/com/netscape/certsrv/base/EBaseException.java
+++ b/base/common/src/com/netscape/certsrv/base/EBaseException.java
@@ -63,25 +63,24 @@ public class EBaseException extends Exception {
     }
 
     /**
-     * Constructs an instance of the exception given the resource key and
-     * a exception parameter.
+     * Constructs an instance of this exception given the resource key and
+     * the cause exception.
      *
-     * <PRE>
-     * 		try {
-     *  		...
-     * 		} catch (IOExeption e) {
-     * 		 	throw new EBaseException(BaseResources.INTERNAL_ERROR_1, e);
-     *      }
-     * </PRE>
-     * <P>
+     * <pre>
+     *     try {
+     *         ...
+     *     } catch (IOExeption e) {
+     *         throw new EBaseException(BaseResources.INTERNAL_ERROR_1, e);
+     *     }
+     * </pre>
      *
      * @param msgFormat The resource key
-     * @param param The parameter as an exception
+     * @param cause The cause exception
      */
-    public EBaseException(String msgFormat, Exception param) {
-        super(msgFormat);
+    public EBaseException(String msgFormat, Exception cause) {
+        super(msgFormat, cause);
         mParams = new Exception[1];
-        mParams[0] = param;
+        mParams[0] = cause;
     }
 
     /**
@@ -98,6 +97,39 @@ public class EBaseException extends Exception {
     }
 
     /**
+     * Constructs an instance of this exception given the resource key,
+     * an array of parameters, and the cause exception.
+     * <P>
+     *
+     * @param msgFormat The resource key
+     * @param params Array of params
+     * @param cause The cause exception
+     */
+    public EBaseException(String msgFormat, Object params[], Exception cause) {
+        super(msgFormat, cause);
+        mParams = params;
+    }
+
+    /**
+     * Constructs an instance of this exception given the cause exception.
+     *
+     * <pre>
+     *     try {
+     *         ...
+     *     } catch (IOExeption e) {
+     *         throw new EBaseException(e);
+     *     }
+     * </pre>
+     *
+     * @param cause The cause exception
+     */
+    public EBaseException(Exception cause) {
+        super(cause.getMessage() == null ? cause.getClass().getName() : cause.getMessage(), cause);
+        mParams = new Exception[1];
+        mParams[0] = cause;
+    }
+
+    /**
      * Returns the list of parameters.
      * <P>
      *
diff --git a/base/server/cms/src/org/dogtagpki/server/rest/SystemConfigService.java b/base/server/cms/src/org/dogtagpki/server/rest/SystemConfigService.java
index 47048c31a06b44cd992d4ebd1f09ff550bde9bf0..a594dad0510242357e86201e9d1125f9d0f929f8 100644
--- a/base/server/cms/src/org/dogtagpki/server/rest/SystemConfigService.java
+++ b/base/server/cms/src/org/dogtagpki/server/rest/SystemConfigService.java
@@ -174,7 +174,7 @@ public class SystemConfigService extends PKIService implements SystemConfigResou
             cs.commit(false);
         } catch (EBaseException e) {
             CMS.debug(e);
-            throw new PKIException("Unable to commit config parameters to file");
+            throw new PKIException("Unable to commit config parameters to file", e);
         }
         initializeDatabase(data);
 
@@ -200,8 +200,8 @@ public class SystemConfigService extends PKIService implements SystemConfigResou
                 ConfigurationUtils.setCertPermissions(cert.getCertTag());
                 CMS.debug("Processed '" + cert.getCertTag() + "' certificate.");
             } catch (Exception e) {
-                e.printStackTrace();
-                throw new PKIException("Error in configuring system certificates" + e);
+                CMS.debug(e);
+                throw new PKIException("Error in configuring system certificates" + e, e);
             }
             if (ret != 0) {
                 throw new PKIException("Error in configuring system certificates");
@@ -234,8 +234,8 @@ public class SystemConfigService extends PKIService implements SystemConfigResou
         try {
             ConfigurationUtils.removePreopConfigEntries();
         } catch (EBaseException e) {
-            e.printStackTrace();
-            throw new PKIException("Errors when removing preop config entries: " + e);
+            CMS.debug(e);
+            throw new PKIException("Errors when removing preop config entries: " + e, e);
         }
 
         // Create an empty file that designates the fact that although
@@ -915,8 +915,8 @@ public class SystemConfigService extends PKIService implements SystemConfigResou
             cs.putString("securitydomain.host", host);
             cs.putInteger("securitydomain.httpsadminport",port);
         } catch (Exception e) {
-            e.printStackTrace();
-            throw new PKIException("Failed to resolve security domain URL");
+            CMS.debug(e);
+            throw new PKIException("Failed to resolve security domain URL", e);
         }
 
         getCertChainFromSecurityDomain(host, port);
@@ -957,8 +957,8 @@ public class SystemConfigService extends PKIService implements SystemConfigResou
         try {
             installToken = ConfigurationUtils.getInstallToken(host, port, user, pass);
         } catch (Exception e) {
-            e.printStackTrace();
-            throw new PKIException("Failed to obtain installation token from security domain: " + e);
+            CMS.debug(e);
+            throw new PKIException("Failed to obtain installation token from security domain: " + e, e);
         }
 
         if (installToken == null) {
diff --git a/base/server/cmscore/src/com/netscape/cmscore/authentication/AuthSubsystem.java b/base/server/cmscore/src/com/netscape/cmscore/authentication/AuthSubsystem.java
index 549ce01f97fde8fb0ea3da9fb194451a43fdc128..137edb5c5a75916fb8a2b2fdf07ab0a6aa56f0fe 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/authentication/AuthSubsystem.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/authentication/AuthSubsystem.java
@@ -219,21 +219,26 @@ public class AuthSubsystem implements IAuthSubsystem {
                     isEnable = true;
 
                     log(ILogger.LL_INFO, CMS.getLogMessage("CMSCORE_AUTH_ADD_AUTH_INSTANCE", insName));
+
                 } catch (ClassNotFoundException e) {
                     log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_AUTH_AUTHSUB_ERROR", e.toString()));
-                    throw new EAuthException(CMS.getUserMessage("CMS_ACL_CLASS_LOAD_FAIL", className));
+                    throw new EAuthException(CMS.getUserMessage("CMS_ACL_CLASS_LOAD_FAIL", className), e);
+
                 } catch (IllegalAccessException e) {
                     log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_AUTH_AUTHSUB_ERROR", e.toString()));
-                    throw new EAuthException(CMS.getUserMessage("CMS_ACL_CLASS_LOAD_FAIL", className));
+                    throw new EAuthException(CMS.getUserMessage("CMS_ACL_CLASS_LOAD_FAIL", className), e);
+
                 } catch (InstantiationException e) {
                     log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_AUTH_AUTHSUB_ERROR", e.toString()));
-                    throw new EAuthException(CMS.getUserMessage("CMS_ACL_CLASS_LOAD_FAIL", className));
+                    throw new EAuthException(CMS.getUserMessage("CMS_ACL_CLASS_LOAD_FAIL", className), e);
+
                 } catch (EBaseException e) {
                     log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_AUTH_AUTH_INIT_ERROR", insName, e.toString()));
                     // Skip the authenticaiton instance if
                     // it is mis-configurated. This give
                     // administrator another chance to
                     // fix the problem via console
+
                 } catch (Throwable e) {
                     log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_AUTH_AUTH_INIT_ERROR", insName, e.toString()));
                     // Skip the authenticaiton instance if
@@ -330,15 +335,18 @@ public class AuthSubsystem implements IAuthSubsystem {
             authMgrInst = (IAuthManager)
                     Class.forName(className).newInstance();
             return (authMgrInst.getConfigParams());
+
         } catch (InstantiationException e) {
             log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_AUTH_INSTANCE_NOT_CREATED", e.toString()));
-            throw new EAuthException(CMS.getUserMessage("CMS_ACL_CLASS_LOAD_FAIL", className));
+
+            throw new EAuthException(CMS.getUserMessage("CMS_ACL_CLASS_LOAD_FAIL", className), e);
         } catch (ClassNotFoundException e) {
             log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_AUTH_INSTANCE_NOT_CREATED", e.toString()));
-            throw new EAuthException(CMS.getUserMessage("CMS_ACL_CLASS_LOAD_FAIL", className));
+            throw new EAuthException(CMS.getUserMessage("CMS_ACL_CLASS_LOAD_FAIL", className), e);
+
         } catch (IllegalAccessException e) {
             log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_AUTH_INSTANCE_NOT_CREATED", e.toString()));
-            throw new EAuthException(CMS.getUserMessage("CMS_ACL_CLASS_LOAD_FAIL", className));
+            throw new EAuthException(CMS.getUserMessage("CMS_ACL_CLASS_LOAD_FAIL", className), e);
         }
     }
 
diff --git a/base/server/cmscore/src/com/netscape/cmscore/authentication/ChallengePhraseAuthentication.java b/base/server/cmscore/src/com/netscape/cmscore/authentication/ChallengePhraseAuthentication.java
index f5bde0b54731c46d0ccff2dbc64d435995b23915..11b6104bf93096da03f622c291eabc4016271228 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/authentication/ChallengePhraseAuthentication.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/authentication/ChallengePhraseAuthentication.java
@@ -103,8 +103,9 @@ public class ChallengePhraseAuthentication implements IAuthManager {
 
         try {
             mSHADigest = MessageDigest.getInstance("SHA1");
+
         } catch (NoSuchAlgorithmException e) {
-            throw new EAuthException(CMS.getUserMessage("CMS_AUTHENTICATION_INTERNAL_ERROR", e.getMessage()));
+            throw new EAuthException(CMS.getUserMessage("CMS_AUTHENTICATION_INTERNAL_ERROR", e.getMessage()), e);
         }
 
         log(ILogger.LL_INFO, CMS.getLogMessage("INIT_DONE", name));
-- 
1.8.4.2



More information about the Pki-devel mailing list