>From cf8fe50d9822708411ca77c13c150f876835e790 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 24 Mar 2017 00:24:48 +0100
Subject: [PATCH] Refactored GroupCLI.

The CertCLI and its submodules have been modified to use lazy
initialization to get the PKIClient object.
---
 .../com/netscape/cmstools/group/GroupAddCLI.java   |  4 +++-
 .../src/com/netscape/cmstools/group/GroupCLI.java  | 23 +++++++++++-----------
 .../com/netscape/cmstools/group/GroupFindCLI.java  |  4 +++-
 .../netscape/cmstools/group/GroupMemberAddCLI.java |  4 +++-
 .../netscape/cmstools/group/GroupMemberCLI.java    | 12 +++++------
 .../cmstools/group/GroupMemberFindCLI.java         |  4 +++-
 .../cmstools/group/GroupMemberRemoveCLI.java       |  4 +++-
 .../cmstools/group/GroupMemberShowCLI.java         |  4 +++-
 .../netscape/cmstools/group/GroupModifyCLI.java    |  4 +++-
 .../netscape/cmstools/group/GroupRemoveCLI.java    |  4 +++-
 .../com/netscape/cmstools/group/GroupShowCLI.java  |  4 +++-
 11 files changed, 43 insertions(+), 28 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupAddCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupAddCLI.java
