1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class ComputeDigest {
51 private static ResourceBundle resources;
52 private static boolean resinit = false;
53
54 private ComputeDigest() { }
55
56
57
58
59
60
61
62
63
64
65
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 }