>From 043aa471f9e243faad58917e9e055affdb694c79 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 24 Mar 2017 02:02:51 +0100
Subject: [PATCH] Refactored AuthorityCLI.

The AuthorityCLI and its submodules have been modified to use lazy
initialization to get the PKIClient object.
---
 .../src/com/netscape/cmstools/authority/AuthorityCLI.java     | 11 ++++++++---
 .../com/netscape/cmstools/authority/AuthorityCreateCLI.java   |  4 +++-
 .../com/netscape/cmstools/authority/AuthorityDisableCLI.java  |  4 +++-
 .../com/netscape/cmstools/authority/AuthorityEnableCLI.java   |  4 +++-
 .../src/com/netscape/cmstools/authority/AuthorityFindCLI.java |  4 +++-
 .../com/netscape/cmstools/authority/AuthorityRemoveCLI.java   |  4 +++-
 .../src/com/netscape/cmstools/authority/AuthorityShowCLI.java |  4 +++-
 7 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCLI.java
index a3fccbb027e4391b2fb83621ff829117a07fa76f..c8d469937e93a89f0901d1ca267ef1bc2b886988 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCLI.java
@@ -4,6 +4,7 @@ import java.math.BigInteger;
 
 import com.netscape.certsrv.authority.AuthorityClient;
 import com.netscape.certsrv.authority.AuthorityData;
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -32,10 +33,14 @@ public class AuthorityCLI extends CLI {
         }
     }
 
-    public void execute(String[] args) throws Exception {
-        client = parent.getClient();
+    public AuthorityClient getAuthorityClient() throws Exception {
+
+        if (authorityClient != null) return authorityClient;
+
+        PKIClient client = getClient();
         authorityClient = new AuthorityClient(client, "ca");
-        super.execute(args);
+
+        return authorityClient;
     }
 
     protected static void printAuthorityData(AuthorityData data) {
diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCreateCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCreateCLI.java
index 9cea963bfd09e025e2dbabafd72f4b04278322b9..e39a126e01d2b5ffccc5e9839d8c9b0aea4edf0f 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCreateCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityCreateCLI.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.authority.AuthorityClient;
 import com.netscape.certsrv.authority.AuthorityData;
 import com.netscape.certsrv.ca.AuthorityID;
 import com.netscape.cmstools.cli.CLI;
@@ -67,7 +68,8 @@ public class AuthorityCreateCLI extends CLI {
         String dn = cmdArgs[0];
         AuthorityData data = new AuthorityData(
             null, dn, null, parentAIDString, null, null, true /* enabled */, desc, null);
-        AuthorityData newData = authorityCLI.authorityClient.createCA(data);
+        AuthorityClient authorityClient = authorityCLI.getAuthorityClient();
+        AuthorityData newData = authorityClient.createCA(data);
         AuthorityCLI.printAuthorityData(newData);
     }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityDisableCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityDisableCLI.java
index 7c5da13de3357d853e5e376786a98a9b9d505600..bae8ada7355c44a813e40167c765a796eed348fb 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityDisableCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityDisableCLI.java
@@ -4,6 +4,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.authority.AuthorityClient;
 import com.netscape.certsrv.authority.AuthorityData;
 import com.netscape.cmstools.cli.CLI;
 
@@ -37,7 +38,8 @@ public class AuthorityDisableCLI extends CLI {
 
         AuthorityData data = new AuthorityData(
             null, null, cmdArgs[0], null, null, null, false, null, null);
-        data = authorityCLI.authorityClient.modifyCA(data);
+        AuthorityClient authorityClient = authorityCLI.getAuthorityClient();
+        data = authorityClient.modifyCA(data);
         AuthorityCLI.printAuthorityData(data);
     }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityEnableCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityEnableCLI.java
index 7ff25a45011e3355bb1c9632ab6f5457d46ed5c8..2b2eac1f03832b0087cc7ba9b49528db845b4209 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityEnableCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityEnableCLI.java
@@ -4,6 +4,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.authority.AuthorityClient;
 import com.netscape.certsrv.authority.AuthorityData;
 import com.netscape.cmstools.cli.CLI;
 
@@ -37,7 +38,8 @@ public class AuthorityEnableCLI extends CLI {
 
         AuthorityData data = new AuthorityData(
             null, null, cmdArgs[0], null, null, null, true, null, null);
-        data = authorityCLI.authorityClient.modifyCA(data);
+        AuthorityClient authorityClient = authorityCLI.getAuthorityClient();
+        data = authorityClient.modifyCA(data);
         AuthorityCLI.printAuthorityData(data);
     }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityFindCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityFindCLI.java
index 2b96e3aab2625a7a06b5f9e4cd70205ce3d2446a..6cd06be9f7a6beb582ba819cd3f454acca3bb487 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityFindCLI.java
@@ -5,6 +5,7 @@ import java.util.List;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.authority.AuthorityClient;
 import com.netscape.certsrv.authority.AuthorityData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -32,7 +33,8 @@ public class AuthorityFindCLI extends CLI {
         @SuppressWarnings("unused")
         CommandLine cmd = parser.parse(options, args);
 
-        List<AuthorityData> datas = authorityCLI.authorityClient.listCAs();
+        AuthorityClient authorityClient = authorityCLI.getAuthorityClient();
+        List<AuthorityData> datas = authorityClient.listCAs();
 
         MainCLI.printMessage(datas.size() + " entries matched");
         if (datas.size() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityRemoveCLI.java
index f69948aafd61275a0271463d2910446a731eb551..343a0bc57274750577ff88f0bfd7742731825628 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityRemoveCLI.java
@@ -6,6 +6,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.authority.AuthorityClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -54,7 +55,8 @@ public class AuthorityRemoveCLI extends CLI {
         }
 
         String aidString = cmdArgs[0];
-        authorityCLI.authorityClient.deleteCA(aidString);
+        AuthorityClient authorityClient = authorityCLI.getAuthorityClient();
+        authorityClient.deleteCA(aidString);
         MainCLI.printMessage("Deleted authority \"" + aidString + "\"");
     }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityShowCLI.java b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityShowCLI.java
index b26371070f394a76cdc6f7a5d074ab8c462d57fc..5bf06319d784882279e098a78e6d4d3ed4163312 100644
--- a/base/java-tools/src/com/netscape/cmstools/authority/AuthorityShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/authority/AuthorityShowCLI.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.authority.AuthorityClient;
 import com.netscape.certsrv.authority.AuthorityData;
 import com.netscape.certsrv.authority.AuthorityResource;
 import com.netscape.cmstools.cli.CLI;
@@ -57,7 +58,8 @@ public class AuthorityShowCLI extends CLI {
             throw new Exception("No ID specified.");
         }
 
-        AuthorityData data = authorityCLI.authorityClient.getCA(caIDString);
+        AuthorityClient authorityClient = authorityCLI.getAuthorityClient();
+        AuthorityData data = authorityClient.getCA(caIDString);
         AuthorityCLI.printAuthorityData(data);
     }
 
-- 
2.9.3

