The key CLIs have been modified to use Exceptions instead of
System.exit() such that errors can be handled consistently.

Pushed to master under trivial rule.

--
Endi S. Dewata
>From ab05e6b094be0547c04092cd087bfe161546ac5e Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <[email protected]>
Date: Wed, 18 Jan 2017 01:10:37 +0100
Subject: [PATCH] Cleaned up error handling in key CLIs.

The key CLIs have been modified to use Exceptions instead of
System.exit() such that errors can be handled consistently.
---
 .../com/netscape/cmstools/key/KeyArchiveCLI.java   | 49 ++++----------------
 .../src/com/netscape/cmstools/key/KeyFindCLI.java  | 21 ++-------
 .../com/netscape/cmstools/key/KeyGenerateCLI.java  | 37 ++++-----------
 .../com/netscape/cmstools/key/KeyModifyCLI.java    | 21 ++-------
 .../com/netscape/cmstools/key/KeyRecoverCLI.java   | 35 +++-----------
 .../netscape/cmstools/key/KeyRequestFindCLI.java   | 21 ++-------
 .../netscape/cmstools/key/KeyRequestReviewCLI.java | 25 ++--------
 .../netscape/cmstools/key/KeyRequestShowCLI.java   | 21 ++-------
 .../com/netscape/cmstools/key/KeyRetrieveCLI.java  | 29 +++---------
 .../src/com/netscape/cmstools/key/KeyShowCLI.java  | 22 ++-------
 .../netscape/cmstools/key/KeyTemplateFindCLI.java  | 39 ++++------------
 .../netscape/cmstools/key/KeyTemplateShowCLI.java  | 54 +++-------------------
 12 files changed, 70 insertions(+), 304 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyArchiveCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyArchiveCLI.java
index e9ce7f2ec48017e6f78ab923dbc0adea2011abf6..c3116a673bba7c57ec9a04881b3a9fb5fc48899f 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyArchiveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyArchiveCLI.java
@@ -10,7 +10,6 @@ import javax.xml.bind.Unmarshaller;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.key.KeyArchivalRequest;
 import com.netscape.certsrv.key.KeyRequestResponse;
@@ -51,31 +50,19 @@ public class KeyArchiveCLI extends CLI {
         options.addOption(option);
     }
 
-    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 != 0) {
-            System.err.println("Error: Too many arguments specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Too many arguments specified.");
         }
 
         String requestFile = cmd.getOptionValue("input");
@@ -104,15 +91,10 @@ public class KeyArchiveCLI extends CLI {
                 }
 
             } catch (JAXBException e) {
-                System.err.println("Error: Cannot parse the request file.");
-                if (verbose)
-                    e.printStackTrace();
-                System.exit(-1);
+                throw new Exception("Cannot parse the request file.", e);
+
             } catch (FileNotFoundException e) {
-                System.err.println("Error: Cannot locate file at path: " + requestFile);
-                if (verbose)
-                    e.printStackTrace();
-                System.exit(-1);
+                throw new Exception("Cannot locate file at path: " + requestFile, e);
             }
 
         } else {
@@ -120,25 +102,14 @@ public class KeyArchiveCLI extends CLI {
             String clientKeyId = cmd.getOptionValue("clientKeyID");
             String passphrase = cmd.getOptionValue("passphrase");
             if (clientKeyId == null) {
-                System.err.println("Error: Client Key Id is not specified.");
-                printHelp();
-                System.exit(-1);
+                throw new Exception("Client Key Id is not specified.");
             }
             if (passphrase == null) {
-                System.err.println("Error: No passphrase provided to archive.");
-                printHelp();
-                System.exit(-1);
+                throw new Exception("No passphrase provided to archive.");
             }
             String realm = cmd.getOptionValue("realm");
 
-            try {
-                response = keyCLI.keyClient.archivePassphrase(clientKeyId, passphrase, realm);
-            } catch (Exception e) {
-                System.err.println(e.getMessage());
-                if (verbose)
-                    e.printStackTrace();
-                System.exit(-1);
-            }
+            response = keyCLI.keyClient.archivePassphrase(clientKeyId, passphrase, realm);
         }
 
         MainCLI.printMessage("Archival request details");
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyFindCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyFindCLI.java
index 954246f7ee2cea606dfb1c01501d46e461769149..87d6b2f98f658634f204eb14fd230b235908df8b 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyFindCLI.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.key.KeyInfo;
 import com.netscape.certsrv.key.KeyInfoCollection;
@@ -78,31 +77,19 @@ public class KeyFindCLI extends CLI {
         options.addOption(option);
     }
 
-    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 != 0) {
-            System.err.println("Error: Too many arguments specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Too many arguments specified.");
         }
 
         String clientKeyID = cmd.getOptionValue("clientKeyID");
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyGenerateCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyGenerateCLI.java
index 4149ee677f36efea070f1b551964b3dc67b12e22..312fbf0780df56787fe4f79a8de732c65448cb2d 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyGenerateCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyGenerateCLI.java
@@ -5,7 +5,6 @@ import java.util.List;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.key.KeyRequestResource;
 import com.netscape.certsrv.key.KeyRequestResponse;
