>From 26c39ed1db8e1fa9f122a538caa7942defb2acc4 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Tue, 17 Jan 2017 23:02:02 +0100
Subject: [PATCH] Cleaned up error handling in cert and profile CLIs.

The cert and profile CLIs have been modified to use Exceptions
instead of System.exit() such that errors can be handled
consistently.
---
 .../com/netscape/cmstools/cert/CertFindCLI.java    | 45 +++++--------------
 .../com/netscape/cmstools/cert/CertHoldCLI.java    | 24 +++-------
 .../netscape/cmstools/cert/CertReleaseHoldCLI.java | 20 ++-------
 .../netscape/cmstools/cert/CertRequestFindCLI.java | 18 ++------
 .../cmstools/cert/CertRequestProfileFindCLI.java   | 19 ++------
 .../cmstools/cert/CertRequestProfileShowCLI.java   | 22 ++-------
 .../cmstools/cert/CertRequestReviewCLI.java        | 46 +++++--------------
 .../netscape/cmstools/cert/CertRequestShowCLI.java | 21 ++-------
 .../cmstools/cert/CertRequestSubmitCLI.java        | 34 +++-----------
 .../com/netscape/cmstools/cert/CertRevokeCLI.java  | 29 +++---------
 .../com/netscape/cmstools/cert/CertShowCLI.java    | 18 ++------
 .../src/com/netscape/cmstools/cli/CLI.java         |  2 +-
 .../netscape/cmstools/profile/ProfileAddCLI.java   | 22 ++-------
 .../com/netscape/cmstools/profile/ProfileCLI.java  |  5 +--
 .../cmstools/profile/ProfileDisableCLI.java        | 18 ++------
 .../netscape/cmstools/profile/ProfileEditCLI.java  | 25 +++--------
 .../cmstools/profile/ProfileEnableCLI.java         | 20 ++-------
 .../netscape/cmstools/profile/ProfileFindCLI.java  | 18 ++------
 .../cmstools/profile/ProfileModifyCLI.java         | 52 +++++++---------------
 .../cmstools/profile/ProfileRemoveCLI.java         | 18 ++------
 .../netscape/cmstools/profile/ProfileShowCLI.java  | 22 ++-------
 21 files changed, 105 insertions(+), 393 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertFindCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertFindCLI.java
index 8e1045bf36cf222c0c0e745a49a73f801546cea2..1a9e4de1f48caa3b7e7675e546ec565c1f23d82c 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertFindCLI.java
@@ -18,18 +18,14 @@
 
 package com.netscape.cmstools.cert;
 
-import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Date;
 
-import javax.xml.bind.JAXBException;
-
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.cert.CertDataInfo;
 import com.netscape.certsrv.cert.CertDataInfos;
@@ -199,27 +195,16 @@ public class CertFindCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-        } catch (ParseException e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length != 0) {
-            System.err.println("Error: Too many arguments specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Too many arguments specified.");
         }
 
         CertSearchRequest searchData = null;
