Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 228701 Details for
Bug 384166
BundleDelegatingClassLoader.findResources only looks at the bundle, not the fallback class loader
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Patch extending the resource retrieval to both backing bundle and bridge classloader
384166-against-master.patch (text/plain), 11.91 KB, created by
Olaf Otto
on 2013-03-20 06:45:04 EDT
(
hide
)
Description:
Patch extending the resource retrieval to both backing bundle and bridge classloader
Filename:
MIME Type:
Creator:
Olaf Otto
Created:
2013-03-20 06:45:04 EDT
Size:
11.91 KB
patch
obsolete
>From 1ff84e289fdc60c3d204c33cb7add8db1bc0b74d Mon Sep 17 00:00:00 2001 >From: "olaf.otto" <olaf.otto@unic.com> >Date: Wed, 20 Mar 2013 11:34:05 +0100 >Subject: [PATCH] 384166: BundleDelegatingClassLoader.findResources only looks > at the bundle, not the fallback class loader > (https://bugs.eclipse.org/bugs/show_bug.cgi?id=384166) > >--- > .../util/BundleDelegatingClassLoader.java | 106 ++++++++++++++++----- > .../util/BundleDelegatingClassLoaderTest.java | 94 ++++++++++++++++-- > .../gemini/blueprint/util/internal/resource.txt | 1 + > 3 files changed, 167 insertions(+), 34 deletions(-) > create mode 100644 core/src/test/resources/org/eclipse/gemini/blueprint/util/internal/resource.txt > >diff --git a/core/src/main/java/org/eclipse/gemini/blueprint/util/BundleDelegatingClassLoader.java b/core/src/main/java/org/eclipse/gemini/blueprint/util/BundleDelegatingClassLoader.java >index f9052db..0b63f77 100644 >--- a/core/src/main/java/org/eclipse/gemini/blueprint/util/BundleDelegatingClassLoader.java >+++ b/core/src/main/java/org/eclipse/gemini/blueprint/util/BundleDelegatingClassLoader.java >@@ -1,29 +1,30 @@ >-/****************************************************************************** >- * Copyright (c) 2006, 2010 VMware Inc., Oracle Inc. >- * All rights reserved. This program and the accompanying materials >- * are made available under the terms of the Eclipse Public License v1.0 >- * and Apache License v2.0 which accompanies this distribution. >- * The Eclipse Public License is available at >- * http://www.eclipse.org/legal/epl-v10.html and the Apache License v2.0 >- * is available at http://www.opensource.org/licenses/apache2.0.php. >- * You may elect to redistribute this code under either of these licenses. >- * >- * Contributors: >- * VMware Inc. >- * Oracle Inc. >- *****************************************************************************/ >- >+/****************************************************************************** >+ * Copyright (c) 2006, 2010 VMware Inc., Oracle Inc. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * and Apache License v2.0 which accompanies this distribution. >+ * The Eclipse Public License is available at >+ * http://www.eclipse.org/legal/epl-v10.html and the Apache License v2.0 >+ * is available at http://www.opensource.org/licenses/apache2.0.php. >+ * You may elect to redistribute this code under either of these licenses. >+ * >+ * Contributors: >+ * VMware Inc. >+ * Oracle Inc. >+ *****************************************************************************/ >+ > package org.eclipse.gemini.blueprint.util; > >-import java.io.IOException; >-import java.net.URL; >-import java.security.AccessController; >-import java.security.PrivilegedAction; >-import java.util.Enumeration; >- >-import org.apache.commons.logging.Log; >-import org.osgi.framework.Bundle; >-import org.springframework.util.Assert; >+import org.apache.commons.logging.Log; >+import org.osgi.framework.Bundle; >+import org.springframework.util.Assert; >+ >+import java.io.IOException; >+import java.net.URL; >+import java.security.AccessController; >+import java.security.PrivilegedAction; >+import java.util.Enumeration; >+import java.util.NoSuchElementException; > > /** > * ClassLoader backed by an OSGi bundle. Provides the ability to use a separate >@@ -40,6 +41,43 @@ import org.springframework.util.Assert; > */ > public class BundleDelegatingClassLoader extends ClassLoader { > >+ private static final Enumeration<URL> EMPTY_RESOURCES = new Enumeration<URL>() { >+ public boolean hasMoreElements() { >+ return false; >+ } >+ public URL nextElement() { >+ throw new NoSuchElementException(); >+ } >+ }; >+ >+ /** >+ * Transparently enumerates across two enumerations. >+ */ >+ private static class CombinedEnumeration<T> implements Enumeration<T> { >+ private final Enumeration<T> e1; >+ private final Enumeration<T> e2; >+ >+ public CombinedEnumeration(Enumeration<T> e1, Enumeration<T> e2) { >+ this.e1 = e1; >+ this.e2 = e2; >+ } >+ >+ public boolean hasMoreElements() { >+ return e1.hasMoreElements() || e2.hasMoreElements(); >+ } >+ >+ public T nextElement() { >+ if (e1.hasMoreElements()) { >+ return e1.nextElement(); >+ } >+ if (e2.hasMoreElements()) { >+ return e2.nextElement(); >+ } >+ >+ throw new NoSuchElementException(); >+ } >+ } >+ > /** use degradable logger */ > private static final Log log = LogUtils.createLogger(BundleDelegatingClassLoader.class); > >@@ -141,7 +179,25 @@ public class BundleDelegatingClassLoader extends ClassLoader { > return enm; > } > >- public URL getResource(String name) { >+ @Override >+ public Enumeration<URL> getResources(String name) throws IOException { >+ @SuppressWarnings("unchecked") >+ Enumeration<URL> resources = this.backingBundle.getResources(name); >+ >+ if (this.bridge != null) { >+ Enumeration<URL> bridgeResources = this.bridge.getResources(name); >+ if (resources == null) { >+ resources = bridgeResources; >+ } else if (bridgeResources != null){ >+ resources = new CombinedEnumeration<URL>(resources, bridgeResources); >+ } >+ } >+ >+ // Classloader contract: Never return null but rather an empty enumeration. >+ return resources != null ? resources : EMPTY_RESOURCES; >+ } >+ >+ public URL getResource(String name) { > URL resource = findResource(name); > if (bridge != null && resource == null) { > resource = bridge.getResource(name); >diff --git a/core/src/test/java/org/eclipse/gemini/blueprint/util/BundleDelegatingClassLoaderTest.java b/core/src/test/java/org/eclipse/gemini/blueprint/util/BundleDelegatingClassLoaderTest.java >index 6eb7b08..b277531 100644 >--- a/core/src/test/java/org/eclipse/gemini/blueprint/util/BundleDelegatingClassLoaderTest.java >+++ b/core/src/test/java/org/eclipse/gemini/blueprint/util/BundleDelegatingClassLoaderTest.java >@@ -14,16 +14,17 @@ > > package org.eclipse.gemini.blueprint.util; > >-import java.net.URL; >-import java.util.Enumeration; >- > import junit.framework.TestCase; >- > import org.easymock.MockControl; >-import org.eclipse.gemini.blueprint.util.BundleDelegatingClassLoader; > import org.osgi.framework.Bundle; > import org.springframework.aop.framework.ProxyFactory; > >+import java.io.IOException; >+import java.net.URL; >+import java.util.Enumeration; >+ >+import static org.eclipse.gemini.blueprint.util.BundleDelegatingClassLoader.createBundleClassLoaderFor; >+ > /** > * @author Costin Leau > * >@@ -36,11 +37,14 @@ public class BundleDelegatingClassLoaderTest extends TestCase { > > private Bundle bundle; > >+ private ClassLoader bridge; >+ > protected void setUp() throws Exception { > bundleCtrl = MockControl.createStrictControl(Bundle.class); > bundle = (Bundle) bundleCtrl.getMock(); >- classLoader = >- BundleDelegatingClassLoader.createBundleClassLoaderFor(bundle, ProxyFactory.class.getClassLoader()); >+ classLoader = createBundleClassLoaderFor(bundle, ProxyFactory.class.getClassLoader()); >+ bridge = getClass().getClassLoader(); >+ > bundleCtrl.reset(); > } > >@@ -56,8 +60,8 @@ public class BundleDelegatingClassLoaderTest extends TestCase { > > assertFalse(classLoader.equals(new Object())); > assertEquals(classLoader, classLoader); >- assertTrue(classLoader.equals(BundleDelegatingClassLoader.createBundleClassLoaderFor(bundle, ProxyFactory.class >- .getClassLoader()))); >+ assertTrue(classLoader.equals(createBundleClassLoaderFor(bundle, ProxyFactory.class >+ .getClassLoader()))); > > // assertEquals(bundle.hashCode(), clientClassLoader.hashCode()); > } >@@ -100,4 +104,76 @@ public class BundleDelegatingClassLoaderTest extends TestCase { > > assertSame(enumeration, classLoader.findResources(resource)); > } >+ >+ public void testGetResourcesFromBundleAndBridge() throws Exception { >+ final String resourceName = "org/eclipse/gemini/blueprint/util/internal/resource.txt"; >+ final URL bundleURL = new URL("file://bundle/resourceName"); >+ >+ MockControl bundleResourcesControl = MockControl.createStrictControl(Enumeration.class); >+ Enumeration bundleResources = (Enumeration) bundleResourcesControl.getMock(); >+ bundleResourcesControl.expectAndReturn(bundleResources.hasMoreElements(), true, 2); >+ bundleResourcesControl.expectAndReturn(bundleResources.nextElement(), bundleURL); >+ bundleResourcesControl.expectAndReturn(bundleResources.hasMoreElements(), false, 2); >+ bundleResourcesControl.replay(); >+ >+ bundleCtrl.expectAndReturn(bundle.getResources(resourceName), bundleResources); >+ bundleCtrl.replay(); >+ >+ Enumeration<URL> resources = createBundleClassLoaderFor(bundle, bridge).getResources(resourceName); >+ >+ assertTrue(resources.hasMoreElements()); >+ assertSame(bundleURL, resources.nextElement()); >+ >+ assertTrue(resources.hasMoreElements()); >+ URL resource = resources.nextElement(); >+ assertNotNull(resource); >+ assertTrue(resource.getFile().endsWith(resourceName)); >+ } >+ >+ public void testGetResourcesFromBundleOnly() throws Exception { >+ final String resourceName = "org/eclipse/gemini/blueprint/util/internal/resource.txt"; >+ final URL bundleURL = new URL("file://bundle/resourceName"); >+ >+ MockControl bundleResourcesControl = MockControl.createStrictControl(Enumeration.class); >+ Enumeration bundleResources = (Enumeration) bundleResourcesControl.getMock(); >+ bundleResourcesControl.expectAndReturn(bundleResources.hasMoreElements(), true); >+ bundleResourcesControl.expectAndReturn(bundleResources.nextElement(), bundleURL); >+ bundleResourcesControl.expectAndReturn(bundleResources.hasMoreElements(), false); >+ bundleResourcesControl.replay(); >+ >+ bundleCtrl.expectAndReturn(bundle.getResources(resourceName), bundleResources); >+ bundleCtrl.replay(); >+ >+ Enumeration<URL> resources = createBundleClassLoaderFor(bundle, null).getResources(resourceName); >+ >+ assertTrue(resources.hasMoreElements()); >+ assertSame(bundleURL, resources.nextElement()); >+ assertFalse(resources.hasMoreElements()); >+ } >+ >+ public void testGetResourcesFromBridgeOnly() throws Exception { >+ final String resourceName = "org/eclipse/gemini/blueprint/util/internal/resource.txt"; >+ >+ bundleCtrl.expectAndReturn(bundle.getResources(resourceName), null); >+ bundleCtrl.replay(); >+ >+ Enumeration<URL> resources = createBundleClassLoaderFor(bundle, bridge).getResources(resourceName); >+ assertTrue(resources.hasMoreElements()); >+ URL resource = resources.nextElement(); >+ assertNotNull(resource); >+ assertTrue(resource.getFile().endsWith(resourceName)); >+ assertFalse(resources.hasMoreElements()); >+ } >+ >+ public void testGetResourcesIsNullSafe() throws IOException { >+ final String resourceName = "org/eclipse/gemini/blueprint/util/internal/resource.txt"; >+ >+ bundleCtrl.expectAndReturn(bundle.getResources(resourceName), null); >+ bundleCtrl.replay(); >+ >+ Enumeration<URL> resources = createBundleClassLoaderFor(bundle, null).getResources(resourceName); >+ >+ assertNotNull(resources); >+ assertFalse(resources.hasMoreElements()); >+ } > } >\ No newline at end of file >diff --git a/core/src/test/resources/org/eclipse/gemini/blueprint/util/internal/resource.txt b/core/src/test/resources/org/eclipse/gemini/blueprint/util/internal/resource.txt >new file mode 100644 >index 0000000..b7e0de2 >--- /dev/null >+++ b/core/src/test/resources/org/eclipse/gemini/blueprint/util/internal/resource.txt >@@ -0,0 +1 @@ >+This is a test resource for the BundleDelegatingClassLoaderTest. >\ No newline at end of file >-- >1.8.0.msysgit.0 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 384166
: 228701