@@ -58,31 +57,19 @@ public class KeyGenerateCLI extends CLI {
         options.addOption(option);
     }
 
-    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: Missing Client Key Id.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Missing Client Key Id.");
         }
 
         String clientKeyId = cmdArgs[0];
@@ -104,13 +91,9 @@ public class KeyGenerateCLI extends CLI {
             case KeyRequestResource.RC2_ALGORITHM:
             case KeyRequestResource.RSA_ALGORITHM:
             case KeyRequestResource.DSA_ALGORITHM:
-                System.err.println("Error: Key size must be specified for the algorithm used.");
-                printHelp();
-                System.exit(-1);
+                throw new Exception("Key size must be specified for the algorithm used.");
             default:
-                System.err.println("Error: Algorithm not supported.");
-                printHelp();
-                System.exit(-1);
+                throw new Exception("Algorithm not supported.");
             }
         }
 
@@ -118,9 +101,7 @@ public class KeyGenerateCLI extends CLI {
         try {
             size = Integer.parseInt(keySize);
         } catch (NumberFormatException e) {
-            System.err.println("Error: Key size must be an integer.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Key size must be an integer.", e);
         }
         List<String> usages = null;
         String givenUsages = cmd.getOptionValue("usages");
@@ -145,9 +126,7 @@ public class KeyGenerateCLI extends CLI {
                     clientKeyId, keyAlgorithm, size, usages, null, realm);
             break;
         default:
-            System.err.println("Error: Algorithm not supported.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Algorithm not supported.");
         }
         MainCLI.printMessage("Key generation request info");
         KeyCLI.printKeyRequestInfo(response.getRequestInfo());
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyModifyCLI.java
index b16bc41d1e1c2c3222585d73a5ec26d614a8bba6..1778fef479b251d156fb0c33ff3c6c70ea5f2daf 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyModifyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyModifyCLI.java
@@ -22,7 +22,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.dbs.keydb.KeyId;
 import com.netscape.certsrv.key.KeyInfo;
@@ -49,31 +48,19 @@ public class KeyModifyCLI extends CLI {
         options.addOption(option);
     }
 
-    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 Key ID specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No Key ID specified.");
         }
 
         String status = cmd.getOptionValue("status");
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRecoverCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRecoverCLI.java
index b23577f53a636414410d199ae6391f1f07565d51..0bb2a2d3e50c354b11d9c9534ea4d457ab1649a0 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyRecoverCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRecoverCLI.java
@@ -10,7 +10,6 @@ import javax.xml.bind.Unmarshaller;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.dbs.keydb.KeyId;
 import com.netscape.certsrv.key.KeyRecoveryRequest;
@@ -43,31 +42,19 @@ public class KeyRecoverCLI extends CLI {
         options.addOption(option);
     }
 
-    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 != 0) {
-            System.err.println("Error: Too many arguments specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Too many arguments specified.");
         }
 
         String requestFile = cmd.getOptionValue("input");
@@ -86,24 +73,16 @@ public class KeyRecoverCLI extends CLI {
                         Utils.base64decode(req.getTransWrappedSessionKey()), Utils.base64decode(req.getNonceData()),
                         req.getCertificate());
             } catch (JAXBException e) {
-                System.err.println("Error: Cannot parse the request file.");
-                if (verbose)
-                    e.printStackTrace();
-                System.exit(-1);
+                throw new Exception("Cannot parse the request file.", e);
             } catch (FileNotFoundException e) {
-                System.err.println("Error: Cannot locate file at path: " + requestFile);
-                if (verbose)
-                    e.printStackTrace();
-                System.exit(-1);
+                throw new Exception("Cannot locate file at path: " + requestFile, e);
             }
 
         } else if (keyID != null) {
             String keyId = cmd.getOptionValue("keyID");
             response = keyCLI.keyClient.recoverKey(new KeyId(keyId), null, null, null, null);
         } else {
-            System.err.println("Error: Neither a key ID nor a request file's path is specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Neither a key ID nor a request file's path is specified.");
         }
 
         MainCLI.printMessage("Key Recovery Request Information");
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java
index de061d63051b11f9d88d119b7c894a6244afdf46..69730f9b30e3c2131a1ac29f2b852fd4a0c2e1f6 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestFindCLI.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.key.KeyRequestInfo;
 import com.netscape.certsrv.key.KeyRequestInfoCollection;
