1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.river.osgi;
20
21 import java.io.IOException;
22 import java.net.URL;
23 import java.security.AccessController;
24 import java.security.PrivilegedAction;
25 import java.security.PrivilegedActionException;
26 import java.security.PrivilegedExceptionAction;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.Enumeration;
30
31 import org.osgi.framework.Bundle;
32
33
34
35
36 public class BundleDelegatingClassLoader extends ClassLoader {
37
38
39
40
41
42 private final Bundle bundle;
43 private final ClassLoader classLoader;
44
45 public BundleDelegatingClassLoader(Bundle bundle) {
46 this(bundle, null);
47 }
48
49 public BundleDelegatingClassLoader(Bundle bundle, ClassLoader classLoader) {
50 this.bundle = bundle;
51 this.classLoader = classLoader;
52 }
53
54 @Override
55 protected Class<?> findClass(final String name) throws ClassNotFoundException {
56 try {
57 return AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>() {
58 public Class<?> run() throws ClassNotFoundException {
59 return bundle.loadClass(name);
60 }
61 });
62 } catch (PrivilegedActionException e) {
63 Exception cause = e.getException();
64
65 if (cause instanceof ClassNotFoundException) {
66 throw (ClassNotFoundException)cause;
67 }
68 throw (RuntimeException)cause;
69 }
70 }
71
72 @Override
73 protected URL findResource(final String name) {
74 URL resource = AccessController.doPrivileged(new PrivilegedAction<URL>() {
75 public URL run() {
76 return bundle.getResource(name);
77 }
78 });
79 if (classLoader != null && resource == null) {
80 resource = classLoader.getResource(name);
81 }
82 return resource;
83 }
84
85 @Override
86 protected Enumeration<URL> findResources(final String name) throws IOException {
87 Enumeration<URL> urls;
88 try {
89 urls = AccessController.doPrivileged(new PrivilegedExceptionAction<Enumeration<URL>>() {
90 public Enumeration<URL> run() throws IOException {
91 return bundle.getResources(name);
92 }
93
94 });
95 } catch (PrivilegedActionException e) {
96 Exception cause = e.getException();
97
98 if (cause instanceof IOException) {
99 throw (IOException)cause;
100 }
101 throw (RuntimeException)cause;
102 }
103
104 if (urls == null) {
105 urls = Collections.enumeration(new ArrayList<URL>());
106 }
107
108 return urls;
109 }
110
111 @Override
112 protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
113 Class<?> clazz;
114 try {
115 clazz = findClass(name);
116 } catch (ClassNotFoundException cnfe) {
117 if (classLoader != null) {
118 try {
119 clazz = classLoader.loadClass(name);
120 } catch (ClassNotFoundException e) {
121 throw new ClassNotFoundException(name + " from bundle " + bundle.getBundleId()
122 + " (" + bundle.getSymbolicName() + ")", cnfe);
123 }
124 } else {
125 throw new ClassNotFoundException(name + " from bundle " + bundle.getBundleId()
126 + " (" + bundle.getSymbolicName() + ")", cnfe);
127 }
128 }
129 if (resolve) {
130 resolveClass(clazz);
131 }
132 return clazz;
133 }
134
135 public Bundle getBundle() {
136 return bundle;
137 }
138 }