diff options
author | Endi S. Dewata <edewata@redhat.com> | 2013-09-24 10:06:35 -0400 |
---|---|---|
committer | Endi S. Dewata <edewata@redhat.com> | 2013-10-01 19:25:28 -0400 |
commit | a3ac3ef0ca5e720a4cd5b17e9181124c17d17ea7 (patch) | |
tree | 0c2376346a001bb4fee229d52b10ea0398442080 /base | |
parent | 2b9fcdae818eded53ab64e5b86b947c80a262722 (diff) | |
download | pki-a3ac3ef0ca5e720a4cd5b17e9181124c17d17ea7.tar.gz pki-a3ac3ef0ca5e720a4cd5b17e9181124c17d17ea7.tar.xz pki-a3ac3ef0ca5e720a4cd5b17e9181124c17d17ea7.zip |
Refactored CLI framework.
The CLI framework has been modified to remove duplicate code
in various CLI modules.
Diffstat (limited to 'base')
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 f47717d3f..8833c1710 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 7acdcce38..d694b397c 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 e30858bfe..b35893505 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 32d580c08..83cd6851c 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 ecece0a09..0adecc171 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 fcdb03ef4..5808254ef 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 56d0bff19..e29c6c4ef 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 1b54bff8d..c22786150 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 f14d30ebc..a7b414e1d 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 de52613ee..bbae43688 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 4a9bf3828..df012e8e8 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 32b6366b3..7ba472454 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 a24a4fd00..e45072de1 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 3ea3e78de..b7b54942a 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 4bb44368d..4fce3bcc3 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 16c2b213b..a26c07f9a 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 6cf0a4d52..74f4df9dd 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) { |