From 0f55b6b9d6a98accff8e0352474df465207806f7 Mon Sep 17 00:00:00 2001 From: Peter Reilly Date: Mon, 4 Sep 2006 22:39:06 +0000 Subject: [PATCH] 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 --- WHATSNEW | 1 + .../org/apache/tools/ant/AntClassLoader.java | 33 ++++++++++++++----- .../ant/AntClassLoaderDelegationTest.java | 16 +++++++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index b57306706..34d855d68 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -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: -------------- diff --git a/src/main/org/apache/tools/ant/AntClassLoader.java b/src/main/org/apache/tools/ant/AntClassLoader.java index baef19bc4..b55e6e22e 100644 --- a/src/main/org/apache/tools/ant/AntClassLoader.java +++ b/src/main/org/apache/tools/ant/AntClassLoader.java @@ -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; + } + } diff --git a/src/testcases/org/apache/tools/ant/AntClassLoaderDelegationTest.java b/src/testcases/org/apache/tools/ant/AntClassLoaderDelegationTest.java index 802f0d040..d66ed7d5e 100644 --- a/src/testcases/org/apache/tools/ant/AntClassLoaderDelegationTest.java +++ b/src/testcases/org/apache/tools/ant/AntClassLoaderDelegationTest.java @@ -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);