>From 0579bd12825dfe7100a23410fed80aa528c21573 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 24 Mar 2017 04:04:52 +0100
Subject: [PATCH] Refactored TPS TokenCLI.

The TPS TokenCLI and its submodules have been modified to use
lazy initialization to get the PKIClient object.
---
 .../src/com/netscape/cmstools/tps/token/TokenAddCLI.java         | 4 +++-
 .../java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java | 9 ++++++---
 .../src/com/netscape/cmstools/tps/token/TokenFindCLI.java        | 4 +++-
 .../src/com/netscape/cmstools/tps/token/TokenModifyCLI.java      | 7 +++++--
 .../src/com/netscape/cmstools/tps/token/TokenRemoveCLI.java      | 4 +++-
 .../src/com/netscape/cmstools/tps/token/TokenShowCLI.java        | 4 +++-
 6 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenAddCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenAddCLI.java
index 847a9967a9a13380cdfb93bacd3d7c75e3361579..94d607f7da1bfd49c78afe167bd629cc41c32595 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenAddCLI.java
@@ -23,6 +23,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.tps.token.TokenClient;
 import com.netscape.certsrv.tps.token.TokenData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -81,7 +82,8 @@ public class TokenAddCLI extends CLI {
         String policy = cmd.getOptionValue("policy");
         tokenData.setPolicy(policy);
 
-        tokenData = tokenCLI.tokenClient.addToken(tokenData);
+        TokenClient tokenClient = tokenCLI.getTokenClient();
+        tokenData = tokenClient.addToken(tokenData);
 
         MainCLI.printMessage("Added token \"" + tokenID + "\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java
index 77ca40bd154d9ca8d517e0a664102db1df52044a..22bc5551987a69ceab448718a791e297d6d0b2e8 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java
@@ -24,6 +24,7 @@ import java.util.Collection;
 import org.apache.commons.lang.StringUtils;
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.tps.token.TokenClient;
 import com.netscape.certsrv.tps.token.TokenData;
 import com.netscape.certsrv.tps.token.TokenData.TokenStatusData;
@@ -47,12 +48,14 @@ public class TokenCLI extends CLI {
         addModule(new TokenShowCLI(this));
     }
 
-    public void execute(String[] args) throws Exception {
+    public TokenClient getTokenClient() throws Exception {
 
-        client = parent.getClient();
+        if (tokenClient != null) return tokenClient;
+
+        PKIClient client = getClient();
         tokenClient = (TokenClient)parent.getClient("token");
 
-        super.execute(args);
+        return tokenClient;
     }
 
     public static void printToken(TokenData token) {
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenFindCLI.java
index d2ecde91b89f3a174d2a508475d303db9f0f612f..b7aed7f42ba4b1cbd5312ffc6bb49886fce7b916 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenFindCLI.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.token.TokenClient;
 import com.netscape.certsrv.tps.token.TokenCollection;
 import com.netscape.certsrv.tps.token.TokenData;
 import com.netscape.certsrv.tps.token.TokenStatus;
@@ -101,7 +102,8 @@ public class TokenFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        TokenCollection result = tokenCLI.tokenClient.findTokens(
+        TokenClient tokenClient = tokenCLI.getTokenClient();
+        TokenCollection result = tokenClient.findTokens(
                 filter,
                 tokenID,
                 userID,
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenModifyCLI.java
index abc383e643ffd8c66ab72348b6ff9802732046e1..75fb498b62bad39ded01d30c4011f641b3e6ccd8 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenModifyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenModifyCLI.java
@@ -23,6 +23,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.tps.token.TokenClient;
 import com.netscape.certsrv.tps.token.TokenData;
 import com.netscape.certsrv.tps.token.TokenStatus;
 import com.netscape.cmstools.cli.CLI;
@@ -93,13 +94,15 @@ public class TokenModifyCLI extends CLI {
             modify = true;
         }
 
+        TokenClient tokenClient = tokenCLI.getTokenClient();
+
         if (modify) {
-            tokenData = tokenCLI.tokenClient.modifyToken(tokenID, tokenData);
+            tokenData = tokenClient.modifyToken(tokenID, tokenData);
         }
 
         String status = cmd.getOptionValue("status");
         if (status != null) {
-            tokenData = tokenCLI.tokenClient.changeTokenStatus(tokenID, TokenStatus.valueOf(status));
+            tokenData = tokenClient.changeTokenStatus(tokenID, TokenStatus.valueOf(status));
         }
 
         if (!modify && status == null) {
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenRemoveCLI.java
index 10446fd9e46ab6ceaa0c36a90850061bcbef33a4..9b75103cdacaf5ef92656d11853af1338cd900dd 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenRemoveCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.tps.token.TokenClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -58,7 +59,8 @@ public class TokenRemoveCLI extends CLI {
 
         String tokenID = args[0];
 
-        tokenCLI.tokenClient.removeToken(tokenID);
+        TokenClient tokenClient = tokenCLI.getTokenClient();
+        tokenClient.removeToken(tokenID);
 
         MainCLI.printMessage("Deleted token \"" + tokenID + "\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenShowCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenShowCLI.java
index 19237937ca301ab04a0eb7039e7305cb5513c5b3..129e4b5c0af117b3d75d9e12b570349305ee5438 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenShowCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.tps.token.TokenClient;
 import com.netscape.certsrv.tps.token.TokenData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -59,7 +60,8 @@ public class TokenShowCLI extends CLI {
 
         String tokenID = args[0];
 
-        TokenData tokenData = tokenCLI.tokenClient.getToken(tokenID);
+        TokenClient tokenClient = tokenCLI.getTokenClient();
+        TokenData tokenData = tokenClient.getToken(tokenID);
 
         MainCLI.printMessage("Token \"" + tokenID + "\"");
 
-- 
2.9.3

