>From 77b75edc9b6fb1442afc5c595c7aa2194a2c1da1 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 24 Mar 2017 04:16:01 +0100
Subject: [PATCH] Refactored TPS ConnectorCLI.

The TPS ConnectorCLI and its submodules have been modified to use
lazy initialization to get the PKIClient object.
---
 .../src/com/netscape/cmstools/tps/connector/ConnectorAddCLI.java | 4 +++-
 .../src/com/netscape/cmstools/tps/connector/ConnectorCLI.java    | 9 ++++++---
 .../com/netscape/cmstools/tps/connector/ConnectorFindCLI.java    | 4 +++-
 .../com/netscape/cmstools/tps/connector/ConnectorModifyCLI.java  | 6 ++++--
 .../com/netscape/cmstools/tps/connector/ConnectorRemoveCLI.java  | 4 +++-
 .../com/netscape/cmstools/tps/connector/ConnectorShowCLI.java    | 4 +++-
 6 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorAddCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorAddCLI.java
index 2591fe20e43eaeee84dbf7f933fc926c9fc1985f..fa2b71fd8e719d6328b8f45bfc1dfab5166dbedb 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorAddCLI.java
@@ -27,6 +27,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.tps.connector.ConnectorClient;
 import com.netscape.certsrv.tps.connector.ConnectorData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -87,7 +88,8 @@ public class ConnectorAddCLI extends CLI {
             connectorData = ConnectorData.valueOf(sw.toString());
         }
 
-        connectorData = connectorCLI.connectorClient.addConnector(connectorData);
+        ConnectorClient connectorClient = connectorCLI.getConnectorClient();
+        connectorData = connectorClient.addConnector(connectorData);
 
         MainCLI.printMessage("Added connector \"" + connectorData.getID() + "\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorCLI.java
index cd59f92d6ff39ef9b4f4ddc96f855a5636de798c..51bf81700f71308ccfd20f53d5729e195395de78 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorCLI.java
@@ -23,6 +23,7 @@ import java.util.Map;
 
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.tps.connector.ConnectorClient;
 import com.netscape.certsrv.tps.connector.ConnectorData;
 import com.netscape.cmstools.cli.CLI;
@@ -44,12 +45,14 @@ public class ConnectorCLI extends CLI {
         addModule(new ConnectorShowCLI(this));
     }
 
-    public void execute(String[] args) throws Exception {
+    public ConnectorClient getConnectorClient() throws Exception {
 
-        client = parent.getClient();
+        if (connectorClient != null) return connectorClient;
+
+        PKIClient client = getClient();
         connectorClient = (ConnectorClient)parent.getClient("connector");
 
-        super.execute(args);
+        return connectorClient;
     }
 
     public static void printConnectorData(ConnectorData connectorData, boolean showProperties) throws IOException {
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorFindCLI.java
index a2297c70574571af3441d8a2ddf2ae30d8b2db68..81aa05a6b7b0a57e31b4c75cd0dff452c50e1d1d 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorFindCLI.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.connector.ConnectorClient;
 import com.netscape.certsrv.tps.connector.ConnectorCollection;
 import com.netscape.certsrv.tps.connector.ConnectorData;
 import com.netscape.cmstools.cli.CLI;
@@ -75,7 +76,8 @@ public class ConnectorFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        ConnectorCollection result = connectorCLI.connectorClient.findConnectors(filter, start, size);
+        ConnectorClient connectorClient = connectorCLI.getConnectorClient();
+        ConnectorCollection result = connectorClient.findConnectors(filter, start, size);
 
         MainCLI.printMessage(result.getTotal() + " entries matched");
         if (result.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorModifyCLI.java
index 1de707402ddd786e8dcc487c10352eac3d5caa76..3b07af2ea1c4a9537a633398d3f48a480ce0f2e1 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorModifyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorModifyCLI.java
@@ -27,6 +27,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.tps.connector.ConnectorClient;
 import com.netscape.certsrv.tps.connector.ConnectorData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -78,6 +79,7 @@ public class ConnectorModifyCLI extends CLI {
         String action = cmd.getOptionValue("action", "update");
         String input = cmd.getOptionValue("input");
 
+        ConnectorClient connectorClient = connectorCLI.getConnectorClient();
         ConnectorData connectorData;
 
         if (action.equals("update")) {
@@ -98,10 +100,10 @@ public class ConnectorModifyCLI extends CLI {
                 connectorData = ConnectorData.valueOf(sw.toString());
             }
 
-            connectorData = connectorCLI.connectorClient.updateConnector(connectorID, connectorData);
+            connectorData = connectorClient.updateConnector(connectorID, connectorData);
 
         } else { // other actions
-            connectorData = connectorCLI.connectorClient.changeConnectorStatus(connectorID, action);
+            connectorData = connectorClient.changeConnectorStatus(connectorID, action);
         }
 
         MainCLI.printMessage("Modified connector \"" + connectorID + "\"");
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorRemoveCLI.java
index 577d73111139c30df0571ee8e747575d4ab0b53b..26f39880079dd9f16a064931e753d83560763ac9 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorRemoveCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.tps.connector.ConnectorClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -58,7 +59,8 @@ public class ConnectorRemoveCLI extends CLI {
 
         String connectorID = args[0];
 
-        connectorCLI.connectorClient.removeConnector(connectorID);
+        ConnectorClient connectorClient = connectorCLI.getConnectorClient();
+        connectorClient.removeConnector(connectorID);
 
         MainCLI.printMessage("Deleted connector \"" + connectorID + "\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorShowCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorShowCLI.java
index 0918461e307c360e1be35f6f326c29251b7c721e..5fc6525702e85c9f33e1999eb07868cd711cbc4a 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/connector/ConnectorShowCLI.java
@@ -25,6 +25,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.tps.connector.ConnectorClient;
 import com.netscape.certsrv.tps.connector.ConnectorData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -71,7 +72,8 @@ public class ConnectorShowCLI extends CLI {
         String connectorID = args[0];
         String output = cmd.getOptionValue("output");
 
-        ConnectorData connectorData = connectorCLI.connectorClient.getConnector(connectorID);
+        ConnectorClient connectorClient = connectorCLI.getConnectorClient();
+        ConnectorData connectorData = connectorClient.getConnector(connectorID);
 
         if (output == null) {
             MainCLI.printMessage("Connector \"" + connectorID + "\"");
-- 
2.9.3

