source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/AntClassLoader.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 46.7 KB
Line 
1/*
2 * Copyright 2000-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant;
18
19import java.io.ByteArrayOutputStream;
20import java.io.File;
21import java.io.FileInputStream;
22import java.io.IOException;
23import java.io.InputStream;
24import java.lang.reflect.Constructor;
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27import java.net.MalformedURLException;
28import java.net.URL;
29import java.util.Enumeration;
30import java.util.Hashtable;
31import java.util.Vector;
32import java.util.zip.ZipEntry;
33import java.util.zip.ZipFile;
34import org.apache.tools.ant.types.Path;
35import org.apache.tools.ant.util.CollectionUtils;
36import org.apache.tools.ant.util.FileUtils;
37import org.apache.tools.ant.util.JavaEnvUtils;
38import org.apache.tools.ant.util.LoaderUtils;
39
40/**
41 * Used to load classes within ant with a different classpath from
42 * that used to start ant. Note that it is possible to force a class
43 * into this loader even when that class is on the system classpath by
44 * using the forceLoadClass method. Any subsequent classes loaded by that
45 * class will then use this loader rather than the system class loader.
46 *
47 */
48public class AntClassLoader extends ClassLoader implements SubBuildListener {
49
50 private static final FileUtils FILE_UTILS = FileUtils.newFileUtils();
51
52 /**
53 * An enumeration of all resources of a given name found within the
54 * classpath of this class loader. This enumeration is used by the
55 * ClassLoader.findResources method, which is in
56 * turn used by the ClassLoader.getResources method.
57 *
58 * @see AntClassLoader#findResources(String)
59 * @see java.lang.ClassLoader#getResources(String)
60 */
61 private class ResourceEnumeration implements Enumeration {
62 /**
63 * The name of the resource being searched for.
64 */
65 private String resourceName;
66
67 /**
68 * The index of the next classpath element to search.
69 */
70 private int pathElementsIndex;
71
72 /**
73 * The URL of the next resource to return in the enumeration. If this
74 * field is <code>null</code> then the enumeration has been completed,
75 * i.e., there are no more elements to return.
76 */
77 private URL nextResource;
78
79 /**
80 * Constructs a new enumeration of resources of the given name found
81 * within this class loader's classpath.
82 *
83 * @param name the name of the resource to search for.
84 */
85 ResourceEnumeration(String name) {
86 this.resourceName = name;
87 this.pathElementsIndex = 0;
88 findNextResource();
89 }
90
91 /**
92 * Indicates whether there are more elements in the enumeration to
93 * return.
94 *
95 * @return <code>true</code> if there are more elements in the
96 * enumeration; <code>false</code> otherwise.
97 */
98 public boolean hasMoreElements() {
99 return (this.nextResource != null);
100 }
101
102 /**
103 * Returns the next resource in the enumeration.
104 *
105 * @return the next resource in the enumeration
106 */
107 public Object nextElement() {
108 URL ret = this.nextResource;
109 findNextResource();
110 return ret;
111 }
112
113 /**
114 * Locates the next resource of the correct name in the classpath and
115 * sets <code>nextResource</code> to the URL of that resource. If no
116 * more resources can be found, <code>nextResource</code> is set to
117 * <code>null</code>.
118 */
119 private void findNextResource() {
120 URL url = null;
121 while ((pathElementsIndex < pathComponents.size())
122 && (url == null)) {
123 try {
124 File pathComponent
125 = (File) pathComponents.elementAt(pathElementsIndex);
126 url = getResourceURL(pathComponent, this.resourceName);
127 pathElementsIndex++;
128 } catch (BuildException e) {
129 // ignore path elements which are not valid relative to the
130 // project
131 }
132 }
133 this.nextResource = url;
134 }
135 }
136
137 /**
138 * The size of buffers to be used in this classloader.
139 */
140 private static final int BUFFER_SIZE = 8192;
141 /**
142 * Number of array elements in a test array of strings
143 */
144 private static final int NUMBER_OF_STRINGS = 256;
145
146 /**
147 * The components of the classpath that the classloader searches
148 * for classes.
149 */
150 private Vector pathComponents = new Vector();
151
152 /**
153 * The project to which this class loader belongs.
154 */
155 private Project project;
156
157 /**
158 * Indicates whether the parent class loader should be
159 * consulted before trying to load with this class loader.
160 */
161 private boolean parentFirst = true;
162
163 /**
164 * These are the package roots that are to be loaded by the parent class
165 * loader regardless of whether the parent class loader is being searched
166 * first or not.
167 */
168 private Vector systemPackages = new Vector();
169
170 /**
171 * These are the package roots that are to be loaded by this class loader
172 * regardless of whether the parent class loader is being searched first
173 * or not.
174 */
175 private Vector loaderPackages = new Vector();
176
177 /**
178 * Whether or not this classloader will ignore the base
179 * classloader if it can't find a class.
180 *
181 * @see #setIsolated(boolean)
182 */
183 private boolean ignoreBase = false;
184
185 /**
186 * The parent class loader, if one is given or can be determined.
187 */
188 private ClassLoader parent = null;
189
190 /**
191 * A hashtable of zip files opened by the classloader (File to ZipFile).
192 */
193 private Hashtable zipFiles = new Hashtable();
194
195 /**
196 * The context loader saved when setting the thread's current
197 * context loader.
198 */
199 private ClassLoader savedContextLoader = null;
200 /**
201 * Whether or not the context loader is currently saved.
202 */
203 private boolean isContextLoaderSaved = false;
204
205 /**
206 * Reflection method reference for getProtectionDomain;
207 * used to avoid 1.1-compatibility problems.
208 */
209 private static Method getProtectionDomain = null;
210
211 /**
212 * Reflection method reference for defineClassProtectionDomain;
213 * used to avoid 1.1-compatibility problems.
214 */
215 private static Method defineClassProtectionDomain = null;
216
217
218 // Set up the reflection-based Java2 methods if possible
219 static {
220 try {
221 getProtectionDomain
222 = Class.class.getMethod("getProtectionDomain", new Class[0]);
223 Class protectionDomain
224 = Class.forName("java.security.ProtectionDomain");
225 Class[] args = new Class[] {String.class, byte[].class,
226 Integer.TYPE, Integer.TYPE, protectionDomain};
227 defineClassProtectionDomain
228 = ClassLoader.class.getDeclaredMethod("defineClass", args);
229 } catch (Exception e) {
230 // ignore failure to get access to 1.2+ methods
231 }
232 }
233
234
235 /**
236 * Create an Ant Class Loader
237 */
238 public AntClassLoader() {
239 setParent(null);
240 }
241
242 /**
243 * Creates a classloader for the given project using the classpath given.
244 *
245 * @param project The project to which this classloader is to belong.
246 * Must not be <code>null</code>.
247 * @param classpath The classpath to use to load the classes. This
248 * is combined with the system classpath in a manner
249 * determined by the value of ${build.sysclasspath}.
250 * May be <code>null</code>, in which case no path
251 * elements are set up to start with.
252 */
253 public AntClassLoader(Project project, Path classpath) {
254 setParent(null);
255 setProject(project);
256 setClassPath(classpath);
257 }
258
259 /**
260 * Creates a classloader for the given project using the classpath given.
261 *
262 * @param parent The parent classloader to which unsatisfied loading
263 * attempts are delegated. May be <code>null</code>,
264 * in which case the classloader which loaded this
265 * class is used as the parent.
266 * @param project The project to which this classloader is to belong.
267 * Must not be <code>null</code>.
268 * @param classpath the classpath to use to load the classes.
269 * May be <code>null</code>, in which case no path
270 * elements are set up to start with.
271 * @param parentFirst If <code>true</code>, indicates that the parent
272 * classloader should be consulted before trying to
273 * load the a class through this loader.
274 */
275 public AntClassLoader(ClassLoader parent, Project project, Path classpath,
276 boolean parentFirst) {
277 this(project, classpath);
278 if (parent != null) {
279 setParent(parent);
280 }
281 setParentFirst(parentFirst);
282 addJavaLibraries();
283 }
284
285
286 /**
287 * Creates a classloader for the given project using the classpath given.
288 *
289 * @param project The project to which this classloader is to belong.
290 * Must not be <code>null</code>.
291 * @param classpath The classpath to use to load the classes. May be
292 * <code>null</code>, in which case no path
293 * elements are set up to start with.
294 * @param parentFirst If <code>true</code>, indicates that the parent
295 * classloader should be consulted before trying to
296 * load the a class through this loader.
297 */
298 public AntClassLoader(Project project, Path classpath,
299 boolean parentFirst) {
300 this(null, project, classpath, parentFirst);
301 }
302
303 /**
304 * Creates an empty class loader. The classloader should be configured
305 * with path elements to specify where the loader is to look for
306 * classes.
307 *
308 * @param parent The parent classloader to which unsatisfied loading
309 * attempts are delegated. May be <code>null</code>,
310 * in which case the classloader which loaded this
311 * class is used as the parent.
312 * @param parentFirst If <code>true</code>, indicates that the parent
313 * classloader should be consulted before trying to
314 * load the a class through this loader.
315 */
316 public AntClassLoader(ClassLoader parent, boolean parentFirst) {
317 setParent(parent);
318 project = null;
319 this.parentFirst = parentFirst;
320 }
321
322 /**
323 * Set the project associated with this class loader
324 *
325 * @param project the project instance
326 */
327 public void setProject(Project project) {
328 this.project = project;
329 if (project != null) {
330 project.addBuildListener(this);
331 }
332 }
333
334 /**
335 * Set the classpath to search for classes to load. This should not be
336 * changed once the classloader starts to server classes
337 *
338 * @param classpath the search classpath consisting of directories and
339 * jar/zip files.
340 */
341 public void setClassPath(Path classpath) {
342 pathComponents.removeAllElements();
343 if (classpath != null) {
344 Path actualClasspath = classpath.concatSystemClasspath("ignore");
345 String[] pathElements = actualClasspath.list();
346 for (int i = 0; i < pathElements.length; ++i) {
347 try {
348 addPathElement(pathElements[i]);
349 } catch (BuildException e) {
350 // ignore path elements which are invalid
351 // relative to the project
352 }
353 }
354 }
355 }
356
357 /**
358 * Set the parent for this class loader. This is the class loader to which
359 * this class loader will delegate to load classes
360 *
361 * @param parent the parent class loader.
362 */
363 public void setParent(ClassLoader parent) {
364 if (parent == null) {
365 this.parent = AntClassLoader.class.getClassLoader();
366 } else {
367 this.parent = parent;
368 }
369 }
370
371 /**
372 * Control whether class lookup is delegated to the parent loader first
373 * or after this loader. Use with extreme caution. Setting this to
374 * false violates the class loader hierarchy and can lead to Linkage errors
375 *
376 * @param parentFirst if true, delegate initial class search to the parent
377 * classloader.
378 */
379 public void setParentFirst(boolean parentFirst) {
380 this.parentFirst = parentFirst;
381 }
382
383
384 /**
385 * Logs a message through the project object if one has been provided.
386 *
387 * @param message The message to log.
388 * Should not be <code>null</code>.
389 *
390 * @param priority The logging priority of the message.
391 */
392 protected void log(String message, int priority) {
393 if (project != null) {
394 project.log(message, priority);
395 }
396// else {
397// System.out.println(message);
398// }
399 }
400
401 /**
402 * Sets the current thread's context loader to this classloader, storing
403 * the current loader value for later resetting.
404 */
405 public void setThreadContextLoader() {
406 if (isContextLoaderSaved) {
407 throw new BuildException("Context loader has not been reset");
408 }
409 if (LoaderUtils.isContextLoaderAvailable()) {
410 savedContextLoader = LoaderUtils.getContextClassLoader();
411 ClassLoader loader = this;
412 if (project != null
413 && "only".equals(project.getProperty("build.sysclasspath"))) {
414 loader = this.getClass().getClassLoader();
415 }
416 LoaderUtils.setContextClassLoader(loader);
417 isContextLoaderSaved = true;
418 }
419 }
420
421 /**
422 * Resets the current thread's context loader to its original value.
423 */
424 public void resetThreadContextLoader() {
425 if (LoaderUtils.isContextLoaderAvailable()
426 && isContextLoaderSaved) {
427 LoaderUtils.setContextClassLoader(savedContextLoader);
428 savedContextLoader = null;
429 isContextLoaderSaved = false;
430 }
431 }
432
433
434 /**
435 * Adds an element to the classpath to be searched.
436 *
437 * @param pathElement The path element to add. Must not be
438 * <code>null</code>.
439 *
440 * @exception BuildException if the given path element cannot be resolved
441 * against the project.
442 */
443 public void addPathElement(String pathElement) throws BuildException {
444 File pathComponent
445 = project != null ? project.resolveFile(pathElement)
446 : new File(pathElement);
447 try {
448 addPathFile(pathComponent);
449 } catch (IOException e) {
450 throw new BuildException(e);
451 }
452 }
453
454 /**
455 * Add a file to the path
456 *
457 * @param pathComponent the file which is to be added to the path for
458 * this class loader
459 *
460 * @throws IOException if data needed from the file cannot be read.
461 */
462 protected void addPathFile(File pathComponent) throws IOException {
463 pathComponents.addElement(pathComponent);
464 }
465
466 /**
467 * Returns the classpath this classloader will consult.
468 *
469 * @return the classpath used for this classloader, with elements
470 * separated by the path separator for the system.
471 */
472 public String getClasspath() {
473 StringBuffer sb = new StringBuffer();
474 boolean firstPass = true;
475 Enumeration componentEnum = pathComponents.elements();
476 while (componentEnum.hasMoreElements()) {
477 if (!firstPass) {
478 sb.append(System.getProperty("path.separator"));
479 } else {
480 firstPass = false;
481 }
482 sb.append(((File) componentEnum.nextElement()).getAbsolutePath());
483 }
484 return sb.toString();
485 }
486
487 /**
488 * Sets whether this classloader should run in isolated mode. In
489 * isolated mode, classes not found on the given classpath will
490 * not be referred to the parent class loader but will cause a
491 * ClassNotFoundException.
492 *
493 * @param isolated Whether or not this classloader should run in
494 * isolated mode.
495 */
496 public synchronized void setIsolated(boolean isolated) {
497 ignoreBase = isolated;
498 }
499
500 /**
501 * Forces initialization of a class in a JDK 1.1 compatible, albeit hacky
502 * way.
503 *
504 * @param theClass The class to initialize.
505 * Must not be <code>null</code>.
506 *
507 * @deprecated use Class.forName with initialize=true instead.
508 */
509 public static void initializeClass(Class theClass) {
510 // ***HACK*** We ask the VM to create an instance
511 // by voluntarily providing illegal arguments to force
512 // the VM to run the class' static initializer, while
513 // at the same time not running a valid constructor.
514
515 final Constructor[] cons = theClass.getDeclaredConstructors();
516 //At least one constructor is guaranteed to be there, but check anyway.
517 if (cons != null) {
518 if (cons.length > 0 && cons[0] != null) {
519 final String[] strs = new String[NUMBER_OF_STRINGS];
520 try {
521 cons[0].newInstance(strs);
522 // Expecting an exception to be thrown by this call:
523 // IllegalArgumentException: wrong number of Arguments
524 } catch (Throwable t) {
525 // Ignore - we are interested only in the side
526 // effect - that of getting the static initializers
527 // invoked. As we do not want to call a valid
528 // constructor to get this side effect, an
529 // attempt is made to call a hopefully
530 // invalid constructor - come on, nobody
531 // would have a constructor that takes in
532 // 256 String arguments ;-)
533 // (In fact, they can't - according to JVM spec
534 // section 4.10, the number of method parameters is limited
535 // to 255 by the definition of a method descriptor.
536 // Constructors count as methods here.)
537 }
538 }
539 }
540 }
541
542 /**
543 * Adds a package root to the list of packages which must be loaded on the
544 * parent loader.
545 *
546 * All subpackages are also included.
547 *
548 * @param packageRoot The root of all packages to be included.
549 * Should not be <code>null</code>.
550 */
551 public void addSystemPackageRoot(String packageRoot) {
552 systemPackages.addElement(packageRoot
553 + (packageRoot.endsWith(".") ? "" : "."));
554 }
555
556 /**
557 * Adds a package root to the list of packages which must be loaded using
558 * this loader.
559 *
560 * All subpackages are also included.
561 *
562 * @param packageRoot The root of all packages to be included.
563 * Should not be <code>null</code>.
564 */
565 public void addLoaderPackageRoot(String packageRoot) {
566 loaderPackages.addElement(packageRoot
567 + (packageRoot.endsWith(".") ? "" : "."));
568 }
569
570 /**
571 * Loads a class through this class loader even if that class is available
572 * on the parent classpath.
573 *
574 * This ensures that any classes which are loaded by the returned class
575 * will use this classloader.
576 *
577 * @param classname The name of the class to be loaded.
578 * Must not be <code>null</code>.
579 *
580 * @return the required Class object
581 *
582 * @exception ClassNotFoundException if the requested class does not exist
583 * on this loader's classpath.
584 */
585 public Class forceLoadClass(String classname)
586 throws ClassNotFoundException {
587 log("force loading " + classname, Project.MSG_DEBUG);
588
589 Class theClass = findLoadedClass(classname);
590
591 if (theClass == null) {
592 theClass = findClass(classname);
593 }
594
595 return theClass;
596 }
597
598 /**
599 * Loads a class through this class loader but defer to the parent class
600 * loader.
601 *
602 * This ensures that instances of the returned class will be compatible
603 * with instances which have already been loaded on the parent
604 * loader.
605 *
606 * @param classname The name of the class to be loaded.
607 * Must not be <code>null</code>.
608 *
609 * @return the required Class object
610 *
611 * @exception ClassNotFoundException if the requested class does not exist
612 * on this loader's classpath.
613 */
614 public Class forceLoadSystemClass(String classname)
615 throws ClassNotFoundException {
616 log("force system loading " + classname, Project.MSG_DEBUG);
617
618 Class theClass = findLoadedClass(classname);
619
620 if (theClass == null) {
621 theClass = findBaseClass(classname);
622 }
623
624 return theClass;
625 }
626
627 /**
628 * Returns a stream to read the requested resource name.
629 *
630 * @param name The name of the resource for which a stream is required.
631 * Must not be <code>null</code>.
632 *
633 * @return a stream to the required resource or <code>null</code> if the
634 * resource cannot be found on the loader's classpath.
635 */
636 public InputStream getResourceAsStream(String name) {
637
638 InputStream resourceStream = null;
639 if (isParentFirst(name)) {
640 resourceStream = loadBaseResource(name);
641 if (resourceStream != null) {
642 log("ResourceStream for " + name
643 + " loaded from parent loader", Project.MSG_DEBUG);
644
645 } else {
646 resourceStream = loadResource(name);
647 if (resourceStream != null) {
648 log("ResourceStream for " + name
649 + " loaded from ant loader", Project.MSG_DEBUG);
650 }
651 }
652 } else {
653 resourceStream = loadResource(name);
654 if (resourceStream != null) {
655 log("ResourceStream for " + name
656 + " loaded from ant loader", Project.MSG_DEBUG);
657
658 } else {
659 resourceStream = loadBaseResource(name);
660 if (resourceStream != null) {
661 log("ResourceStream for " + name
662 + " loaded from parent loader", Project.MSG_DEBUG);
663 }
664 }
665 }
666
667 if (resourceStream == null) {
668 log("Couldn't load ResourceStream for " + name,
669 Project.MSG_DEBUG);
670 }
671
672 return resourceStream;
673 }
674
675 /**
676 * Returns a stream to read the requested resource name from this loader.
677 *
678 * @param name The name of the resource for which a stream is required.
679 * Must not be <code>null</code>.
680 *
681 * @return a stream to the required resource or <code>null</code> if
682 * the resource cannot be found on the loader's classpath.
683 */
684 private InputStream loadResource(String name) {
685 // we need to search the components of the path to see if we can
686 // find the class we want.
687 InputStream stream = null;
688
689 Enumeration e = pathComponents.elements();
690 while (e.hasMoreElements() && stream == null) {
691 File pathComponent = (File) e.nextElement();
692 stream = getResourceStream(pathComponent, name);
693 }
694 return stream;
695 }
696
697 /**
698 * Finds a system resource (which should be loaded from the parent
699 * classloader).
700 *
701 * @param name The name of the system resource to load.
702 * Must not be <code>null</code>.
703 *
704 * @return a stream to the named resource, or <code>null</code> if
705 * the resource cannot be found.
706 */
707 private InputStream loadBaseResource(String name) {
708 if (parent == null) {
709 return getSystemResourceAsStream(name);
710 } else {
711 return parent.getResourceAsStream(name);
712 }
713 }
714
715 /**
716 * Returns an inputstream to a given resource in the given file which may
717 * either be a directory or a zip file.
718 *
719 * @param file the file (directory or jar) in which to search for the
720 * resource. Must not be <code>null</code>.
721 * @param resourceName The name of the resource for which a stream is
722 * required. Must not be <code>null</code>.
723 *
724 * @return a stream to the required resource or <code>null</code> if
725 * the resource cannot be found in the given file.
726 */
727 private InputStream getResourceStream(File file, String resourceName) {
728 try {
729 if (!file.exists()) {
730 return null;
731 }
732
733 if (file.isDirectory()) {
734 File resource = new File(file, resourceName);
735
736 if (resource.exists()) {
737 return new FileInputStream(resource);
738 }
739 } else {
740 // is the zip file in the cache
741 ZipFile zipFile = (ZipFile) zipFiles.get(file);
742 if (zipFile == null) {
743 zipFile = new ZipFile(file);
744 zipFiles.put(file, zipFile);
745 }
746 ZipEntry entry = zipFile.getEntry(resourceName);
747 if (entry != null) {
748 return zipFile.getInputStream(entry);
749 }
750 }
751 } catch (Exception e) {
752 log("Ignoring Exception " + e.getClass().getName()
753 + ": " + e.getMessage() + " reading resource " + resourceName
754 + " from " + file, Project.MSG_VERBOSE);
755 }
756
757 return null;
758 }
759
760 /**
761 * Tests whether or not the parent classloader should be checked for
762 * a resource before this one. If the resource matches both the
763 * "use parent classloader first" and the "use this classloader first"
764 * lists, the latter takes priority.
765 *
766 * @param resourceName The name of the resource to check.
767 * Must not be <code>null</code>.
768 *
769 * @return whether or not the parent classloader should be checked for a
770 * resource before this one is.
771 */
772 private boolean isParentFirst(String resourceName) {
773 // default to the global setting and then see
774 // if this class belongs to a package which has been
775 // designated to use a specific loader first
776 // (this one or the parent one)
777
778 // XXX - shouldn't this always return false in isolated mode?
779
780 boolean useParentFirst = parentFirst;
781
782 for (Enumeration e = systemPackages.elements(); e.hasMoreElements();) {
783 String packageName = (String) e.nextElement();
784 if (resourceName.startsWith(packageName)) {
785 useParentFirst = true;
786 break;
787 }
788 }
789
790 for (Enumeration e = loaderPackages.elements(); e.hasMoreElements();) {
791 String packageName = (String) e.nextElement();
792 if (resourceName.startsWith(packageName)) {
793 useParentFirst = false;
794 break;
795 }
796 }
797
798 return useParentFirst;
799 }
800
801 /**
802 * Finds the resource with the given name. A resource is
803 * some data (images, audio, text, etc) that can be accessed by class
804 * code in a way that is independent of the location of the code.
805 *
806 * @param name The name of the resource for which a stream is required.
807 * Must not be <code>null</code>.
808 *
809 * @return a URL for reading the resource, or <code>null</code> if the
810 * resource could not be found or the caller doesn't have
811 * adequate privileges to get the resource.
812 */
813 public URL getResource(String name) {
814 // we need to search the components of the path to see if
815 // we can find the class we want.
816 URL url = null;
817 if (isParentFirst(name)) {
818 url = (parent == null) ? super.getResource(name)
819 : parent.getResource(name);
820 }
821
822 if (url != null) {
823 log("Resource " + name + " loaded from parent loader",
824 Project.MSG_DEBUG);
825
826 } else {
827 // try and load from this loader if the parent either didn't find
828 // it or wasn't consulted.
829 Enumeration e = pathComponents.elements();
830 while (e.hasMoreElements() && url == null) {
831 File pathComponent = (File) e.nextElement();
832 url = getResourceURL(pathComponent, name);
833 if (url != null) {
834 log("Resource " + name
835 + " loaded from ant loader",
836 Project.MSG_DEBUG);
837 }
838 }
839 }
840
841 if (url == null && !isParentFirst(name)) {
842 // this loader was first but it didn't find it - try the parent
843
844 url = (parent == null) ? super.getResource(name)
845 : parent.getResource(name);
846 if (url != null) {
847 log("Resource " + name + " loaded from parent loader",
848 Project.MSG_DEBUG);
849 }
850 }
851
852 if (url == null) {
853 log("Couldn't load Resource " + name, Project.MSG_DEBUG);
854 }
855
856 return url;
857 }
858
859 /**
860 * Returns an enumeration of URLs representing all the resources with the
861 * given name by searching the class loader's classpath.
862 *
863 * @param name The resource name to search for.
864 * Must not be <code>null</code>.
865 * @return an enumeration of URLs for the resources
866 * @exception IOException if I/O errors occurs (can't happen)
867 */
868 protected Enumeration/*<URL>*/ findResources(String name) throws IOException {
869 Enumeration/*<URL>*/ mine = new ResourceEnumeration(name);
870 Enumeration/*<URL>*/ base;
871 if (parent != null && parent != getParent()) {
872 // Delegate to the parent:
873 base = parent.getResources(name);
874 // Note: could cause overlaps in case ClassLoader.this.parent has matches.
875 } else {
876 // ClassLoader.this.parent is already delegated to from ClassLoader.getResources, no need:
877 base = new CollectionUtils.EmptyEnumeration();
878 }
879 if (isParentFirst(name)) {
880 // Normal case.
881 return CollectionUtils.append(base, mine);
882 } else {
883 // Inverted.
884 return CollectionUtils.append(mine, base);
885 }
886 }
887
888 /**
889 * Returns the URL of a given resource in the given file which may
890 * either be a directory or a zip file.
891 *
892 * @param file The file (directory or jar) in which to search for
893 * the resource. Must not be <code>null</code>.
894 * @param resourceName The name of the resource for which a stream
895 * is required. Must not be <code>null</code>.
896 *
897 * @return a stream to the required resource or <code>null</code> if the
898 * resource cannot be found in the given file object.
899 */
900 protected URL getResourceURL(File file, String resourceName) {
901 try {
902 if (!file.exists()) {
903 return null;
904 }
905
906 if (file.isDirectory()) {
907 File resource = new File(file, resourceName);
908
909 if (resource.exists()) {
910 try {
911 return FILE_UTILS.getFileURL(resource);
912 } catch (MalformedURLException ex) {
913 return null;
914 }
915 }
916 } else {
917 ZipFile zipFile = (ZipFile) zipFiles.get(file);
918 if (zipFile == null) {
919 zipFile = new ZipFile(file);
920 zipFiles.put(file, zipFile);
921 }
922
923 ZipEntry entry = zipFile.getEntry(resourceName);
924 if (entry != null) {
925 try {
926 return new URL("jar:" + FILE_UTILS.getFileURL(file)
927 + "!/" + entry);
928 } catch (MalformedURLException ex) {
929 return null;
930 }
931 }
932 }
933 } catch (Exception e) {
934 e.printStackTrace();
935 }
936
937 return null;
938 }
939
940 /**
941 * Loads a class with this class loader.
942 *
943 * This class attempts to load the class in an order determined by whether
944 * or not the class matches the system/loader package lists, with the
945 * loader package list taking priority. If the classloader is in isolated
946 * mode, failure to load the class in this loader will result in a
947 * ClassNotFoundException.
948 *
949 * @param classname The name of the class to be loaded.
950 * Must not be <code>null</code>.
951 * @param resolve <code>true</code> if all classes upon which this class
952 * depends are to be loaded.
953 *
954 * @return the required Class object
955 *
956 * @exception ClassNotFoundException if the requested class does not exist
957 * on the system classpath (when not in isolated mode) or this loader's
958 * classpath.
959 */
960 protected synchronized Class loadClass(String classname, boolean resolve)
961 throws ClassNotFoundException {
962 // 'sync' is needed - otherwise 2 threads can load the same class
963 // twice, resulting in LinkageError: duplicated class definition.
964 // findLoadedClass avoids that, but without sync it won't work.
965
966 Class theClass = findLoadedClass(classname);
967 if (theClass != null) {
968 return theClass;
969 }
970
971 if (isParentFirst(classname)) {
972 try {
973 theClass = findBaseClass(classname);
974 log("Class " + classname + " loaded from parent loader "
975 + "(parentFirst)", Project.MSG_DEBUG);
976 } catch (ClassNotFoundException cnfe) {
977 theClass = findClass(classname);
978 log("Class " + classname + " loaded from ant loader "
979 + "(parentFirst)", Project.MSG_DEBUG);
980 }
981 } else {
982 try {
983 theClass = findClass(classname);
984 log("Class " + classname + " loaded from ant loader",
985 Project.MSG_DEBUG);
986 } catch (ClassNotFoundException cnfe) {
987 if (ignoreBase) {
988 throw cnfe;
989 }
990 theClass = findBaseClass(classname);
991 log("Class " + classname + " loaded from parent loader",
992 Project.MSG_DEBUG);
993 }
994 }
995
996 if (resolve) {
997 resolveClass(theClass);
998 }
999
1000 return theClass;
1001 }
1002
1003 /**
1004 * Converts the class dot notation to a filesystem equivalent for
1005 * searching purposes.
1006 *
1007 * @param classname The class name in dot format (eg java.lang.Integer).
1008 * Must not be <code>null</code>.
1009 *
1010 * @return the classname in filesystem format (eg java/lang/Integer.class)
1011 */
1012 private String getClassFilename(String classname) {
1013 return classname.replace('.', '/') + ".class";
1014 }
1015
1016 /**
1017 * Define a class given its bytes
1018 *
1019 * @param container the container from which the class data has been read
1020 * may be a directory or a jar/zip file.
1021 *
1022 * @param classData the bytecode data for the class
1023 * @param classname the name of the class
1024 *
1025 * @return the Class instance created from the given data
1026 *
1027 * @throws IOException if the class data cannot be read.
1028 */
1029 protected Class defineClassFromData(File container, byte[] classData,
1030 String classname) throws IOException {
1031 // Simply put:
1032 // defineClass(classname, classData, 0, classData.length,
1033 // Project.class.getProtectionDomain());
1034 // Made more elaborate to be 1.1-safe.
1035 if (defineClassProtectionDomain != null) {
1036 try {
1037 Object domain
1038 = getProtectionDomain.invoke(Project.class, new Object[0]);
1039 Object[] args
1040 = new Object[] {classname, classData, new Integer(0),
1041 new Integer(classData.length), domain};
1042 return (Class) defineClassProtectionDomain.invoke(this, args);
1043 } catch (InvocationTargetException ite) {
1044 Throwable t = ite.getTargetException();
1045 if (t instanceof ClassFormatError) {
1046 throw (ClassFormatError) t;
1047 } else if (t instanceof NoClassDefFoundError) {
1048 throw (NoClassDefFoundError) t;
1049 } else if (t instanceof SecurityException) {
1050 throw (SecurityException) t;
1051 } else {
1052 throw new IOException(t.toString());
1053 }
1054 } catch (Exception e) {
1055 throw new IOException(e.toString());
1056 }
1057 } else {
1058 return defineClass(classname, classData, 0, classData.length);
1059 }
1060 }
1061
1062 /**
1063 * Reads a class definition from a stream.
1064 *
1065 * @param stream The stream from which the class is to be read.
1066 * Must not be <code>null</code>.
1067 * @param classname The name of the class in the stream.
1068 * Must not be <code>null</code>.
1069 * @param container the file or directory containing the class.
1070 *
1071 * @return the Class object read from the stream.
1072 *
1073 * @exception IOException if there is a problem reading the class from the
1074 * stream.
1075 * @exception SecurityException if there is a security problem while
1076 * reading the class from the stream.
1077 */
1078 private Class getClassFromStream(InputStream stream, String classname,
1079 File container)
1080 throws IOException, SecurityException {
1081 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1082 int bytesRead = -1;
1083 byte[] buffer = new byte[BUFFER_SIZE];
1084
1085 while ((bytesRead = stream.read(buffer, 0, BUFFER_SIZE)) != -1) {
1086 baos.write(buffer, 0, bytesRead);
1087 }
1088
1089 byte[] classData = baos.toByteArray();
1090 return defineClassFromData(container, classData, classname);
1091 }
1092
1093 /**
1094 * Searches for and load a class on the classpath of this class loader.
1095 *
1096 * @param name The name of the class to be loaded. Must not be
1097 * <code>null</code>.
1098 *
1099 * @return the required Class object
1100 *
1101 * @exception ClassNotFoundException if the requested class does not exist
1102 * on this loader's classpath.
1103 */
1104 public Class findClass(String name) throws ClassNotFoundException {
1105 log("Finding class " + name, Project.MSG_DEBUG);
1106
1107 return findClassInComponents(name);
1108 }
1109
1110 /**
1111 * Indicate if the given file is in this loader's path
1112 *
1113 * @param component the file which is to be checked
1114 *
1115 * @return true if the file is in the class path
1116 */
1117 protected boolean isInPath(File component) {
1118 for (Enumeration e = pathComponents.elements(); e.hasMoreElements();) {
1119 File pathComponent = (File) e.nextElement();
1120 if (pathComponent.equals(component)) {
1121 return true;
1122 }
1123 }
1124 return false;
1125 }
1126
1127
1128 /**
1129 * Finds a class on the given classpath.
1130 *
1131 * @param name The name of the class to be loaded. Must not be
1132 * <code>null</code>.
1133 *
1134 * @return the required Class object
1135 *
1136 * @exception ClassNotFoundException if the requested class does not exist
1137 * on this loader's classpath.
1138 */
1139 private Class findClassInComponents(String name)
1140 throws ClassNotFoundException {
1141 // we need to search the components of the path to see if
1142 // we can find the class we want.
1143 InputStream stream = null;
1144 String classFilename = getClassFilename(name);
1145 try {
1146 Enumeration e = pathComponents.elements();
1147 while (e.hasMoreElements()) {
1148 File pathComponent = (File) e.nextElement();
1149 try {
1150 stream = getResourceStream(pathComponent, classFilename);
1151 if (stream != null) {
1152 log("Loaded from " + pathComponent + " "
1153 + classFilename, Project.MSG_DEBUG);
1154 return getClassFromStream(stream, name, pathComponent);
1155 }
1156 } catch (SecurityException se) {
1157 throw se;
1158 } catch (IOException ioe) {
1159 // ioe.printStackTrace();
1160 log("Exception reading component " + pathComponent
1161 + " (reason: " + ioe.getMessage() + ")",
1162 Project.MSG_VERBOSE);
1163 }
1164 }
1165
1166 throw new ClassNotFoundException(name);
1167 } finally {
1168 try {
1169 if (stream != null) {
1170 stream.close();
1171 }
1172 } catch (IOException e) {
1173 //ignore
1174 }
1175 }
1176 }
1177
1178 /**
1179 * Finds a system class (which should be loaded from the same classloader
1180 * as the Ant core).
1181 *
1182 * For JDK 1.1 compatibility, this uses the findSystemClass method if
1183 * no parent classloader has been specified.
1184 *
1185 * @param name The name of the class to be loaded.
1186 * Must not be <code>null</code>.
1187 *
1188 * @return the required Class object
1189 *
1190 * @exception ClassNotFoundException if the requested class does not exist
1191 * on this loader's classpath.
1192 */
1193 private Class findBaseClass(String name) throws ClassNotFoundException {
1194 if (parent == null) {
1195 return findSystemClass(name);
1196 } else {
1197 return parent.loadClass(name);
1198 }
1199 }
1200
1201 /**
1202 * Cleans up any resources held by this classloader. Any open archive
1203 * files are closed.
1204 */
1205 public synchronized void cleanup() {
1206 for (Enumeration e = zipFiles.elements(); e.hasMoreElements();) {
1207 ZipFile zipFile = (ZipFile) e.nextElement();
1208 try {
1209 zipFile.close();
1210 } catch (IOException ioe) {
1211 // ignore
1212 }
1213 }
1214 zipFiles = new Hashtable();
1215 if (project != null) {
1216 project.removeBuildListener(this);
1217 }
1218 project = null;
1219 }
1220
1221 /**
1222 * Empty implementation to satisfy the BuildListener interface.
1223 *
1224 * @param event the buildStarted event
1225 */
1226 public void buildStarted(BuildEvent event) {
1227 }
1228
1229 /**
1230 * Cleans up any resources held by this classloader at the end
1231 * of a build.
1232 *
1233 * @param event the buildFinished event
1234 */
1235 public void buildFinished(BuildEvent event) {
1236 cleanup();
1237 }
1238
1239 /**
1240 * Cleans up any resources held by this classloader at the end of
1241 * a subbuild if it has been created for the subbuild's project
1242 * instance.
1243 *
1244 * @param event the buildFinished event
1245 *
1246 * @since Ant 1.6.2
1247 */
1248 public void subBuildFinished(BuildEvent event) {
1249 if (event.getProject() == project) {
1250 cleanup();
1251 }
1252 }
1253
1254 /**
1255 * Empty implementation to satisfy the BuildListener interface.
1256 *
1257 * @param event the buildStarted event
1258 *
1259 * @since Ant 1.6.2
1260 */
1261 public void subBuildStarted(BuildEvent event) {
1262 }
1263
1264 /**
1265 * Empty implementation to satisfy the BuildListener interface.
1266 *
1267 * @param event the targetStarted event
1268 */
1269 public void targetStarted(BuildEvent event) {
1270 }
1271
1272 /**
1273 * Empty implementation to satisfy the BuildListener interface.
1274 *
1275 * @param event the targetFinished event
1276 */
1277 public void targetFinished(BuildEvent event) {
1278 }
1279
1280 /**
1281 * Empty implementation to satisfy the BuildListener interface.
1282 *
1283 * @param event the taskStarted event
1284 */
1285 public void taskStarted(BuildEvent event) {
1286 }
1287
1288 /**
1289 * Empty implementation to satisfy the BuildListener interface.
1290 *
1291 * @param event the taskFinished event
1292 */
1293 public void taskFinished(BuildEvent event) {
1294 }
1295
1296 /**
1297 * Empty implementation to satisfy the BuildListener interface.
1298 *
1299 * @param event the messageLogged event
1300 */
1301 public void messageLogged(BuildEvent event) {
1302 }
1303
1304 /**
1305 * add any libraries that come with different java versions
1306 * here
1307 */
1308 public void addJavaLibraries() {
1309 Vector packages = JavaEnvUtils.getJrePackages();
1310 Enumeration e = packages.elements();
1311 while (e.hasMoreElements()) {
1312 String packageName = (String) e.nextElement();
1313 addSystemPackageRoot(packageName);
1314 }
1315 }
1316
1317}
Note: See TracBrowser for help on using the repository browser.