conor 01/03/15 05:53:30
Modified: src/main/org/apache/tools/ant AntClassLoader.java Log: Make the AntClassLoader load resources in the same order as it currently loads classes. Revision Changes Path 1.16 +90 -41 jakarta-ant/src/main/org/apache/tools/ant/AntClassLoader.java Index: AntClassLoader.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/AntClassLoader.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- AntClassLoader.java 2001/02/18 13:44:41 1.15 +++ AntClassLoader.java 2001/03/15 13:53:29 1.16 @@ -244,6 +244,35 @@ * found on the loader's classpath. */ public InputStream getResourceAsStream(String name) { + + InputStream resourceStream = null; + if (isSystemFirst(name)) { + resourceStream = loadBaseResource(name); + if (resourceStream == null) { + resourceStream = loadResource(name); + } + } + else { + resourceStream = loadResource(name); + if (resourceStream == null) { + resourceStream = loadBaseResource(name); + } + } + + return resourceStream; + } + + + + /** + * Get a stream to read the requested resource name from this loader. + * + * @param name the name of the resource for which a stream is required. + * + * @return a stream to the required resource or null if the resource cannot be + * found on the loader's classpath. + */ + private InputStream loadResource(String name) { // we need to search the components of the path to see if we can find the // class we want. InputStream stream = null; @@ -258,6 +287,19 @@ } /** + * Find a system resource (which should be loaded from the same classloader as the Ant core). + */ + private InputStream loadBaseResource(String name) { + ClassLoader base = AntClassLoader.class.getClassLoader(); + if (base == null) { + return getSystemResourceAsStream(name); + } + else { + return base.getResourceAsStream(name); + } + } + + /** * Get an inputstream to a given resource in the given file which may * either be a directory or a zip file. * @@ -313,24 +355,7 @@ return null; } - /** - * Load a class with this class loader. - * - * This method will load a class. - * - * This class attempts to load the class firstly using the parent class loader. For - * JDK 1.1 compatability, this uses the findSystemClass method. - * - * @param classname the name of the class to be loaded. - * @param resolve true if all classes upon which this class depends are to be loaded. - * - * @return the required Class object - * - * @throws ClassNotFoundException if the requested class does not exist on - * the system classpath or this loader's classpath. - */ - protected Class loadClass(String classname, boolean resolve) throws ClassNotFoundException { - + private boolean isSystemFirst(String resourceName) { // default to the global setting and then see // if this class belongs to a package which has been // designated to use a specific loader first (this one or the system one) @@ -338,7 +363,7 @@ for (Enumeration e = systemPackages.elements(); e.hasMoreElements();) { String packageName = (String)e.nextElement(); - if (classname.startsWith(packageName)) { + if (resourceName.startsWith(packageName)) { useSystemFirst = true; break; } @@ -346,36 +371,60 @@ for (Enumeration e = loaderPackages.elements(); e.hasMoreElements();) { String packageName = (String)e.nextElement(); - if (classname.startsWith(packageName)) { + if (resourceName.startsWith(packageName)) { useSystemFirst = false; break; } } + + return useSystemFirst; + } + + /** + * Load a class with this class loader. + * + * This method will load a class. + * + * This class attempts to load the class firstly using the parent class loader. For + * JDK 1.1 compatability, this uses the findSystemClass method. + * + * @param classname the name of the class to be loaded. + * @param resolve true if all classes upon which this class depends are to be loaded. + * + * @return the required Class object + * + * @throws ClassNotFoundException if the requested class does not exist on + * the system classpath or this loader's classpath. + */ + protected Class loadClass(String classname, boolean resolve) throws ClassNotFoundException { + Class theClass = findLoadedClass(classname); - if (theClass == null) { - if (useSystemFirst) { - try { - theClass = findBaseClass(classname); - project.log("Class " + classname + " loaded from system loader", Project.MSG_DEBUG); - } - catch (ClassNotFoundException cnfe) { - theClass = findClass(classname); - project.log("Class " + classname + " loaded from ant loader", Project.MSG_DEBUG); - } + if (theClass != null) { + return theClass; + } + + if (isSystemFirst(classname)) { + try { + theClass = findBaseClass(classname); + project.log("Class " + classname + " loaded from system loader", Project.MSG_DEBUG); + } + catch (ClassNotFoundException cnfe) { + theClass = findClass(classname); + project.log("Class " + classname + " loaded from ant loader", Project.MSG_DEBUG); } - else { - try { - theClass = findClass(classname); - project.log("Class " + classname + " loaded from ant loader", Project.MSG_DEBUG); - } - catch (ClassNotFoundException cnfe) { - if (ignoreBase) { - throw cnfe; - } - theClass = findBaseClass(classname); - project.log("Class " + classname + " loaded from system loader", Project.MSG_DEBUG); + } + else { + try { + theClass = findClass(classname); + project.log("Class " + classname + " loaded from ant loader", Project.MSG_DEBUG); + } + catch (ClassNotFoundException cnfe) { + if (ignoreBase) { + throw cnfe; } + theClass = findBaseClass(classname); + project.log("Class " + classname + " loaded from system loader", Project.MSG_DEBUG); } }