>From 31655d244a1d07f8f9af4c3aa9abdc8824a30dba Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 24 Mar 2017 01:17:55 +0100
Subject: [PATCH] Refactored UserCLI.

The UserCLI and its submodules have been modified to use lazy
initialization to get the PKIClient object.
---
 .../src/com/netscape/cmstools/user/UserAddCLI.java |  4 +++-
 .../src/com/netscape/cmstools/user/UserCLI.java    | 23 +++++++++++-----------
 .../com/netscape/cmstools/user/UserCertAddCLI.java |  4 +++-
 .../com/netscape/cmstools/user/UserCertCLI.java    | 12 +++++------
 .../netscape/cmstools/user/UserCertFindCLI.java    |  4 +++-
 .../netscape/cmstools/user/UserCertRemoveCLI.java  |  4 +++-
 .../netscape/cmstools/user/UserCertShowCLI.java    |  4 +++-
 .../com/netscape/cmstools/user/UserFindCLI.java    |  4 +++-
 .../cmstools/user/UserMembershipAddCLI.java        |  4 +++-
 .../netscape/cmstools/user/UserMembershipCLI.java  | 12 +++++------
 .../cmstools/user/UserMembershipFindCLI.java       |  4 +++-
 .../cmstools/user/UserMembershipRemoveCLI.java     |  4 +++-
 .../com/netscape/cmstools/user/UserModifyCLI.java  |  4 +++-
 .../com/netscape/cmstools/user/UserRemoveCLI.java  |  4 +++-
 .../com/netscape/cmstools/user/UserShowCLI.java    |  4 +++-
 15 files changed, 57 insertions(+), 38 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserAddCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserAddCLI.java
