>From 707486923941cffa307e6c8a404fe5248cb10d83 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 24 Mar 2017 04:11:32 +0100
Subject: [PATCH] Refactored CA ProfileMappingCLI.

The CA ProfileMappingCLI and its submodules have been modified to
use lazy initialization to get the PKIClient object.
---
 .../com/netscape/cmstools/tps/profile/ProfileMappingAddCLI.java  | 4 +++-
 .../src/com/netscape/cmstools/tps/profile/ProfileMappingCLI.java | 9 ++++++---
 .../com/netscape/cmstools/tps/profile/ProfileMappingFindCLI.java | 4 +++-
 .../netscape/cmstools/tps/profile/ProfileMappingModifyCLI.java   | 6 ++++--
 .../netscape/cmstools/tps/profile/ProfileMappingRemoveCLI.java   | 4 +++-
 .../com/netscape/cmstools/tps/profile/ProfileMappingShowCLI.java | 4 +++-
 6 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingAddCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingAddCLI.java
index 3fd67bdbc708057cc71f050e514a05025ee785b5..618aea2842617a2d25449de67aa9bc6306eeae03 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingAddCLI.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.ProfileMappingClient;
 import com.netscape.certsrv.tps.profile.ProfileMappingData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -87,7 +88,8 @@ public class ProfileMappingAddCLI extends CLI {
             profileMappingData = ProfileMappingData.valueOf(sw.toString());
         }
 
-        profileMappingData = profileMappingCLI.profileMappingClient.addProfileMapping(profileMappingData);
+        ProfileMappingClient profileMappingClient = profileMappingCLI.getProfileMappingClient();
+        profileMappingData = profileMappingClient.addProfileMapping(profileMappingData);
 
         MainCLI.printMessage("Added profile mapping \"" + profileMappingData.getID() + "\"");
 
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingCLI.java
index 9c2f7183c0df351ec095a8b3ecb60a4a7fd9a8ca..4c897613773eff977ceb05a7563e686a0d979eb3 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingCLI.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.ProfileMappingClient;
 import com.netscape.certsrv.tps.profile.ProfileMappingData;
 import com.netscape.cmstools.cli.CLI;
@@ -44,12 +45,14 @@ public class ProfileMappingCLI extends CLI {
         addModule(new ProfileMappingShowCLI(this));
     }
 
-    public void execute(String[] args) throws Exception {
+    public ProfileMappingClient getProfileMappingClient() throws Exception {
 
-        client = parent.getClient();
+        if (profileMappingClient != null) return profileMappingClient;
+
+        PKIClient client = getClient();
         profileMappingClient = (ProfileMappingClient)parent.getClient("profile-mapping");
 
-        super.execute(args);
+        return profileMappingClient;
     }
 
     public static void printProfileMappingData(ProfileMappingData profileMappingData, boolean showProperties) throws IOException {
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingFindCLI.java
index e28090064613532e3d4e2e7161abc4910754c469..be6ddf02df04f2cc6e295e830be51641c6db1ffe 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingFindCLI.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.ProfileMappingClient;
 import com.netscape.certsrv.tps.profile.ProfileMappingCollection;
 import com.netscape.certsrv.tps.profile.ProfileMappingData;
 import com.netscape.cmstools.cli.CLI;
@@ -75,7 +76,8 @@ public class ProfileMappingFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        ProfileMappingCollection result = profileMappingCLI.profileMappingClient.findProfileMappings(filter, start, size);
+        ProfileMappingClient profileMappingClient = profileMappingCLI.getProfileMappingClient();
+        ProfileMappingCollection result = profileMappingClient.findProfileMappings(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/ProfileMappingModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingModifyCLI.java
index 3296e2c3cac0c96c47a5f8f538cb33a5a8dd4696..8e03858f2f79c9629eb4e64f8ec8d0c05eea9a13 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingModifyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingModifyCLI.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.ProfileMappingClient;
 import com.netscape.certsrv.tps.profile.ProfileMappingData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -78,6 +79,7 @@ public class ProfileMappingModifyCLI extends CLI {
         String action = cmd.getOptionValue("action", "update");
         String input = cmd.getOptionValue("input");
 
+        ProfileMappingClient profileMappingClient = profileMappingCLI.getProfileMappingClient();
         ProfileMappingData profileMappingData;
 
         if (action.equals("update")) {
@@ -98,10 +100,10 @@ public class ProfileMappingModifyCLI extends CLI {
                 profileMappingData = ProfileMappingData.valueOf(sw.toString());
             }
 
-            profileMappingData = profileMappingCLI.profileMappingClient.updateProfileMapping(profileMappingID, profileMappingData);
+            profileMappingData = profileMappingClient.updateProfileMapping(profileMappingID, profileMappingData);
 
         } else { // other actions
-            profileMappingData = profileMappingCLI.profileMappingClient.changeProfileMappingStatus(profileMappingID, action);
+            profileMappingData = profileMappingClient.changeProfileMappingStatus(profileMappingID, action);
         }
 
         MainCLI.printMessage("Modified profile mapping \"" + profileMappingID + "\"");
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingRemoveCLI.java
index 1d1c9d84cb90de866cec9a90948985814dc9c63f..7208eb831ca1da99281cdfe7715b6b57850f3ce8 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingRemoveCLI.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.tps.profile.ProfileMappingClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -58,7 +59,8 @@ public class ProfileMappingRemoveCLI extends CLI {
 
         String profileMappingID = args[0];
 
-        profileMappingCLI.profileMappingClient.removeProfileMapping(profileMappingID);
+        ProfileMappingClient profileMappingClient = profileMappingCLI.getProfileMappingClient();
+        profileMappingClient.removeProfileMapping(profileMappingID);
 
         MainCLI.printMessage("Deleted profile mapping \"" + profileMappingID + "\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingShowCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingShowCLI.java
index 0aa6ec092c7598bafdcd43c52902bfeeff98fd41..3b4f64901c521ee88d88c0231cb55da744344f38 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/profile/ProfileMappingShowCLI.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.ProfileMappingClient;
 import com.netscape.certsrv.tps.profile.ProfileMappingData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -71,7 +72,8 @@ public class ProfileMappingShowCLI extends CLI {
         String profileMappingID = cmdArgs[0];
         String output = cmd.getOptionValue("output");
 
-        ProfileMappingData profileMappingData = profileMappingCLI.profileMappingClient.getProfileMapping(profileMappingID);
+        ProfileMappingClient profileMappingClient = profileMappingCLI.getProfileMappingClient();
+        ProfileMappingData profileMappingData = profileMappingClient.getProfileMapping(profileMappingID);
 
         if (output == null) {
             MainCLI.printMessage("ProfileMapping \"" + profileMappingID + "\"");
-- 
2.9.3

