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