64 } else if (path2 == null || path2.length() == 0) {
65 return path1;
66 } else {
67 return path1 + File.pathSeparator + path2;
68 }
69 }
70
71 public DocletInvoker(Messager messager,
72 String docletClassName, String docletPath,
73 ClassLoader docletParentClassLoader) {
74 this.messager = messager;
75 this.docletClassName = docletClassName;
76
77 // construct class loader
78 String cpString = null; // make sure env.class.path defaults to dot
79
80 // do prepends to get correct ordering
81 cpString = appendPath(System.getProperty("env.class.path"), cpString);
82 cpString = appendPath(System.getProperty("java.class.path"), cpString);
83 cpString = appendPath(docletPath, cpString);
84 URL[] urls = pathToURLs(cpString);
85 if (docletParentClassLoader == null)
86 appClassLoader = new URLClassLoader(urls, getDelegationClassLoader(docletClassName));
87 else
88 appClassLoader = new URLClassLoader(urls, docletParentClassLoader);
89
90 // attempt to find doclet
91 Class<?> dc = null;
92 try {
93 dc = appClassLoader.loadClass(docletClassName);
94 } catch (ClassNotFoundException exc) {
95 messager.error(null, "main.doclet_class_not_found", docletClassName);
96 messager.exit();
97 }
98 docletClass = dc;
99 }
100
101 /*
102 * Returns the delegation class loader to use when creating
103 * appClassLoader (used to load the doclet). The context class
104 * loader is the best choice, but legacy behavior was to use the
296 docletClassName, methodName);
297 throw new DocletInvokeException();
298 } catch (NullPointerException exc) {
299 messager.error(null, "main.internal_error_exception_thrown",
300 docletClassName, methodName, exc.toString());
301 throw new DocletInvokeException();
302 } catch (InvocationTargetException exc) {
303 Throwable err = exc.getTargetException();
304 if (err instanceof java.lang.OutOfMemoryError) {
305 messager.error(null, "main.out.of.memory");
306 } else {
307 messager.error(null, "main.exception_thrown",
308 docletClassName, methodName, exc.toString());
309 exc.getTargetException().printStackTrace();
310 }
311 throw new DocletInvokeException();
312 } finally {
313 Thread.currentThread().setContextClassLoader(savedCCL);
314 }
315 }
316
317 /**
318 * Utility method for converting a search path string to an array
319 * of directory and JAR file URLs.
320 *
321 * @param path the search path string
322 * @return the resulting array of directory and JAR file URLs
323 */
324 static URL[] pathToURLs(String path) {
325 StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
326 URL[] urls = new URL[st.countTokens()];
327 int count = 0;
328 while (st.hasMoreTokens()) {
329 URL url = fileToURL(new File(st.nextToken()));
330 if (url != null) {
331 urls[count++] = url;
332 }
333 }
334 if (urls.length != count) {
335 URL[] tmp = new URL[count];
336 System.arraycopy(urls, 0, tmp, 0, count);
337 urls = tmp;
338 }
339 return urls;
340 }
341
342 /**
343 * Returns the directory or JAR file URL corresponding to the specified
344 * local file name.
345 *
346 * @param file the File object
347 * @return the resulting directory or JAR file URL, or null if unknown
348 */
349 static URL fileToURL(File file) {
350 String name;
351 try {
352 name = file.getCanonicalPath();
353 } catch (IOException e) {
354 name = file.getAbsolutePath();
355 }
356 name = name.replace(File.separatorChar, '/');
357 if (!name.startsWith("/")) {
358 name = "/" + name;
359 }
360 // If the file does not exist, then assume that it's a directory
361 if (!file.isFile()) {
362 name = name + "/";
363 }
364 try {
365 return new URL("file", "", name);
366 } catch (MalformedURLException e) {
367 throw new IllegalArgumentException("file");
368 }
369 }
370 }
|
64 } else if (path2 == null || path2.length() == 0) {
65 return path1;
66 } else {
67 return path1 + File.pathSeparator + path2;
68 }
69 }
70
71 public DocletInvoker(Messager messager,
72 String docletClassName, String docletPath,
73 ClassLoader docletParentClassLoader) {
74 this.messager = messager;
75 this.docletClassName = docletClassName;
76
77 // construct class loader
78 String cpString = null; // make sure env.class.path defaults to dot
79
80 // do prepends to get correct ordering
81 cpString = appendPath(System.getProperty("env.class.path"), cpString);
82 cpString = appendPath(System.getProperty("java.class.path"), cpString);
83 cpString = appendPath(docletPath, cpString);
84 URL[] urls = com.sun.tools.javac.file.Paths.pathToURLs(cpString);
85 if (docletParentClassLoader == null)
86 appClassLoader = new URLClassLoader(urls, getDelegationClassLoader(docletClassName));
87 else
88 appClassLoader = new URLClassLoader(urls, docletParentClassLoader);
89
90 // attempt to find doclet
91 Class<?> dc = null;
92 try {
93 dc = appClassLoader.loadClass(docletClassName);
94 } catch (ClassNotFoundException exc) {
95 messager.error(null, "main.doclet_class_not_found", docletClassName);
96 messager.exit();
97 }
98 docletClass = dc;
99 }
100
101 /*
102 * Returns the delegation class loader to use when creating
103 * appClassLoader (used to load the doclet). The context class
104 * loader is the best choice, but legacy behavior was to use the
296 docletClassName, methodName);
297 throw new DocletInvokeException();
298 } catch (NullPointerException exc) {
299 messager.error(null, "main.internal_error_exception_thrown",
300 docletClassName, methodName, exc.toString());
301 throw new DocletInvokeException();
302 } catch (InvocationTargetException exc) {
303 Throwable err = exc.getTargetException();
304 if (err instanceof java.lang.OutOfMemoryError) {
305 messager.error(null, "main.out.of.memory");
306 } else {
307 messager.error(null, "main.exception_thrown",
308 docletClassName, methodName, exc.toString());
309 exc.getTargetException().printStackTrace();
310 }
311 throw new DocletInvokeException();
312 } finally {
313 Thread.currentThread().setContextClassLoader(savedCCL);
314 }
315 }
316 }
|