@@ -83,31 +82,19 @@ public class KeyRequestFindCLI extends CLI {
         options.addOption(option);
     }
 
-    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 != 0) {
-            System.err.println("Error: Too many arguments specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Too many arguments specified.");
         }
 
         String status = cmd.getOptionValue("status");
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestReviewCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestReviewCLI.java
index 15ca4f8044e9a719619c46e67185645a9a6b393c..d3ab01b70299d0da8015077907ebde8922517232 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestReviewCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestReviewCLI.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.key.KeyRequestInfo;
 import com.netscape.certsrv.request.RequestId;
@@ -33,31 +32,19 @@ public class KeyRequestReviewCLI extends CLI {
         options.addOption(option);
     }
 
-    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: Incorrect number of arguments specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Incorrect number of arguments specified.");
         }
 
         RequestId reqId = new RequestId(cmdArgs[0]);
@@ -74,9 +61,7 @@ public class KeyRequestReviewCLI extends CLI {
             keyCLI.keyClient.cancelRequest(reqId);
             break;
         default:
-            System.err.println("Error: Invalid action.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Invalid action.");
         }
 
         KeyRequestInfo keyRequestInfo = keyCLI.keyClient.getRequestInfo(reqId);
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestShowCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestShowCLI.java
index fe3ef14463f4a3c90a5760c9b24be80d07c83629..82aabfc42f962f33e7d68f925a7d7a9f1d9f6c19 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyRequestShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRequestShowCLI.java
@@ -21,7 +21,6 @@ package com.netscape.cmstools.key;
 import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.key.KeyRequestInfo;
 import com.netscape.certsrv.request.RequestId;
@@ -40,32 +39,20 @@ public class KeyRequestShowCLI extends CLI {
         formatter.printHelp(getFullName() + " <Request ID> [OPTIONS...]", options);
     }
 
-    public void execute(String[] args) {
+    public void execute(String[] args) throws Exception {
 
         // Check for "--help"
         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 Request ID specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No Request ID specified.");
         }
 
         RequestId requestId = new RequestId(args[0].trim());
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyRetrieveCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyRetrieveCLI.java
index 5a2b77e509de777cb31be6707bd702765c68a231..23e220a9a9e1f45d2e71ed02a171f25ed8a3e008 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyRetrieveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyRetrieveCLI.java
@@ -10,7 +10,6 @@ import javax.xml.bind.Unmarshaller;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.dbs.keydb.KeyId;
 import com.netscape.certsrv.key.Key;
@@ -60,34 +59,20 @@ public class KeyRetrieveCLI 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.");
         }
 
         if (cmd.getOptions().length == 0) {
-            System.err.println("Error: Incorrect number of parameters provided.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Incorrect number of parameters provided.");
         }
 
         String requestFile = cmd.getOptionValue("input");
