diff options
author | Raif S. Naffah <raif@swiftdsl.com.au> | 2006-05-13 00:34:54 +0000 |
---|---|---|
committer | Raif S. Naffah <raif@swiftdsl.com.au> | 2006-05-13 00:34:54 +0000 |
commit | a7ecd35a7bf9eb05fbe34afe2c598fdc0d863a14 (patch) | |
tree | 1da54d8a66c070f3349af2e7950aebeff1b1fbf5 /tools/gnu/classpath/tools | |
parent | 9bed728379aef945960e8979af03bcb4ca37471c (diff) | |
download | classpath-a7ecd35a7bf9eb05fbe34afe2c598fdc0d863a14.tar.gz |
2006-05-13 Raif S. Naffah <raif@swiftdsl.com.au>
* resource/gnu/classpath/tools/jarsigner/MessageBundle.properties:
Added help text.
* tools/Makefile.am (JARSIGNER_HELPS): Removed.
* tools/gnu/classpath/tools/jarsigner/jarsigner.txt: Removed.
* tools/gnu/classpath/tools/jarsigner/Main.java:
Increased visibility of fields used by parser anonymous classes.
(HELP_PATH): Removed.
(cmdLineParser): New field.
(main): Handle JVM exit status.
Handle command line parsing exceptions.
(processArgs): Use getopt command line parser.
(getParser): New method.
(setupCommonParams): Removed checks now handled by processArgs().
(setupSigningParams): Likewise.
* tools/gnu/classpath/tools/jarsigner/JarSigner.java (start):
Reuse an existing message-bundle constant.
Diffstat (limited to 'tools/gnu/classpath/tools')
-rw-r--r-- | tools/gnu/classpath/tools/jarsigner/JarSigner.java | 2 | ||||
-rw-r--r-- | tools/gnu/classpath/tools/jarsigner/Main.java | 266 | ||||
-rw-r--r-- | tools/gnu/classpath/tools/jarsigner/jarsigner.txt | 116 |
3 files changed, 190 insertions, 194 deletions
diff --git a/tools/gnu/classpath/tools/jarsigner/JarSigner.java b/tools/gnu/classpath/tools/jarsigner/JarSigner.java index 40bee9fe9..8d3bc31af 100644 --- a/tools/gnu/classpath/tools/jarsigner/JarSigner.java +++ b/tools/gnu/classpath/tools/jarsigner/JarSigner.java @@ -141,7 +141,7 @@ public class JarSigner main.isInternalSF()); log.finer("Created .DSA file"); //$NON-NLS-1$ if (main.isVerbose()) - System.out.println(Messages.getString("JarSigner.11") + dsaFileName); //$NON-NLS-1$ + System.out.println(Messages.getString("JarSigner.8") + dsaFileName); //$NON-NLS-1$ // cleanup outSignedJarFile.close(); diff --git a/tools/gnu/classpath/tools/jarsigner/Main.java b/tools/gnu/classpath/tools/jarsigner/Main.java index f460a96cc..a5ad96fad 100644 --- a/tools/gnu/classpath/tools/jarsigner/Main.java +++ b/tools/gnu/classpath/tools/jarsigner/Main.java @@ -39,9 +39,13 @@ exception statement from your version. */ package gnu.classpath.tools.jarsigner; import gnu.classpath.SystemProperties; -import gnu.classpath.tools.HelpPrinter; import gnu.classpath.tools.common.CallbackUtil; 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.OID; import gnu.java.security.Registry; import gnu.javax.security.auth.callback.ConsoleCallbackHandler; @@ -82,7 +86,6 @@ import javax.security.auth.callback.UnsupportedCallbackException; public class Main { private static final Logger log = Logger.getLogger(Main.class.getName()); - private static final String HELP_PATH = "jarsigner/jarsigner.txt"; //$NON-NLS-1$ private static final Locale EN_US_LOCALE = new Locale("en", "US"); //$NON-NLS-1$ //$NON-NLS-2$ static final String DIGEST = "SHA1-Digest"; //$NON-NLS-1$ static final String DIGEST_MANIFEST = "SHA1-Digest-Manifest"; //$NON-NLS-1$ @@ -91,18 +94,18 @@ public class Main static final OID DSA_SIGNATURE_OID = new OID(Registry.DSA_OID_STRING); static final OID RSA_SIGNATURE_OID = new OID(Registry.RSA_OID_STRING); - private boolean verify; - private String ksURL; - private String ksType; - private String password; - private String ksPassword; - private String sigFileName; - private String signedJarFileName; - private boolean verbose; - private boolean certs; - private boolean internalSF; - private boolean sectionsOnly; - private String providerClassName; + protected boolean verify; + protected String ksURL; + protected String ksType; + protected String password; + protected String ksPassword; + protected String sigFileName; + protected String signedJarFileName; + protected boolean verbose; + protected boolean certs; + protected boolean internalSF; + protected boolean sectionsOnly; + protected String providerClassName; private String jarFileName; private String alias; @@ -115,6 +118,8 @@ public class Main private Certificate[] signerCertificateChain; /** The callback handler to use when needing to interact with user. */ private CallbackHandler handler; + /** The command line parser. */ + private Parser cmdLineParser; private Main() { @@ -126,11 +131,19 @@ public class Main log.entering(Main.class.getName(), "main", args); //$NON-NLS-1$ Main tool = new Main(); + int result = 1; try { tool.processArgs(args); tool.start(); + result = 0; } + catch (OptionException x) + { + System.err.println(x.getMessage()); //$NON-NLS-1$ + if (tool.cmdLineParser != null) + tool.cmdLineParser.printHelp(); + } catch (SecurityException x) { log.throwing(Main.class.getName(), "main", x); //$NON-NLS-1$ @@ -141,11 +154,13 @@ public class Main log.throwing(Main.class.getName(), "main", x); //$NON-NLS-1$ System.err.println(Messages.getString("Main.9") + 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 ----------------------------------------------------------- @@ -155,65 +170,29 @@ public class Main * preparation for the user desired action. * * @param args an array of options (strings). - * @throws Exception if an exceptio occurs during the process. + * @throws Exception if an exception occurs during the process. */ private void processArgs(String[] args) throws Exception { log.entering(this.getClass().getName(), "processArgs", args); //$NON-NLS-1$ - HelpPrinter.checkHelpKey(args, HELP_PATH); + cmdLineParser = getParser(); if (args == null || args.length == 0) - HelpPrinter.printHelpAndExit(HELP_PATH); - - int limit = args.length; - log.finest("args.length=" + limit); //$NON-NLS-1$ - int i = 0; - String opt; - while (i < limit) - { - opt = args[i++]; - log.finest("args[" + (i - 1) + "]=" + opt); //$NON-NLS-1$ //$NON-NLS-2$ - if (opt == null || opt.length() == 0) - continue; - - if ("-verify".equals(opt)) // -verify //$NON-NLS-1$ - verify = true; - else if ("-keystore".equals(opt)) // -keystore URL //$NON-NLS-1$ - ksURL = args[i++]; - else if ("-storetype".equals(opt)) // -storetype STORE_TYPE //$NON-NLS-1$ - ksType = args[i++]; - else if ("-storepass".equals(opt)) // -storepass PASSWORD //$NON-NLS-1$ - ksPassword = args[i++]; - else if ("-keypass".equals(opt)) // -keypass PASSWORD //$NON-NLS-1$ - password = args[i++]; - else if ("-sigfile".equals(opt)) // -sigfile NAME //$NON-NLS-1$ - sigFileName = args[i++]; - else if ("-signedjar".equals(opt)) // -signedjar FILE_NAME //$NON-NLS-1$ - signedJarFileName = args[i++]; - else if ("-verbose".equals(opt)) // -verbose //$NON-NLS-1$ - verbose = true; - else if ("-certs".equals(opt)) // -certs //$NON-NLS-1$ - certs = true; - else if ("-internalsf".equals(opt)) // -internalsf //$NON-NLS-1$ - internalSF = true; - else if ("-sectionsonly".equals(opt)) // -sectionsonly //$NON-NLS-1$ - sectionsOnly = true; - else if ("-provider".equals(opt)) // -provider PROVIDER_CLASS_NAME //$NON-NLS-1$ - providerClassName = args[i++]; - else - { - jarFileName = opt; - if (! verify) - alias = args[i++]; - - break; - } - } - - if (i < limit) // more options than needed - log.fine("Last argument is assumed at index #" + (i - 1) //$NON-NLS-1$ - + ". Remaining arguments (" + args[i] //$NON-NLS-1$ - + "...) will be ignored"); //$NON-NLS-1$ + throw new OptionException(Messages.getString("Main.133")); //$NON-NLS-1$ + + String[] fileAndAlias = cmdLineParser.parse(args); + if (fileAndAlias.length < 1) + throw new OptionException(Messages.getString("Main.133")); //$NON-NLS-1$ + + jarFileName = fileAndAlias[0]; + if (! verify) // must have an ALIAS. use "mykey" if undefined + if (fileAndAlias.length < 2) + { + log.finer("Missing ALIAS argument. Will use [mykey] instead"); //$NON-NLS-1$ + alias = "mykey"; //$NON-NLS-1$ + } + else + alias = fileAndAlias[1]; setupCommonParams(); if (verify) @@ -250,6 +229,145 @@ public class Main log.exiting(this.getClass().getName(), "processArgs"); //$NON-NLS-1$ } + private Parser getParser() + { + log.entering(this.getClass().getName(), "getParser"); //$NON-NLS-1$ + + Parser result = new ClasspathToolParser("jarsigner", true); //$NON-NLS-1$ + result.setHeader(Messages.getString("Main.2")); //$NON-NLS-1$ + result.setFooter(Messages.getString("Main.1")); //$NON-NLS-1$ + + OptionGroup signGroup = new OptionGroup(Messages.getString("Main.0")); //$NON-NLS-1$ + + signGroup.add(new Option("keystore", //$NON-NLS-1$ + Messages.getString("Main.101"), //$NON-NLS-1$ + Messages.getString("Main.102")) //$NON-NLS-1$ + { + public void parsed(String argument) throws OptionException + { + ksURL = argument; + } + }); + + signGroup.add(new Option("storetype", //$NON-NLS-1$ + Messages.getString("Main.104"), //$NON-NLS-1$ + Messages.getString("Main.105")) //$NON-NLS-1$ + { + public void parsed(String argument) throws OptionException + { + ksType = argument; + } + }); + + signGroup.add(new Option("storepass", //$NON-NLS-1$ + Messages.getString("Main.107"), //$NON-NLS-1$ + Messages.getString("Main.108")) //$NON-NLS-1$ + { + public void parsed(String argument) throws OptionException + { + ksPassword = argument; + } + }); + + signGroup.add(new Option("keypass", //$NON-NLS-1$ + Messages.getString("Main.110"), //$NON-NLS-1$ + Messages.getString("Main.111")) //$NON-NLS-1$ + { + public void parsed(String argument) throws OptionException + { + password = argument; + } + }); + + signGroup.add(new Option("sigfile", //$NON-NLS-1$ + Messages.getString("Main.113"), //$NON-NLS-1$ + Messages.getString("Main.114")) //$NON-NLS-1$ + { + public void parsed(String argument) throws OptionException + { + sigFileName = argument; + } + }); + + signGroup.add(new Option("signedjar", //$NON-NLS-1$ + Messages.getString("Main.116"), //$NON-NLS-1$ + Messages.getString("Main.117")) //$NON-NLS-1$ + { + public void parsed(String argument) throws OptionException + { + signedJarFileName = argument; + } + }); + + result.add(signGroup); + + OptionGroup verifyGroup = new OptionGroup(Messages.getString("Main.118")); //$NON-NLS-1$ + + verifyGroup.add(new Option("verify", //$NON-NLS-1$ + Messages.getString("Main.120")) //$NON-NLS-1$ + { + public void parsed(String argument) throws OptionException + { + verify = true; + } + }); + + verifyGroup.add(new Option("certs", //$NON-NLS-1$ + Messages.getString("Main.122")) //$NON-NLS-1$ + { + public void parsed(String argument) throws OptionException + { + certs = true; + } + }); + + result.add(verifyGroup); + + OptionGroup commonGroup = new OptionGroup(Messages.getString("Main.123")); //$NON-NLS-1$ + + commonGroup.add(new Option("verbose", //$NON-NLS-1$ + Messages.getString("Main.125")) //$NON-NLS-1$ + { + public void parsed(String argument) throws OptionException + { + verbose = true; + } + }); + + commonGroup.add(new Option("internalsf", //$NON-NLS-1$ + Messages.getString("Main.127")) //$NON-NLS-1$ + { + public void parsed(String argument) throws OptionException + { + internalSF = true; + } + }); + + commonGroup.add(new Option("sectionsonly", //$NON-NLS-1$ + Messages.getString("Main.129")) //$NON-NLS-1$ + { + public void parsed(String argument) throws OptionException + { + sectionsOnly = true; + } + }); + + commonGroup.add(new Option("provider", //$NON-NLS-1$ + Messages.getString("Main.131"), //$NON-NLS-1$ + Messages.getString("Main.132")) //$NON-NLS-1$ + { + public void parsed(String argument) throws OptionException + { + providerClassName = argument; + } + }); + + result.add(commonGroup); + + log.exiting(this.getClass().getName(), "getParser", result); //$NON-NLS-1$ + return result; + } + /** * Invokes the <code>start()</code> method of the concrete handler. * <p> @@ -319,9 +437,6 @@ public class Main { log.entering(this.getClass().getName(), "setupCommonParams"); //$NON-NLS-1$ - if (jarFileName == null) - HelpPrinter.printHelpAndExit(HELP_PATH); - File jar = new File(jarFileName); if (! jar.exists()) throw new FileNotFoundException(jarFileName); @@ -429,9 +544,6 @@ public class Main InputStream stream = url.openStream(); store.load(stream, ksPasswordChars); - if (alias == null) - HelpPrinter.printHelpAndExit(HELP_PATH); - if (! store.containsAlias(alias)) throw new SecurityException(Messages.getFormattedString("Main.6", alias)); //$NON-NLS-1$ diff --git a/tools/gnu/classpath/tools/jarsigner/jarsigner.txt b/tools/gnu/classpath/tools/jarsigner/jarsigner.txt deleted file mode 100644 index e615609c1..000000000 --- a/tools/gnu/classpath/tools/jarsigner/jarsigner.txt +++ /dev/null @@ -1,116 +0,0 @@ -NAME - jarsigner - Java ARchive (JAR) file signing and verification tool - -SYNOPSIS - jarsigner [OPTION]... FILE ALIAS - jarsigner -verify [OPTION]... FILE - -DESCRIPTION - When the first form is used, the tool signs the designated JAR file. - The second form, on the other hand, is used to verify a previously - signed JAR file. - - FILE is the .JAR file to process; i.e. to sign if the first syntax form - is used, or to verify if the second syntax form is used instead. - - ALIAS must be a known Alias of a Key Entry in the designated key store. - The private key material associated with this Alias is then used for - signing the designated .JAR file. - -SIGNING OPTIONS - -keystore URL - Use this option to specify the location of the key store to use. - The default value is a file URL referencing the file named - ".keystore" (all lower case and without the enclosing quotes) - located in the path returned by the call to - java.lang.System#getProperty(String) using "user.home" as - argument. - - If a URL was specified, but was found to be malformed --e.g. - missing protocol element-- the tool will attempt to use the URL - value as a file-name (with absolute or relative path-name) of a - key store --as if the protocol was "file:". - - -storetype STORE_TYPE - Use this option to specify the type of the key store to use. - The default value, if this option is omitted, is that of the - property "keystore.type" in the security properties file, which - is obtained by invoking the static method call getDefaultType() - in java.security.KeyStore. - - -storepass PASSWORD - Use this option to specify the password which will be used to - unlock the key store. If this option is missing, the User will - be prompted to provide a password. - - -keypass PASSWORD - Use this option to specify the password which the tool will use - to unlock the Key Entry associated with the designated Alias. - - If this option is omitted, the tool will first attempt to unlock - the Key Entry using the same password protecting the key store. - If this fails, you will then be prompted to provide a password. - - -sigfile NAME - Use this option to designate a literal that will be used to - construct file names for both the .SF and .DSA signature files. - These files will be generated, by the tool, and placed in the - META-INF directory of the signed JAR. Permissible characters - for NAME must be in the range "a-zA-Z0-9_-". All characters - will be converted to upper-case ones. - - If this option is missing, the first eight characters of the - ALIAS argument will be used. When this is the case, any - character in ALIAS that is outside the permissible range of - characters will be replaced by an underscore. - - -signedjar FILE_NAME - Use this option to specify the file name of the signed JAR. If - this option is omitted, then the signed JAR will be named the - same as FILE; i.e. the input JAR file will be replaced with the - signed copy. - -VERIFICATION OPTIONS - -verify - Use this option to indicate that the tool is to be used for - verification purposes. - - -certs This option is used in conjunction with the -verbose option. - When present, along with the -verbose option, the tool will - print more detailed information about the certificates of the - signer(s) being processed. - -COMMON OPTIONS - -verbose - Use this option to force the tool to generate more verbose - messages, during its processing. - - -internalsf - When present, the tool will include --which otherwise it does - not-- the .SF file in the .DSA generated file. - - -sectionsonly - When present, the tool will include in the .SF generated file - --which otherwise it does not-- a header containing a hash of - the whole manifest file. When that header is included, the - tool can quickly check, during verification, if the hash (in - the header) matches or not the manifest file. - - -provider PROVIDER_CLASS_NAME - A fully qualified class name of a Security Provider to add to - the current list of Security Providers already installed in the - JVM in-use. If a provider class is specified with this option, - and was successfully added to the runtime --i.e. it was not - already installed-- then the tool will attempt to remove this - Security Provider before exiting. - - -help Prints this help text. - -REPORTING BUGS - Please report bugs at http://www.gnu.org/software/classpath/bugs.html - -COPYRIGHT - Copyright (C) 2006 Free Software Foundation, Inc. - This is free software; see the source for copying conditions. There is - NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. |