>From 61cfd9cd7736ccf71c001445f5f9f32e62af3613 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 24 Mar 2017 01:52:54 +0100
Subject: [PATCH] Refactored SubsystemCLI.

The SubsystemCLI and its subclasses have been modified to use
lazy initialization to get the PKIClient object. They also have
been simplified by moving common methods to the base class.
---
 .../src/com/netscape/cmstools/cli/CACLI.java       | 22 ++++++++--------------
 .../src/com/netscape/cmstools/cli/KRACLI.java      | 22 ++++++++--------------
 .../src/com/netscape/cmstools/cli/OCSPCLI.java     | 22 ++++++++--------------
 .../com/netscape/cmstools/cli/SubsystemCLI.java    | 22 +++++++++++++++++-----
 .../src/com/netscape/cmstools/cli/TKSCLI.java      | 22 ++++++++--------------
 .../src/com/netscape/cmstools/cli/TPSCLI.java      | 22 ++++++++--------------
 6 files changed, 57 insertions(+), 75 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/cli/CACLI.java b/base/java-tools/src/com/netscape/cmstools/cli/CACLI.java
index 39dd2a2ce723daaf336ba4f3ddc2e2be4afbf407..2ec20dcb02467af7255dc3f181f9c9de9fd30284 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/CACLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/CACLI.java
@@ -19,7 +19,8 @@
 package com.netscape.cmstools.cli;
 
 import com.netscape.certsrv.ca.CAClient;
-import com.netscape.certsrv.client.Client;
+import com.netscape.certsrv.client.PKIClient;
+import com.netscape.certsrv.client.SubsystemClient;
 import com.netscape.cmstools.authority.AuthorityCLI;
 import com.netscape.cmstools.cert.CertCLI;
 import com.netscape.cmstools.feature.FeatureCLI;
@@ -49,20 +50,13 @@ public class CACLI extends SubsystemCLI {
         addModule(new UserCLI(this));
     }
 