@@ -101,8 +86,7 @@ public class KeyRetrieveCLI extends CLI {
             KeyRecoveryRequest req = (KeyRecoveryRequest) unmarshaller.unmarshal(fis);
 
             if (req.getKeyId() == null) {
-                System.err.println("Error: Key ID must be specified in the request file.");
-                System.exit(-1);
+                throw new Exception("Key ID must be specified in the request file.");
             }
 
             if (req.getCertificate() != null) {
@@ -133,8 +117,7 @@ public class KeyRetrieveCLI extends CLI {
             String requestId = cmd.getOptionValue("requestID");
 
             if ((requestId == null) && (keyId == null)) {
-                System.out.println("Either requestID or keyID must be specified");
-                System.exit(1);
+                throw new Exception("Either requestID or keyID must be specified");
             }
 
             if (passphrase != null) {
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyShowCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyShowCLI.java
index ebc5741b6c8fa1ee0e72929f973ad5251b0fff64..d877222dc3850ad24b30071e8f59295324c22bec 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyShowCLI.java
@@ -22,7 +22,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.dbs.keydb.KeyId;
 import com.netscape.certsrv.key.KeyInfo;
@@ -49,24 +48,14 @@ public class KeyShowCLI extends CLI {
         options.addOption(option);
     }
 
-    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();
         String clientKeyId = cmd.getOptionValue("clientKeyID");
@@ -80,10 +69,7 @@ public class KeyShowCLI extends CLI {
             keyInfo = keyCLI.keyClient.getActiveKeyInfo(clientKeyId);
 
         } else {
-            System.err.println("Error: Missing Key ID or Client Key ID.");
-            printHelp();
-            System.exit(-1);
-            return;
+            throw new Exception("Missing Key ID or Client Key ID.");
         }
 
         KeyCLI.printKeyInfo(keyInfo);
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateFindCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateFindCLI.java
index 3303560a69f449b92ba25d0ac568e8a2bca6626b..32a3f02fa6943cf2f08be9731d10845f7d95e970 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateFindCLI.java
@@ -1,14 +1,10 @@
 package com.netscape.cmstools.key;
 
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.util.ArrayList;
 import java.util.Arrays;
 
-import javax.xml.bind.JAXBException;
-
 import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.base.ResourceMessage;
 import com.netscape.certsrv.key.KeyTemplate;
@@ -30,41 +26,23 @@ public class KeyTemplateFindCLI extends CLI {
         formatter.printHelp(getFullName() + " [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 != 0) {
-            System.err.println("Error: Too many arguments specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Too many arguments specified.");
         }
 
-        try {
-            createTemplateList();
-        } catch (FileNotFoundException | JAXBException e) {
-            System.err.println("Error: " + e.getMessage());
-            if (verbose)
-                e.printStackTrace();
-            System.exit(-1);
-        }
+        createTemplateList();
+
         MainCLI.printMessage(templates.size() + " entries matched");
         for (KeyTemplate template : templates) {
             template.printTemplateInfo();
@@ -73,12 +51,11 @@ public class KeyTemplateFindCLI extends CLI {
         MainCLI.printMessage("Number of entries returned " + templates.size());
     }
 
-    public void createTemplateList() throws FileNotFoundException, JAXBException {
+    public void createTemplateList() throws Exception {
         String templateDir = "/usr/share/pki/key/templates/";
         File file = new File(templateDir);
         if (!file.exists()) {
-            System.err.println("Error: Missing template files.");
-            System.exit(-1);
+            throw new Exception("Missing template files.");
         }
         KeyTemplate template = null;
         ResourceMessage data = null;
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateShowCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateShowCLI.java
index 30a40390a29b101c831b55fc4533f4abdfde88b6..c9ef4b19bb2059887e8ada961e642934e1ef7be6 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyTemplateShowCLI.java
@@ -1,15 +1,10 @@
 package com.netscape.cmstools.key;
 
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
-import java.io.IOException;
 import java.util.Arrays;
 
-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.base.ResourceMessage;
 import com.netscape.certsrv.key.KeyArchivalRequest;
@@ -36,71 +31,34 @@ public class KeyTemplateShowCLI extends CLI {
         options.addOption(option);
     }
 
-    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 Template ID specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No Template ID specified.");
         }
 
         String templateId = cmdArgs[0];
         String writeToFile = cmd.getOptionValue("output");
         String templateDir = "/usr/share/pki/key/templates/";
         String templatePath = templateDir + templateId + ".xml";
-        ResourceMessage data = null;
-        try {
-            data = ResourceMessage.unmarshall(KeyArchivalRequest.class, templatePath);
-        } catch (FileNotFoundException | JAXBException e2) {
-            System.err.println("Error: " + e2.getMessage());
-            if (verbose)
-                e2.printStackTrace();
-            System.exit(-1);
-        }
+        ResourceMessage data = ResourceMessage.unmarshall(KeyArchivalRequest.class, templatePath);
 
         if (writeToFile != null) {
             try (FileOutputStream fOS = new FileOutputStream(writeToFile)) {
                 data.marshall(fOS);
-            } catch (JAXBException e) {
-                System.err.println("Error: Cannot write the file");
-                if (verbose)
-                    e.printStackTrace();
-            } catch (FileNotFoundException e) {
-                System.err.println("Error: Cannot write the file");
-                if (verbose)
-                    e.printStackTrace();
-            } catch (IOException e1) {
-                System.err.println("Error: " + e1.getMessage());
-                if (verbose)
-                    e1.printStackTrace();
             }
         } else {
             MainCLI.printMessage(data.getAttribute("description"));
-            try {
-                data.marshall(System.out);
-            } catch (JAXBException e) {
-                System.err.println(e.getMessage());
-                if (verbose)
-                    e.printStackTrace();
-            }
+            data.marshall(System.out);
         }
     }
 }
-- 
2.5.5

_______________________________________________
Pki-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/pki-devel

Reply via email to