Browse Source

Bugzilla 38747: isolate resources in antclassloader when requested

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@440199 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Reilly 19 years ago
parent
commit
0f55b6b9d6
3 changed files with 41 additions and 9 deletions
  1. +1
    -0
      WHATSNEW
  2. +24
    -9
      src/main/org/apache/tools/ant/AntClassLoader.java
  3. +16
    -0
      src/testcases/org/apache/tools/ant/AntClassLoaderDelegationTest.java

+ 1
- 0
WHATSNEW View File

@@ -14,6 +14,7 @@ Fixed bugs:
* Invalid hash code of Target causes XmlLogger to fail.
Bugzilla report 40207.
* Macro element did not include top level Text. Bugzilla report 36803.
* AntClassLoader did not isolate resources when isolate was set. Bugzilla report 38747.

Other changes:
--------------


+ 24
- 9
src/main/org/apache/tools/ant/AntClassLoader.java View File

@@ -33,6 +33,7 @@ import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;
@@ -205,7 +206,7 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
*
* @see #setIsolated(boolean)
*/
private boolean ignoreBase = false;
private boolean isolated = false;

/**
* The parent class loader, if one is given or can be determined.
@@ -601,7 +602,7 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
* isolated mode.
*/
public synchronized void setIsolated(boolean isolated) {
ignoreBase = isolated;
this.isolated = isolated;
}

/**
@@ -883,8 +884,6 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
// designated to use a specific loader first
// (this one or the parent one)

// XXX - shouldn't this always return false in isolated mode?

boolean useParentFirst = parentFirst;

for (Enumeration e = systemPackages.elements(); e.hasMoreElements();) {
@@ -1076,7 +1075,7 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
log("Class " + classname + " loaded from ant loader",
Project.MSG_DEBUG);
} catch (ClassNotFoundException cnfe) {
if (ignoreBase) {
if (isolated) {
throw cnfe;
}
theClass = findBaseClass(classname);
@@ -1540,7 +1539,8 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
}

/**
* Override ClassLoader.getResources() to handle the reverse case.
* Override ClassLoader.getResources() to handle the reverse case
* and the isolate case.
* @param name The resource name to seach for.
* @return an enumeration of URLs for the resources
* @exception IOException if I/O errors occurs.
@@ -1554,8 +1554,23 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
parentEnum = new ClassLoader(){}.getResources(name);
}
Enumeration mine = findResources(name);
return isParentFirst(name)
? CollectionUtils.append(parentEnum, mine)
: CollectionUtils.append(mine, parentEnum);
boolean parentEnumFirst = isParentFirst(name);
if (isolated && !parentEnumFirst) {
return mine;
} else if (parentEnumFirst) {
return CollectionUtils.append(parentEnum, mine);
} else {
return CollectionUtils.append(mine, parentEnum);
}
}
/**
* Accessor for derived classloaders to access the path components.
* @return the pathcomponents.
*/
protected List getPathComponents() {
return pathComponents;
}

}

+ 16
- 0
src/testcases/org/apache/tools/ant/AntClassLoaderDelegationTest.java View File

@@ -77,6 +77,22 @@ public class AntClassLoaderDelegationTest extends TestCase {
Arrays.asList(new URL[] {urlFromPath, urlFromParent}),
enum2List(acl.getResources(TEST_RESOURCE)));
}

public void testFindIsolateResources() throws Exception {
String buildTestcases = System.getProperty("build.tests");
assertNotNull("defined ${build.tests}", buildTestcases);
assertTrue("have a dir " + buildTestcases, new File(buildTestcases).isDirectory());
Path path = new Path(p, buildTestcases);
// A special parent loader which is not the system class loader:
ClassLoader parent = new ParentLoader();

URL urlFromPath = new URL(FILE_UTILS.toURI(buildTestcases) + TEST_RESOURCE);
AntClassLoader acl = new AntClassLoader(parent, p, path, false);
acl.setIsolated(true);
assertEquals("correct resources (reverse delegation order)",
Arrays.asList(new URL[] {urlFromPath}),
enum2List(acl.getResources(TEST_RESOURCE)));
}
private static List enum2List(Enumeration e) {
// JDK 1.4: return Collections.list(e);


Loading…
Cancel
Save