Browse Source

Make the AntClassLoader load resources in the same order as it currently

loads classes.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268841 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 24 years ago
parent
commit
b31472d21f
1 changed files with 89 additions and 40 deletions
  1. +89
    -40
      src/main/org/apache/tools/ant/AntClassLoader.java

+ 89
- 40
src/main/org/apache/tools/ant/AntClassLoader.java View File

@@ -244,6 +244,35 @@ public class AntClassLoader extends ClassLoader {
* found on the loader's classpath. * found on the loader's classpath.
*/ */
public InputStream getResourceAsStream(String name) { 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 // we need to search the components of the path to see if we can find the
// class we want. // class we want.
InputStream stream = null; InputStream stream = null;
@@ -257,6 +286,19 @@ public class AntClassLoader extends ClassLoader {
return stream; return stream;
} }


/**
* 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 * Get an inputstream to a given resource in the given file which may
* either be a directory or a zip file. * either be a directory or a zip file.
@@ -313,6 +355,32 @@ public class AntClassLoader extends ClassLoader {
return null; return null;
} }


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)
boolean useSystemFirst = systemFirst;

for (Enumeration e = systemPackages.elements(); e.hasMoreElements();) {
String packageName = (String)e.nextElement();
if (resourceName.startsWith(packageName)) {
useSystemFirst = true;
break;
}
}

for (Enumeration e = loaderPackages.elements(); e.hasMoreElements();) {
String packageName = (String)e.nextElement();
if (resourceName.startsWith(packageName)) {
useSystemFirst = false;
break;
}
}
return useSystemFirst;
}


/** /**
* Load a class with this class loader. * Load a class with this class loader.
* *
@@ -331,51 +399,32 @@ public class AntClassLoader extends ClassLoader {
*/ */
protected Class loadClass(String classname, boolean resolve) throws ClassNotFoundException { protected Class loadClass(String classname, boolean resolve) throws ClassNotFoundException {


// 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)
boolean useSystemFirst = systemFirst;

for (Enumeration e = systemPackages.elements(); e.hasMoreElements();) {
String packageName = (String)e.nextElement();
if (classname.startsWith(packageName)) {
useSystemFirst = true;
break;
}
Class theClass = findLoadedClass(classname);
if (theClass != null) {
return theClass;
} }

for (Enumeration e = loaderPackages.elements(); e.hasMoreElements();) {
String packageName = (String)e.nextElement();
if (classname.startsWith(packageName)) {
useSystemFirst = false;
break;
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);
} }
} }

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);
}
else {
try {
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);
catch (ClassNotFoundException cnfe) {
if (ignoreBase) {
throw cnfe;
} }
theClass = findBaseClass(classname);
project.log("Class " + classname + " loaded from system loader", Project.MSG_DEBUG);
} }
} }


Loading…
Cancel
Save