Bug 409314 - Wrong ClassLoader.getResources with IBM JDKs

Revert "Bug 375783 - DefaultClassLoader.getResources() ALWAYS delegates to parent Classloader"

This reverts commit 0d5c40d55fff4174d33ec45c039837dcafa934a1.
diff --git a/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/core/BundleHost.java b/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/core/BundleHost.java
index 312e1d0..cfc55b0 100644
--- a/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/core/BundleHost.java
+++ b/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/framework/internal/core/BundleHost.java
@@ -287,7 +287,7 @@
 		if (loader == null)
 			result = bundledata.findLocalResources(name);
 		else
-			result = loader.findResources(name);
+			result = loader.getResources(name);
 		if (result != null && result.hasMoreElements())
 			return result;
 		return null;
diff --git a/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/internal/loader/BundleLoader.java b/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/internal/loader/BundleLoader.java
index 2d377dd..15359c7 100644
--- a/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/internal/loader/BundleLoader.java
+++ b/bundles/org.eclipse.osgi/core/framework/org/eclipse/osgi/internal/loader/BundleLoader.java
@@ -574,6 +574,10 @@
 	 * Finds the resource for a bundle.  This method is used for delegation by the bundle's classloader.
 	 */
 	public URL findResource(String name) {
+		return findResource(name, true);
+	}
+
+	URL findResource(String name, boolean checkParent) {
 		if ((name.length() > 1) && (name.charAt(0) == '/')) /* if name has a leading slash */
 			name = name.substring(1); /* remove leading slash before search */
 		String pkgName = getResourcePackageName(name);
@@ -581,7 +585,7 @@
 		ClassLoader parentCL = getParentClassLoader();
 		// follow the OSGi delegation model
 		// First check the parent classloader for system resources, if it is a java resource.
-		if (parentCL != null) {
+		if (checkParent && parentCL != null) {
 			if (pkgName.startsWith(JAVA_PACKAGE))
 				// 1) if startsWith "java." delegate to parent and terminate search
 				// we never delegate java resource requests past the parent
@@ -641,9 +645,9 @@
 			result = policy.doBuddyResourceLoading(name);
 		if (result != null)
 			return result;
-		// hack to support backwards compatibility for bootdelegation
+		// hack to support backwards compatibiility for bootdelegation
 		// or last resort; do class context trick to work around VM bugs
-		if (parentCL != null && !bootDelegation && (bundle.getFramework().compatibiltyBootDelegation || isRequestFromVM()))
+		if (parentCL != null && !bootDelegation && ((checkParent && bundle.getFramework().compatibiltyBootDelegation) || isRequestFromVM()))
 			// we don't need to continue if the resource is not found here
 			return parentCL.getResource(name);
 		return result;
@@ -657,58 +661,41 @@
 		if ((name.length() > 1) && (name.charAt(0) == '/')) /* if name has a leading slash */
 			name = name.substring(1); /* remove leading slash before search */
 		String pkgName = getResourcePackageName(name);
-		Enumeration<URL> result = Collections.enumeration(Collections.<URL> emptyList());
-		boolean bootDelegation = false;
-		ClassLoader parentCL = getParentClassLoader();
-		// follow the OSGi delegation model
-		// First check the parent classloader for system resources, if it is a java resource.
-		if (parentCL != null) {
-			if (pkgName.startsWith(JAVA_PACKAGE))
-				// 1) if startsWith "java." delegate to parent and terminate search
-				// we never delegate java resource requests past the parent
-				return parentCL.getResources(name);
-			else if (bundle.getFramework().isBootDelegationPackage(pkgName)) {
-				// 2) if part of the bootdelegation list then delegate to parent and continue
-				result = compoundEnumerations(result, parentCL.getResources(name));
-				bootDelegation = true;
-			}
-		}
+		Enumeration<URL> result = null;
 		try {
-			Enumeration<URL> hookResources = searchHooks(name, PRE_RESOURCES);
-			if (hookResources != null) {
-				return compoundEnumerations(result, hookResources);
-			}
+			result = searchHooks(name, PRE_RESOURCES);
 		} catch (ClassNotFoundException e) {
 			// will not happen
 		} catch (FileNotFoundException e) {
-			return result;
+			return null;
 		}
-
+		if (result != null)
+			return result;
+		// start at step 3 because of the comment above about ClassLoader#getResources
 		// 3) search the imported packages
 		PackageSource source = findImportedSource(pkgName, null);
 		if (source != null)
 			// 3) found import source terminate search at the source
-			return compoundEnumerations(result, source.getResources(name));
+			return source.getResources(name);
 		// 4) search the required bundles
 		source = findRequiredSource(pkgName, null);
 		if (source != null)
 			// 4) attempt to load from source but continue on failure
-			result = compoundEnumerations(result, source.getResources(name));
+			result = source.getResources(name);
 
 		// 5) search the local bundle
 		// compound the required source results with the local ones
 		Enumeration<URL> localResults = findLocalResources(name);
 		result = compoundEnumerations(result, localResults);
 		// 6) attempt to find a dynamic import source; only do this if a required source was not found
-		if (source == null && !result.hasMoreElements()) {
+		if (result == null && source == null) {
 			source = findDynamicSource(pkgName);
 			if (source != null)
-				return compoundEnumerations(result, source.getResources(name));
+				return source.getResources(name);
 		}
-		if (!result.hasMoreElements())
+		if (result == null)
 			try {
-				Enumeration<URL> hookResources = searchHooks(name, POST_RESOURCES);
-				result = compoundEnumerations(result, hookResources);
+				result = searchHooks(name, POST_RESOURCES);
 			} catch (ClassNotFoundException e) {
 				// will not happen
 			} catch (FileNotFoundException e) {
@@ -718,13 +705,6 @@
 			Enumeration<URL> buddyResult = policy.doBuddyResourcesLoading(name);
 			result = compoundEnumerations(result, buddyResult);
 		}
-		// hack to support backwards compatibility for bootdelegation
-		// or last resort; do class context trick to work around VM bugs
-		if (!result.hasMoreElements()) {
-			if (parentCL != null && !bootDelegation && (bundle.getFramework().compatibiltyBootDelegation || isRequestFromVM()))
-				// we don't need to continue if the resource is not found here
-				return parentCL.getResources(name);
-		}
 		return result;
 	}
 
@@ -794,6 +774,27 @@
 		return result;
 	}
 
+	/*
+	 * This method is used by Bundle.getResources to do proper parent delegation.
+	 */
+	public Enumeration<URL> getResources(String name) throws IOException {
+		if ((name.length() > 1) && (name.charAt(0) == '/')) /* if name has a leading slash */
+			name = name.substring(1); /* remove leading slash before search */
+		String pkgName = getResourcePackageName(name);
+		// follow the OSGi delegation model
+		// First check the parent classloader for system resources, if it is a java resource.
+		Enumeration<URL> result = null;
+		if (pkgName.startsWith(JAVA_PACKAGE) || bundle.getFramework().isBootDelegationPackage(pkgName)) {
+			// 1) if startsWith "java." delegate to parent and terminate search
+			// 2) if part of the bootdelegation list then delegate to parent and continue of failure
+			ClassLoader parentCL = getParentClassLoader();
+			result = parentCL == null ? null : parentCL.getResources(name);
+			if (pkgName.startsWith(JAVA_PACKAGE))
+				return result;
+		}
+		return compoundEnumerations(result, findResources(name));
+	}
+
 	public static <E> Enumeration<E> compoundEnumerations(Enumeration<E> list1, Enumeration<E> list2) {
 		if (list2 == null || !list2.hasMoreElements())
 			return list1;
diff --git a/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader.java b/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader.java
index 7e2e4f8..6d252fd 100644
--- a/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader.java
+++ b/bundles/org.eclipse.osgi/defaultAdaptor/src/org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader.java
@@ -152,24 +152,16 @@
 	}
 
 	/**
-	 * Gets resources for the bundle.  First delegate.findResources(name) is
-	 * called. The delegate will query the system class loader, bundle imports,
-	 * bundle local resources, bundle hosts and fragments.  The delegate will
-	 * call BundleClassLoader.findLocalResources(name) to find a resource local
-	 * to this bundle.
-	 * @param name The resource path to get.
-	 * @return The Enumeration of the resource URLs.
+	 * Finds all resources with the specified name.  This method must call
+	 * delegate.findResources(name) to find all the resources.
+	 * @param name The resource path to find.
+	 * @return An Enumeration of all resources found or null if the resource.
+	 * @throws IOException 
 	 */
-	public Enumeration<URL> getResources(String name) throws IOException {
-		if (Debug.DEBUG_LOADER) {
-			Debug.println("BundleClassLoader[" + delegate + "].getResources(" + name + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
-		}
+	protected Enumeration<URL> findResources(String name) throws IOException {
 		Enumeration<URL> result = delegate.findResources(name);
-		if (Debug.DEBUG_LOADER) {
-			if (result == null || !result.hasMoreElements()) {
-				Debug.println("BundleClassLoader[" + delegate + "].getResources(" + name + ") failed."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
-			}
-		}
+		if (result == null)
+			return EMPTY_ENUMERATION;
 		return result;
 	}