diff options
Diffstat (limited to 'libjava/classpath/tools/gnu/classpath/tools/keytool/Main.java')
-rw-r--r-- | libjava/classpath/tools/gnu/classpath/tools/keytool/Main.java | 231 |
1 files changed, 162 insertions, 69 deletions
diff --git a/libjava/classpath/tools/gnu/classpath/tools/keytool/Main.java b/libjava/classpath/tools/gnu/classpath/tools/keytool/Main.java index fb7aa45..582aba0 100644 --- a/libjava/classpath/tools/gnu/classpath/tools/keytool/Main.java +++ b/libjava/classpath/tools/gnu/classpath/tools/keytool/Main.java @@ -38,8 +38,12 @@ exception statement from your version. */ package gnu.classpath.tools.keytool; -import gnu.classpath.tools.HelpPrinter; import gnu.classpath.tools.common.ProviderUtil; +import gnu.classpath.tools.getopt.ClasspathToolParser; +import gnu.classpath.tools.getopt.Option; +import gnu.classpath.tools.getopt.OptionException; +import gnu.classpath.tools.getopt.OptionGroup; +import gnu.classpath.tools.getopt.Parser; import gnu.java.security.Registry; import gnu.javax.crypto.jce.GnuCrypto; import gnu.javax.security.auth.callback.GnuCallbacks; @@ -57,8 +61,51 @@ import java.util.logging.Logger; public class Main { private static final Logger log = Logger.getLogger(Main.class.getName()); - /** The relative file path to the command tool's help text. */ - private static final String HELP_PATH = "keytool/keytool.txt"; //$NON-NLS-1$ + static final String KEYTOOL_TOOL = "keytool"; //$NON-NLS-1$ + static final String GENKEY_CMD = "genkey"; //$NON-NLS-1$ + static final String IMPORT_CMD = "import"; //$NON-NLS-1$ + static final String SELFCERT_CMD = "selfcert"; //$NON-NLS-1$ + static final String IDENTITYDB_CMD = "identitydb"; //$NON-NLS-1$ + static final String CERTREQ_CMD = "certreq"; //$NON-NLS-1$ + static final String EXPORT_CMD = "export"; //$NON-NLS-1$ + static final String LIST_CMD = "list"; //$NON-NLS-1$ + static final String PRINTCERT_CMD = "printcert"; //$NON-NLS-1$ + static final String KEYCLONE_CMD = "keyclone"; //$NON-NLS-1$ + static final String STOREPASSWD_CMD = "storepasswd"; //$NON-NLS-1$ + static final String KEYPASSWD_CMD = "keypasswd"; //$NON-NLS-1$ + static final String DELETE_CMD = "delete"; //$NON-NLS-1$ + + static final String _GENKEY = "-" + GENKEY_CMD; //$NON-NLS-1$ + static final String _IMPORT = "-" + IMPORT_CMD; //$NON-NLS-1$ + static final String _SELFCERT = "-" + SELFCERT_CMD; //$NON-NLS-1$ + static final String _IDENTITYDB = "-" + IDENTITYDB_CMD; //$NON-NLS-1$ + static final String _CERTREQ = "-" + CERTREQ_CMD; //$NON-NLS-1$ + static final String _EXPORT = "-" + EXPORT_CMD; //$NON-NLS-1$ + static final String _LIST = "-" + LIST_CMD; //$NON-NLS-1$ + static final String _PRINTCERT = "-" + PRINTCERT_CMD; //$NON-NLS-1$ + static final String _KEYCLONE = "-" + KEYCLONE_CMD; //$NON-NLS-1$ + static final String _STOREPASSWD = "-" + STOREPASSWD_CMD; //$NON-NLS-1$ + static final String _KEYPASSWD = "-" + KEYPASSWD_CMD; //$NON-NLS-1$ + static final String _DELETE = "-" + DELETE_CMD; //$NON-NLS-1$ + static final String _HELP = "-help"; //$NON-NLS-1$ + + static final String ALIAS_OPT = "alias"; //$NON-NLS-1$ + static final String SIGALG_OPT = "sigalg"; //$NON-NLS-1$ + static final String KEYALG_OPT = "keyalg"; //$NON-NLS-1$ + static final String KEYSIZE_OPT = "keysize"; //$NON-NLS-1$ + static final String KEYPASS_OPT = "keypass"; //$NON-NLS-1$ + static final String VALIDITY_OPT = "validity"; //$NON-NLS-1$ + static final String STORETYPE_OPT = "storetype"; //$NON-NLS-1$ + static final String STOREPASS_OPT = "storepass"; //$NON-NLS-1$ + static final String KEYSTORE_OPT = "keystore"; //$NON-NLS-1$ + static final String PROVIDER_OPT = "provider"; //$NON-NLS-1$ + static final String FILE_OPT = "file"; //$NON-NLS-1$ + static final String VERBOSE_OPT = "v"; //$NON-NLS-1$ + static final String DEST_OPT = "dest"; //$NON-NLS-1$ + static final String NEW_OPT = "new"; //$NON-NLS-1$ + static final String RFC_OPT = "rfc"; //$NON-NLS-1$ + static final String DNAME_OPT = "dname"; //$NON-NLS-1$ + /** The Preferences key name for the last issued certificate serial nbr. */ static final String LAST_SERIAL_NUMBER = "lastSerialNumber"; //$NON-NLS-1$ /** Constant denoting the X.509 certificate type. */ @@ -70,6 +117,8 @@ public class Main private int gnuCryptoProviderNdx = -2; /** The new position of GNU Callbacks provider if it is not already installed. */ private int gnuCallbacksNdx = -2; + /** The command line parser. */ + private Parser cmdLineParser; private Main() { @@ -81,117 +130,140 @@ public class Main log.entering(Main.class.getName(), "main", args); //$NON-NLS-1$ Main tool = new Main(); + int result = 1; try { tool.setup(); tool.start(args); + result = 0; + } + catch (OptionException x) + { + System.err.println(x.getMessage()); + if (tool.cmdLineParser != null) + tool.cmdLineParser.printHelp(); } catch (SecurityException x) { log.throwing(Main.class.getName(), "main", x); //$NON-NLS-1$ - System.err.println(Messages.getString("Main.6") + x.getMessage()); //$NON-NLS-1$ + System.err.println(Messages.getFormattedString("Main.6", //$NON-NLS-1$ + x.getMessage())); } catch (Exception x) { log.throwing(Main.class.getName(), "main", x); //$NON-NLS-1$ - System.err.println(Messages.getString("Main.8") + x); //$NON-NLS-1$ + System.err.println(Messages.getFormattedString("Main.8", x)); //$NON-NLS-1$ } finally - { - tool.teardown(); - } + { + tool.teardown(); + } - log.exiting(Main.class.getName(), "main"); //$NON-NLS-1$ - // System.exit(0); + log.exiting(Main.class.getName(), "main", Integer.valueOf(result)); //$NON-NLS-1$ + System.exit(result); } // helper methods ----------------------------------------------------------- + private void setup() + { + log.entering(this.getClass().getName(), "setup"); //$NON-NLS-1$ + + cmdLineParser = getParser(); + gnuCryptoProviderNdx = ProviderUtil.addProvider(new GnuCrypto()); + gnuCallbacksNdx = ProviderUtil.addProvider(new GnuCallbacks()); + + log.exiting(this.getClass().getName(), "setup"); //$NON-NLS-1$ + } + private void start(String[] args) throws Exception { - log.entering(this.getClass().getName(), "start", args); //$NON-NLS-1$ + log.entering(this.getClass().getName(), "start"); //$NON-NLS-1$ - if (args == null) - args = new String[0]; + if (args == null || args.length == 0) + throw new OptionException(""); //$NON-NLS-1$ - int limit = args.length; - log.finest("args.length=" + limit); //$NON-NLS-1$ - int i = 0; String opt; Command cmd; - while (i < limit) + while (args.length > 0) { - opt = args[i]; - log.finest("args[" + i + "]=" + opt); //$NON-NLS-1$ //$NON-NLS-2$ - if (opt == null || opt.length() == 0) - continue; - + opt = args[0]; cmd = null; - if ("-genkey".equals(opt)) //$NON-NLS-1$ + if (_GENKEY.equals(opt)) cmd = new GenKeyCmd(); - else if ("-import".equals(opt)) //$NON-NLS-1$ + else if (_IMPORT.equals(opt)) cmd = new ImportCmd(); - else if ("-selfcert".equals(opt)) //$NON-NLS-1$ + else if (_SELFCERT.equals(opt)) cmd = new SelfCertCmd(); - else if ("-identitydb".equals(opt)) //$NON-NLS-1$ + else if (_IDENTITYDB.equals(opt)) cmd = new IdentityDBCmd(); - else if ("-certreq".equals(opt)) //$NON-NLS-1$ + else if (_CERTREQ.equals(opt)) cmd = new CertReqCmd(); - else if ("-export".equals(opt)) //$NON-NLS-1$ + else if (_EXPORT.equals(opt)) cmd = new ExportCmd(); - else if ("-list".equals(opt)) //$NON-NLS-1$ + else if (_LIST.equals(opt)) cmd = new ListCmd(); - else if ("-printcert".equals(opt)) //$NON-NLS-1$ + else if (_PRINTCERT.equals(opt)) cmd = new PrintCertCmd(); - else if ("-keyclone".equals(opt)) //$NON-NLS-1$ + else if (_KEYCLONE.equals(opt)) cmd = new KeyCloneCmd(); - else if ("-storepasswd".equals(opt)) //$NON-NLS-1$ + else if (_STOREPASSWD.equals(opt)) cmd = new StorePasswdCmd(); - else if ("-keypasswd".equals(opt)) //$NON-NLS-1$ + else if (_KEYPASSWD.equals(opt)) cmd = new KeyPasswdCmd(); - else if ("-delete".equals(opt)) //$NON-NLS-1$ + else if (_DELETE.equals(opt)) cmd = new DeleteCmd(); - else if ("-help".equals(opt)) //$NON-NLS-1$ - { - printHelp(); - i++; - } + else if (_HELP.equals(opt)) + throw new OptionException(""); //$NON-NLS-1$ else - { - log.fine("Unknown command [" + opt + "] at index #" + i //$NON-NLS-1$ //$NON-NLS-2$ - + ". Arguments from that token onward will be ignored"); //$NON-NLS-1$ - break; - } - - if (cmd != null) - { - i = cmd.processArgs(args, i); - cmd.doCommand(); - } - } - - // the -help command is the default; i.e. - // keytool - // is equivalent to: - // keytool -help - if (i == 0) - printHelp(); + throw new OptionException(Messages.getFormattedString("Main.18", //$NON-NLS-1$ + opt)); - if (i < limit) // more options than needed - log.fine("Last recognized argument is assumed at index #" + (i - 1) //$NON-NLS-1$ - + ". Remaining arguments (" + args[i] + "...) will be ignored"); //$NON-NLS-1$ //$NON-NLS-2$ + String[] cmdArgs = new String[args.length - 1]; + System.arraycopy(args, 1, cmdArgs, 0, cmdArgs.length); + args = cmd.processArgs(cmdArgs); + cmd.doCommand(); + } log.exiting(this.getClass().getName(), "start"); //$NON-NLS-1$ } - private void setup() + private Parser getParser() { - log.entering(this.getClass().getName(), "setup"); //$NON-NLS-1$ - - gnuCryptoProviderNdx = ProviderUtil.addProvider(new GnuCrypto()); - gnuCallbacksNdx = ProviderUtil.addProvider(new GnuCallbacks()); - - log.exiting(this.getClass().getName(), "setup"); //$NON-NLS-1$ + log.entering(this.getClass().getName(), "getParser"); //$NON-NLS-1$ + + Parser result = new ClasspathToolParser(KEYTOOL_TOOL, true); + result.setHeader(Messages.getString("Main.19")); //$NON-NLS-1$ + result.setFooter(Messages.getString("Main.20")); //$NON-NLS-1$ + OptionGroup cmdGroup = new OptionGroup(Messages.getString("Main.21")); //$NON-NLS-1$ + cmdGroup.add(new NoParseOption(GENKEY_CMD, + Messages.getString("Main.22"))); //$NON-NLS-1$ + cmdGroup.add(new NoParseOption(IMPORT_CMD, + Messages.getString("Main.23"))); //$NON-NLS-1$ + cmdGroup.add(new NoParseOption(SELFCERT_CMD, + Messages.getString("Main.24"))); //$NON-NLS-1$ + cmdGroup.add(new NoParseOption(IDENTITYDB_CMD, + Messages.getString("Main.25"))); //$NON-NLS-1$ + cmdGroup.add(new NoParseOption(CERTREQ_CMD, + Messages.getString("Main.26"))); //$NON-NLS-1$ + cmdGroup.add(new NoParseOption(EXPORT_CMD, + Messages.getString("Main.27"))); //$NON-NLS-1$ + cmdGroup.add(new NoParseOption(LIST_CMD, + Messages.getString("Main.28"))); //$NON-NLS-1$ + cmdGroup.add(new NoParseOption(PRINTCERT_CMD, + Messages.getString("Main.29"))); //$NON-NLS-1$ + cmdGroup.add(new NoParseOption(KEYCLONE_CMD, + Messages.getString("Main.30"))); //$NON-NLS-1$ + cmdGroup.add(new NoParseOption(STOREPASSWD_CMD, + Messages.getString("Main.31"))); //$NON-NLS-1$ + cmdGroup.add(new NoParseOption(KEYPASSWD_CMD, + Messages.getString("Main.32"))); //$NON-NLS-1$ + cmdGroup.add(new NoParseOption(DELETE_CMD, + Messages.getString("Main.33"))); //$NON-NLS-1$ + result.add(cmdGroup); + + log.exiting(this.getClass().getName(), "getParser", result); //$NON-NLS-1$ + return result; } private void teardown() @@ -213,7 +285,28 @@ public class Main if (helpPrinted) return; - HelpPrinter.printHelp(HELP_PATH); helpPrinted = true; } + + // Inner class(es) + // ========================================================================== + + private class NoParseOption + extends Option + { + public NoParseOption(String name, String description) + { + super(name, description); + } + + public NoParseOption(String name, String description, String param) + { + super(name, description, param); + } + + public void parsed(String argument) throws OptionException + { + // do nothing + } + } } |