>From 8cf4c5fadd2d5a154c99430be9898f37163bdac7 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 24 Mar 2017 04:21:10 +0100
Subject: [PATCH] Refactored TPSConnectorCLI for TKS.

The TPSConnectorCLI for TKS and its submodules have been modified
to use lazy initialization to get the PKIClient object.
---
 .../src/com/netscape/cmstools/system/TPSConnectorAddCLI.java     | 4 +++-
 .../src/com/netscape/cmstools/system/TPSConnectorCLI.java        | 9 ++++++---
 .../src/com/netscape/cmstools/system/TPSConnectorFindCLI.java    | 4 +++-
 .../src/com/netscape/cmstools/system/TPSConnectorModCLI.java     | 4 +++-
 .../src/com/netscape/cmstools/system/TPSConnectorRemoveCLI.java  | 4 +++-
 .../src/com/netscape/cmstools/system/TPSConnectorShowCLI.java    | 4 +++-
 6 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorAddCLI.java b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorAddCLI.java
index 82409723b8452362219209442b3f4cc4aca51bff..7c89c50c5796c19328d5923d1ebef6f89446a4cf 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorAddCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.system.TPSConnectorClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -70,7 +71,8 @@ public class TPSConnectorAddCLI extends CLI {
         String tpsHost = cmd.getOptionValue("host");
         String tpsPort = cmd.getOptionValue("port");
 
-        tpsConnectorCLI.tpsConnectorClient.createConnector(tpsHost, tpsPort);
+        TPSConnectorClient tpsConnectorClient = tpsConnectorCLI.getTPSConnectorClient();
+        tpsConnectorClient.createConnector(tpsHost, tpsPort);
 
         MainCLI.printMessage("Added TPS connector \""+tpsHost + ":" + tpsPort +"\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorCLI.java b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorCLI.java
index 06527a78f0811777a3fc7c0956163eefd202b6ac..762dd64596032b14745be92d82766472500ee038 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorCLI.java
@@ -19,6 +19,7 @@ package com.netscape.cmstools.system;
 
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.system.TPSConnectorClient;
 import com.netscape.certsrv.system.TPSConnectorData;
 import com.netscape.cmstools.cli.CLI;
@@ -43,12 +44,14 @@ public class TPSConnectorCLI extends CLI {
         return parent.getFullName() + "-" + name;
     }
 
-    public void execute(String[] args) throws Exception {
+    public TPSConnectorClient getTPSConnectorClient() throws Exception {
 
-        client = parent.getClient();
+        if (tpsConnectorClient != null) return tpsConnectorClient;
+
+        PKIClient client = getClient();
         tpsConnectorClient = (TPSConnectorClient)parent.getClient("tpsconnector");
 
-        super.execute(args);
+        return tpsConnectorClient;
     }
 
     public static void printConnectorInfo(TPSConnectorData data) {
diff --git a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorFindCLI.java b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorFindCLI.java
index d52c3441254da3d28f9ec00e26d80d9d999dd322..d18400c3f6591ac77b781b60310224e2edfd6d0b 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorFindCLI.java
@@ -23,6 +23,7 @@ import java.util.Collection;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.system.TPSConnectorClient;
 import com.netscape.certsrv.system.TPSConnectorCollection;
 import com.netscape.certsrv.system.TPSConnectorData;
 import com.netscape.cmstools.cli.CLI;
@@ -77,7 +78,8 @@ public class TPSConnectorFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        TPSConnectorCollection result = tpsConnectorCLI.tpsConnectorClient.findConnectors(start, size);
+        TPSConnectorClient tpsConnectorClient = tpsConnectorCLI.getTPSConnectorClient();
+        TPSConnectorCollection result = tpsConnectorClient.findConnectors(start, size);
 
         MainCLI.printMessage(result.getTotal() + " entries matched");
         if (result.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorModCLI.java b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorModCLI.java
index 2bc2a644f85b77c3170fc50c459f02745cfa7c20..1d1bd6607073e9ffd361552f63c0d073d4962c78 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorModCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorModCLI.java
@@ -5,6 +5,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.system.TPSConnectorClient;
 import com.netscape.certsrv.system.TPSConnectorData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -57,7 +58,8 @@ public class TPSConnectorModCLI extends CLI {
         data.setHost(cmd.getOptionValue("host"));
         data.setPort(cmd.getOptionValue("port"));
 
-        data = tpsConnectorCLI.tpsConnectorClient.modifyConnector(connID, data);
+        TPSConnectorClient tpsConnectorClient = tpsConnectorCLI.getTPSConnectorClient();
+        data = tpsConnectorClient.modifyConnector(connID, data);
 
         MainCLI.printMessage("Modified TPS connector \""+connID +"\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorRemoveCLI.java
index 96432cf3b914c7ea86884a8a3be3872d6760a847..c708f7f0f687b9bd64afff33a3a75d06f4f7bfab 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorRemoveCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.system.TPSConnectorClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -70,7 +71,8 @@ public class TPSConnectorRemoveCLI extends CLI {
         String tpsHost = cmd.getOptionValue("host");
         String tpsPort = cmd.getOptionValue("port");
 
-        tpsConnectorCLI.tpsConnectorClient.deleteConnector(tpsHost, tpsPort);
+        TPSConnectorClient tpsConnectorClient = tpsConnectorCLI.getTPSConnectorClient();
+        tpsConnectorClient.deleteConnector(tpsHost, tpsPort);
 
         MainCLI.printMessage("Removed TPS connector \""+tpsHost + ":" + tpsPort +"\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorShowCLI.java b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorShowCLI.java
index c0f09e4f07f6215b34764ff3a69ab4061fba4026..8d37ffe5a111c19a64dec7aecb2f65e202304fa1 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/TPSConnectorShowCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.system.TPSConnectorClient;
 import com.netscape.certsrv.system.TPSConnectorData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -73,7 +74,8 @@ public class TPSConnectorShowCLI extends CLI {
         String tpsHost = cmd.getOptionValue("host");
         String tpsPort = cmd.getOptionValue("port", "443");
 
-        TPSConnectorData data = tpsConnectorCLI.tpsConnectorClient.getConnector(tpsHost, tpsPort);
+        TPSConnectorClient tpsConnectorClient = tpsConnectorCLI.getTPSConnectorClient();
+        TPSConnectorData data = tpsConnectorClient.getConnector(tpsHost, tpsPort);
 
         MainCLI.printMessage("TPS Connector \"" + tpsHost + ":" + tpsPort + "\"");
         TPSConnectorCLI.printConnectorInfo(data);
-- 
2.9.3

