>From f44965d97cb8d501797ad574ba4aee42d41634c7 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 24 Mar 2017 03:49:46 +0100
Subject: [PATCH] Refactored AuthenticatorCLI.

The AuthenticatorCLI and its submodules have been modified to use
lazy initialization to get the PKIClient object.
---
 .../netscape/cmstools/tps/authenticator/AuthenticatorAddCLI.java | 4 +++-
 .../netscape/cmstools/tps/authenticator/AuthenticatorCLI.java    | 9 ++++++---
 .../cmstools/tps/authenticator/AuthenticatorFindCLI.java         | 4 +++-
 .../cmstools/tps/authenticator/AuthenticatorModifyCLI.java       | 6 ++++--
 .../cmstools/tps/authenticator/AuthenticatorRemoveCLI.java       | 4 +++-
 .../cmstools/tps/authenticator/AuthenticatorShowCLI.java         | 4 +++-
 6 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorAddCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorAddCLI.java
index 5ae04afa5f31d0cd9269b5177616a07e354c51e8..163b89c768a1ef978519766800301f5c3a877dc6 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorAddCLI.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.authenticator.AuthenticatorClient;
 import com.netscape.certsrv.tps.authenticator.AuthenticatorData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -87,7 +88,8 @@ public class AuthenticatorAddCLI extends CLI {
             authenticatorData = AuthenticatorData.valueOf(sw.toString());
         }
 
-        authenticatorData = authenticatorCLI.authenticatorClient.addAuthenticator(authenticatorData);
+        AuthenticatorClient authenticatorClient = authenticatorCLI.getAuthenticatorClient();
+        authenticatorData = authenticatorClient.addAuthenticator(authenticatorData);
 
         MainCLI.printMessage("Added authenticator \"" + authenticatorData.getID() + "\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorCLI.java
index 8f802f57b681dca5fba491249636282d2b726a20..e35f414307a8e40605f66adc6447b870764e5677 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorCLI.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.authenticator.AuthenticatorClient;
 import com.netscape.certsrv.tps.authenticator.AuthenticatorData;
 import com.netscape.cmstools.cli.CLI;
@@ -44,12 +45,14 @@ public class AuthenticatorCLI extends CLI {
         addModule(new AuthenticatorShowCLI(this));
     }
 
-    public void execute(String[] args) throws Exception {
+    public AuthenticatorClient getAuthenticatorClient() throws Exception {
 
-        client = parent.getClient();
+        if (authenticatorClient != null) return authenticatorClient;
+
+        PKIClient client = getClient();
         authenticatorClient = (AuthenticatorClient)parent.getClient("authenticator");
 
-        super.execute(args);
+        return authenticatorClient;
     }
 
     public static void printAuthenticatorData(AuthenticatorData authenticatorData, boolean showProperties) throws IOException {
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorFindCLI.java
index 778f370f00efb34ce7e35a1731e9ba7282e417ca..ec46476afdfe503a6c32e2a70b717ad9074e158b 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorFindCLI.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.authenticator.AuthenticatorClient;
 import com.netscape.certsrv.tps.authenticator.AuthenticatorCollection;
 import com.netscape.certsrv.tps.authenticator.AuthenticatorData;
 import com.netscape.cmstools.cli.CLI;
@@ -75,7 +76,8 @@ public class AuthenticatorFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        AuthenticatorCollection result = authenticatorCLI.authenticatorClient.findAuthenticators(filter, start, size);
+        AuthenticatorClient authenticatorClient = authenticatorCLI.getAuthenticatorClient();
+        AuthenticatorCollection result = authenticatorClient.findAuthenticators(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/authenticator/AuthenticatorModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorModifyCLI.java
index db5849b3191c1dc87dffdd54b07ee96b40c9abd4..4e6baeb9b4c408a0281523efc98582bdcf90352b 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorModifyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorModifyCLI.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.authenticator.AuthenticatorClient;
 import com.netscape.certsrv.tps.authenticator.AuthenticatorData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -78,6 +79,7 @@ public class AuthenticatorModifyCLI extends CLI {
         String action = cmd.getOptionValue("action", "update");
         String input = cmd.getOptionValue("input");
 
+        AuthenticatorClient authenticatorClient = authenticatorCLI.getAuthenticatorClient();
         AuthenticatorData authenticatorData;
 
         if (action.equals("update")) {
@@ -98,10 +100,10 @@ public class AuthenticatorModifyCLI extends CLI {
                 authenticatorData = AuthenticatorData.valueOf(sw.toString());
             }
 
-            authenticatorData = authenticatorCLI.authenticatorClient.updateAuthenticator(authenticatorID, authenticatorData);
+            authenticatorData = authenticatorClient.updateAuthenticator(authenticatorID, authenticatorData);
 
         } else { // other actions
-            authenticatorData = authenticatorCLI.authenticatorClient.changeAuthenticatorStatus(authenticatorID, action);
+            authenticatorData = authenticatorClient.changeAuthenticatorStatus(authenticatorID, action);
         }
 
         MainCLI.printMessage("Modified authenticator \"" + authenticatorID + "\"");
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorRemoveCLI.java
index 1e40873db63b88bfe4c24c80ed62b80ae3be0cc6..e3327b5322ff7c91ceb3b95dd6f445659db00732 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorRemoveCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.tps.authenticator.AuthenticatorClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -58,7 +59,8 @@ public class AuthenticatorRemoveCLI extends CLI {
 
         String authenticatorID = args[0];
 
-        authenticatorCLI.authenticatorClient.removeAuthenticator(authenticatorID);
+        AuthenticatorClient authenticatorClient = authenticatorCLI.getAuthenticatorClient();
+        authenticatorClient.removeAuthenticator(authenticatorID);
 
         MainCLI.printMessage("Deleted authenticator \"" + authenticatorID + "\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorShowCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorShowCLI.java
index 6df9129c069f0383023be4a398054e38d8e35fa3..f764bde1d5d02145af5327983c7b07c61031aaf0 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/authenticator/AuthenticatorShowCLI.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.authenticator.AuthenticatorClient;
 import com.netscape.certsrv.tps.authenticator.AuthenticatorData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -71,7 +72,8 @@ public class AuthenticatorShowCLI extends CLI {
         String authenticatorID = args[0];
         String output = cmd.getOptionValue("output");
 
-        AuthenticatorData authenticatorData = authenticatorCLI.authenticatorClient.getAuthenticator(authenticatorID);
+        AuthenticatorClient authenticatorClient = authenticatorCLI.getAuthenticatorClient();
+        AuthenticatorData authenticatorData = authenticatorClient.getAuthenticator(authenticatorID);
 
         if (output == null) {
             MainCLI.printMessage("Authenticator \"" + authenticatorID + "\"");
-- 
2.9.3