@@ -228,9 +213,7 @@ public class CertFindCLI extends CLI {
         if (cmd.hasOption("input")) {
             fileName = cmd.getOptionValue("input");
             if (fileName == null || fileName.length() < 1) {
-                System.err.println("Error: No file name specified.");
-                printHelp();
-                System.exit(-1);
+                throw new Exception("No file name specified.");
             }
         }
 
@@ -240,14 +223,6 @@ public class CertFindCLI extends CLI {
                 reader = new FileReader(fileName);
                 searchData = CertSearchRequest.valueOf(reader);
 
-            } catch (FileNotFoundException e) {
-                System.err.println("Error: " + e.getMessage());
-                System.exit(-1);
-
-            } catch (JAXBException e) {
-                System.err.println("Error: " + e.getMessage());
-                System.exit(-1);
-
             } finally {
                 if (reader != null)
                     try {
@@ -290,7 +265,7 @@ public class CertFindCLI extends CLI {
         MainCLI.printMessage("Number of entries returned " + certs.getEntries().size());
     }
 
-    public Long convertValidityDurationUnit(String unit) {
+    public Long convertValidityDurationUnit(String unit) throws Exception {
 
         if (unit.equalsIgnoreCase("day")) {
             return 86400000l;
@@ -305,12 +280,12 @@ public class CertFindCLI extends CLI {
             return 31536000000l;
 
         } else {
-            throw new Error("Invalid validity duration unit: "+unit);
+            throw new Exception("Invalid validity duration unit: " + unit);
         }
     }
 
     public void addSearchAttribute(CommandLine cmd, CertSearchRequest csd)
-            throws java.text.ParseException {
+            throws Exception {
 
         if (cmd.hasOption("minSerialNumber")) {
             csd.setSerialNumberRangeInUse(true);
@@ -387,7 +362,7 @@ public class CertFindCLI extends CLI {
             if (reason != null) {
                 csd.setRevocationReason(Integer.toString(reason.getCode()));
             } else {
-                throw new Error("Invalid revocation reason");
+                throw new Exception("Invalid revocation reason");
             }
         }
         if (cmd.hasOption("issuedBy")) {
@@ -466,11 +441,11 @@ public class CertFindCLI extends CLI {
         if (csd.getValidityLengthInUse()) {
 
             if (csd.getValidityOperation() == null) {
-                throw new Error("Mising validity duration operation");
+                throw new Exception("Mising validity duration operation");
             }
 
             if (csd.getValidityCount() == null) {
-                throw new Error("Mising validity duration count");
+                throw new Exception("Mising validity duration count");
             }
 
             if (csd.getValidityUnit() == null) {
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java
index a4b184871be4ce44d59e244fc4bb68c356531f63..9c30ee0039d01cf6e5e4dc265032d0e675f1a058 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertHoldCLI.java
@@ -22,8 +22,6 @@ import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.util.Arrays;
 
-import netscape.security.x509.RevocationReason;
-
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
@@ -35,6 +33,8 @@ import com.netscape.certsrv.request.RequestStatus;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
+import netscape.security.x509.RevocationReason;
+
 /**
  * @author Endi S. Dewata
  */
@@ -64,28 +64,16 @@ public class CertHoldCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-
-        } catch (Exception e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length != 1) {
-            System.err.println("Error: Missing Serial Number.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Missing Serial Number.");
         }
 
         CertId certID = new CertId(cmdArgs[0]);
@@ -104,7 +92,7 @@ public class CertHoldCLI extends CLI {
             BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
             String line = reader.readLine();
             if (!line.equalsIgnoreCase("Y")) {
-                System.exit(-1);
+                return;
             }
         }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java
index 78beb12d084d29b701944720fd92905c9a4cd068..a767282551754483f7277b15f9ec1e726a7b51d5 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertReleaseHoldCLI.java
@@ -56,28 +56,16 @@ public class CertReleaseHoldCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-
-        } catch (Exception e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length != 1) {
-            System.err.println("Error: Missing Serial Number.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Missing Serial Number.");
         }
 
         CertId certID = new CertId(cmdArgs[0]);
@@ -96,7 +84,7 @@ public class CertReleaseHoldCLI extends CLI {
             BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
             String line = reader.readLine();
             if (!line.equalsIgnoreCase("Y")) {
-                System.exit(-1);
+                return;
             }
         }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestFindCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestFindCLI.java
index 2d8779d0746c16d5ff5ff515e4c548577b8992c5..71d669e50591e8885b16ecbc6d697e5bbf2f1cbf 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestFindCLI.java
@@ -23,7 +23,6 @@ import java.util.Collection;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.cert.CertRequestInfo;
 import com.netscape.certsrv.cert.CertRequestInfos;
@@ -84,27 +83,16 @@ public class CertRequestFindCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-        } catch (ParseException e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length != 0) {
-            System.err.println("Error: Too many arguments specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Too many arguments specified.");
         }
 
         String s = cmd.getOptionValue("start");
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileFindCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileFindCLI.java
index f5d2df26bb52987791f5dd0d1f60b7048152efed..220ee7c18b17c819f6eade29539c9e1976448783 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileFindCLI.java
@@ -5,7 +5,6 @@ import java.util.Collection;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.profile.ProfileDataInfo;
 import com.netscape.certsrv.profile.ProfileDataInfos;
@@ -41,28 +40,16 @@ public class CertRequestProfileFindCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-
-        } catch (ParseException e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length != 0) {
-            System.err.println("Error: Too many arguments specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Too many arguments specified.");
         }
 
         String s = cmd.getOptionValue("start");
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileShowCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileShowCLI.java
index 8b626703f8f8b4e885bb13242b9f46dacf3deafb..cb3a79bfb6f788642f9ae4a2a5db67c853a1fd20 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestProfileShowCLI.java
@@ -4,7 +4,6 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.cert.CertEnrollmentRequest;
 import com.netscape.cmstools.cli.CLI;
@@ -35,27 +34,16 @@ public class CertRequestProfileShowCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-        } catch (ParseException e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length < 1) {
-            System.err.println("Error: Missing Profile ID.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Missing Profile ID.");
         }
 
         String profileId = cmdArgs[0];
@@ -65,9 +53,7 @@ public class CertRequestProfileShowCLI extends CLI {
             filename = cmd.getOptionValue("output");
 
             if (filename == null || filename.trim().length() == 0) {
-                System.err.println("Error: Missing output file name.");
-                printHelp();
-                System.exit(-1);
+                throw new Exception("Missing output file name.");
             }
         }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java
index d25639281d48d0ee39023f37558bb3819c2a566c..bc3df309a1e98c876370276a572d122c1f16347c 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java
@@ -13,10 +13,8 @@ import javax.xml.bind.Unmarshaller;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 import org.apache.commons.lang.StringUtils;
 
-import com.netscape.certsrv.base.PKIException;
 import com.netscape.certsrv.cert.CertRequestInfo;
 import com.netscape.certsrv.cert.CertReviewResponse;
 import com.netscape.certsrv.request.RequestId;
@@ -58,44 +56,30 @@ public class CertRequestReviewCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-        } catch (ParseException e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length < 1) {
-            System.err.println("Error: Missing Certificate Request ID.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Missing Certificate Request ID.");
         }
 
         RequestId requestId = null;
         try {
             requestId = new RequestId(cmdArgs[0]);
         } catch (NumberFormatException e) {
-            System.err.println("Error: Invalid certificate request ID " + cmdArgs[0] + ".");
-            System.exit(-1);
+            throw new Exception("Invalid certificate request ID " + cmdArgs[0] + ".", e);
         }
 
         // Since "--action <action>" and "--file <filename>" are mutually
         // exclusive, check to make certain that only one has been set
         if (cmd.hasOption("action") && cmd.hasOption("file")) {
-            System.err.println("Error: The '--action <action>' and '--file <filename>' " +
-                               "options are mutually exclusive!");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("The '--action <action>' and '--file <filename>' " +
+                                "options are mutually exclusive!");
         }
 
         String action = cmd.getOptionValue("action");
@@ -105,26 +89,16 @@ public class CertRequestReviewCLI extends CLI {
             if (cmd.hasOption("file")) {
                 filename = cmd.getOptionValue("file");
             } else {
-                System.err.println("Error: Missing '--action <action>' or '--file <filename>' option.");
-                printHelp();
-                System.exit(-1);
+                throw new Exception("Missing '--action <action>' or '--file <filename>' option.");
             }
 
             if (filename == null || filename.trim().length() == 0) {
-                System.err.println("Error: Missing output file name.");
-                printHelp();
-                System.exit(-1);
+                throw new Exception("Missing output file name.");
             }
         }
 
         // Retrieve certificate request.
-        CertReviewResponse reviewInfo = null;
-        try {
-            reviewInfo = certCLI.certClient.reviewRequest(requestId);
-        } catch (PKIException e) {
-            System.err.println(e.getMessage());
-            System.exit(-1);
-        }
+        CertReviewResponse reviewInfo = certCLI.certClient.reviewRequest(requestId);
 
         if (action == null) {
             // Store certificate request in a file.
@@ -187,7 +161,7 @@ public class CertRequestReviewCLI extends CLI {
             MainCLI.printMessage("Unassigned certificate request " + requestId);
 
         } else {
-            throw new Error("Invalid action: " + action);
+            throw new Exception("Invalid action: " + action);
         }
 
         CertRequestInfo certRequest = certCLI.certClient.getRequest(requestId);
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestShowCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestShowCLI.java
index aac18a679ea262098599064357230a8ebe1363ba..6df333eae074243aa0654c85d817549d45557e48 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestShowCLI.java
@@ -3,7 +3,6 @@ package com.netscape.cmstools.cert;
 import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.cert.CertRequestInfo;
 import com.netscape.certsrv.request.RequestId;
@@ -29,35 +28,23 @@ public class CertRequestShowCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-        } catch (ParseException e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length < 1) {
-            System.err.println("Error: Missing Certificate Request ID.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Missing Certificate Request ID.");
         }
 
         RequestId requestId = null;
         try {
             requestId = new RequestId(cmdArgs[0]);
         } catch (NumberFormatException e) {
-            System.err.println("Error: Invalid certificate request ID " + cmdArgs[0] + ".");
-            System.exit(-1);
+            throw new Exception("Invalid certificate request ID " + cmdArgs[0] + ".", e);
         }
 
         CertRequestInfo certRequest = certCLI.certClient.getRequest(requestId);
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestSubmitCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestSubmitCLI.java
index 991ab462be4dc15f40d41e3d59acdba0470f9c63..acdaebe0825f217f8b88cd181bfebe509aa8d7c5 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRequestSubmitCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRequestSubmitCLI.java
@@ -12,7 +12,6 @@ import java.util.Vector;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.ca.AuthorityID;
 import com.netscape.certsrv.cert.CertEnrollmentRequest;
@@ -74,20 +73,11 @@ public class CertRequestSubmitCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-        } catch (ParseException e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
@@ -95,15 +85,11 @@ public class CertRequestSubmitCLI extends CLI {
         String profileID = cmd.getOptionValue("profile");
 
         if (requestFilename == null && profileID == null) {
-            System.err.println("Error: Missing request file or profile ID.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Missing request file or profile ID.");
         }
 
         if (requestFilename != null && profileID != null) {
-            System.err.println("Error: Request file and profile ID are mutually exclusive.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Request file and profile ID are mutually exclusive.");
         }
 
         AuthorityID aid = null;
@@ -112,9 +98,7 @@ public class CertRequestSubmitCLI extends CLI {
             try {
                 aid = new AuthorityID(aidString);
             } catch (IllegalArgumentException e) {
-                System.err.println("Bad AuthorityID: " + aidString);
-                printHelp();
-                System.exit(-1);
+                throw new Exception("Bad AuthorityID: " + aidString, e);
             }
         }
 
@@ -124,16 +108,12 @@ public class CertRequestSubmitCLI extends CLI {
             try {
                 adn = new X500Name(adnString);
             } catch (IOException e) {
-                System.err.println("Bad DN: " + adnString);
-                printHelp();
-                System.exit(-1);
+                throw new Exception("Bad DN: " + adnString, e);
             }
         }
 
         if (aid != null && adn != null) {
-            System.err.println("--issuer-id and --issuer-dn options are mutually exclusive");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("--issuer-id and --issuer-dn options are mutually exclusive");
         }
 
         String requestType = cmd.getOptionValue("request-type");
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java
index afb0e03fd337f4f7e086f932d4d6d86074188b85..e61401b903a765e6dbec2da9e8776ef85403e591 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertRevokeCLI.java
@@ -22,8 +22,6 @@ import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.util.Arrays;
 
-import netscape.security.x509.RevocationReason;
-
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
@@ -35,6 +33,8 @@ import com.netscape.certsrv.request.RequestStatus;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
+import netscape.security.x509.RevocationReason;
+
 /**
  * @author Endi S. Dewata
  */
@@ -81,28 +81,16 @@ public class CertRevokeCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-
-        } catch (Exception e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length != 1) {
-            System.err.println("Error: Missing Serial Number.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Missing Serial Number.");
         }
 
         CertId certID = new CertId(cmdArgs[0]);
@@ -111,10 +99,7 @@ public class CertRevokeCLI extends CLI {
         RevocationReason reason = RevocationReason.valueOf(string);
 
         if (reason == null) {
-            System.err.println("Error: Invalid revocation reason: "+string);
-            printHelp();
-            System.exit(-1);
-            return;
+            throw new Exception("Invalid revocation reason: " + string);
         }
 
         CertData certData = certCLI.certClient.reviewCert(certID);
@@ -138,7 +123,7 @@ public class CertRevokeCLI extends CLI {
             BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
             String line = reader.readLine();
             if (!line.equalsIgnoreCase("Y")) {
-                System.exit(-1);
+                return;
             }
         }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java
index febc4a49447da8001a95e3d68857fe7ff9e837f5..a31b02253fa41176ff64b1c950e532576a94d277 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertShowCLI.java
@@ -60,28 +60,16 @@ public class CertShowCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-
-        } catch (Exception e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length != 1) {
-            System.err.println("Error: Missing Serial Number.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Missing Serial Number.");
         }
 
         boolean showPrettyPrint = cmd.hasOption("pretty");
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/CLI.java b/base/java-tools/src/com/netscape/cmstools/cli/CLI.java
index 42c56747a2395cd73a910a83eee934b36d5be6e7..0a9106705f6d965b62b2600710ffb855b3a94485 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/CLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/CLI.java
@@ -249,7 +249,7 @@ public class CLI {
         if ((args.length == 0) || (args[0].equals("--help"))) {
             // Print commands associated with this module
             printHelp();
-            System.exit(0);
+            return;
         }
 
         // TODO: Rewrite using findModules().
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java
index fc144c13dd555330b4d153bee709e9f36dcb7624..ced286e5fe1d0b9462e82c86b54d955bfa8ad5b5 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java
@@ -5,7 +5,6 @@ import java.util.Properties;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.profile.ProfileData;
 import com.netscape.cmstools.cli.CLI;
@@ -31,34 +30,21 @@ public class ProfileAddCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-        } catch (ParseException e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length < 1) {
-            System.err.println("Error: No filename specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No filename specified.");
         }
 
         String filename = cmdArgs[0];
         if (filename == null || filename.trim().length() == 0) {
-            System.err.println("Error: Missing input file name.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Missing input file name.");
         }
 
         if (cmd.hasOption("raw")) {
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
index ecfa75340286ee4caff6eb4ef4c4eda25c6a7135..e4acdb1956f655c3faa8e18ae14e69c15353ba09 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
@@ -3,7 +3,6 @@ package com.netscape.cmstools.profile;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
-import java.io.IOException;
 import java.net.URI;
 import java.nio.file.Files;
 import java.nio.file.Paths;
@@ -139,12 +138,12 @@ public class ProfileCLI extends CLI {
     }
 
     public static Properties readRawProfileFromFile(String filename)
-            throws IOException, RuntimeException {
+            throws Exception {
         Properties properties = new Properties();
         properties.load(Files.newInputStream(Paths.get(filename)));
         String profileId = properties.getProperty("profileId");
         if (profileId == null)
-            throw new RuntimeException("Error: Missing profileId property in profile data.");
+            throw new Exception("Missing profileId property in profile data.");
         return properties;
     }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileDisableCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileDisableCLI.java
index 7e7f50f78f53b5871f4bc3b40c76560f73743a29..dc21ac4a8b0eb086415c7f7486732b769171fc21 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileDisableCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileDisableCLI.java
@@ -23,28 +23,16 @@ public class ProfileDisableCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-
-        } catch (Exception e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length != 1) {
-            System.err.println("Error: No Profile ID specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No Profile ID specified.");
         }
 
         String profileId = args[0];
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileEditCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileEditCLI.java
index 50600ba155b9b84edfdeb54e1177b4d0d46cc397..b8bb9f03f082d186c231f550e685150dc523b206 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileEditCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileEditCLI.java
@@ -18,14 +18,12 @@
 
 package com.netscape.cmstools.profile;
 
-import java.lang.ProcessBuilder;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.Properties;
 
 import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.cmstools.cli.CLI;
 
@@ -45,27 +43,16 @@ public class ProfileEditCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-        } catch (ParseException e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length < 1) {
-            System.err.println("Error: No Profile ID specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No Profile ID specified.");
         }
 
         String profileId = cmdArgs[0];
@@ -74,8 +61,7 @@ public class ProfileEditCLI extends CLI {
         Properties orig = profileCLI.profileClient.retrieveProfileRaw(profileId);
         String enabled = orig.getProperty("enable");
         if (Boolean.valueOf(enabled)) {
-            System.err.println("Error: Cannot edit profile. Profile must be disabled.");
-            System.exit(-1);
+            throw new Exception("Cannot edit profile. Profile must be disabled.");
         }
         Path tempFile = Files.createTempFile("pki", ".cfg");
 
@@ -94,8 +80,7 @@ public class ProfileEditCLI extends CLI {
             pb.inheritIO();
             int exitCode = pb.start().waitFor();
             if (exitCode != 0) {
-                System.err.println("Error: editor exited abnormally.");
-                System.exit(-1);
+                throw new Exception("Exited abnormally.");
             }
 
             // read data from temporary file and modify if changed
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileEnableCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileEnableCLI.java
index 73157c2b1926a050ec8864a51aa3b170a38651f6..f8c56ba6483a3471f7de116947950cba87a09cd8 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileEnableCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileEnableCLI.java
@@ -23,31 +23,19 @@ public class ProfileEnableCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-
-        } catch (Exception e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length != 1) {
-            System.err.println("Error: No Profile ID specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No Profile ID specified.");
         }
 
-        String profileId = args[0];
+        String profileId = cmdArgs[0];
 
         profileCLI.profileClient.enableProfile(profileId);
 
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileFindCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileFindCLI.java
index 9552fd70db06c18ff970ce8577e4d90214b16283..512c7cd3053a3ac597ca1a3db7c7b9d5a8d86e99 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileFindCLI.java
@@ -39,28 +39,16 @@ public class ProfileFindCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-
-        } catch (Exception e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length != 0) {
-            System.err.println("Error: Too many arguments specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Too many arguments specified.");
         }
 
         String s = cmd.getOptionValue("start");
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java
index cc0f415b78facbaa4cacb7ab4e914717586cfd0e..835f628c15efa9b034209a3338b22bafa6a7ef1a 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java
@@ -1,14 +1,10 @@
 package com.netscape.cmstools.profile;
 
-import java.io.IOException;
 import java.util.Arrays;
 import java.util.Properties;
 
-import javax.xml.bind.JAXBException;
-
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.profile.ProfileData;
 import com.netscape.cmstools.cli.CLI;
@@ -31,56 +27,38 @@ public class ProfileModifyCLI extends CLI {
         formatter.printHelp(getFullName() + " <file> [OPTIONS...]", options);
     }
 
-    public void execute(String[] args) {
+    public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-        } catch (ParseException e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length < 1) {
-            System.err.println("Error: No filename specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No filename specified.");
         }
 
         String filename = cmdArgs[0];
         if (filename == null || filename.trim().length() == 0) {
-            System.err.println("Error: Missing input file name.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Missing input file name.");
         }
 
-        try {
-            if (cmd.hasOption("raw")) {
-                Properties properties = ProfileCLI.readRawProfileFromFile(filename);
-                String profileId = properties.getProperty("profileId");
-                profileCLI.profileClient.modifyProfileRaw(profileId, properties).store(System.out, null);
-                MainCLI.printMessage("Modified profile " + profileId);
-            } else {
-                ProfileData data = ProfileCLI.readProfileFromFile(filename);
-                data = profileCLI.profileClient.modifyProfile(data);
+        if (cmd.hasOption("raw")) {
+            Properties properties = ProfileCLI.readRawProfileFromFile(filename);
+            String profileId = properties.getProperty("profileId");
+            profileCLI.profileClient.modifyProfileRaw(profileId, properties).store(System.out, null);
+            MainCLI.printMessage("Modified profile " + profileId);
+        } else {
+            ProfileData data = ProfileCLI.readProfileFromFile(filename);
+            data = profileCLI.profileClient.modifyProfile(data);
 
-                MainCLI.printMessage("Modified profile " + data.getId());
+            MainCLI.printMessage("Modified profile " + data.getId());
 
-                ProfileCLI.printProfile(data, profileCLI.getClient().getConfig().getServerURI());
-            }
-        } catch (IOException | JAXBException  e) {
-            System.err.println("Error: " + e.getMessage());
-            System.exit(-1);
+            ProfileCLI.printProfile(data, profileCLI.getClient().getConfig().getServerURI());
         }
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileRemoveCLI.java
index b9f5aa68931f82e1ee832be12afec75b2faa2d59..f0920cde3e831bdc649f4b8c2ff70c744a53bc5d 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileRemoveCLI.java
@@ -23,28 +23,16 @@ public class ProfileRemoveCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-
-        } catch (Exception e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length != 1) {
-            System.err.println("Error: No Profile ID specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No Profile ID specified.");
         }
 
         String profileId = args[0];
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java
index 1dd85f43bf349c4dbca9b4b764f2d40fe7f421aa..5134530bc790ffa271facff11557552f1f98fbdc 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java
@@ -6,7 +6,6 @@ import java.util.Properties;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.profile.ProfileData;
 import com.netscape.cmstools.cli.CLI;
@@ -40,27 +39,16 @@ public class ProfileShowCLI extends CLI {
     public void execute(String[] args) throws Exception {
         // Always check for "--help" prior to parsing
         if (Arrays.asList(args).contains("--help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
-        CommandLine cmd = null;
-
-        try {
-            cmd = parser.parse(options, args);
-        } catch (ParseException e) {
-            System.err.println("Error: " + e.getMessage());
-            printHelp();
-            System.exit(-1);
-        }
+        CommandLine cmd = parser.parse(options, args);
 
         String[] cmdArgs = cmd.getArgs();
 
         if (cmdArgs.length < 1) {
-            System.err.println("Error: No Profile ID specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No Profile ID specified.");
         }
 
         String profileId = cmdArgs[0];
@@ -70,9 +58,7 @@ public class ProfileShowCLI extends CLI {
             filename = cmd.getOptionValue("output");
 
             if (filename == null || filename.trim().length() == 0) {
-                System.err.println("Error: Missing output file name.");
-                printHelp();
-                System.exit(-1);
+                throw new Exception("Missing output file name.");
             }
         }
 
-- 
2.5.5

