diff options
author | Endi S. Dewata <edewata@redhat.com> | 2017-06-03 02:28:00 +0200 |
---|---|---|
committer | Endi S. Dewata <edewata@redhat.com> | 2017-06-03 03:11:21 +0200 |
commit | 729468e46612569da4c93b15bc0d674099003aba (patch) | |
tree | f09e170ad026f95037c527b305be237d6c634401 /base | |
parent | 9741b7873005419b922ba79c61ef98ae17cb58be (diff) | |
download | pki-729468e46612569da4c93b15bc0d674099003aba.tar.gz pki-729468e46612569da4c93b15bc0d674099003aba.tar.xz pki-729468e46612569da4c93b15bc0d674099003aba.zip |
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
Diffstat (limited to 'base')
-rw-r--r-- | base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java | 21 |
1 files changed, 9 insertions, 12 deletions
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); |