The feature and authority 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 ad388b56995e7283cf2a505c70ace1d320bae56e Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <[email protected]>
Date: Wed, 18 Jan 2017 02:13:34 +0100
Subject: [PATCH] Cleaned up error handling in feature and authority CLIs.

The feature and authority CLIs have been modified to use
Exceptions instead of System.exit() such that errors can be
handled consistently.
---
 .../cmstools/authority/AuthorityCreateCLI.java     | 35 ++++----------
 .../cmstools/authority/AuthorityDisableCLI.java    | 18 ++-----
 .../cmstools/authority/AuthorityEnableCLI.java     | 18 ++-----
 .../cmstools/authority/AuthorityFindCLI.java       | 14 +-----
 .../cmstools/authority/AuthorityKeyExportCLI.java  | 56 +++++++---------------
 .../cmstools/authority/AuthorityRemoveCLI.java     | 29 ++++-------
 .../cmstools/authority/AuthorityShowCLI.java       | 28 +++--------
 .../netscape/cmstools/feature/FeatureFindCLI.java  | 14 +-----
 .../netscape/cmstools/feature/FeatureShowCLI.java  | 22 ++-------
 9 files changed, 57 insertions(+), 177 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCreateCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCreateCLI.java
index 7f40662b6b20844a05ee9bed1ad89fc77ee1118c..9cea963bfd09e025e2dbabafd72f4b04278322b9 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCreateCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCreateCLI.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.authority.AuthorityData;
 import com.netscape.certsrv.ca.AuthorityID;
@@ -34,29 +33,19 @@ public class AuthorityCreateCLI 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) {
-            if (cmdArgs.length < 1)
-                System.err.println("No DN specified.");
-            else
-                System.err.println("Too many arguments.");
-            printHelp();
-            System.exit(-1);
+
+        if (cmdArgs.length < 1) {
+            throw new Exception("No DN specified.");
+
+        } else if (cmdArgs.length > 1) {
+            throw new Exception("Too many arguments.");
         }
 
         String parentAIDString = null;
@@ -65,14 +54,10 @@ public class AuthorityCreateCLI extends CLI {
             try {
                 new AuthorityID(parentAIDString);
             } catch (IllegalArgumentException e) {
-                System.err.println("Bad CA ID: " + parentAIDString);
-                printHelp();
-                System.exit(-1);
+                throw new Exception("Bad CA ID: " + parentAIDString, e);
             }
         } else {
-            System.err.println("Must specify parent authority");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Must specify parent authority");
         }
 
         String desc = null;
diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityDisableCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityDisableCLI.java
index b1265b50393a6c23b44f3fd290d468551c1e5a09..7c5da13de3357d853e5e376786a98a9b9d505600 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityDisableCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityDisableCLI.java
@@ -3,7 +3,6 @@ package com.netscape.cmstools.authority;
 import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.authority.AuthorityData;
 import com.netscape.cmstools.cli.CLI;
@@ -24,27 +23,16 @@ public class AuthorityDisableCLI 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 ID specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No ID specified.");
         }
 
         AuthorityData data = new AuthorityData(
diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityEnableCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityEnableCLI.java
index 5afef455bfc6cb2cb6a24375c892a5585872538a..7ff25a45011e3355bb1c9632ab6f5457d46ed5c8 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityEnableCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityEnableCLI.java
@@ -3,7 +3,6 @@ package com.netscape.cmstools.authority;
 import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.authority.AuthorityData;
 import com.netscape.cmstools.cli.CLI;
@@ -24,27 +23,16 @@ public class AuthorityEnableCLI 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 ID specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No ID specified.");
         }
 
         AuthorityData data = new AuthorityData(
diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityFindCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityFindCLI.java
index c1aa99fc627e8e0ccfd1f12a23610a13dd5cfbbb..2b96e3aab2625a7a06b5f9e4cd70205ce3d2446a 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityFindCLI.java
@@ -4,7 +4,6 @@ import java.util.Arrays;
 import java.util.List;
 
 import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.authority.AuthorityData;
 import com.netscape.cmstools.cli.CLI;
@@ -26,21 +25,12 @@ public class AuthorityFindCLI 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;
         }
 
         @SuppressWarnings("unused")
-        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);
 
         List<AuthorityData> datas = authorityCLI.authorityClient.listCAs();
 
diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityKeyExportCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityKeyExportCLI.java
index a3dee82c8d7ef3ad923aa53635b0825f3d272998..2fafe52049b1c9812d4de43499a53209ece631bd 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityKeyExportCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityKeyExportCLI.java
@@ -6,8 +6,6 @@ import java.security.PublicKey;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
-import org.apache.commons.cli.ParseException;
-
 import org.mozilla.jss.CryptoManager;
 import org.mozilla.jss.crypto.CryptoToken;
 import org.mozilla.jss.crypto.IVParameterSpec;
@@ -46,64 +44,44 @@ public class AuthorityKeyExportCLI extends CLI {
     }
 
     public void execute(String[] args) throws Exception {
-        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);
 
         if (cmd.hasOption("help")) {
-            // Display usage
             printHelp();
-            System.exit(0);
+            return;
         }
 
         String filename = cmd.getOptionValue("output");
         if (filename == null) {
-            System.err.println("Error: No output file specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No output file specified.");
         }
 
         String wrapNick = cmd.getOptionValue("wrap-nickname");
         if (wrapNick == null) {
-            System.err.println("Error: no wrapping key nickname specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No wrapping key nickname specified.");
         }
 
         String targetNick = cmd.getOptionValue("target-nickname");
         if (targetNick == null) {
-            System.err.println("Error: no target key nickname specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No target key nickname specified.");
         }
 
