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

The TPS ProfileCLI and its submodules have been modified to use
lazy initialization to get the PKIClient object.
---
 .../src/com/netscape/cmstools/tps/profile/ProfileAddCLI.java     | 4 +++-
 .../src/com/netscape/cmstools/tps/profile/ProfileCLI.java        | 9 ++++++---
 .../src/com/netscape/cmstools/tps/profile/ProfileFindCLI.java    | 4 +++-
 .../src/com/netscape/cmstools/tps/profile/ProfileModifyCLI.java  | 6 ++++--
 .../src/com/netscape/cmstools/tps/profile/ProfileRemoveCLI.java  | 4 +++-
 .../src/com/netscape/cmstools/tps/profile/ProfileShowCLI.java    | 4 +++-
 6 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileAddCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileAddCLI.java
index 1f5cec65667bd197364ebf0c4331652517642de5..48c3552d52365258452b8220916e73c0f8bf3f8d 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileAddCLI.java
@@ -27,6 +27,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.tps.profile.ProfileClient;
 import com.netscape.certsrv.tps.profile.ProfileData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -87,7 +88,8 @@ public class ProfileAddCLI extends CLI {
             profileData = ProfileData.valueOf(sw.toString());
         }
 
-        profileData = profileCLI.profileClient.addProfile(profileData);
+        ProfileClient profileClient = profileCLI.getProfileClient();
+        profileData = profileClient.addProfile(profileData);
 
         MainCLI.printMessage("Added profile \"" + profileData.getID() + "\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileCLI.java
index 7c68c8ebb220ae1fff629f0bc1f6237b4fef8a6e..a323c4e9b4bf4b283c110f969905487d7ff1a0b5 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileCLI.java
@@ -23,6 +23,7 @@ import java.util.Map;
 
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.tps.profile.ProfileClient;
 import com.netscape.certsrv.tps.profile.ProfileData;
 import com.netscape.cmstools.cli.CLI;
@@ -51,12 +52,14 @@ public class ProfileCLI extends CLI {
         return "pki-tps-profile";
     }
 
-    public void execute(String[] args) throws Exception {
+    public ProfileClient getProfileClient() throws Exception {
 
-        client = parent.getClient();
+        if (profileClient != null) return profileClient;
+
+        PKIClient client = getClient();
         profileClient = (ProfileClient)parent.getClient("profile");
 
-        super.execute(args);
+        return profileClient;
     }
 
     public static void printProfileData(ProfileData profileData, boolean showProperties) throws IOException {
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileFindCLI.java
index d07c542252409d6a79289e9dd20624f65385373f..f2670f078131a3e6e33f35fa007f5abb2f58e93a 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileFindCLI.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.profile.ProfileClient;
 import com.netscape.certsrv.tps.profile.ProfileCollection;
 import com.netscape.certsrv.tps.profile.ProfileData;
 import com.netscape.cmstools.cli.CLI;
@@ -75,7 +76,8 @@ public class ProfileFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        ProfileCollection result = profileCLI.profileClient.findProfiles(filter, start, size);
+        ProfileClient profileClient = profileCLI.getProfileClient();
+        ProfileCollection result = profileClient.findProfiles(filter, start, size);
 
         MainCLI.printMessage(result.getTotal() + " entries matched");
         if (result.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileModifyCLI.java
index e51740cf783997d9f9e9af2a61f1957e3b9265f5..4c218a287f17141e8354da8ea0c503ff511e47dd 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileModifyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileModifyCLI.java
@@ -27,6 +27,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.tps.profile.ProfileClient;
 import com.netscape.certsrv.tps.profile.ProfileData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -78,6 +79,7 @@ public class ProfileModifyCLI extends CLI {
         String action = cmd.getOptionValue("action", "update");
         String input = cmd.getOptionValue("input");
 
+        ProfileClient profileClient = profileCLI.getProfileClient();
         ProfileData profileData;
 
         if (action.equals("update")) {
@@ -98,10 +100,10 @@ public class ProfileModifyCLI extends CLI {
                 profileData = ProfileData.valueOf(sw.toString());
             }
 
-            profileData = profileCLI.profileClient.updateProfile(profileID, profileData);
+            profileData = profileClient.updateProfile(profileID, profileData);
 
         } else { // other actions
-            profileData = profileCLI.profileClient.changeProfileStatus(profileID, action);
+            profileData = profileClient.changeProfileStatus(profileID, action);
         }
 
         MainCLI.printMessage("Modified profile \"" + profileID + "\"");
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileRemoveCLI.java
index e77f72ac04bae4f9d7f9d0112de82e5cec1cf23c..2f18e1f660711113b9fa07a7c50fe22dfc82a2a4 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileRemoveCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.tps.profile.ProfileClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -58,7 +59,8 @@ public class ProfileRemoveCLI extends CLI {
 
         String profileID = args[0];
 
-        profileCLI.profileClient.removeProfile(profileID);
+        ProfileClient profileClient = profileCLI.getProfileClient();
+        profileClient.removeProfile(profileID);
 
         MainCLI.printMessage("Deleted profile \"" + profileID + "\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileShowCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileShowCLI.java
index b273f5fd5461efc0b47ab55b6e3e50e1468b6acb..cd4d5b8ed566c38219e7fd74d2ed3cc97e998104 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileShowCLI.java
@@ -25,6 +25,7 @@ import java.util.Arrays;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.tps.profile.ProfileClient;
 import com.netscape.certsrv.tps.profile.ProfileData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -71,7 +72,8 @@ public class ProfileShowCLI extends CLI {
         String profileID = args[0];
         String output = cmd.getOptionValue("output");
 
-        ProfileData profileData = profileCLI.profileClient.getProfile(profileID);
+        ProfileClient profileClient = profileCLI.getProfileClient();
+        ProfileData profileData = profileClient.getProfile(profileID);
 
         if (output == null) {
             MainCLI.printMessage("Profile \"" + profileID + "\"");
-- 
2.9.3