-    public void init() throws Exception {
-        client = parent.getClient();
+    public SubsystemClient getSubsystemClient() throws Exception {
+
+        if (caClient != null) return caClient;
+
+        PKIClient client = getClient();
         caClient = new CAClient(client);
-    }
 
-    public void login() {
-        caClient.login();
-    }
-
-    public void logout() {
-        caClient.logout();
-    }
-
-    public Client getClient(String name) {
-        return caClient.getClient(name);
+        return caClient;
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/KRACLI.java b/base/java-tools/src/com/netscape/cmstools/cli/KRACLI.java
index 8a9f789cd1099a9613615f943adad4fd78d2a666..2db85aafe08b516516e9d52cbf5e22a88a5585e5 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/KRACLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/KRACLI.java
@@ -18,7 +18,8 @@
 
 package com.netscape.cmstools.cli;
 
-import com.netscape.certsrv.client.Client;
+import com.netscape.certsrv.client.PKIClient;
+import com.netscape.certsrv.client.SubsystemClient;
 import com.netscape.certsrv.kra.KRAClient;
 import com.netscape.cmstools.group.GroupCLI;
 import com.netscape.cmstools.key.KeyCLI;
@@ -41,20 +42,13 @@ public class KRACLI extends SubsystemCLI {
         addModule(new UserCLI(this));
     }
 
-    public void init() throws Exception {
-        client = parent.getClient();
+    public SubsystemClient getSubsystemClient() throws Exception {
+
+        if (kraClient != null) return kraClient;
+
+        PKIClient client = getClient();
         kraClient = new KRAClient(client);
-    }
 
-    public void login() {
-        kraClient.login();
-    }
-
-    public void logout() {
-        kraClient.logout();
-    }
-
-    public Client getClient(String name) {
-        return kraClient.getClient(name);
+        return kraClient;
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/OCSPCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/OCSPCLI.java
index 3768a48e745e4632d22a755751e137ec40ec6299..6348359e22c736943fbea4844110a9ac0941fa3c 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/OCSPCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/OCSPCLI.java
@@ -18,7 +18,8 @@
 
 package com.netscape.cmstools.cli;
 
-import com.netscape.certsrv.client.Client;
+import com.netscape.certsrv.client.PKIClient;
+import com.netscape.certsrv.client.SubsystemClient;
 import com.netscape.certsrv.ocsp.OCSPClient;
 import com.netscape.cmstools.group.GroupCLI;
 import com.netscape.cmstools.selftests.SelfTestCLI;
@@ -39,20 +40,13 @@ public class OCSPCLI extends SubsystemCLI {
         addModule(new UserCLI(this));
     }
 
-    public void init() throws Exception {
-        client = parent.getClient();
+    public SubsystemClient getSubsystemClient() throws Exception {
+
+        if (ocspClient != null) return ocspClient;
+
+        PKIClient client = getClient();
         ocspClient = new OCSPClient(client);
-    }
 
-    public void login() {
-        ocspClient.login();
-    }
-
-    public void logout() {
-        ocspClient.logout();
-    }
-
-    public Client getClient(String name) {
-        return ocspClient.getClient(name);
+        return ocspClient;
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/SubsystemCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/SubsystemCLI.java
index b28271dd74371ae75434ffb7f43919b210caa3ac..0f9a497810fc3f947a50fbcf92ff0b3f4f11f6eb 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/SubsystemCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/SubsystemCLI.java
@@ -18,7 +18,9 @@
 
 package com.netscape.cmstools.cli;
 
+import com.netscape.certsrv.client.Client;
 import com.netscape.certsrv.client.ClientConfig;
+import com.netscape.certsrv.client.SubsystemClient;
 
 
 /**
@@ -35,21 +37,29 @@ public class SubsystemCLI extends CLI {
         return name;
     }
 
-    public void init() throws Exception {
+    public SubsystemClient getSubsystemClient() throws Exception {
+        return null;
     }
 
     public void login() throws Exception {
+        SubsystemClient subsystemClient = getSubsystemClient();
+        subsystemClient.login();
     }
 
     public void logout() throws Exception {
+        SubsystemClient subsystemClient = getSubsystemClient();
+        subsystemClient.logout();
+    }
+
+    public Client getClient(String name) throws Exception {
+        SubsystemClient subsystemClient = getSubsystemClient();
+        return subsystemClient.getClient(name);
     }
 
     public void execute(String[] args) throws Exception {
 
-        init();
-
         // login if username or nickname is specified
-        ClientConfig config = getClient().getConfig();
+        ClientConfig config = getConfig();
         if (config.getUsername() != null || config.getCertNickname() != null) {
             login();
         }
@@ -57,6 +67,8 @@ public class SubsystemCLI extends CLI {
         super.execute(args);
 
         // logout if there is no failures
-        logout();
+        if (config.getUsername() != null || config.getCertNickname() != null) {
+            logout();
+        }
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/TKSCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/TKSCLI.java
index dd4f179c262036b286d3f44526f9a4a8fac2211e..1afdf649ff7fcd91cff7ab1dd454107ac1947cd6 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/TKSCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/TKSCLI.java
@@ -18,7 +18,8 @@
 
 package com.netscape.cmstools.cli;
 
-import com.netscape.certsrv.client.Client;
+import com.netscape.certsrv.client.PKIClient;
+import com.netscape.certsrv.client.SubsystemClient;
 import com.netscape.certsrv.tks.TKSClient;
 import com.netscape.cmstools.group.GroupCLI;
 import com.netscape.cmstools.selftests.SelfTestCLI;
@@ -41,20 +42,13 @@ public class TKSCLI extends SubsystemCLI {
         addModule(new UserCLI(this));
     }
 
-    public void init() throws Exception {
-        client = parent.getClient();
+    public SubsystemClient getSubsystemClient() throws Exception {
+
+        if (tksClient != null) return tksClient;
+
+        PKIClient client = getClient();
         tksClient = new TKSClient(client);
-    }
 
-    public void login() {
-        tksClient.login();
-    }
-
-    public void logout() {
-        tksClient.logout();
-    }
-
-    public Client getClient(String name) {
-        return tksClient.getClient(name);
+        return tksClient;
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
index 1931b2baa56130122be1ef7d1fd2efa1d54f3b01..95836a45be393543dd4affa7cbd307676f558f07 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
@@ -18,7 +18,8 @@
 
 package com.netscape.cmstools.cli;
 
-import com.netscape.certsrv.client.Client;
+import com.netscape.certsrv.client.PKIClient;
+import com.netscape.certsrv.client.SubsystemClient;
 import com.netscape.certsrv.tps.TPSClient;
 import com.netscape.cmstools.group.GroupCLI;
 import com.netscape.cmstools.logging.ActivityCLI;
@@ -64,20 +65,13 @@ public class TPSCLI extends SubsystemCLI {
         }
     }
 
-    public void init() throws Exception {
-        client = parent.getClient();
+    public SubsystemClient getSubsystemClient() throws Exception {
+
+        if (tpsClient != null) return tpsClient;
+
+        PKIClient client = getClient();
         tpsClient = new TPSClient(client);
-    }
 
-    public void login() {
-        tpsClient.login();
-    }
-
-    public void logout() {
-        tpsClient.logout();
-    }
-
-    public Client getClient(String name) {
-        return tpsClient.getClient(name);
+        return tpsClient;
     }
 }
-- 
2.9.3

