Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 384166
Collapse All | Expand All

(-)a/core/src/main/java/org/eclipse/gemini/blueprint/util/BundleDelegatingClassLoader.java (-25 / +81 lines)
Lines 1-29 Link Here
1
/******************************************************************************
1
/******************************************************************************
2
 * Copyright (c) 2006, 2010 VMware Inc., Oracle Inc.
2
 * Copyright (c) 2006, 2010 VMware Inc., Oracle Inc.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * and Apache License v2.0 which accompanies this distribution. 
5
 * and Apache License v2.0 which accompanies this distribution. 
6
 * The Eclipse Public License is available at 
6
 * The Eclipse Public License is available at 
7
 * http://www.eclipse.org/legal/epl-v10.html and the Apache License v2.0
7
 * http://www.eclipse.org/legal/epl-v10.html and the Apache License v2.0
8
 * is available at http://www.opensource.org/licenses/apache2.0.php.
8
 * is available at http://www.opensource.org/licenses/apache2.0.php.
9
 * You may elect to redistribute this code under either of these licenses. 
9
 * You may elect to redistribute this code under either of these licenses. 
10
 * 
10
 * 
11
 * Contributors:
11
 * Contributors:
12
 *   VMware Inc.
12
 *   VMware Inc.
13
 *   Oracle Inc.
13
 *   Oracle Inc.
14
 *****************************************************************************/
14
 *****************************************************************************/
15
15
16
package org.eclipse.gemini.blueprint.util;
16
package org.eclipse.gemini.blueprint.util;
17
17
18
import java.io.IOException;
18
import org.apache.commons.logging.Log;
19
import java.net.URL;
19
import org.osgi.framework.Bundle;
20
import java.security.AccessController;
20
import org.springframework.util.Assert;
21
import java.security.PrivilegedAction;
21
22
import java.util.Enumeration;
22
import java.io.IOException;
23
23
import java.net.URL;
24
import org.apache.commons.logging.Log;
24
import java.security.AccessController;
25
import org.osgi.framework.Bundle;
25
import java.security.PrivilegedAction;
26
import org.springframework.util.Assert;
26
import java.util.Enumeration;
27
import java.util.NoSuchElementException;
27
28
28
/**
29
/**
29
 * ClassLoader backed by an OSGi bundle. Provides the ability to use a separate
30
 * ClassLoader backed by an OSGi bundle. Provides the ability to use a separate
Lines 40-45 import org.springframework.util.Assert; Link Here
40
 */
41
 */
