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.util.ClassLoaderUtils;
20 import org.kuali.rice.core.util.reflect.BaseTargetedInvocationHandler;
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 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 if (proxiedObject == null) {
47 return null;
48 }
49 if (proxyClassLoader == null) {
50
51 proxyClassLoader = proxiedObject.getClass().getClassLoader();
52 }
53 if (objectClassLoader == null) {
54 objectClassLoader = proxiedObject.getClass().getClassLoader();
55 }
56 if (classesToProxy == null) {
57 classesToProxy = getInterfacesToProxy(proxyClassLoader, proxiedObject);
58 }
59
60
61
62
63
64
65
66
67 ContextClassLoaderProxy handler = new ContextClassLoaderProxy(objectClassLoader, proxiedObject);
68 LOG.debug("Installed a ContextClassLoaderProxy on object: " + proxiedObject.getClass().getName());
69 proxiedObject = Proxy.newProxyInstance(proxyClassLoader, classesToProxy, handler);
70
71
72 return proxiedObject;
73 }
74
75 public static Object wrap(Object proxiedObject, ClassLoader proxyClassLoader, ClassLoader objectClassLoader) {
76 return wrap(proxiedObject, null, proxyClassLoader, objectClassLoader);
77 }
78
79 public static Object wrap(Object proxiedObject, ClassLoader classLoader) {
80 return wrap(proxiedObject, classLoader, classLoader);
81 }
82
83 public static Object wrap(Object proxiedObject, Class<?>[] classesToProxy) {
84 return wrap(proxiedObject, classesToProxy, null, null);
85 }
86
87 public static Object wrap(Object proxiedObject, Class<?>[] classesToProxy, ClassLoader classLoader) {
88 return wrap(proxiedObject, classesToProxy, classLoader, classLoader);
89 }
90
91 public static Object wrap(Object proxiedObject) {
92 return wrap(proxiedObject, null, null, null);
93 }
94
95 public static Class<?>[] getInterfacesToProxy(Object proxiedObject) {
96 return getInterfacesToProxy(null, proxiedObject);
97 }
98
99
100
101
102 public static Class<?>[] getInterfacesToProxy(ClassLoader proxyClassLoader, Object proxiedObject) {
103 return ClassLoaderUtils.getInterfacesToProxy(proxiedObject, proxyClassLoader, null);
104 }
105
106 private ClassLoader classLoader;
107
108 public ContextClassLoaderProxy(ClassLoader classLoader, Object target) {
109 super(target);
110 this.classLoader = classLoader;
111 }
112
113 protected Object invokeInternal(Object proxy, Method m, Object[] args) throws Throwable {
114 ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
115 try {
116 Thread.currentThread().setContextClassLoader(this.classLoader);
117 return m.invoke(getTarget(), args);
118 } catch (InvocationTargetException e) {
119 throw (e.getCause() != null ? e.getCause() : e);
120 } finally {
121 Thread.currentThread().setContextClassLoader(oldCl);
122 }
123 }
124 }