Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/sun/rmi/rmic/BatchEnvironment.java
+++ new/src/share/classes/sun/rmi/rmic/BatchEnvironment.java
1 1 /*
2 2 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26 /*****************************************************************************/
27 27 /* Copyright (c) IBM Corporation 1998 */
28 28 /* */
29 29 /* (C) Copyright IBM Corp. 1998 */
30 30 /* */
31 31 /*****************************************************************************/
32 32
33 33 package sun.rmi.rmic;
34 34
35 35 import java.io.File;
36 36 import java.io.IOException;
37 37 import java.io.OutputStream;
38 38 import java.util.Collection;
39 39 import java.util.Enumeration;
40 40 import java.util.Iterator;
41 41 import java.util.LinkedHashSet;
42 42 import java.util.StringTokenizer;
43 43 import java.util.Vector;
44 44 import java.util.jar.JarFile;
45 45 import java.util.jar.Manifest;
46 46 import java.util.jar.Attributes;
47 47 import sun.tools.java.ClassPath;
48 48
49 49 /**
50 50 * BatchEnvironment for rmic extends javac's version in four ways:
51 51 * 1. It overrides errorString() to handle looking for rmic-specific
52 52 * error messages in rmic's resource bundle
53 53 * 2. It provides a mechanism for recording intermediate generated
54 54 * files so that they can be deleted later.
55 55 * 3. It holds a reference to the Main instance so that generators
56 56 * can refer to it.
57 57 * 4. It provides access to the ClassPath passed to the constructor.
58 58 *
59 59 * WARNING: The contents of this source file are not part of any
60 60 * supported API. Code that depends on them does so at its own risk:
61 61 * they are subject to change or removal without notice.
62 62 */
63 63
64 64 public class BatchEnvironment extends sun.tools.javac.BatchEnvironment {
65 65
66 66 /** instance of Main which created this environment */
67 67 private Main main;
68 68
69 69 /**
70 70 * Create a ClassPath object for rmic from a class path string.
71 71 */
72 72 public static ClassPath createClassPath(String classPathString) {
73 73 ClassPath[] paths = classPaths(null, classPathString, null, null);
74 74 return paths[1];
75 75 }
76 76
77 77 /**
78 78 * Create a ClassPath object for rmic from the relevant command line
79 79 * options for class path, boot class path, and extension directories.
80 80 */
81 81 public static ClassPath createClassPath(String classPathString,
82 82 String sysClassPathString,
83 83 String extDirsString)
84 84 {
85 85 /**
86 86 * Previously, this method delegated to the
87 87 * sun.tools.javac.BatchEnvironment.classPaths method in order
88 88 * to supply default values for paths not specified on the
89 89 * command line, expand extensions directories into specific
90 90 * JAR files, and construct the ClassPath object-- but as part
91 91 * of the fix for 6473331, which adds support for Class-Path
92 92 * manifest entries in JAR files, those steps are now handled
93 93 * here directly, with the help of a Path utility class copied
94 94 * from the new javac implementation (see below).
95 95 */
96 96 Path path = new Path();
97 97
98 98 if (sysClassPathString == null) {
99 99 sysClassPathString = System.getProperty("sun.boot.class.path");
100 100 }
101 101 if (sysClassPathString != null) {
102 102 path.addFiles(sysClassPathString);
103 103 }
104 104
105 105 /*
106 106 * Class-Path manifest entries are supported for JAR files
107 107 * everywhere except in the boot class path.
108 108 */
109 109 path.expandJarClassPaths(true);
110 110
111 111 if (extDirsString == null) {
112 112 extDirsString = System.getProperty("java.ext.dirs");
113 113 }
114 114 if (extDirsString != null) {
115 115 path.addDirectories(extDirsString);
116 116 }
117 117
118 118 /*
119 119 * In the application class path, an empty element means
120 120 * the current working directory.
121 121 */
122 122 path.emptyPathDefault(".");
123 123
124 124 if (classPathString == null) {
125 125 // The env.class.path property is the user's CLASSPATH
126 126 // environment variable, and it set by the wrapper (ie,
127 127 // javac.exe).
128 128 classPathString = System.getProperty("env.class.path");
129 129 if (classPathString == null) {
130 130 classPathString = ".";
131 131 }
132 132 }
133 133 path.addFiles(classPathString);
134 134
135 135 return new ClassPath(path.toArray(new String[path.size()]));
136 136 }
137 137
138 138 /**
139 139 * Create a BatchEnvironment for rmic with the given class path,
140 140 * stream for messages and Main.
141 141 */
142 142 public BatchEnvironment(OutputStream out, ClassPath path, Main main) {
143 143 super(out, new ClassPath(""), path);
144 144 // use empty "sourcePath" (see 4666958)
145 145 this.main = main;
146 146 }
147 147
148 148 /**
149 149 * Get the instance of Main which created this environment.
150 150 */
151 151 public Main getMain() {
152 152 return main;
↓ open down ↓ |
152 lines elided |
↑ open up ↑ |
153 153 }
154 154
155 155 /**
156 156 * Get the ClassPath.
157 157 */
158 158 public ClassPath getClassPath() {
159 159 return binaryPath;
160 160 }
161 161
162 162 /** list of generated source files created in this environment */
163 - private Vector generatedFiles = new Vector();
163 + private Vector<File> generatedFiles = new Vector<>();
164 164
165 165 /**
166 166 * Remember a generated source file generated so that it
167 167 * can be removed later, if appropriate.
168 168 */
169 169 public void addGeneratedFile(File file) {
170 170 generatedFiles.addElement(file);
171 171 }
172 172
173 173 /**
174 174 * Delete all the generated source files made during the execution
175 175 * of this environment (those that have been registered with the
176 176 * "addGeneratedFile" method).
177 177 */
178 178 public void deleteGeneratedFiles() {
179 179 synchronized(generatedFiles) {
180 - Enumeration enumeration = generatedFiles.elements();
180 + Enumeration<File> enumeration = generatedFiles.elements();
181 181 while (enumeration.hasMoreElements()) {
182 - File file = (File) enumeration.nextElement();
182 + File file = enumeration.nextElement();
183 183 file.delete();
184 184 }
185 185 generatedFiles.removeAllElements();
186 186 }
187 187 }
188 188
189 189 /**
190 190 * Release resources, if any.
191 191 */
192 192 public void shutdown() {
193 193 main = null;
194 194 generatedFiles = null;
195 195 super.shutdown();
196 196 }
197 197
198 198 /**
199 199 * Return the formatted, localized string for a named error message
200 200 * and supplied arguments. For rmic error messages, with names that
201 201 * being with "rmic.", look up the error message in rmic's resource
202 202 * bundle; otherwise, defer to java's superclass method.
203 203 */
204 204 public String errorString(String err,
205 205 Object arg0, Object arg1, Object arg2)
206 206 {
207 207 if (err.startsWith("rmic.") || err.startsWith("warn.rmic.")) {
208 208 String result = Main.getText(err,
209 209 (arg0 != null ? arg0.toString() : null),
210 210 (arg1 != null ? arg1.toString() : null),
211 211 (arg2 != null ? arg2.toString() : null));
212 212
213 213 if (err.startsWith("warn.")) {
214 214 result = "warning: " + result;
215 215 }
216 216 return result;
217 217 } else {
218 218 return super.errorString(err, arg0, arg1, arg2);
219 219 }
220 220 }
221 221 public void reset() {
222 222 }
223 223
224 224 /**
225 225 * Utility for building paths of directories and JAR files. This
226 226 * class was copied from com.sun.tools.javac.util.Paths as part of
227 227 * the fix for 6473331, which adds support for Class-Path manifest
228 228 * entries in JAR files. Diagnostic code is simply commented out
229 229 * because rmic silently ignored these conditions historically.
230 230 */
231 231 private static class Path extends LinkedHashSet<String> {
232 232 private static final long serialVersionUID = 0;
233 233 private static final boolean warn = false;
234 234
235 235 private static class PathIterator implements Collection<String> {
236 236 private int pos = 0;
237 237 private final String path;
238 238 private final String emptyPathDefault;
239 239
240 240 public PathIterator(String path, String emptyPathDefault) {
241 241 this.path = path;
242 242 this.emptyPathDefault = emptyPathDefault;
243 243 }
244 244 public PathIterator(String path) { this(path, null); }
245 245 public Iterator<String> iterator() {
246 246 return new Iterator<String>() {
247 247 public boolean hasNext() {
248 248 return pos <= path.length();
249 249 }
250 250 public String next() {
251 251 int beg = pos;
252 252 int end = path.indexOf(File.pathSeparator, beg);
253 253 if (end == -1)
254 254 end = path.length();
255 255 pos = end + 1;
256 256
257 257 if (beg == end && emptyPathDefault != null)
258 258 return emptyPathDefault;
259 259 else
260 260 return path.substring(beg, end);
261 261 }
262 262 public void remove() {
263 263 throw new UnsupportedOperationException();
264 264 }
265 265 };
266 266 }
267 267
268 268 // required for Collection.
269 269 public int size() {
270 270 throw new UnsupportedOperationException();
271 271 }
272 272 public boolean isEmpty() {
273 273 throw new UnsupportedOperationException();
274 274 }
275 275 public boolean contains(Object o) {
276 276 throw new UnsupportedOperationException();
277 277 }
278 278 public Object[] toArray() {
279 279 throw new UnsupportedOperationException();
280 280 }
281 281 public <T> T[] toArray(T[] a) {
282 282 throw new UnsupportedOperationException();
283 283 }
284 284 public boolean add(String o) {
285 285 throw new UnsupportedOperationException();
286 286 }
287 287 public boolean remove(Object o) {
288 288 throw new UnsupportedOperationException();
289 289 }
290 290 public boolean containsAll(Collection<?> c) {
291 291 throw new UnsupportedOperationException();
292 292 }
293 293 public boolean addAll(Collection<? extends String> c) {
294 294 throw new UnsupportedOperationException();
295 295 }
296 296 public boolean removeAll(Collection<?> c) {
297 297 throw new UnsupportedOperationException();
298 298 }
299 299 public boolean retainAll(Collection<?> c) {
300 300 throw new UnsupportedOperationException();
301 301 }
302 302 public void clear() {
303 303 throw new UnsupportedOperationException();
304 304 }
305 305 public boolean equals(Object o) {
306 306 throw new UnsupportedOperationException();
307 307 }
308 308 public int hashCode() {
309 309 throw new UnsupportedOperationException();
310 310 }
311 311 }
312 312
313 313 /** Is this the name of a zip file? */
314 314 private static boolean isZip(String name) {
315 315 return new File(name).isFile();
316 316 }
317 317
318 318 private boolean expandJarClassPaths = false;
319 319
320 320 public Path expandJarClassPaths(boolean x) {
321 321 expandJarClassPaths = x;
322 322 return this;
323 323 }
324 324
325 325 /** What to use when path element is the empty string */
326 326 private String emptyPathDefault = null;
327 327
328 328 public Path emptyPathDefault(String x) {
329 329 emptyPathDefault = x;
330 330 return this;
331 331 }
332 332
333 333 public Path() { super(); }
334 334
335 335 public Path addDirectories(String dirs, boolean warn) {
336 336 if (dirs != null)
337 337 for (String dir : new PathIterator(dirs))
338 338 addDirectory(dir, warn);
339 339 return this;
340 340 }
341 341
342 342 public Path addDirectories(String dirs) {
343 343 return addDirectories(dirs, warn);
344 344 }
345 345
346 346 private void addDirectory(String dir, boolean warn) {
347 347 if (! new File(dir).isDirectory()) {
348 348 // if (warn)
349 349 // log.warning(Position.NOPOS,
350 350 // "dir.path.element.not.found", dir);
351 351 return;
352 352 }
353 353
354 354 for (String direntry : new File(dir).list()) {
355 355 String canonicalized = direntry.toLowerCase();
356 356 if (canonicalized.endsWith(".jar") ||
357 357 canonicalized.endsWith(".zip"))
358 358 addFile(dir + File.separator + direntry, warn);
359 359 }
360 360 }
361 361
362 362 public Path addFiles(String files, boolean warn) {
363 363 if (files != null)
364 364 for (String file : new PathIterator(files, emptyPathDefault))
365 365 addFile(file, warn);
366 366 return this;
367 367 }
368 368
369 369 public Path addFiles(String files) {
370 370 return addFiles(files, warn);
371 371 }
372 372
373 373 private void addFile(String file, boolean warn) {
374 374 if (contains(file)) {
375 375 /* Discard duplicates and avoid infinite recursion */
376 376 return;
377 377 }
378 378
379 379 File ele = new File(file);
380 380 if (! ele.exists()) {
381 381 /* No such file or directory exist */
382 382 if (warn)
383 383 // log.warning(Position.NOPOS,
384 384 // "path.element.not.found", file);
385 385 return;
386 386 }
387 387
388 388 if (ele.isFile()) {
389 389 /* File is an ordinay file */
390 390 String arcname = file.toLowerCase();
391 391 if (! (arcname.endsWith(".zip") ||
392 392 arcname.endsWith(".jar"))) {
393 393 /* File name don't have right extension */
394 394 // if (warn)
395 395 // log.warning(Position.NOPOS,
396 396 // "invalid.archive.file", file);
397 397 return;
398 398 }
399 399 }
400 400
401 401 /* Now what we have left is either a directory or a file name
402 402 confirming to archive naming convention */
403 403
404 404 super.add(file);
405 405 if (expandJarClassPaths && isZip(file))
406 406 addJarClassPath(file, warn);
407 407 }
408 408
409 409 // Adds referenced classpath elements from a jar's Class-Path
410 410 // Manifest entry. In some future release, we may want to
411 411 // update this code to recognize URLs rather than simple
412 412 // filenames, but if we do, we should redo all path-related code.
413 413 private void addJarClassPath(String jarFileName, boolean warn) {
414 414 try {
415 415 String jarParent = new File(jarFileName).getParent();
416 416 JarFile jar = new JarFile(jarFileName);
417 417
418 418 try {
419 419 Manifest man = jar.getManifest();
420 420 if (man == null) return;
421 421
422 422 Attributes attr = man.getMainAttributes();
423 423 if (attr == null) return;
424 424
425 425 String path = attr.getValue(Attributes.Name.CLASS_PATH);
426 426 if (path == null) return;
427 427
428 428 for (StringTokenizer st = new StringTokenizer(path);
429 429 st.hasMoreTokens();) {
430 430 String elt = st.nextToken();
431 431 if (jarParent != null)
432 432 elt = new File(jarParent, elt).getCanonicalPath();
433 433 addFile(elt, warn);
434 434 }
435 435 } finally {
436 436 jar.close();
437 437 }
438 438 } catch (IOException e) {
439 439 // log.error(Position.NOPOS,
440 440 // "error.reading.file", jarFileName,
441 441 // e.getLocalizedMessage());
442 442 }
443 443 }
444 444 }
445 445 }
↓ open down ↓ |
253 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX