1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.river.tool;
20
21 import java.io.FileNotFoundException;
22 import java.net.URL;
23 import java.security.NoSuchAlgorithmException;
24 import java.text.MessageFormat;
25 import java.util.MissingResourceException;
26 import java.util.ResourceBundle;
27 import net.jini.url.httpmd.HttpmdUtil;
28
29 /**
30 * Prints the message digest for the contents of a URL. This utility is run
31 * from the {@linkplain #main command line}. <p>
32 *
33 * An example command line (shown with lines wrapped for readability) is:
34 *
35 * <blockquote>
36 * <pre>
37 * java -jar <var><b>install_dir</b></var>/lib/computedigest.jar \
38 * <var><b>install_dir</b></var>/lib/reggie.jar \
39 * SHA-1
40 * </pre>
41 * </blockquote>
42 *
43 * where <var><b>install_dir</b></var> is the directory where the JGDMS release
44 * is installed. This command prints out the message digest for the
45 * <code>reggie.jar</code> JAR file, using the <code>SHA-1</code> algorithm.
46 *
47 * @author Sun Microsystems, Inc.
48 * @since 2.0
49 */
50 public class ComputeDigest {
51 private static ResourceBundle resources;
52 private static boolean resinit = false;
53
54 private ComputeDigest() { }
55
56 /**
57 * Prints the message digest for the contents of a URL. The command
58 * line arguments are:
59 * <pre>
60 * <var><b>url</b></var> [ <var><b>algorithm</b></var> ]
61 * </pre>
62 * The first argument specifies the URL, which is parsed in the context
63 * of a <code>file:</code> URL. The second argument, if present,
64 * specifies the message digest algorithm, which defaults to
65 * <code>SHA-1</code>.
66 */
67 public static void main(String[] args) {
68 if (args.length < 1 || args.length > 2) {
69 print("computedigest.usage", null);
70 System.exit(1);
71 }
72 String algorithm = args.length > 1 ? args[1] : "SHA-1";
73 try {
74 URL url = new URL(new URL("file:"), args[0]);
75 System.out.println(HttpmdUtil.computeDigest(url, algorithm));
76 return;
77 } catch (FileNotFoundException e) {
78 print("computedigest.notfound", args[0]);
79 } catch (NoSuchAlgorithmException e) {
80 print("computedigest.badalg", algorithm);
81 } catch (Throwable t) {
82 t.printStackTrace();
83 }
84 System.exit(1);
85 }
86
87 private static synchronized String getString(String key) {
88 try {
89 if (!resinit) {
90 resources = ResourceBundle.getBundle(
91 "org.apache.river.tool.resources.computedigest");
92 resinit = true;
93 }
94 return resources.getString(key);
95 } catch (MissingResourceException e) {
96 e.printStackTrace();
97 System.err.println("Unable to find a required resource.");
98 System.exit(1);
99 return null;
100 }
101 }
102
103 private static void print(String key, String val) {
104 String fmt = getString(key);
105 if (fmt == null)
106 fmt = "no text found: \"" + key + "\" {0}";
107 System.err.println(MessageFormat.format(fmt, new String[]{val}));
108 }
109 }