1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.impl.resourceloader; |
18 | |
|
19 | |
import org.kuali.rice.core.impl.proxy.BaseTargetedInvocationHandler; |
20 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
21 | |
|
22 | |
import java.lang.reflect.InvocationTargetException; |
23 | |
import java.lang.reflect.Method; |
24 | |
import java.lang.reflect.Proxy; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class ContextClassLoaderProxy extends BaseTargetedInvocationHandler { |
33 | |
|
34 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ContextClassLoaderProxy.class); |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public static Object wrap(Object proxiedObject, Class[] classesToProxy, ClassLoader proxyClassLoader, ClassLoader objectClassLoader) { |
46 | 0 | if (proxiedObject == null) { |
47 | 0 | return null; |
48 | |
} |
49 | 0 | if (proxyClassLoader == null) { |
50 | |
|
51 | 0 | proxyClassLoader = proxiedObject.getClass().getClassLoader(); |
52 | |
} |
53 | 0 | if (objectClassLoader == null) { |
54 | 0 | objectClassLoader = proxiedObject.getClass().getClassLoader(); |
55 | |
} |
56 | 0 | if (classesToProxy == null) { |
57 | 0 | classesToProxy = getInterfacesToProxy(proxyClassLoader, proxiedObject); |
58 | |
} |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 0 | ContextClassLoaderProxy handler = new ContextClassLoaderProxy(objectClassLoader, proxiedObject); |
68 | 0 | LOG.debug("Installed a ContextClassLoaderProxy on object: " + proxiedObject.getClass().getName()); |
69 | 0 | proxiedObject = Proxy.newProxyInstance(proxyClassLoader, classesToProxy, handler); |
70 | |
|
71 | |
|
72 | 0 | return proxiedObject; |
73 | |
} |
74 | |
|
75 | |
public static Object wrap(Object proxiedObject, ClassLoader proxyClassLoader, ClassLoader objectClassLoader) { |
76 | 0 | return wrap(proxiedObject, null, proxyClassLoader, objectClassLoader); |
77 | |
} |
78 | |
|
79 | |
public static Object wrap(Object proxiedObject, ClassLoader classLoader) { |
80 | 0 | return wrap(proxiedObject, classLoader, classLoader); |
81 | |
} |
82 | |
|
83 | |
public static Object wrap(Object proxiedObject, Class[] classesToProxy) { |
84 | 0 | return wrap(proxiedObject, classesToProxy, null, null); |
85 | |
} |
86 | |
|
87 | |
public static Object wrap(Object proxiedObject, Class[] classesToProxy, ClassLoader classLoader) { |
88 | 0 | return wrap(proxiedObject, classesToProxy, classLoader, classLoader); |
89 | |
} |
90 | |
|
91 | |
public static Object wrap(Object proxiedObject) { |
92 | 0 | return wrap(proxiedObject, null, null, null); |
93 | |
} |
94 | |
|
95 | |
public static Class[] getInterfacesToProxy(Object proxiedObject) { |
96 | 0 | return getInterfacesToProxy(null, proxiedObject); |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public static Class[] getInterfacesToProxy(ClassLoader proxyClassLoader, Object proxiedObject) { |
103 | 0 | return ClassLoaderUtils.getInterfacesToProxy(proxiedObject, proxyClassLoader, null); |
104 | |
} |
105 | |
|
106 | |
private ClassLoader classLoader; |
107 | |
|
108 | |
public ContextClassLoaderProxy(ClassLoader classLoader, Object target) { |
109 | 0 | super(target); |
110 | 0 | this.classLoader = classLoader; |
111 | 0 | } |
112 | |
|
113 | |
protected Object invokeInternal(Object proxy, Method m, Object[] args) throws Throwable { |
114 | 0 | ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); |
115 | |
try { |
116 | 0 | Thread.currentThread().setContextClassLoader(this.classLoader); |
117 | 0 | return m.invoke(getTarget(), args); |
118 | 0 | } catch (InvocationTargetException e) { |
119 | 0 | throw (e.getCause() != null ? e.getCause() : e); |
120 | |
} finally { |
121 | 0 | Thread.currentThread().setContextClassLoader(oldCl); |
122 | |
} |
123 | |
} |
124 | |
} |