index 4cccd7adf9aa679a8022c9312e65da928cb2644e..b244ab385d2607ed0a0a86d9bf5f0ea54a438c07 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserAddCLI.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.user.UserClient;
 import com.netscape.certsrv.user.UserData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -98,7 +99,8 @@ public class UserAddCLI extends CLI {
         userData.setType(cmd.getOptionValue("type"));
         userData.setState(cmd.getOptionValue("state"));
 
-        userData = userCLI.userClient.addUser(userData);
+        UserClient userClient = userCLI.getUserClient();
+        userData = userClient.addUser(userData);
 
         MainCLI.printMessage("Added user \"" + userID + "\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserCLI.java
index 5382c4711657d32d727236c2b96a813f80b89086..57a132c4670c27cda877ca2ed90d6037b3b1a3b5 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserCLI.java
@@ -21,6 +21,7 @@ package com.netscape.cmstools.user;
 import org.apache.commons.lang.StringUtils;
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.user.UserClient;
 import com.netscape.certsrv.user.UserData;
 import com.netscape.certsrv.user.UserResource;
@@ -62,22 +63,20 @@ public class UserCLI extends CLI {
         return "pki-user";
     }
 
-    public void execute(String[] args) throws Exception {
+    public UserClient getUserClient() throws Exception {
 
-        client = parent.getClient();
-        userClient = (UserClient)parent.getClient("user");
+        if (userClient != null) return userClient;
 
-        // if this is a top-level command
-        if (userClient == null) {
-            // determine the subsystem
-            String subsystem = client.getSubsystem();
-            if (subsystem == null) subsystem = "ca";
+        PKIClient client = getClient();
 
-            // create new user client
-            userClient = new UserClient(client, subsystem);
-        }
+        // determine the subsystem
+        String subsystem = client.getSubsystem();
+        if (subsystem == null) subsystem = "ca";
 
-        super.execute(args);
+        // create new user client
+        userClient = new UserClient(client, subsystem);
+
+        return userClient;
     }
 
     public static void printUser(UserData userData) {
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserCertAddCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserCertAddCLI.java
index 3b233cdb25df5499af96e63ec6d8b291bf574536..79f02937b205a6972ec497ad0987014caba707f8 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserCertAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserCertAddCLI.java
@@ -30,6 +30,7 @@ import com.netscape.certsrv.cert.CertClient;
 import com.netscape.certsrv.cert.CertData;
 import com.netscape.certsrv.dbs.certdb.CertId;
 import com.netscape.certsrv.user.UserCertData;
+import com.netscape.certsrv.user.UserClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -117,7 +118,8 @@ public class UserCertAddCLI extends CLI {
             System.out.println(userCertData);
         }
 
-        userCertData = userCertCLI.userClient.addUserCert(userID, userCertData);
+        UserClient userClient = userCertCLI.getUserClient();
+        userCertData = userClient.addUserCert(userID, userCertData);
 
         MainCLI.printMessage("Added certificate \"" + userCertData.getID() + "\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserCertCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserCertCLI.java
index d8ea917202711ef1aa5f5430590db34d241f6df1..e6d81e8e70da021f116ac411326fedc33a73c121 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserCertCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserCertCLI.java
@@ -29,11 +29,13 @@ import com.netscape.cmstools.cli.CLI;
  */
 public class UserCertCLI extends CLI {
 
-    public UserClient userClient;
+    UserCLI parent;
 
     public UserCertCLI(UserCLI parent) {
         super("cert", "User certificate management commands", parent);
 
+        this.parent = parent;
+
         addModule(new UserCertFindCLI(this));
         addModule(new UserCertShowCLI(this));
         addModule(new UserCertAddCLI(this));
@@ -45,12 +47,8 @@ public class UserCertCLI extends CLI {
         return "pki-user-cert";
     }
 
-    public void execute(String[] args) throws Exception {
-
-        client = parent.getClient();
-        userClient = ((UserCLI)parent).userClient;
-
-        super.execute(args);
+    public UserClient getUserClient() throws Exception {
+        return parent.getUserClient();
     }
 
     public static void printCert(
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserCertFindCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserCertFindCLI.java
index 4f6980fd83a19b3b9b05e13b7f498dab074c30cc..bbcb00f8f1a2e86eba42c43c96d5e93b84b4bcd9 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserCertFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserCertFindCLI.java
@@ -26,6 +26,7 @@ import org.apache.commons.cli.Option;
 
 import com.netscape.certsrv.user.UserCertCollection;
 import com.netscape.certsrv.user.UserCertData;
+import com.netscape.certsrv.user.UserClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -80,7 +81,8 @@ public class UserCertFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        UserCertCollection response = userCertCLI.userClient.findUserCerts(userID, start, size);
+        UserClient userClient = userCertCLI.getUserClient();
+        UserCertCollection response = userClient.findUserCerts(userID, start, size);
 
         MainCLI.printMessage(response.getTotal() + " entries matched");
         if (response.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserCertRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserCertRemoveCLI.java
index e1f15cac525be03e14c92c2446fb4c365c02439e..935092a70246a96591b800a89f604428de23bd86 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserCertRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserCertRemoveCLI.java
@@ -23,6 +23,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.user.UserClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -65,7 +66,8 @@ public class UserCertRemoveCLI extends CLI {
             System.out.println("Removing cert "+certID+" from user "+userID+".");
         }
 
-        userCertCLI.userClient.removeUserCert(userID, URLEncoder.encode(certID, "UTF-8"));
+        UserClient userClient = userCertCLI.getUserClient();
+        userClient.removeUserCert(userID, URLEncoder.encode(certID, "UTF-8"));
 
         MainCLI.printMessage("Deleted certificate \"" + certID + "\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserCertShowCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserCertShowCLI.java
index 1267dbc80ebfc84d7d5d56a2b50f5bc477c56b72..8c1f53ddd791d2ef9be3426a9293a5ab1c690dc8 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserCertShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserCertShowCLI.java
@@ -27,6 +27,7 @@ import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
 import com.netscape.certsrv.user.UserCertData;
+import com.netscape.certsrv.user.UserClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -79,7 +80,8 @@ public class UserCertShowCLI extends CLI {
         String certID = cmdArgs[1];
         String file = cmd.getOptionValue("output");
 
-        UserCertData userCertData = userCertCLI.userClient.getUserCert(userID, URLEncoder.encode(certID, "UTF-8"));
+        UserClient userClient = userCertCLI.getUserClient();
+        UserCertData userCertData = userClient.getUserCert(userID, URLEncoder.encode(certID, "UTF-8"));
 
         String encoded = userCertData.getEncoded();
         if (encoded != null && file != null) {
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserFindCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserFindCLI.java
index 8f9b8ba7f96e540453754271cb2c9dd510e25fa6..283779dbcd7f13d3e2375851405e9fa932159cb6 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserFindCLI.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.user.UserClient;
 import com.netscape.certsrv.user.UserCollection;
 import com.netscape.certsrv.user.UserData;
 import com.netscape.cmstools.cli.CLI;
@@ -75,7 +76,8 @@ public class UserFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        UserCollection response = userCLI.userClient.findUsers(filter, start, size);
+        UserClient userClient = userCLI.getUserClient();
+        UserCollection response = userClient.findUsers(filter, start, size);
 
         MainCLI.printMessage(response.getTotal() + " entries matched");
         if (response.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserMembershipAddCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserMembershipAddCLI.java
index 966e8ed405d58068b61054f2e244060b85b2fe91..4cb0848abebfa39c11b8220f7a95d85c04081c2e 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserMembershipAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserMembershipAddCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.user.UserClient;
 import com.netscape.certsrv.user.UserMembershipData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -60,7 +61,8 @@ public class UserMembershipAddCLI extends CLI {
         String userID = args[0];
         String groupID = args[1];
 
-        UserMembershipData userMembershipData = userMembershipCLI.userClient.addUserMembership(userID, groupID);
+        UserClient userClient = userMembershipCLI.getUserClient();
+        UserMembershipData userMembershipData = userClient.addUserMembership(userID, groupID);
 
         MainCLI.printMessage("Added membership in \""+groupID+"\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserMembershipCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserMembershipCLI.java
index fb418c07504fe2e021e69815b71fb20d3fc9344e..4267421ad5af5151ae3210a14a7b4afcb88e149d 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserMembershipCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserMembershipCLI.java
@@ -29,22 +29,20 @@ import com.netscape.cmstools.cli.CLI;
  */
 public class UserMembershipCLI extends CLI {
 
-    public UserClient userClient;
+    UserCLI parent;
 
     public UserMembershipCLI(UserCLI parent) {
         super("membership", "User membership management commands", parent);
 
+        this.parent = parent;
+
         addModule(new UserMembershipFindCLI(this));
         addModule(new UserMembershipAddCLI(this));
         addModule(new UserMembershipRemoveCLI(this));
     }
 
-    public void execute(String[] args) throws Exception {
-
-        client = parent.getClient();
-        userClient = ((UserCLI)parent).userClient;
-
-        super.execute(args);
+    public UserClient getUserClient() throws Exception {
+        return parent.getUserClient();
     }
 
     public static void printUserMembership(UserMembershipData userMembershipData) {
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserMembershipFindCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserMembershipFindCLI.java
index f51fc49b50528619469e5ddbd3c8f2ffc1435a38..7a19429aaa27f8aad0b3bf7f2c2f0a36e855f601 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserMembershipFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserMembershipFindCLI.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.user.UserClient;
 import com.netscape.certsrv.user.UserMembershipCollection;
 import com.netscape.certsrv.user.UserMembershipData;
 import com.netscape.cmstools.cli.CLI;
@@ -81,7 +82,8 @@ public class UserMembershipFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        UserMembershipCollection response = userMembershipCLI.userClient.findUserMemberships(userID, filter, start, size);
+        UserClient userClient = userMembershipCLI.getUserClient();
+        UserMembershipCollection response = userClient.findUserMemberships(userID, filter, start, size);
 
         MainCLI.printMessage(response.getTotal() + " entries matched");
         if (response.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserMembershipRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserMembershipRemoveCLI.java
index 1573cc8abe4ac7931cf8decf9f22b56ec908c57a..6fe71e2667990cce8e4e09f442fccf1da94d505e 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserMembershipRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserMembershipRemoveCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.user.UserClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -59,7 +60,8 @@ public class UserMembershipRemoveCLI extends CLI {
         String userID = args[0];
         String groupID = args[1];
 
-        userMembershipCLI.userClient.removeUserMembership(userID, groupID);
+        UserClient userClient = userMembershipCLI.getUserClient();
+        userClient.removeUserMembership(userID, groupID);
 
         MainCLI.printMessage("Deleted membership in group \""+groupID+"\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserModifyCLI.java
index e632672d79ed1ba5deb7d22fd9fc5bf8345ae7f9..5ef3398e1c8025d9ff91e99ae76d08a0d434f8ae 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserModifyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserModifyCLI.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.user.UserClient;
 import com.netscape.certsrv.user.UserData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -99,7 +100,8 @@ public class UserModifyCLI extends CLI {
         // userData.setType(cmd.getOptionValue("type"));
         userData.setState(cmd.getOptionValue("state"));
 
-        userData = userCLI.userClient.modifyUser(userId, userData);
+        UserClient userClient = userCLI.getUserClient();
+        userData = userClient.modifyUser(userId, userData);
 
         MainCLI.printMessage("Modified user \"" + userId + "\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserRemoveCLI.java
index 4ad8bf1b3ddf99955cc803b877e7d3f92cd2ead0..dbc9d630d3bb1b34e5f66a609318bdafc214722a 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserRemoveCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.user.UserClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -58,7 +59,8 @@ public class UserRemoveCLI extends CLI {
 
         String userID = args[0];
 
-        userCLI.userClient.removeUser(userID);
+        UserClient userClient = userCLI.getUserClient();
+        userClient.removeUser(userID);
 
         MainCLI.printMessage("Deleted user \"" + userID + "\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/user/UserShowCLI.java b/base/java-tools/src/com/netscape/cmstools/user/UserShowCLI.java
index e46013940db068c08e7683156410eba3ea091edf..244187abfa4bb38120c985add858c44f6ffbd7a9 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserShowCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.user.UserClient;
 import com.netscape.certsrv.user.UserData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -59,7 +60,8 @@ public class UserShowCLI extends CLI {
 
         String userId = args[0];
 
-        UserData userData = userCLI.userClient.getUser(userId);
+        UserClient userClient = userCLI.getUserClient();
+        UserData userData = userClient.getUser(userId);
 
         MainCLI.printMessage("User \"" + userId + "\"");
 
-- 
2.9.3

