>From 87431b72520f7c2426312faaefa0bf83af8cdfcd Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Fri, 24 Mar 2017 02:24:38 +0100
Subject: [PATCH] Refactored CA ProfileCLI.

The CA ProfileCLI and its submodules have been modified to use
lazy initialization to get the PKIClient object.
---
 .../src/com/netscape/cmstools/profile/ProfileAddCLI.java         | 9 ++++++---
 .../java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java | 9 ++++++---
 .../src/com/netscape/cmstools/profile/ProfileDisableCLI.java     | 4 +++-
 .../src/com/netscape/cmstools/profile/ProfileEditCLI.java        | 7 +++++--
 .../src/com/netscape/cmstools/profile/ProfileEnableCLI.java      | 4 +++-
 .../src/com/netscape/cmstools/profile/ProfileFindCLI.java        | 4 +++-
 .../src/com/netscape/cmstools/profile/ProfileModifyCLI.java      | 9 ++++++---
 .../src/com/netscape/cmstools/profile/ProfileRemoveCLI.java      | 4 +++-
 .../src/com/netscape/cmstools/profile/ProfileShowCLI.java        | 9 ++++++---
 9 files changed, 41 insertions(+), 18 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java
index ced286e5fe1d0b9462e82c86b54d955bfa8ad5b5..d7fed3fe2014b2af5cafb766d11feb2aa8a367e0 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileAddCLI.java
@@ -6,6 +6,7 @@ import java.util.Properties;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.profile.ProfileClient;
 import com.netscape.certsrv.profile.ProfileData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -47,18 +48,20 @@ public class ProfileAddCLI extends CLI {
             throw new Exception("Missing input file name.");
         }
 
+        ProfileClient profileClient = profileCLI.getProfileClient();
+
         if (cmd.hasOption("raw")) {
             Properties properties = ProfileCLI.readRawProfileFromFile(filename);
             String profileId = properties.getProperty("profileId");
-            profileCLI.profileClient.createProfileRaw(properties).store(System.out, null);
+            profileClient.createProfileRaw(properties).store(System.out, null);
             MainCLI.printMessage("Added profile " + profileId);
         } else {
             ProfileData data = ProfileCLI.readProfileFromFile(filename);
-            data = profileCLI.profileClient.createProfile(data);
+            data = profileClient.createProfile(data);
 
             MainCLI.printMessage("Added profile " + data.getId());
 
-            ProfileCLI.printProfile(data, profileCLI.getClient().getConfig().getServerURI());
+            ProfileCLI.printProfile(data, profileCLI.getConfig().getServerURI());
         }
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
index e4acdb1956f655c3faa8e18ae14e69c15353ba09..23ca89d687079fd4319f13590bf5693bbb720565 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
@@ -15,6 +15,7 @@ import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
 
 import com.netscape.certsrv.cert.CertEnrollmentRequest;
+import com.netscape.certsrv.client.PKIClient;
 import com.netscape.certsrv.profile.ProfileAttribute;
 import com.netscape.certsrv.profile.ProfileClient;
 import com.netscape.certsrv.profile.ProfileData;
@@ -55,9 +56,11 @@ public class ProfileCLI extends CLI {
         return "pki-ca-profile";
     }
 
