From 729468e46612569da4c93b15bc0d674099003aba Mon Sep 17 00:00:00 2001 From: "Endi S. Dewata" Date: Sat, 3 Jun 2017 02:28:00 +0200 Subject: Refactored MainCLI.loadPassword() (part 3). The MainCLI.loadPassword() has been modified to use try-with- resources. Some log messages have been added for clarity. https://pagure.io/dogtagpki/issue/2717 Change-Id: Ic4950ba677613565f548b51d1f985177c6726510 --- .../src/com/netscape/cmstools/cli/MainCLI.java | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'base/java-tools/src/com/netscape') diff --git a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java index 2b6b173b8..dcc60e25f 100644 --- a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java +++ b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java @@ -230,12 +230,11 @@ public class MainCLI extends CLI { } public String[] loadPassword(String pwfile) throws Exception { + String[] tokenPassword = { null, null }; - BufferedReader br = null; String delimiter = "="; - try { - br = new BufferedReader(new FileReader(pwfile)); + try (BufferedReader br = new BufferedReader(new FileReader(pwfile))) { String line = br.readLine(); @@ -276,11 +275,6 @@ public class MainCLI extends CLI { // Set simple 'password' (do not trim leading/trailing whitespace) tokenPassword[1] = line; } - - } finally { - if (br != null) { - br.close(); - } } return tokenPassword; @@ -399,7 +393,7 @@ public class MainCLI extends CLI { config.setCertNickname(certNickname); if (certPasswordFile != null) { - // read client security database password from specified file + if (verbose) System.out.println("Loading NSS password from " + certPasswordFile); tokenPasswordPair = loadPassword(certPasswordFile); // XXX TBD set client security database token @@ -413,7 +407,7 @@ public class MainCLI extends CLI { config.setUsername(username); if (passwordFile != null) { - // read user password from specified file + if (verbose) System.out.println("Loading user password from " + passwordFile); tokenPasswordPair = loadPassword(passwordFile); // XXX TBD set user token @@ -494,15 +488,18 @@ public class MainCLI extends CLI { // If password is specified, use password to access security token if (config.getCertPassword() != null) { - if (verbose) System.out.println("Logging into security token"); + try { CryptoManager manager = CryptoManager.getInstance(); String tokenName = config.getTokenName(); - CryptoToken token = CryptoUtil.getKeyStorageToken(tokenName); + if (verbose) System.out.println("Getting " + (tokenName == null ? "internal" : tokenName) + " token"); + CryptoToken token = CryptoUtil.getKeyStorageToken(tokenName); manager.setThreadToken(token); + if (verbose) System.out.println("Logging into " + token.getName()); + Password password = new Password(config.getCertPassword().toCharArray()); token.login(password); -- cgit