From 22a6beee3bf10cb742a885e3f559653f96592142 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Tue, 24 Sep 2013 10:06:35 -0400
Subject: [PATCH] Refactored CLI framework.

The CLI framework has been modified to remove duplicate code
in various CLI modules.
---
 .../src/com/netscape/certsrv/client/Client.java    | 15 +++++
 .../netscape/certsrv/client/SubsystemClient.java   | 12 +---
 .../src/com/netscape/certsrv/tps/TPSClient.java    |  2 +-
 .../src/com/netscape/cmstools/cert/CertCLI.java    | 24 +------
 .../src/com/netscape/cmstools/cli/CLI.java         | 73 +++++++++++++++-------
 .../src/com/netscape/cmstools/cli/MainCLI.java     | 26 ++------
 .../src/com/netscape/cmstools/cli/TPSCLI.java      |  2 +-
 .../com/netscape/cmstools/client/ClientCLI.java    | 25 +-------
 .../src/com/netscape/cmstools/group/GroupCLI.java  | 25 +-------
 .../src/com/netscape/cmstools/key/KeyCLI.java      | 25 +-------
 .../com/netscape/cmstools/logging/ActivityCLI.java | 25 +-------
 .../com/netscape/cmstools/profile/ProfileCLI.java  | 24 +------
 .../netscape/cmstools/system/KRAConnectorCLI.java  | 27 +-------
 .../cmstools/system/SecurityDomainCLI.java         | 25 +-------
 .../com/netscape/cmstools/tps/cert/TPSCertCLI.java | 25 +-------
 .../com/netscape/cmstools/tps/token/TokenCLI.java  | 25 +-------
 .../src/com/netscape/cmstools/user/UserCLI.java    | 25 +-------
 17 files changed, 86 insertions(+), 319 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/client/Client.java b/base/common/src/com/netscape/certsrv/client/Client.java
index f47717d3f6172572e13eb0f0b627b5c0063db60b..8833c17106f126f185080844ea5375736b396886 100644
--- a/base/common/src/com/netscape/certsrv/client/Client.java
+++ b/base/common/src/com/netscape/certsrv/client/Client.java
@@ -18,6 +18,7 @@
 package com.netscape.certsrv.client;
 
 import java.net.URISyntaxException;