-    public void execute(String[] args) throws Exception {
+    public ProfileClient getProfileClient() throws Exception {
 
-        client = parent.getClient();
+        if (profileClient != null) return profileClient;
+
+        PKIClient client = getClient();
 
         // determine the subsystem
         String subsystem = client.getSubsystem();
@@ -66,7 +69,7 @@ public class ProfileCLI extends CLI {
         // create new profile client
         profileClient = new ProfileClient(client, subsystem);
 
-        super.execute(args);
+        return profileClient;
     }
 
     public static void printProfileDataInfo(ProfileDataInfo info) {
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileDisableCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileDisableCLI.java
index dc21ac4a8b0eb086415c7f7486732b769171fc21..cb877be13acba4998dad54e6bec60dfbb3f98263 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileDisableCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileDisableCLI.java
@@ -4,6 +4,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.profile.ProfileClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -37,7 +38,8 @@ public class ProfileDisableCLI extends CLI {
 
         String profileId = args[0];
 
-        profileCLI.profileClient.disableProfile(profileId);
+        ProfileClient profileClient = profileCLI.getProfileClient();
+        profileClient.disableProfile(profileId);
 
         MainCLI.printMessage("Disabled profile \"" + profileId + "\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileEditCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileEditCLI.java
index b8bb9f03f082d186c231f550e685150dc523b206..18bb7e566c7be0bf147e9cc51369f101fb5c84e3 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileEditCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileEditCLI.java
@@ -25,6 +25,7 @@ import java.util.Properties;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.profile.ProfileClient;
 import com.netscape.cmstools.cli.CLI;
 
 public class ProfileEditCLI extends CLI {
@@ -57,8 +58,10 @@ public class ProfileEditCLI extends CLI {
 
         String profileId = cmdArgs[0];
 
+        ProfileClient profileClient = profileCLI.getProfileClient();
+
         // read profile into temporary file
-        Properties orig = profileCLI.profileClient.retrieveProfileRaw(profileId);
+        Properties orig = profileClient.retrieveProfileRaw(profileId);
         String enabled = orig.getProperty("enable");
         if (Boolean.valueOf(enabled)) {
             throw new Exception("Cannot edit profile. Profile must be disabled.");
@@ -88,7 +91,7 @@ public class ProfileEditCLI extends CLI {
             cur.load(Files.newInputStream(tempFile));
 
             if (!cur.equals(orig)) {
-                profileCLI.profileClient.modifyProfileRaw(profileId, cur);
+                profileClient.modifyProfileRaw(profileId, cur);
             }
             cur.store(System.out, null);
         } finally {
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileEnableCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileEnableCLI.java
index f8c56ba6483a3471f7de116947950cba87a09cd8..34c6021085cf5ebd68ac03ea15a804604bd0a9d0 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileEnableCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileEnableCLI.java
@@ -4,6 +4,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.profile.ProfileClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -37,7 +38,8 @@ public class ProfileEnableCLI extends CLI {
 
         String profileId = cmdArgs[0];
 
-        profileCLI.profileClient.enableProfile(profileId);
+        ProfileClient profileClient = profileCLI.getProfileClient();
+        profileClient.enableProfile(profileId);
 
         MainCLI.printMessage("Enabled profile \"" + profileId + "\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileFindCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileFindCLI.java
index 512c7cd3053a3ac597ca1a3db7c7b9d5a8d86e99..9eb74687ed28896d7923233b6cc89aac8a17648b 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileFindCLI.java
@@ -6,6 +6,7 @@ import java.util.Collection;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.profile.ProfileClient;
 import com.netscape.certsrv.profile.ProfileDataInfo;
 import com.netscape.certsrv.profile.ProfileDataInfos;
 import com.netscape.cmstools.cli.CLI;
@@ -57,7 +58,8 @@ public class ProfileFindCLI extends CLI {
         s = cmd.getOptionValue("size");
         Integer size = s == null ? null : Integer.valueOf(s);
 
-        ProfileDataInfos response = profileCLI.profileClient.listProfiles(start, size);
+        ProfileClient profileClient = profileCLI.getProfileClient();
+        ProfileDataInfos response = profileClient.listProfiles(start, size);
 
         MainCLI.printMessage(response.getTotal() + " entries matched");
         if (response.getTotal() == 0) return;
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java
index 835f628c15efa9b034209a3338b22bafa6a7ef1a..c478b50caa52c4d724b7fd060e61443879338853 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileModifyCLI.java
@@ -6,6 +6,7 @@ import java.util.Properties;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.profile.ProfileClient;
 import com.netscape.certsrv.profile.ProfileData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -47,18 +48,20 @@ public class ProfileModifyCLI extends CLI {
             throw new Exception("Missing input file name.");
         }
 
+        ProfileClient profileClient = profileCLI.getProfileClient();
+
         if (cmd.hasOption("raw")) {
             Properties properties = ProfileCLI.readRawProfileFromFile(filename);
             String profileId = properties.getProperty("profileId");
-            profileCLI.profileClient.modifyProfileRaw(profileId, properties).store(System.out, null);
+            profileClient.modifyProfileRaw(profileId, properties).store(System.out, null);
             MainCLI.printMessage("Modified profile " + profileId);
         } else {
             ProfileData data = ProfileCLI.readProfileFromFile(filename);
-            data = profileCLI.profileClient.modifyProfile(data);
+            data = profileClient.modifyProfile(data);
 
             MainCLI.printMessage("Modified profile " + data.getId());
 
-            ProfileCLI.printProfile(data, profileCLI.getClient().getConfig().getServerURI());
+            ProfileCLI.printProfile(data, profileCLI.getConfig().getServerURI());
         }
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileRemoveCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileRemoveCLI.java
index f0920cde3e831bdc649f4b8c2ff70c744a53bc5d..8422400527a21d8d724410b9c7fa3ed5df308892 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileRemoveCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileRemoveCLI.java
@@ -4,6 +4,7 @@ import java.util.Arrays;
 
 import org.apache.commons.cli.CommandLine;
 
+import com.netscape.certsrv.profile.ProfileClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
 
@@ -37,7 +38,8 @@ public class ProfileRemoveCLI extends CLI {
 
         String profileId = args[0];
 
-        profileCLI.profileClient.deleteProfile(profileId);
+        ProfileClient profileClient = profileCLI.getProfileClient();
+        profileClient.deleteProfile(profileId);
 
         MainCLI.printMessage("Deleted profile \"" + profileId + "\"");
     }
diff --git a/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java b/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java
index 5134530bc790ffa271facff11557552f1f98fbdc..1a90cf3baccbcb9b3ce4efc3d96308d5fe8fbce3 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileShowCLI.java
@@ -7,6 +7,7 @@ import java.util.Properties;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 
+import com.netscape.certsrv.profile.ProfileClient;
 import com.netscape.certsrv.profile.ProfileData;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -62,8 +63,10 @@ public class ProfileShowCLI extends CLI {
             }
         }
 
+        ProfileClient profileClient = profileCLI.getProfileClient();
+
         if (cmd.hasOption("raw")) {
-            Properties profileConfig = profileCLI.profileClient.retrieveProfileRaw(profileId);
+            Properties profileConfig = profileClient.retrieveProfileRaw(profileId);
 
             if (filename != null) {
                 profileConfig.store(new FileOutputStream(filename), null);
@@ -73,12 +76,12 @@ public class ProfileShowCLI extends CLI {
             }
         } else {
             MainCLI.printMessage("Profile \"" + profileId + "\"");
-            ProfileData profileData = profileCLI.profileClient.retrieveProfile(profileId);
+            ProfileData profileData = profileClient.retrieveProfile(profileId);
 
             if (filename != null) {
                 ProfileCLI.saveProfileToFile(filename, profileData);
             } else {
-                ProfileCLI.printProfile(profileData, profileCLI.getClient().getConfig().getServerURI());
+                ProfileCLI.printProfile(profileData, profileCLI.getConfig().getServerURI());
             }
         }
     }
-- 
2.9.3

