>From 0fc2d665dadc062d9115aba2083eb780e91b6513 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Thu, 23 Mar 2017 23:21:12 +0100
Subject: [PATCH] Refactored ClientCLI.

The ClientCLI and its submodules have been modified to use lazy
initialization to get the PKIClient object.
---
 base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java    | 7 -------
 .../src/com/netscape/cmstools/client/ClientCertFindCLI.java        | 3 ++-
 .../src/com/netscape/cmstools/client/ClientCertImportCLI.java      | 4 ++--
 .../src/com/netscape/cmstools/client/ClientCertRemoveCLI.java      | 3 ++-
 .../src/com/netscape/cmstools/client/ClientCertRequestCLI.java     | 6 ++++--
 .../src/com/netscape/cmstools/client/ClientCertShowCLI.java        | 3 ++-
 6 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java
index 8bafd84f67f61da24f5df212c9c5b6c0debfe97f..c2826df692676a4c7aef566c55e23420fd572299 100644
--- a/base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java
@@ -56,13 +56,6 @@ public class ClientCLI extends CLI {
         return "pki-client";
     }
 
-    public void execute(String[] args) throws Exception {
-
-        client = parent.getClient();
-
-        super.execute(args);
-    }
-
     public static void printCertInfo(X509Certificate cert) {
         System.out.println("  Serial Number: "+new CertId(cert.getSerialNumber()).toHexString());
         System.out.println("  Nickname: "+cert.getNickname());
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 c758e3c9a9a601858d3cd636b266eb0bf1cb34d0..0516d9c936d9f1a5100310e16f0a9de80d0a3051 100644
--- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertFindCLI.java
@@ -23,6 +23,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.mozilla.jss.crypto.X509Certificate;
 
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -63,7 +64,7 @@ public class ClientCertFindCLI extends CLI {
             throw new Exception("Too many arguments specified.");
         }
 
-        client = parent.getClient();
+        PKIClient client = getClient();
 
         X509Certificate[] certs;
         if (cmd.hasOption("ca")) {
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 7f67d92403c96690366c2e5db5da97988ff030f4..9cb3e67235e4426d4d90ee3fe4cd4913f77dc46f 100644
--- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertImportCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertImportCLI.java
@@ -194,7 +194,7 @@ public class ClientCertImportCLI extends CLI {
             // late initialization
             mainCLI.init();
 
-            client = mainCLI.getClient();
+            PKIClient client = getClient();
             URI serverURI = mainCLI.config.getServerURI();
 
             String caServerURI = serverURI.getScheme() + "://" +
@@ -315,7 +315,7 @@ public class ClientCertImportCLI extends CLI {
     public void run(String[] command) throws IOException, InterruptedException {
 
         if (verbose) {
-           System.out.println("Command: " + StringUtils.join(command));
+           System.out.println("Command: " + StringUtils.join(command, " "));
         }
 
         Runtime rt = Runtime.getRuntime();
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 fa350c4123c72797205b9e8592e7e31c7237423b..7dc4bdaac1555b0a80f1e2d2ebc4c0933779d90c 100644
--- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertRemoveCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -56,7 +57,7 @@ public class ClientCertRemoveCLI extends CLI {
             throw new Exception("No nickname specified.");
         }
 
-        client = parent.getClient();
+        PKIClient client = getClient();
 
         String nickname = cmdArgs[0];
         client.removeCert(nickname);
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 37d0e81aec36bd7c39f6caf299b7d961d0ea9647..cbbf4e9729bc55e541814044e71902a933ab79b0 100644
--- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertRequestCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertRequestCLI.java
@@ -40,6 +40,7 @@ import org.mozilla.jss.pkix.primitive.Name;
 import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertEnrollmentRequest;
 import com.netscape.certsrv.cert.CertRequestInfos;
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.profile.ProfileAttribute;
 import com.netscape.certsrv.profile.ProfileInput;
 import com.netscape.certsrv.system.SystemCertClient;
@@ -212,19 +213,20 @@ public class ClientCertRequestCLI extends CLI {
         }
 
         String csr;
+        PKIClient client;
         if ("pkcs10".equals(requestType)) {
             csr = generatePkcs10Request(certDatabase, password, algorithm, length, subjectDN);
 
             // initialize database after PKCS10Client to avoid conflict
             mainCLI.init();
-            client = mainCLI.getClient();
+            client = getClient();
 
 
         } else if ("crmf".equals(requestType)) {
 
             // initialize database before CRMFPopClient to load transport certificate
             mainCLI.init();
-            client = mainCLI.getClient();
+            client = getClient();
 
             String encoded;
             if (transportCertFilename == null) {
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 963460c35dd34a0aa827d678f3f961a7af236298..2242b37f84d31db6be227e07eeb4f9acf12e3b6b 100644
--- a/base/java-tools/src/com/netscape/cmstools/client/ClientCertShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCertShowCLI.java
@@ -29,6 +29,7 @@ import org.apache.commons.lang.RandomStringUtils;
 import org.apache.commons.lang.StringUtils;
 import org.mozilla.jss.crypto.X509Certificate;
 
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -126,7 +127,7 @@ public class ClientCertShowCLI extends CLI {
 
             mainCLI.init();
 
-            client = mainCLI.getClient();
+            PKIClient client = getClient();
             X509Certificate cert = client.getCert(nickname);
 
             ClientCLI.printCertInfo(cert);
-- 
2.9.3

