From 10542684d16c86f9ab7fbc67e596d1d5e3df19ef Mon Sep 17 00:00:00 2001 From: "Endi S. Dewata" Date: Wed, 18 Jan 2017 01:24:13 +0100 Subject: Cleaned up error handling in client and PKCS12 CLIs. The client and PKCS12 CLIs have been modified to use Exceptions instead of System.exit() such that errors can be handled consistently. --- .../cmstools/client/ClientCertFindCLI.java | 20 +++--------- .../cmstools/client/ClientCertImportCLI.java | 26 +++------------- .../cmstools/client/ClientCertModifyCLI.java | 22 +++---------- .../cmstools/client/ClientCertRemoveCLI.java | 18 ++--------- .../cmstools/client/ClientCertRequestCLI.java | 24 +++------------ .../cmstools/client/ClientCertShowCLI.java | 22 +++---------- .../cmstools/client/ClientCertValidateCLI.java | 27 +++------------- .../netscape/cmstools/client/ClientInitCLI.java | 21 +++---------- .../netscape/cmstools/pkcs12/PKCS12CertAddCLI.java | 25 +++------------ .../cmstools/pkcs12/PKCS12CertExportCLI.java | 36 +++++----------------- .../cmstools/pkcs12/PKCS12CertFindCLI.java | 25 +++------------ .../netscape/cmstools/pkcs12/PKCS12CertModCLI.java | 29 ++++------------- .../cmstools/pkcs12/PKCS12CertRemoveCLI.java | 25 +++------------ .../netscape/cmstools/pkcs12/PKCS12ExportCLI.java | 20 +++--------- .../netscape/cmstools/pkcs12/PKCS12ImportCLI.java | 20 +++--------- .../netscape/cmstools/pkcs12/PKCS12KeyFindCLI.java | 25 +++------------ .../cmstools/pkcs12/PKCS12KeyRemoveCLI.java | 25 +++------------ 17 files changed, 80 insertions(+), 330 deletions(-) (limited to 'base/java-tools/src/com/netscape/cmstools') diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCertFindCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCertFindCLI.java index 6d0bb6215..c758e3c9a 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertFindCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertFindCLI.java @@ -51,28 +51,16 @@ public class ClientCertFindCLI 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."); } client = parent.getClient(); @@ -86,7 +74,7 @@ public class ClientCertFindCLI extends CLI { if (certs == null || certs.length == 0) { MainCLI.printMessage("No certificates found"); - System.exit(0); // valid result + return; } MainCLI.printMessage(certs.length + " certificate(s) found"); diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCertImportCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCertImportCLI.java index a920079c4..7f67d9240 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertImportCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertImportCLI.java @@ -91,28 +91,16 @@ public class ClientCertImportCLI 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: Too many arguments specified."); - printHelp(); - System.exit(-1); + throw new Exception("Too many arguments specified."); } MainCLI mainCLI = (MainCLI)parent.getParent(); @@ -265,10 +253,7 @@ public class ClientCertImportCLI extends CLI { trustAttributes); } else { - System.err.println("Error: Missing certificate to import"); - printHelp(); - System.exit(-1); - return; + throw new Exception("Missing certificate to import"); } if (nickname == null) { @@ -286,8 +271,7 @@ public class ClientCertImportCLI extends CLI { String trustAttributes) throws Exception { if (nickname == null) { - System.err.println("Error: Missing certificate nickname."); - System.exit(-1); + throw new Exception("Missing certificate nickname."); } String[] command = { diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCertModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCertModifyCLI.java index 738dca07c..f229e6718 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertModifyCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertModifyCLI.java @@ -52,35 +52,21 @@ public class ClientCertModifyCLI extends CLI { public void execute(String[] args) throws Exception { - 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); if (cmd.hasOption("help")) { - // Display usage printHelp(); - System.exit(0); + return; } String[] cmdArgs = cmd.getArgs(); if (cmdArgs.length > 1) { - System.err.println("Error: Too many arguments specified."); - printHelp(); - System.exit(-1); + throw new Exception("Too many arguments specified."); } if (cmdArgs.length == 0) { - System.err.println("Error: Missing certificate nickname."); - printHelp(); - System.exit(-1); + throw new Exception("Missing certificate nickname."); } MainCLI mainCLI = (MainCLI)parent.getParent(); diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCertRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCertRemoveCLI.java index 03743f644..fa350c412 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertRemoveCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertRemoveCLI.java @@ -44,28 +44,16 @@ public class ClientCertRemoveCLI 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 nickname specified."); - printHelp(); - System.exit(-1); + throw new Exception("No nickname specified."); } client = parent.getClient(); diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCertRequestCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCertRequestCLI.java index aff3220f0..37d0e81ae 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertRequestCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertRequestCLI.java @@ -127,28 +127,17 @@ public class ClientCertRequestCLI extends CLI { } public void execute(String[] args) throws Exception { - 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 (cmd.hasOption("help")) { printHelp(); - System.exit(0); + return; } if (cmdArgs.length > 1) { - System.err.println("Error: Too many arguments specified."); - printHelp(); - System.exit(-1); + throw new Exception("Too many arguments specified."); } String certRequestUsername = cmd.getOptionValue("username"); @@ -157,9 +146,7 @@ public class ClientCertRequestCLI extends CLI { if (cmdArgs.length == 0) { if (certRequestUsername == null) { - System.err.println("Error: Missing subject DN or request username."); - printHelp(); - System.exit(-1); + throw new Exception("Missing subject DN or request username."); } subjectDN = "UID=" + certRequestUsername; @@ -221,8 +208,7 @@ public class ClientCertRequestCLI extends CLI { String password = mainCLI.config.getCertPassword(); if (password == null) { - System.err.println("Error: Missing security database password."); - System.exit(-1); + throw new Exception("Missing security database password."); } String csr; diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCertShowCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCertShowCLI.java index e44fae745..963460c35 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertShowCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertShowCLI.java @@ -74,35 +74,21 @@ public class ClientCertShowCLI extends CLI { public void execute(String[] args) throws Exception { - 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); if (cmd.hasOption("help")) { - // Display usage printHelp(); - System.exit(0); + return; } String[] cmdArgs = cmd.getArgs(); if (cmdArgs.length > 1) { - System.err.println("Error: Too many arguments specified."); - printHelp(); - System.exit(-1); + throw new Exception("Too many arguments specified."); } if (cmdArgs.length == 0) { - System.err.println("Error: Missing certificate nickname."); - printHelp(); - System.exit(-1); + throw new Exception("Missing certificate nickname."); } MainCLI mainCLI = (MainCLI)parent.getParent(); diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCertValidateCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCertValidateCLI.java index 50cd96f43..22bddcf32 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertValidateCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertValidateCLI.java @@ -60,28 +60,16 @@ public class ClientCertValidateCLI 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: Invalid number of arguments."); - printHelp(); - System.exit(-1); + throw new Exception("Invalid number of arguments."); } // Get nickname from command argument. @@ -89,14 +77,7 @@ public class ClientCertValidateCLI extends CLI { // get usages from options String certusage = cmd.getOptionValue("certusage"); - boolean isValid = false; - - try { - isValid = verifySystemCertByNickname(nickname, certusage); - } catch (Exception e) { - System.err.println("Certificate verification failed: " + e); - isValid = false; - } + boolean isValid = verifySystemCertByNickname(nickname, certusage); if (isValid) { System.exit(0); diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientInitCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientInitCLI.java index bcce8785d..968539136 100644 --- a/base/java-tools/src/com/netscape/cmstools/client/ClientInitCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/client/ClientInitCLI.java @@ -53,35 +53,22 @@ public class ClientInitCLI 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."); } MainCLI mainCLI = (MainCLI)parent.getParent(); if (mainCLI.config.getCertPassword() == null) { - System.err.println("Error: Security database password is required."); - System.exit(-1); + throw new Exception("Security database password is required."); } boolean force = cmd.hasOption("force"); diff --git a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertAddCLI.java b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertAddCLI.java index a422b200d..0e8b5553f 100644 --- a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertAddCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertAddCLI.java @@ -26,7 +26,6 @@ import java.util.logging.Logger; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import org.mozilla.jss.util.Password; import com.netscape.cmstools.cli.CLI; @@ -75,19 +74,11 @@ public class PKCS12CertAddCLI 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")) { printHelp(); - System.exit(0); + return; } if (cmd.hasOption("verbose")) { @@ -104,9 +95,7 @@ public class PKCS12CertAddCLI extends CLI { String[] cmdArgs = cmd.getArgs(); if (cmdArgs.length == 0) { - System.err.println("Error: Missing certificate nickname."); - printHelp(); - System.exit(-1); + throw new Exception("Missing certificate nickname."); } String nickname = cmdArgs[0]; @@ -114,9 +103,7 @@ public class PKCS12CertAddCLI extends CLI { String filename = cmd.getOptionValue("pkcs12-file"); if (filename == null) { - System.err.println("Error: Missing PKCS #12 file."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 file."); } String passwordString = cmd.getOptionValue("pkcs12-password"); @@ -132,9 +119,7 @@ public class PKCS12CertAddCLI extends CLI { } if (passwordString == null) { - System.err.println("Error: Missing PKCS #12 password."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 password."); } Password password = new Password(passwordString.toCharArray()); diff --git a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertExportCLI.java b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertExportCLI.java index 8fb526d48..73ae9eef0 100644 --- a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertExportCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertExportCLI.java @@ -30,7 +30,6 @@ import java.util.logging.Logger; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import org.mozilla.jss.util.Password; import com.netscape.cmstools.cli.CLI; @@ -84,19 +83,11 @@ public class PKCS12CertExportCLI 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")) { printHelp(); - System.exit(0); + return; } if (cmd.hasOption("verbose")) { @@ -114,15 +105,11 @@ public class PKCS12CertExportCLI extends CLI { String id = cmd.getOptionValue("cert-id"); if (cmdArgs.length < 1 && id == null) { - System.err.println("Error: Missing certificate nickname or ID."); - printHelp(); - System.exit(-1); + throw new Exception("Missing certificate nickname or ID."); } if (cmdArgs.length >= 1 && id != null) { - System.err.println("Error: Certificate nickname and ID are mutually exclusive."); - printHelp(); - System.exit(-1); + throw new Exception("Certificate nickname and ID are mutually exclusive."); } String nickname = null; @@ -137,9 +124,7 @@ public class PKCS12CertExportCLI extends CLI { String pkcs12File = cmd.getOptionValue("pkcs12-file"); if (pkcs12File == null) { - System.err.println("Error: Missing PKCS #12 file."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 file."); } String passwordString = cmd.getOptionValue("pkcs12-password"); @@ -155,9 +140,7 @@ public class PKCS12CertExportCLI extends CLI { } if (passwordString == null) { - System.err.println("Error: Missing PKCS #12 password."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 password."); } Password password = new Password(passwordString.toCharArray()); @@ -165,9 +148,7 @@ public class PKCS12CertExportCLI extends CLI { String certFile = cmd.getOptionValue("cert-file"); if (certFile == null) { - System.err.println("Error: Missing certificate file."); - printHelp(); - System.exit(-1); + throw new Exception("Missing certificate file."); } try { @@ -187,8 +168,7 @@ public class PKCS12CertExportCLI extends CLI { } if (certInfos.isEmpty()) { - System.err.println("Error: Certificate not found."); - System.exit(-1); + throw new Exception("Certificate not found."); } try (PrintStream os = new PrintStream(new FileOutputStream(certFile))) { diff --git a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertFindCLI.java b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertFindCLI.java index 9bb4ad3ba..f978dc1e4 100644 --- a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertFindCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertFindCLI.java @@ -26,7 +26,6 @@ import java.util.logging.Logger; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import org.mozilla.jss.util.Password; import com.netscape.cmstools.cli.CLI; @@ -71,19 +70,11 @@ public class PKCS12CertFindCLI 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")) { printHelp(); - System.exit(0); + return; } if (cmd.hasOption("verbose")) { @@ -100,17 +91,13 @@ public class PKCS12CertFindCLI extends CLI { 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 filename = cmd.getOptionValue("pkcs12-file"); if (filename == null) { - System.err.println("Error: Missing PKCS #12 file."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 file."); } String passwordString = cmd.getOptionValue("pkcs12-password"); @@ -126,9 +113,7 @@ public class PKCS12CertFindCLI extends CLI { } if (passwordString == null) { - System.err.println("Error: Missing PKCS #12 password."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 password."); } Password password = new Password(passwordString.toCharArray()); diff --git a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertModCLI.java b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertModCLI.java index a0db0f766..a8aae4371 100644 --- a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertModCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertModCLI.java @@ -26,7 +26,6 @@ import java.util.logging.Logger; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import org.mozilla.jss.util.Password; import com.netscape.cmstools.cli.CLI; @@ -75,19 +74,11 @@ public class PKCS12CertModCLI 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")) { printHelp(); - System.exit(0); + return; } if (cmd.hasOption("verbose")) { @@ -104,9 +95,7 @@ public class PKCS12CertModCLI extends CLI { String[] cmdArgs = cmd.getArgs(); if (cmdArgs.length == 0) { - System.err.println("Error: Missing certificate nickname."); - printHelp(); - System.exit(-1); + throw new Exception("Missing certificate nickname."); } String nickname = cmdArgs[0]; @@ -114,9 +103,7 @@ public class PKCS12CertModCLI extends CLI { String filename = cmd.getOptionValue("pkcs12-file"); if (filename == null) { - System.err.println("Error: Missing PKCS #12 file."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 file."); } String passwordString = cmd.getOptionValue("pkcs12-password"); @@ -132,9 +119,7 @@ public class PKCS12CertModCLI extends CLI { } if (passwordString == null) { - System.err.println("Error: Missing PKCS #12 password."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 password."); } Password password = new Password(passwordString.toCharArray()); @@ -142,9 +127,7 @@ public class PKCS12CertModCLI extends CLI { String trustFlags = cmd.getOptionValue("trust-flags"); if (trustFlags == null) { - System.err.println("Error: Missing trust flags."); - printHelp(); - System.exit(-1); + throw new Exception("Missing trust flags."); } try { diff --git a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertRemoveCLI.java index 8f7f11398..cb813ebc4 100644 --- a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertRemoveCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12CertRemoveCLI.java @@ -25,7 +25,6 @@ import java.util.logging.Logger; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import org.mozilla.jss.util.Password; import com.netscape.cmstools.cli.CLI; @@ -69,19 +68,11 @@ public class PKCS12CertRemoveCLI 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")) { printHelp(); - System.exit(0); + return; } if (cmd.hasOption("verbose")) { @@ -98,9 +89,7 @@ public class PKCS12CertRemoveCLI extends CLI { String[] cmdArgs = cmd.getArgs(); if (cmdArgs.length == 0) { - System.err.println("Error: Missing certificate nickname."); - printHelp(); - System.exit(-1); + throw new Exception("Missing certificate nickname."); } String nickname = cmdArgs[0]; @@ -108,9 +97,7 @@ public class PKCS12CertRemoveCLI extends CLI { String filename = cmd.getOptionValue("pkcs12-file"); if (filename == null) { - System.err.println("Error: Missing PKCS #12 file."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 file."); } String passwordString = cmd.getOptionValue("pkcs12-password"); @@ -126,9 +113,7 @@ public class PKCS12CertRemoveCLI extends CLI { } if (passwordString == null) { - System.err.println("Error: Missing PKCS #12 password."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 password."); } Password password = new Password(passwordString.toCharArray()); diff --git a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12ExportCLI.java b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12ExportCLI.java index 08a0850ff..3fb6e74fd 100644 --- a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12ExportCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12ExportCLI.java @@ -73,19 +73,11 @@ public class PKCS12ExportCLI extends CLI { public void execute(String[] args) throws Exception { - CommandLine cmd = null; - - try { - cmd = parser.parse(options, args, true); - } catch (Exception e) { - System.err.println("Error: " + e.getMessage()); - printHelp(); - System.exit(-1); - } + CommandLine cmd = parser.parse(options, args, true); if (cmd.hasOption("help")) { printHelp(); - System.exit(0); + return; } if (cmd.hasOption("verbose")) { @@ -103,9 +95,7 @@ public class PKCS12ExportCLI extends CLI { String filename = cmd.getOptionValue("pkcs12-file"); if (filename == null) { - System.err.println("Error: Missing PKCS #12 file."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 file."); } String passwordString = cmd.getOptionValue("pkcs12-password"); @@ -121,9 +111,7 @@ public class PKCS12ExportCLI extends CLI { } if (passwordString == null) { - System.err.println("Error: Missing PKCS #12 password."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 password."); } Password password = new Password(passwordString.toCharArray()); diff --git a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12ImportCLI.java b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12ImportCLI.java index 862fffb64..da5478c60 100644 --- a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12ImportCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12ImportCLI.java @@ -70,19 +70,11 @@ public class PKCS12ImportCLI extends CLI { public void execute(String[] args) throws Exception { - CommandLine cmd = null; - - try { - cmd = parser.parse(options, args, true); - } catch (Exception e) { - System.err.println("Error: " + e.getMessage()); - printHelp(); - System.exit(-1); - } + CommandLine cmd = parser.parse(options, args, true); if (cmd.hasOption("help")) { printHelp(); - System.exit(0); + return; } if (cmd.hasOption("verbose")) { @@ -100,9 +92,7 @@ public class PKCS12ImportCLI extends CLI { String filename = cmd.getOptionValue("pkcs12-file"); if (filename == null) { - System.err.println("Error: Missing PKCS #12 file."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 file."); } String passwordString = cmd.getOptionValue("pkcs12-password"); @@ -118,9 +108,7 @@ public class PKCS12ImportCLI extends CLI { } if (passwordString == null) { - System.err.println("Error: Missing PKCS #12 password."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 password."); } Password password = new Password(passwordString.toCharArray()); diff --git a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12KeyFindCLI.java b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12KeyFindCLI.java index 0dc39f470..835e91924 100644 --- a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12KeyFindCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12KeyFindCLI.java @@ -26,7 +26,6 @@ import java.util.logging.Logger; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import org.mozilla.jss.util.Password; import com.netscape.cmstools.cli.CLI; @@ -71,19 +70,11 @@ public class PKCS12KeyFindCLI 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")) { printHelp(); - System.exit(0); + return; } if (cmd.hasOption("verbose")) { @@ -100,17 +91,13 @@ public class PKCS12KeyFindCLI extends CLI { 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 filename = cmd.getOptionValue("pkcs12-file"); if (filename == null) { - System.err.println("Error: Missing PKCS #12 file."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 file."); } String passwordString = cmd.getOptionValue("pkcs12-password"); @@ -126,9 +113,7 @@ public class PKCS12KeyFindCLI extends CLI { } if (passwordString == null) { - System.err.println("Error: Missing PKCS #12 password."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 password."); } Password password = new Password(passwordString.toCharArray()); diff --git a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12KeyRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12KeyRemoveCLI.java index 19b368765..50ff015d7 100644 --- a/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12KeyRemoveCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/pkcs12/PKCS12KeyRemoveCLI.java @@ -26,7 +26,6 @@ import java.util.logging.Logger; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.apache.commons.cli.ParseException; import org.mozilla.jss.util.Password; import com.netscape.cmstools.cli.CLI; @@ -70,19 +69,11 @@ public class PKCS12KeyRemoveCLI 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")) { printHelp(); - System.exit(0); + return; } if (cmd.hasOption("verbose")) { @@ -99,9 +90,7 @@ public class PKCS12KeyRemoveCLI extends CLI { String[] cmdArgs = cmd.getArgs(); if (cmdArgs.length == 0) { - System.err.println("Error: Missing key ID."); - printHelp(); - System.exit(-1); + throw new Exception("Missing key ID."); } BigInteger keyID = new BigInteger(cmdArgs[0], 16); @@ -109,9 +98,7 @@ public class PKCS12KeyRemoveCLI extends CLI { String filename = cmd.getOptionValue("pkcs12-file"); if (filename == null) { - System.err.println("Error: Missing PKCS #12 file."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 file."); } String passwordString = cmd.getOptionValue("pkcs12-password"); @@ -127,9 +114,7 @@ public class PKCS12KeyRemoveCLI extends CLI { } if (passwordString == null) { - System.err.println("Error: Missing PKCS #12 password."); - printHelp(); - System.exit(-1); + throw new Exception("Missing PKCS #12 password."); } Password password = new Password(passwordString.toCharArray()); -- cgit