>From 98a767027aa05b9ea7ef7c66553c66a5056f23a2 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 24 Mar 2017 03:53:07 +0100
Subject: [PATCH] Refactored TPSCertCLI.

The TPSCertCLI and its submodules have been modified to use lazy
initialization to get the PKIClient object.
---
 .../src/com/netscape/cmstools/tps/cert/TPSCertCLI.java           | 9 ++++++---
 .../src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java       | 4 +++-
 .../src/com/netscape/cmstools/tps/cert/TPSCertShowCLI.java       | 4 +++-
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertCLI.java
index 4fce3bcc3511ff3ca0bf3c265a00c8074ac3e5c3..835a522e1f066e62a7b7448a67548c888a85d3ec 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertCLI.java
@@ -20,6 +20,7 @@ package com.netscape.cmstools.tps.cert;
 
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.tps.cert.TPSCertClient;
 import com.netscape.certsrv.tps.cert.TPSCertData;
 import com.netscape.cmstools.cli.CLI;
@@ -38,12 +39,14 @@ public class TPSCertCLI extends CLI {
         addModule(new TPSCertShowCLI(this));
     }
 
-    public void execute(String[] args) throws Exception {
+    public TPSCertClient getTPSCertClient() throws Exception {
 
-        client = parent.getClient();
+        if (certClient != null) return certClient;
+
+        PKIClient client = getClient();
         certClient = (TPSCertClient)parent.getClient("cert");
 
-        super.execute(args);
+        return certClient;
     }
 
     public static void printCert(TPSCertData cert) {
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java
index bea4266c0c473fcd6020068fc8e8de7764b65eec..a73a211d83deb15faa12ddc5883f237a9aa54c1a 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java
@@ -24,6 +24,7 @@ import java.util.Collection;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.tps.cert.TPSCertClient;
 import com.netscape.certsrv.tps.cert.TPSCertCollection;
 import com.netscape.certsrv.tps.cert.TPSCertData;
 import com.netscape.cmstools.cli.CLI;
@@ -91,7 +92,8 @@ public class TPSCertFindCLI extends CLI {
             throw new Exception("Invalid value for --size parameter: " + string4, e);
         }
 
-        TPSCertCollection result = certCLI.certClient.findCerts(filter, tokenID, start, size);
+        TPSCertClient certClient = certCLI.getTPSCertClient();
+        TPSCertCollection result = certClient.findCerts(filter, tokenID, start, size);
 
         MainCLI.printMessage(result.getTotal() + " entries matched");
         if (result.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertShowCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertShowCLI.java
index 16eaaf25b34d07d3f7c4de9450d2509f35d51b49..f21f9f6f8d5ec4125538c60b4c1b6c3c985a9afa 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertShowCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.tps.cert.TPSCertClient;
 import com.netscape.certsrv.tps.cert.TPSCertData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -59,7 +60,8 @@ public class TPSCertShowCLI extends CLI {
 
         String certID = cmdArgs[0];
 
-        TPSCertData certData = certCLI.certClient.getCert(certID);
+        TPSCertClient certClient = certCLI.getTPSCertClient();
+        TPSCertData certData = certClient.getCert(certID);
 
         MainCLI.printMessage("Certificate \"" + certID + "\"");
 
-- 
2.9.3

