From a9a02ae0b8b136fbf5e044e881f65c1c63b5653a Mon Sep 17 00:00:00 2001 From: "Endi S. Dewata" Date: Tue, 17 Jan 2017 23:02:02 +0100 Subject: 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 | 56 +++++++--------------- .../cmstools/profile/ProfileRemoveCLI.java | 18 ++----- .../netscape/cmstools/profile/ProfileShowCLI.java | 22 ++------- 21 files changed, 107 insertions(+), 395 deletions(-) (limited to 'base/java-tools/src/com') 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 8e1045bf3..1a9e4de1f 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 a4b184871..9c30ee003 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 78beb12d0..a76728255 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 2d8779d07..71d669e50 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 f5d2df26b..220ee7c18 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 8b626703f..cb3a79bfb 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 d25639281..bc3df309a 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 " and "--file " 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 ' and '--file ' " + - "options are mutually exclusive!"); - printHelp(); - System.exit(-1); + throw new Exception("The '--action ' and '--file ' " + + "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 ' or '--file ' option."); - printHelp(); - System.exit(-1); + throw new Exception("Missing '--action ' or '--file ' 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 aac18a679..6df333eae 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 991ab462b..acdaebe08 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 afb0e03fd..e61401b90 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 febc4a494..a31b02253 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 42c56747a..0a9106705 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 fc144c13d..ced286e5f 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 ecfa75340..e4acdb195 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 7e7f50f78..dc21ac4a8 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 50600ba15..b8bb9f03f 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 73157c2b1..f8c56ba64 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 9552fd70d..512c7cd30 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 cc0f415b7..835f628c1 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() + " [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); - - 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); + 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()); + + 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 b9f5aa689..f0920cde3 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 1dd85f43b..5134530bc 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."); } } -- cgit