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.File;
22 import java.io.FileNotFoundException;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.text.MessageFormat;
26 import java.util.MissingResourceException;
27 import java.util.ResourceBundle;
28 import net.jini.url.httpmd.Handler;
29 import net.jini.url.httpmd.HttpmdUtil;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public class ComputeHttpmdCodebase {
61 private static ResourceBundle resources;
62 private static boolean resinit = false;
63
64 private ComputeHttpmdCodebase() { }
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public static void main(String[] args) {
87 if (args.length < 2) {
88 print("computecodebase.usage", (String) null);
89 System.exit(1);
90 }
91 Handler handler = new Handler();
92 StringBuffer codebase = new StringBuffer();
93 try {
94 boolean isURL;
95 URL base;
96 try {
97 base = new URL(args[0].endsWith("/") ?
98 args[0] : args[0] + "/");
99 isURL = true;
100 } catch (MalformedURLException e) {
101 File sourceDirectory = new File(args[0]);
102 if (!sourceDirectory.isDirectory()) {
103 print("computecodebase.notdir", args[0]);
104 System.exit(1);
105 }
106 base = sourceDirectory.toURI().toURL();
107 isURL = false;
108 }
109 for (int i = 1; i < args.length; i++) {
110 String spec = args[i];
111 if (!"httpmd:".regionMatches(true, 0, spec, 0, 7)) {
112 print("computecodebase.nonhttpmd", spec);
113 System.exit(1);
114 }
115 URL url;
116 try {
117 url = new URL(null, spec, handler);
118 } catch (MalformedURLException e) {
119 print("computecodebase.badurl",
120 new String[]{spec, e.getLocalizedMessage()});
121 System.exit(1);
122 return;
123 }
124 String path = url.getPath();
125 int paramIndex = path.lastIndexOf(';');
126 int equalsIndex = path.indexOf('=', paramIndex);
127 int commentIndex = path.indexOf(',', equalsIndex);
128 String algorithm = path.substring(paramIndex + 1, equalsIndex);
129 URL source =
130 new URL(base,
131 path.substring(path.startsWith("/") ? 1 : 0,
132 path.indexOf(';')));
133 String digest;
134 try {
135 digest = HttpmdUtil.computeDigest(source, algorithm);
136 } catch (FileNotFoundException e) {
137 print("computecodebase.notfound",
138 isURL ? source.toExternalForm() : source.getPath());
139 System.exit(1);
140 return;
141 }
142 URL result = new URL(
143 url,
144 path.substring(0, equalsIndex + 1) + digest +
145 (commentIndex < 0 ? "" : path.substring(commentIndex)) +
146 (url.getQuery() == null ? "" : '?' + url.getQuery()) +
147 (url.getRef() == null ? "" : '#' + url.getRef()));
148 if (codebase.length() > 0) {
149 codebase.append(' ');
150 }
151 codebase.append(result);
152 }
153 } catch (Throwable t) {
154 t.printStackTrace();
155 System.exit(1);
156 }
157 System.out.println(codebase);
158 }
159
160 private static synchronized String getString(String key) {
161 try {
162 if (!resinit) {
163 resources = ResourceBundle.getBundle(
164 "org.apache.river.tool.resources.computecodebase");
165 resinit = true;
166 }
167 return resources.getString(key);
168 } catch (MissingResourceException e) {
169 e.printStackTrace();
170 System.err.println("Unable to find a required resource.");
171 System.exit(1);
172 return null;
173 }
174 }
175
176 private static void print(String key, String val) {
177 print(key, new String[]{val});
178 }
179
180 private static void print(String key, String[] vals) {
181 String fmt = getString(key);
182 if (fmt == null)
183 fmt = "no text found: \"" + key + "\" {0}";
184 System.err.println(MessageFormat.format(fmt, vals));
185 }
186 }