index 0ae506f3f1ee6f12e015bac58bfe871cd91e80ea..5a9da56684eb7b8ef9e8f92fae5672b8cafd6a7b 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupAddCLI.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.group.GroupClient;
 import com.netscape.certsrv.group.GroupData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -72,7 +73,8 @@ public class GroupAddCLI extends CLI {
         groupData.setGroupID(groupID);
         groupData.setDescription(cmd.getOptionValue("description"));
 
-        groupData = groupCLI.groupClient.addGroup(groupData);
+        GroupClient groupClient = groupCLI.getGroupClient();
+        groupData = groupClient.addGroup(groupData);
 
         MainCLI.printMessage("Added group \""+groupID+"\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupCLI.java
index ca151301ed4031aaf7d2e9e3580a44c71720a058..bd4651d9ee8b46382831bb71ee44446c6b35d1cb 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupCLI.java
@@ -21,6 +21,7 @@ package com.netscape.cmstools.group;
 import org.apache.commons.lang.StringUtils;
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.group.GroupClient;
 import com.netscape.certsrv.group.GroupData;
 import com.netscape.cmstools.cli.CLI;
@@ -59,22 +60,20 @@ public class GroupCLI extends CLI {
         return "pki-group";
     }
 
-    public void execute(String[] args) throws Exception {
+    public GroupClient getGroupClient() throws Exception {
 
-        client = parent.getClient();
-        groupClient = (GroupClient)parent.getClient("group");
+        if (groupClient != null) return groupClient;
 
-        // if this is a top-level command
-        if (groupClient == null) {
-            // determine the subsystem
-            String subsystem = client.getSubsystem();
-            if (subsystem == null) subsystem = "ca";
+        PKIClient client = getClient();
 
-            // create new group client
-            groupClient = new GroupClient(client, subsystem);
-        }
+        // determine the subsystem
+        String subsystem = client.getSubsystem();
+        if (subsystem == null) subsystem = "ca";
 
-        super.execute(args);
+        // create new group client
+        groupClient = new GroupClient(client, subsystem);
+
+        return groupClient;
     }
 
     public static void printGroup(GroupData groupData) {
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupFindCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupFindCLI.java
index dda3dacbc51fc3639eacaae3e0289a0b374a392d..1ff577f0b6b14eb9fd18a9af2e1726b8935a9792 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupFindCLI.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.group.GroupClient;
 import com.netscape.certsrv.group.GroupCollection;
 import com.netscape.certsrv.group.GroupData;
 import com.netscape.cmstools.cli.CLI;
@@ -75,7 +76,8 @@ public class GroupFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        GroupCollection response = groupCLI.groupClient.findGroups(filter, start, size);
+        GroupClient groupClient = groupCLI.getGroupClient();
+        GroupCollection response = groupClient.findGroups(filter, start, size);
 
         MainCLI.printMessage(response.getTotal() + " entries matched");
         if (response.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberAddCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberAddCLI.java
index cc00417733dbbdbb4330962c66c6509119182f4e..eab9e232ef8daa5017f3d7a48b670383d9ab2a7b 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberAddCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.group.GroupClient;
 import com.netscape.certsrv.group.GroupMemberData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -60,7 +61,8 @@ public class GroupMemberAddCLI extends CLI {
         String groupID = cmdArgs[0];
         String memberID = cmdArgs[1];
 
-        GroupMemberData groupMemberData = groupMemberCLI.groupClient.addGroupMember(groupID, memberID);
+        GroupClient groupClient = groupMemberCLI.getGroupClient();
+        GroupMemberData groupMemberData = groupClient.addGroupMember(groupID, memberID);
 
         MainCLI.printMessage("Added group member \""+memberID+"\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberCLI.java
index 1df404bc553dd4024d44e62822f6a580075f0cdf..527c37290e6957aad87d12cb797cc6e7c6f5028e 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberCLI.java
@@ -29,11 +29,13 @@ import com.netscape.cmstools.cli.CLI;
  */
 public class GroupMemberCLI extends CLI {
 
-    public GroupClient groupClient;
+    GroupCLI parent;
 
     public GroupMemberCLI(GroupCLI parent) {
         super("member", "Group member management commands", parent);
 
+        this.parent = parent;
+
         addModule(new GroupMemberFindCLI(this));
         addModule(new GroupMemberShowCLI(this));
         addModule(new GroupMemberAddCLI(this));
@@ -45,12 +47,8 @@ public class GroupMemberCLI extends CLI {
         return "pki-group-member";
     }
 
-    public void execute(String[] args) throws Exception {
-
-        client = parent.getClient();
-        groupClient = ((GroupCLI)parent).groupClient;
-
-        super.execute(args);
+    public GroupClient getGroupClient() throws Exception {
+        return parent.getGroupClient();
     }
 
     public static void printGroupMember(GroupMemberData groupMemberData) {
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberFindCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberFindCLI.java
index 788827abf83961b02896440070677e56f2078410..d4f3a4b2711257ddfef99b1e05f1c9455a31d121 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberFindCLI.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.group.GroupClient;
 import com.netscape.certsrv.group.GroupMemberCollection;
 import com.netscape.certsrv.group.GroupMemberData;
 import com.netscape.cmstools.cli.CLI;
@@ -81,7 +82,8 @@ public class GroupMemberFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        GroupMemberCollection response = groupMemberCLI.groupClient.findGroupMembers(groupID, filter, start, size);
+        GroupClient groupClient = groupMemberCLI.getGroupClient();
+        GroupMemberCollection response = groupClient.findGroupMembers(groupID, filter, start, size);
 
         MainCLI.printMessage(response.getTotal() + " entries matched");
         if (response.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberRemoveCLI.java
index 89cb668559620e4abed795363005729d2301f38a..b737d059c8bbbc19f05e1605972767283af9c122 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberRemoveCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.group.GroupClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -59,7 +60,8 @@ public class GroupMemberRemoveCLI extends CLI {
         String groupID = cmdArgs[0];
         String memberID = cmdArgs[1];
 
-        groupMemberCLI.groupClient.removeGroupMember(groupID, memberID);
+        GroupClient groupClient = groupMemberCLI.getGroupClient();
+        groupClient.removeGroupMember(groupID, memberID);
 
         MainCLI.printMessage("Deleted group member \""+memberID+"\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberShowCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberShowCLI.java
index a0e9f22310784c8b6be3d8ba5a36bee36764b82d..9579de3793c6883d150f116f8b51df16f65d0cbc 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupMemberShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupMemberShowCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.group.GroupClient;
 import com.netscape.certsrv.group.GroupMemberData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -60,7 +61,8 @@ public class GroupMemberShowCLI extends CLI {
         String groupID = cmdArgs[0];
         String memberID = cmdArgs[1];
 
-        GroupMemberData groupMemberData = groupMemberCLI.groupClient.getGroupMember(groupID, memberID);
+        GroupClient groupClient = groupMemberCLI.getGroupClient();
+        GroupMemberData groupMemberData = groupClient.getGroupMember(groupID, memberID);
 
         MainCLI.printMessage("Group member \""+memberID+"\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupModifyCLI.java
index f3dd2f951302d5a16a7601c311551e4d352ce794..d4d81c5797ceed32bc5a9cbc7424baf1689b29a7 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupModifyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupModifyCLI.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.group.GroupClient;
 import com.netscape.certsrv.group.GroupData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -72,7 +73,8 @@ public class GroupModifyCLI extends CLI {
         groupData.setID(groupID);
         groupData.setDescription(cmd.getOptionValue("description"));
 
-        groupData = groupCLI.groupClient.modifyGroup(groupID, groupData);
+        GroupClient groupClient = groupCLI.getGroupClient();
+        groupData = groupClient.modifyGroup(groupID, groupData);
 
         MainCLI.printMessage("Modified group \""+groupID+"\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupRemoveCLI.java
index 68a17483e87217b70d4d152f0f027fb7d9263af3..be6565491dbd8a0885ca5a5908e34eae0340df94 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupRemoveCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.group.GroupClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -59,7 +60,8 @@ public class GroupRemoveCLI extends CLI {
 
         String groupID = cmdArgs[0];
 
-        groupCLI.groupClient.removeGroup(groupID);
+        GroupClient groupClient = groupCLI.getGroupClient();
+        groupClient.removeGroup(groupID);
 
         MainCLI.printMessage("Deleted group \""+groupID+"\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/group/GroupShowCLI.java b/base/java-tools/src/com/netscape/cmstools/group/GroupShowCLI.java
index 39cc66b03b1d7ac2816893fb8d94ee268034982b..6f096af532515cbe37f69c8fd1226ca63dd13046 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupShowCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.group.GroupClient;
 import com.netscape.certsrv.group.GroupData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -59,7 +60,8 @@ public class GroupShowCLI extends CLI {
 
         String groupID = cmdArgs[0];
 
-        GroupData groupData = groupCLI.groupClient.getGroup(groupID);
+        GroupClient groupClient = groupCLI.getGroupClient();
+        GroupData groupData = groupClient.getGroup(groupID);
 
         MainCLI.printMessage("Group \""+groupID+"\"");
 
-- 
2.9.3

