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