+import java.util.LinkedHashMap;
 
 /**
  * @author Endi S. Dewata
@@ -33,6 +34,8 @@ public class Client {
     // client name
     public String name;
 
+    public LinkedHashMap<String, Client> clients = new LinkedHashMap<String, Client>();
+
     public Client(PKIClient client, String name) {
         // by default use the subsystem specified in server URI
         this(client, client.getSubsystem(), name);
@@ -52,6 +55,18 @@ public class Client {
         return name;
     }
 
+    public void addClient(Client client) {
+        clients.put(client.getName(), client);
+    }
+
+    public Client getClient(String name) {
+        return clients.get(name);
+    }
+
+    public void removeClient(String name) {
+        clients.remove(name);
+    }
+
     public <T> T createProxy(Class<T> clazz) throws URISyntaxException {
         return client.createProxy(subsystem, clazz);
     }
diff --git a/base/common/src/com/netscape/certsrv/client/SubsystemClient.java b/base/common/src/com/netscape/certsrv/client/SubsystemClient.java
index 7acdcce38ff134a52a836bc9b6ff225018545530..d694b397c71f87ae567ea0c2fc476e075bfe9032 100644
--- a/base/common/src/com/netscape/certsrv/client/SubsystemClient.java
+++ b/base/common/src/com/netscape/certsrv/client/SubsystemClient.java
@@ -18,7 +18,6 @@
 package com.netscape.certsrv.client;
 
 import java.net.URISyntaxException;
-import java.util.LinkedHashMap;
 
 import com.netscape.certsrv.account.AccountClient;
 
@@ -30,21 +29,12 @@ public class SubsystemClient extends Client {
 
     public AccountClient accountClient;
 
-    public LinkedHashMap<String, Client> clients = new LinkedHashMap<String, Client>();
-
     public SubsystemClient(PKIClient client, String name) throws URISyntaxException {
         // subsystem name should match the client name
         super(client, name, name);
 
         accountClient = new AccountClient(client, name);
-    }
-
-    public void addClient(Client client) {
-        clients.put(client.getName(), client);
-    }
-
-    public Client getClient(String name) {
-        return clients.get(name);
+        addClient(accountClient);
     }
 
     /**
diff --git a/base/common/src/com/netscape/certsrv/tps/TPSClient.java b/base/common/src/com/netscape/certsrv/tps/TPSClient.java
index e30858bfec342f2e43856125c0752dd9aa8579e8..b35893505d304f987ed1f0fe25ebfe7affca087b 100644
--- a/base/common/src/com/netscape/certsrv/tps/TPSClient.java
+++ b/base/common/src/com/netscape/certsrv/tps/TPSClient.java
@@ -43,11 +43,11 @@ public class TPSClient extends SubsystemClient {
     public void init() throws URISyntaxException {
         addClient(new ActivityClient(client, name));
         addClient(new AuthenticatorClient(client, name));
+        addClient(new TPSCertClient(client, name));
         addClient(new ConfigClient(client, name));
         addClient(new ConnectionClient(client, name));
         addClient(new GroupClient(client, name));
         addClient(new TokenClient(client, name));
-        addClient(new TPSCertClient(client, name));
         addClient(new UserClient(client, name));
     }
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java b/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
index 32d580c080052a0ea06304ff3fd919ecd61bfd63..83cd6851c8393a2e4bb88a1ac09dffa3a323c7dd 100644
--- a/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cert/CertCLI.java
@@ -19,7 +19,6 @@
 package com.netscape.cmstools.cert;
 
 import java.text.SimpleDateFormat;
-import java.util.Arrays;
 
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
@@ -71,28 +70,7 @@ public class CertCLI extends CLI {
 
         certClient = new CertClient(parent.getClient());
 
-        if (args.length == 0) {
-            printHelp();
-            System.exit(1);
-        }
-
-        String command = args[0];
-        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
-
-        if (command == null) {
-            printHelp();
-            System.exit(1);
-        }
-
-        CLI module = getModule(command);
-        if (module != null) {
-            module.execute(commandArgs);
-
-        } else {
-            System.err.println("Error: Invalid command \"" + command + "\"");
-            printHelp();
-            System.exit(1);
-        }
+        super.execute(args);
     }
 
     public static String getAlgorithmNameFromOID(String oid) {
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/CLI.java b/base/java-tools/src/com/netscape/cmstools/cli/CLI.java
index ecece0a09203b477e829d745da41f39a12c52509..0adecc17143f8d43f1c000ca7c910fb021d3689d 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/CLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/CLI.java
@@ -71,10 +71,14 @@ public class CLI {
         if (parent == null) {
             return name;
         } else {
-            return parent.getName() + "-" + name;
+            return parent.getFullName() + "-" + name;
         }
     }
 
+    public String getFullModuleName(String moduleName) {
+        return getFullName() + "-" + moduleName;
+    }
+
     public String getDescription() {
         return description;
     }
@@ -96,6 +100,7 @@ public class CLI {
     }
 
     public Object getClient(String name) {
+        if (parent != null) return parent.getClient(name);
         return null;
     }
 
@@ -107,7 +112,7 @@ public class CLI {
         int rightPadding = 25;
 
         for (CLI module : modules.values()) {
-            String label = getFullName() + "-" + module.getName();
+            String label = module.getFullName();
 
             int padding = rightPadding - leftPadding - label.length();
             if (padding < 1)
@@ -127,38 +132,64 @@ public class CLI {
             System.exit(1);
         }
 
+        // A command consists of parts joined by dashes: <part 1>-<part 2>-...-<part N>.
+        // For example: cert-request-find
         String command = args[0];
-        String moduleName;
-        String moduleCommand;
-
-        // If a command contains a '-' sign it will be
-        // split into module name and module command.
-        // Otherwise it's a single command.
-        int i = command.indexOf('-');
-        if (i >= 0) { // <module name>-<module command>
-            moduleName = command.substring(0, i);
-            moduleCommand = command.substring(i+1);
-
-        } else { // <command>
-            moduleName = command;
-            moduleCommand = null;
+
+        // The command will be split into module name and module command, for example:
+        //  - module name: cert
+        //  - module command: request-find
+        String moduleName = null;
+        String moduleCommand = null;
+
+        // Search the module by incrementally adding parts into module name.
+        // Repeat until it finds the module or until there is no more parts to add.
+        CLI module = null;
+        int position = 0;
+
+        while (true) {
+
+            // Find the next dash.
+            int i = command.indexOf('-', position);
+            if (i >= 0) {
+                // If dash found, split command.
+                moduleName = command.substring(0, i);
+                moduleCommand = command.substring(i+1);
+
+            } else {
+                // If dash not found, use the whole command.
+                moduleName = command;
+                moduleCommand = null;
+            }
+
+            // Find module with that name.
+            module = getModule(moduleName);
+
+            // If module found, stop.
+            if (module != null) break;
+
+            // If there's no more dashes, stop.
+            if (i < 0) break;
+
+            position = i + 1;
         }
 
-        // get command module
-        if (verbose) System.out.println("Module: " + moduleName);
-        CLI module = getModule(moduleName);
         if (module == null) {
-            throw new Error("Invalid module \"" + moduleName + "\".");
+            throw new Error("Invalid module \"" + getFullModuleName(moduleName) + "\".");
         }
 
-        // prepare module arguments
+        if (verbose) System.out.println("Module: " + moduleName);
+
+        // Prepare module arguments.
         String[] moduleArgs;
         if (moduleCommand != null) {
+            // If module command exists, include it as arguments: <module command> <args>...
             moduleArgs = new String[args.length];
             moduleArgs[0] = moduleCommand;
             System.arraycopy(args, 1, moduleArgs, 1, args.length-1);
 
         } else {
+            // Otherwise, pass the original arguments: <args>...
             moduleArgs = new String[args.length-1];
             System.arraycopy(args, 1, moduleArgs, 0, args.length-1);
         }
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
index fcdb03ef48e896b441124a532108cdbf31e25e38..5808254ef143fa7f5cf4fac5b23a95188518d9fb 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
@@ -82,6 +82,10 @@ public class MainCLI extends CLI {
         addModule(new UserCLI(this));
     }
 
+    public String getFullModuleName(String moduleName) {
+        return moduleName;
+    }
+
     public void printVersion() {
         Package pkg = MainCLI.class.getPackage();
         System.out.println("PKI Command-Line Interface "+pkg.getImplementationVersion());
@@ -90,29 +94,9 @@ public class MainCLI extends CLI {
     public void printHelp() {
 
         formatter.printHelp(name+" [OPTIONS..] <command> [ARGS..]", options);
-
         System.out.println();
-        System.out.println("Commands:");
 
-        int leftPadding = 1;
-        int rightPadding = 18;
-
-        for (CLI plugin : modules.values()) {
-            String label = plugin.getName();
-
-            int padding = rightPadding - leftPadding - label.length();
-            if (padding < 1) padding = 1;
-
-            System.out.print(StringUtils.repeat(" ", leftPadding));
-            System.out.print(label);
-            System.out.print(StringUtils.repeat(" ", padding));
-            System.out.println(plugin.getDescription());
-        }
-    }
-
-    public void printHelpCommand(String pluginName) {
-        CLI plugin = getModule(pluginName);
-        plugin.printHelp();
+        super.printHelp();
     }
 
     public void createOptions(Options options) throws UnknownHostException {
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
index 56d0bff1924d9f7dc222719b1a88c1eb91c68d41..e29c6c4efc02a6d75f7ced8f9e69736ad4d3f77f 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/TPSCLI.java
@@ -41,11 +41,11 @@ public class TPSCLI extends SubsystemCLI {
 
         addModule(new ActivityCLI(this));
         addModule(new AuthenticatorCLI(this));
+        addModule(new TPSCertCLI(this));
         addModule(new ConfigCLI(this));
         addModule(new ConnectionCLI(this));
         addModule(new GroupCLI(this));
         addModule(new TokenCLI(this));
-        addModule(new TPSCertCLI(this));
         addModule(new UserCLI(this));
     }
 
diff --git a/base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java b/base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java
index 1b54bff8d378d5ef6808f6251585e38d99df3eb7..c22786150316dc5b01e5c2a3a6a94a0984293fd0 100644
--- a/base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/client/ClientCLI.java
@@ -18,8 +18,6 @@
 
 package com.netscape.cmstools.client;
 
-import java.util.Arrays;
-
 import org.mozilla.jss.crypto.X509Certificate;
 
 import com.netscape.certsrv.dbs.certdb.CertId;
@@ -52,28 +50,7 @@ public class ClientCLI extends CLI {
 
         client = parent.getClient();
 
-        if (args.length == 0) {
-            printHelp();
-            System.exit(1);
-        }
-
-        String command = args[0];
-        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
-
-        if (command == null) {
-            printHelp();
-            System.exit(1);
-        }
-
-        CLI module = getModule(command);
-        if (module != null) {
-            module.execute(commandArgs);
-
-        } else {
-            System.err.println("Error: Invalid command \"" + command + "\"");
-            printHelp();
-            System.exit(1);
-        }
+        super.execute(args);
     }
 
     public static void printCertInfo(X509Certificate cert) {
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 f14d30ebc10d8365c9b0c779de0bad6f972eeb93..a7b414e1dd6dbfab7e022b033a69a2db43d389cf 100644
--- a/base/java-tools/src/com/netscape/cmstools/group/GroupCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/group/GroupCLI.java
@@ -18,8 +18,6 @@
 
 package com.netscape.cmstools.group;
 
-import java.util.Arrays;
-
 import org.apache.commons.lang.StringUtils;
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
@@ -70,28 +68,7 @@ public class GroupCLI extends CLI {
             groupClient = new GroupClient(client);
         }
 
-        if (args.length == 0) {
-            printHelp();
-            System.exit(1);
-        }
-
-        String command = args[0];
-        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
-
-        if (command == null) {
-            printHelp();
-            System.exit(1);
-        }
-
-        CLI module = getModule(command);
-        if (module != null) {
-            module.execute(commandArgs);
-
-        } else {
-            System.err.println("Error: Invalid command \""+command+"\"");
-            printHelp();
-            System.exit(1);
-        }
+        super.execute(args);
     }
 
     public static void printGroup(GroupData groupData) {
diff --git a/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java b/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
index de52613eef2db6ff015a7a3f03ec2b32ff506567..bbae4368851097ce5592bc6fc0ced954d9038312 100644
--- a/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/key/KeyCLI.java
@@ -18,8 +18,6 @@
 
 package com.netscape.cmstools.key;
 
-import java.util.Arrays;
-
 import com.netscape.certsrv.key.KeyClient;
 import com.netscape.certsrv.key.KeyDataInfo;
 import com.netscape.certsrv.key.KeyRequestInfo;
@@ -54,28 +52,7 @@ public class KeyCLI extends CLI {
         client = parent.getClient();
         keyClient = new KeyClient(client);
 
-        if (args.length == 0) {
-            printHelp();
-            System.exit(1);
-        }
-
-        String command = args[0];
-        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
-
-        if (command == null) {
-            printHelp();
-            System.exit(1);
-        }
-
-        CLI module = getModule(command);
-        if (module != null) {
-            module.execute(commandArgs);
-
-        } else {
-            System.err.println("Error: Invalid command \"" + command + "\"");
-            printHelp();
-            System.exit(1);
-        }
+        super.execute(args);
     }
 
     public static void printKeyInfo(KeyDataInfo info) {
diff --git a/base/java-tools/src/com/netscape/cmstools/logging/ActivityCLI.java b/base/java-tools/src/com/netscape/cmstools/logging/ActivityCLI.java
index 4a9bf38280049a1152936a2a3192c5b33374b39a..df012e8e8763ca8ee0fa9267476ff55ac7827527 100644
--- a/base/java-tools/src/com/netscape/cmstools/logging/ActivityCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/logging/ActivityCLI.java
@@ -18,8 +18,6 @@
 
 package com.netscape.cmstools.logging;
 
-import java.util.Arrays;
-
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
 import com.netscape.certsrv.logging.ActivityClient;
@@ -45,28 +43,7 @@ public class ActivityCLI extends CLI {
         client = parent.getClient();
         activityClient = (ActivityClient)parent.getClient("activity");
 
-        if (args.length == 0) {
-            printHelp();
-            System.exit(1);
-        }
-
-        String command = args[0];
-        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
-
-        if (command == null) {
-            printHelp();
-            System.exit(1);
-        }
-
-        CLI module = getModule(command);
-        if (module != null) {
-            module.execute(commandArgs);
-
-        } else {
-            System.err.println("Error: Invalid command \"" + command + "\"");
-            printHelp();
-            System.exit(1);
-        }
+        super.execute(args);
     }
 
     public static void printActivity(ActivityData activity) {
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 32b6366b3128c126f407ee7ec42b325f2fd9628f..7ba472454aa49761c970c900d16ee2cc6f143a8a 100644
--- a/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/profile/ProfileCLI.java
@@ -4,7 +4,6 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.net.URI;
-import java.util.Arrays;
 import java.util.Locale;
 
 import javax.xml.bind.JAXBContext;
@@ -52,28 +51,7 @@ public class ProfileCLI extends CLI {
         client = parent.getClient();
         profileClient = new ProfileClient(client);
 
-        if (args.length == 0) {
-            printHelp();
-            System.exit(1);
-        }
-
-        String command = args[0];
-        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
-
-        if (command == null) {
-            printHelp();
-            System.exit(1);
-        }
-
-        CLI module = getModule(command);
-        if (module != null) {
-            module.execute(commandArgs);
-
-        } else {
-            System.err.println("Error: Invalid command \"" + command + "\"");
-            printHelp();
-            System.exit(1);
-        }
+        super.execute(args);
     }
 
     public static void printProfileDataInfo(ProfileDataInfo info) {
diff --git a/base/java-tools/src/com/netscape/cmstools/system/KRAConnectorCLI.java b/base/java-tools/src/com/netscape/cmstools/system/KRAConnectorCLI.java
index a24a4fd00a304ded72061d53e3f9b3ab1f7c95cc..e45072de166d26946e912b0186d0bd5e536226ed 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/KRAConnectorCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/KRAConnectorCLI.java
@@ -17,8 +17,6 @@
 // --- END COPYRIGHT BLOCK ---
 package com.netscape.cmstools.system;
 
-import java.util.Arrays;
-
 import com.netscape.certsrv.system.KRAConnectorClient;
 import com.netscape.cmstools.cli.CLI;
 import com.netscape.cmstools.cli.MainCLI;
@@ -51,29 +49,6 @@ public class KRAConnectorCLI extends CLI {
         client = parent.getClient();
         kraConnectorClient = new KRAConnectorClient(client);
 
-        if (args.length == 0) {
-            printHelp();
-            System.exit(1);
-        }
-
-        String command = args[0];
-        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
-
-        if (command == null) {
-            printHelp();
-            System.exit(1);
-        }
-
-        CLI module = getModule(command);
-        if (module != null) {
-            module.execute(commandArgs);
-
-        } else {
-            System.err.println("Error: Invalid command \""+command+"\"");
-            printHelp();
-            System.exit(1);
-        }
+        super.execute(args);
     }
-
-
 }
diff --git a/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainCLI.java b/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainCLI.java
index 3ea3e78dee65d91b287ec418159d4e0e8f37f754..b7b54942a80abda5c50b186cfa0e762d147eb5a6 100644
--- a/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/system/SecurityDomainCLI.java
@@ -18,8 +18,6 @@
 
 package com.netscape.cmstools.system;
 
-import java.util.Arrays;
-
 import com.netscape.certsrv.system.DomainInfo;
 import com.netscape.certsrv.system.SecurityDomainClient;
 import com.netscape.certsrv.system.SecurityDomainHost;
@@ -55,28 +53,7 @@ public class SecurityDomainCLI extends CLI {
         client = parent.getClient();
         securityDomainClient = new SecurityDomainClient(client);
 
-        if (args.length == 0) {
-            printHelp();
-            System.exit(1);
-        }
-
-        String command = args[0];
-        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
-
-        if (command == null) {
-            printHelp();
-            System.exit(1);
-        }
-
-        CLI module = getModule(command);
-        if (module != null) {
-            module.execute(commandArgs);
-
-        } else {
-            System.err.println("Error: Invalid command \"" + command + "\"");
-            printHelp();
-            System.exit(1);
-        }
+        super.execute(args);
     }
 
     public static void printSecurityDomain(DomainInfo domain) {
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertCLI.java
index 4bb44368d8377ebea299f1f02cbfa91787ccc089..4fce3bcc3511ff3ca0bf3c265a00c8074ac3e5c3 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertCLI.java
@@ -18,8 +18,6 @@
 
 package com.netscape.cmstools.tps.cert;
 
-import java.util.Arrays;
-
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
 import com.netscape.certsrv.tps.cert.TPSCertClient;
@@ -45,28 +43,7 @@ public class TPSCertCLI extends CLI {
         client = parent.getClient();
         certClient = (TPSCertClient)parent.getClient("cert");
 
-        if (args.length == 0) {
-            printHelp();
-            System.exit(1);
-        }
-
-        String command = args[0];
-        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
-
-        if (command == null) {
-            printHelp();
-            System.exit(1);
-        }
-
-        CLI module = getModule(command);
-        if (module != null) {
-            module.execute(commandArgs);
-
-        } else {
-            System.err.println("Error: Invalid command \"" + command + "\"");
-            printHelp();
-            System.exit(1);
-        }
+        super.execute(args);
     }
 
     public static void printCert(TPSCertData cert) {
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java
index 16c2b213bfc7b63e5324a829e5d00d8b82ff80bf..a26c07f9ae169a39f48812909c5304d6252254c2 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenCLI.java
@@ -18,8 +18,6 @@
 
 package com.netscape.cmstools.tps.token;
 
-import java.util.Arrays;
-
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
 import com.netscape.certsrv.tps.token.TokenClient;
@@ -48,28 +46,7 @@ public class TokenCLI extends CLI {
         client = parent.getClient();
         tokenClient = (TokenClient)parent.getClient("token");
 
-        if (args.length == 0) {
-            printHelp();
-            System.exit(1);
-        }
-
-        String command = args[0];
-        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
-
-        if (command == null) {
-            printHelp();
-            System.exit(1);
-        }
-
-        CLI module = getModule(command);
-        if (module != null) {
-            module.execute(commandArgs);
-
-        } else {
-            System.err.println("Error: Invalid command \"" + command + "\"");
-            printHelp();
-            System.exit(1);
-        }
+        super.execute(args);
     }
 
     public static void printToken(TokenData token) {
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 6cf0a4d5289511a1cc99e75212cd12667c39913e..74f4df9ddae328b6ba92ebc46c2ba2cc5ffb5688 100644
--- a/base/java-tools/src/com/netscape/cmstools/user/UserCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/user/UserCLI.java
@@ -18,8 +18,6 @@
 
 package com.netscape.cmstools.user;
 
-import java.util.Arrays;
-
 import org.apache.commons.lang.StringUtils;
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
@@ -76,28 +74,7 @@ public class UserCLI extends CLI {
             userClient = new UserClient(client);
         }
 
-        if (args.length == 0) {
-            printHelp();
-            System.exit(1);
-        }
-
-        String command = args[0];
-        String[] commandArgs = Arrays.copyOfRange(args, 1, args.length);
-
-        if (command == null) {
-            printHelp();
-            System.exit(1);
-        }
-
-        CLI module = getModule(command);
-        if (module != null) {
-            module.execute(commandArgs);
-
-        } else {
-            System.err.println("Error: Invalid command \"" + command + "\"");
-            printHelp();
-            System.exit(1);
-        }
+        super.execute(args);
     }
 
     public static void printUser(UserData userData) {
-- 
1.8.1.4