-        try {
-            CryptoManager cm = CryptoManager.getInstance();
-            X509Certificate wrapCert = cm.findCertByNickname(wrapNick);
-            X509Certificate targetCert = cm.findCertByNickname(targetNick);
+        CryptoManager cm = CryptoManager.getInstance();
+        X509Certificate wrapCert = cm.findCertByNickname(wrapNick);
+        X509Certificate targetCert = cm.findCertByNickname(targetNick);
 
-            PublicKey wrappingKey = wrapCert.getPublicKey();
-            PrivateKey toBeWrapped = cm.findPrivKeyByCert(targetCert);
-            CryptoToken token = cm.getInternalKeyStorageToken();
+        PublicKey wrappingKey = wrapCert.getPublicKey();
+        PrivateKey toBeWrapped = cm.findPrivKeyByCert(targetCert);
+        CryptoToken token = cm.getInternalKeyStorageToken();
 
-            byte iv[] = { 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 };
-            IVParameterSpec ivps = new IVParameterSpec(iv);
+        byte iv[] = { 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 };
+        IVParameterSpec ivps = new IVParameterSpec(iv);
 
-            byte[] data = CryptoUtil.createPKIArchiveOptions(
-                token, wrappingKey, toBeWrapped,
-                KeyGenAlgorithm.DES3, 0, ivps);
-
-            Files.newOutputStream(Paths.get(filename)).write(data);
-        } catch (Throwable e) {
-            e.printStackTrace();
-            System.exit(-1);
-        }
+        byte[] data = CryptoUtil.createPKIArchiveOptions(
+            token, wrappingKey, toBeWrapped,
+            KeyGenAlgorithm.DES3, 0, ivps);
 
+        Files.newOutputStream(Paths.get(filename)).write(data);
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityRemoveCLI.java
index 42265b180ba85b6097ef2354e969fbd0495ac73b..f69948aafd61275a0271463d2910446a731eb551 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityRemoveCLI.java
@@ -5,7 +5,6 @@ import java.io.InputStreamReader;
 import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -28,29 +27,19 @@ public class AuthorityRemoveCLI 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) {
-            if (cmdArgs.length < 1)
-                System.err.println("No ID specified.");
-            else
-                System.err.println("Too many arguments.");
-            printHelp();
-            System.exit(-1);
+
+        if (cmdArgs.length < 1) {
+            throw new Exception("No ID specified.");
+
+        } else if (cmdArgs.length > 1) {
+            throw new Exception("Too many arguments.");
         }
 
         if (!cmd.hasOption("force")) {
@@ -60,7 +49,7 @@ public class AuthorityRemoveCLI 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/authority/AuthorityShowCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityShowCLI.java
index c9566024872fdfa781f956ed7acfd494367a7d32..b26371070f394a76cdc6f7a5d074ab8c462d57fc 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityShowCLI.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.authority.AuthorityData;
 import com.netscape.certsrv.authority.AuthorityResource;
@@ -30,45 +29,32 @@ public class AuthorityShowCLI 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();
 
         String caIDString = null;
+
         if (cmdArgs.length > 1) {
-            System.err.println("Error: too many arguments.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Too many arguments.");
+
         } else if (cmdArgs.length == 1) {
             caIDString = cmdArgs[0];
         }
 
         if (cmd.hasOption("host-authority")) {
             if (caIDString != null) {
-                System.err.println("Error: authority ID and --host-authority are mutually exclusive.");
-                printHelp();
-                System.exit(-1);
+                throw new Exception("Authority ID and --host-authority are mutually exclusive.");
             }
             caIDString = AuthorityResource.HOST_AUTHORITY;
         }
 
         if (caIDString == null) {
-            System.err.println("Error: No ID specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No ID specified.");
         }
 
         AuthorityData data = authorityCLI.authorityClient.getCA(caIDString);
diff --git a/base/java-tools/src/com/netscape/cmstools/feature/FeatureFindCLI.java b/base/java-tools/src/com/netscape/cmstools/feature/FeatureFindCLI.java
index 5bc77071a91901565931b8f3727051a183f8f8f0..72f22ec7128846dc445b369b0e924ea8bc4acd79 100644
--- a/base/java-tools/src/com/netscape/cmstools/feature/FeatureFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/feature/FeatureFindCLI.java
@@ -21,7 +21,6 @@ import java.util.Arrays;
 import java.util.List;
 
 import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.system.Feature;
 import com.netscape.cmstools.cli.CLI;
@@ -43,21 +42,12 @@ public class FeatureFindCLI 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;
         }
 
         @SuppressWarnings("unused")
-        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);
 
         List<Feature> features = featureCLI.featureClient.listFeatures();
 
diff --git a/base/java-tools/src/com/netscape/cmstools/feature/FeatureShowCLI.java b/base/java-tools/src/com/netscape/cmstools/feature/FeatureShowCLI.java
index 59cfb67eb9b8900dbcec2c1cee9d14137b171328..7eda79951ce6a2794e3427bc2fdafece6989d56f 100644
--- a/base/java-tools/src/com/netscape/cmstools/feature/FeatureShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/feature/FeatureShowCLI.java
@@ -20,7 +20,6 @@ package com.netscape.cmstools.feature;
 import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.ParseException;
 
 import com.netscape.certsrv.system.Feature;
 import com.netscape.cmstools.cli.CLI;
@@ -41,33 +40,20 @@ public class FeatureShowCLI 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: too many arguments.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("Too many arguments.");
         }
 
         if (cmdArgs.length == 0) {
-            System.err.println("Error: No ID specified.");
-            printHelp();
-            System.exit(-1);
+            throw new Exception("No ID specified.");
         }
 
         String featureID = cmdArgs[0];
-- 
2.5.5

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

Reply via email to