41
public class BundleDelegatingClassLoader extends ClassLoader {
42
public class BundleDelegatingClassLoader extends ClassLoader {
42
43
44
    private static final Enumeration<URL>  EMPTY_RESOURCES = new Enumeration<URL>() {
45
        public boolean hasMoreElements() {
46
            return false;
47
        }
48
        public URL nextElement() {
49
            throw new NoSuchElementException();
50
        }
51
    };
52
53
    /**
54
     * Transparently enumerates across two enumerations.
55
     */
56
    private static class CombinedEnumeration<T> implements Enumeration<T> {
57
        private final Enumeration<T> e1;
58
        private final Enumeration<T> e2;
59
60
        public CombinedEnumeration(Enumeration<T> e1, Enumeration<T> e2) {
61
            this.e1 = e1;
62
            this.e2 = e2;
63
        }
64
65
        public boolean hasMoreElements() {
66
            return e1.hasMoreElements() || e2.hasMoreElements();
67
        }
68
69
        public T nextElement() {
70
            if (e1.hasMoreElements()) {
71
                return e1.nextElement();
72
            }
73
            if (e2.hasMoreElements()) {
74
                return e2.nextElement();
75
            }
76
77
            throw new NoSuchElementException();
78
        }
79
    }
80
43
	/** use degradable logger */
81
	/** use degradable logger */
44
	private static final Log log = LogUtils.createLogger(BundleDelegatingClassLoader.class);
82
	private static final Log log = LogUtils.createLogger(BundleDelegatingClassLoader.class);
45
83
Lines 141-147 public class BundleDelegatingClassLoader extends ClassLoader { Link Here
141
		return enm;
179
		return enm;
142
	}
180
	}
143
181
144
	public URL getResource(String name) {
182
    @Override
183
    public Enumeration<URL> getResources(String name) throws IOException {
184
        @SuppressWarnings("unchecked")
185
        Enumeration<URL> resources = this.backingBundle.getResources(name);
186
187
        if (this.bridge != null) {
188
            Enumeration<URL> bridgeResources = this.bridge.getResources(name);
189
            if (resources == null) {
190
                resources = bridgeResources;
191
            } else if (bridgeResources != null){
192
                resources = new CombinedEnumeration<URL>(resources, bridgeResources);
193
            }
194
        }
195
196
        // Classloader contract: Never return null but rather an empty enumeration.
197
        return resources != null ? resources : EMPTY_RESOURCES;
198
    }
199
200
    public URL getResource(String name) {
145
		URL resource = findResource(name);
201
		URL resource = findResource(name);
146
		if (bridge != null && resource == null) {
202
		if (bridge != null && resource == null) {
147
			resource = bridge.getResource(name);
203
			resource = bridge.getResource(name);
(-)a/core/src/test/java/org/eclipse/gemini/blueprint/util/BundleDelegatingClassLoaderTest.java (-9 / +85 lines)
Lines 14-29 Link Here
14
14
15
package org.eclipse.gemini.blueprint.util;
15
package org.eclipse.gemini.blueprint.util;
16
16
17
import java.net.URL;
18
import java.util.Enumeration;
19
20
import junit.framework.TestCase;
17
import junit.framework.TestCase;
21
22
import org.easymock.MockControl;
18
import org.easymock.MockControl;
23
import org.eclipse.gemini.blueprint.util.BundleDelegatingClassLoader;
24
import org.osgi.framework.Bundle;
19
import org.osgi.framework.Bundle;
25
import org.springframework.aop.framework.ProxyFactory;
20
import org.springframework.aop.framework.ProxyFactory;
26
21
22
import java.io.IOException;
23
import java.net.URL;
24
import java.util.Enumeration;
25
26
import static org.eclipse.gemini.blueprint.util.BundleDelegatingClassLoader.createBundleClassLoaderFor;
27
27
/**
28
/**
28
 * @author Costin Leau
29
 * @author Costin Leau
29
 * 
30
 * 
Lines 36-46 public class BundleDelegatingClassLoaderTest extends TestCase { Link Here
36
37
37
	private Bundle bundle;
38
	private Bundle bundle;
38
39
40
    private ClassLoader bridge;
41
39
	protected void setUp() throws Exception {
42
	protected void setUp() throws Exception {
40
		bundleCtrl = MockControl.createStrictControl(Bundle.class);
43
		bundleCtrl = MockControl.createStrictControl(Bundle.class);
41
		bundle = (Bundle) bundleCtrl.getMock();
44
		bundle = (Bundle) bundleCtrl.getMock();
42
		classLoader =
45
		classLoader = createBundleClassLoaderFor(bundle, ProxyFactory.class.getClassLoader());
43
				BundleDelegatingClassLoader.createBundleClassLoaderFor(bundle, ProxyFactory.class.getClassLoader());
46
        bridge = getClass().getClassLoader();
47
44
		bundleCtrl.reset();
48
		bundleCtrl.reset();
45
	}
49
	}
46
50
Lines 56-63 public class BundleDelegatingClassLoaderTest extends TestCase { Link Here
56
60
57
		assertFalse(classLoader.equals(new Object()));
61
		assertFalse(classLoader.equals(new Object()));
58
		assertEquals(classLoader, classLoader);
62
		assertEquals(classLoader, classLoader);
59
		assertTrue(classLoader.equals(BundleDelegatingClassLoader.createBundleClassLoaderFor(bundle, ProxyFactory.class
63
		assertTrue(classLoader.equals(createBundleClassLoaderFor(bundle, ProxyFactory.class
60
				.getClassLoader())));
64
                .getClassLoader())));
61
65
62
		// assertEquals(bundle.hashCode(), clientClassLoader.hashCode());
66
		// assertEquals(bundle.hashCode(), clientClassLoader.hashCode());
63
	}
67
	}
Lines 100-103 public class BundleDelegatingClassLoaderTest extends TestCase { Link Here
100
104
101
		assertSame(enumeration, classLoader.findResources(resource));
105
		assertSame(enumeration, classLoader.findResources(resource));
102
	}
106
	}
107
108
    public void testGetResourcesFromBundleAndBridge() throws Exception {
109
        final String resourceName = "org/eclipse/gemini/blueprint/util/internal/resource.txt";
110
        final URL bundleURL = new URL("file://bundle/resourceName");
111
112
        MockControl bundleResourcesControl = MockControl.createStrictControl(Enumeration.class);
113
        Enumeration bundleResources = (Enumeration) bundleResourcesControl.getMock();
114
        bundleResourcesControl.expectAndReturn(bundleResources.hasMoreElements(), true, 2);
115
        bundleResourcesControl.expectAndReturn(bundleResources.nextElement(), bundleURL);
116
        bundleResourcesControl.expectAndReturn(bundleResources.hasMoreElements(), false, 2);
117
        bundleResourcesControl.replay();
118
119
        bundleCtrl.expectAndReturn(bundle.getResources(resourceName), bundleResources);
120
        bundleCtrl.replay();
121
122
        Enumeration<URL> resources = createBundleClassLoaderFor(bundle, bridge).getResources(resourceName);
123
124
        assertTrue(resources.hasMoreElements());
125
        assertSame(bundleURL, resources.nextElement());
126
127
        assertTrue(resources.hasMoreElements());
128
        URL resource = resources.nextElement();
129
        assertNotNull(resource);
130
        assertTrue(resource.getFile().endsWith(resourceName));
131
    }
132
133
    public void testGetResourcesFromBundleOnly() throws Exception {
134
        final String resourceName = "org/eclipse/gemini/blueprint/util/internal/resource.txt";
135
        final URL bundleURL = new URL("file://bundle/resourceName");
136
137
        MockControl bundleResourcesControl = MockControl.createStrictControl(Enumeration.class);
138
        Enumeration bundleResources = (Enumeration) bundleResourcesControl.getMock();
139
        bundleResourcesControl.expectAndReturn(bundleResources.hasMoreElements(), true);
140
        bundleResourcesControl.expectAndReturn(bundleResources.nextElement(), bundleURL);
141
        bundleResourcesControl.expectAndReturn(bundleResources.hasMoreElements(), false);
142
        bundleResourcesControl.replay();
143
144
        bundleCtrl.expectAndReturn(bundle.getResources(resourceName), bundleResources);
145
        bundleCtrl.replay();
146
147
        Enumeration<URL> resources = createBundleClassLoaderFor(bundle, null).getResources(resourceName);
148
149
        assertTrue(resources.hasMoreElements());
150
        assertSame(bundleURL, resources.nextElement());
151
        assertFalse(resources.hasMoreElements());
152
    }
153
154
    public void testGetResourcesFromBridgeOnly() throws Exception {
155
        final String resourceName = "org/eclipse/gemini/blueprint/util/internal/resource.txt";
156
157
        bundleCtrl.expectAndReturn(bundle.getResources(resourceName), null);
158
        bundleCtrl.replay();
159
160
        Enumeration<URL> resources = createBundleClassLoaderFor(bundle, bridge).getResources(resourceName);
161
        assertTrue(resources.hasMoreElements());
162
        URL resource = resources.nextElement();
163
        assertNotNull(resource);
164
        assertTrue(resource.getFile().endsWith(resourceName));
165
        assertFalse(resources.hasMoreElements());
166
    }
167
168
    public void testGetResourcesIsNullSafe() throws IOException {
169
        final String resourceName = "org/eclipse/gemini/blueprint/util/internal/resource.txt";
170
171
        bundleCtrl.expectAndReturn(bundle.getResources(resourceName), null);
172
        bundleCtrl.replay();
173
174
        Enumeration<URL> resources = createBundleClassLoaderFor(bundle, null).getResources(resourceName);
175
176
        assertNotNull(resources);
177
        assertFalse(resources.hasMoreElements());
178
    }
103
}
179
}
(-)a/core/src/test/resources/org/eclipse/gemini/blueprint/util/internal/resource.txt (-1 / +1 lines)
Added Link Here
0
- 
1
This is a test resource for the BundleDelegatingClassLoaderTest.

Return to bug 384166