| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.core.util; |
| 18 | |
|
| 19 | |
import java.lang.reflect.InvocationHandler; |
| 20 | |
import java.lang.reflect.Proxy; |
| 21 | |
import java.util.Iterator; |
| 22 | |
import java.util.List; |
| 23 | |
|
| 24 | |
import org.apache.commons.lang.ClassUtils; |
| 25 | |
import org.apache.commons.lang.StringUtils; |
| 26 | |
import org.kuali.rice.core.exception.RiceRuntimeException; |
| 27 | |
import org.kuali.rice.core.proxy.TargetedInvocationHandler; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | 0 | public class ClassLoaderUtils { |
| 35 | |
|
| 36 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ClassLoaderUtils.class); |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public static ClassLoader getDefaultClassLoader() { |
| 45 | 0 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
| 46 | 0 | if (classLoader == null) { |
| 47 | 0 | classLoader = ClassLoaderUtils.class.getClassLoader(); |
| 48 | |
} |
| 49 | 0 | return classLoader; |
| 50 | |
} |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public static boolean isInstanceOf(Object object, Class instanceClass) { |
| 57 | 0 | if (object == null) { |
| 58 | 0 | return false; |
| 59 | |
} |
| 60 | 0 | if (instanceClass.isInstance(object)) { |
| 61 | 0 | return true; |
| 62 | |
} |
| 63 | 0 | return isInstanceOf(unwrapFromProxyOnce(object), instanceClass); |
| 64 | |
} |
| 65 | |
|
| 66 | |
public static Object unwrapFromProxy(Object proxy) { |
| 67 | 0 | Object unwrapped = unwrapFromProxyOnce(proxy); |
| 68 | 0 | if (unwrapped == null) { |
| 69 | 0 | return proxy; |
| 70 | |
} |
| 71 | 0 | return unwrapFromProxy(unwrapped); |
| 72 | |
} |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
private static Object unwrapFromProxyOnce(Object proxy) { |
| 79 | 0 | if (proxy != null && Proxy.isProxyClass(proxy.getClass())) { |
| 80 | 0 | InvocationHandler invocationHandler = Proxy.getInvocationHandler(proxy); |
| 81 | 0 | if (invocationHandler instanceof TargetedInvocationHandler) { |
| 82 | 0 | return ((TargetedInvocationHandler)invocationHandler).getTarget(); |
| 83 | |
} |
| 84 | |
} |
| 85 | 0 | return null; |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
public static boolean isClassVisible(ClassLoader classLoader, Class classToCheck) { |
| 92 | |
try { |
| 93 | 0 | Class classFound = classLoader.loadClass(classToCheck.getName()); |
| 94 | 0 | return classFound.equals(classToCheck); |
| 95 | 0 | } catch (ClassNotFoundException e) { |
| 96 | 0 | return false; |
| 97 | |
} |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public static Class[] getInterfacesToProxy(Object object, ClassLoader proxyClassLoader, String[] packageNamesToFilter) { |
| 104 | 0 | List interfaces = ClassUtils.getAllInterfaces(object.getClass()); |
| 105 | 0 | outer:for (Iterator iterator = interfaces.iterator(); iterator.hasNext();) { |
| 106 | 0 | Class objectInterface = (Class) iterator.next(); |
| 107 | |
|
| 108 | 0 | if (packageNamesToFilter != null) { |
| 109 | 0 | for (String packageNames : packageNamesToFilter) { |
| 110 | 0 | if (objectInterface.getName().startsWith(packageNames)) { |
| 111 | 0 | iterator.remove(); |
| 112 | 0 | continue outer; |
| 113 | |
} |
| 114 | |
} |
| 115 | |
} |
| 116 | |
|
| 117 | 0 | if (proxyClassLoader != null) { |
| 118 | 0 | if (!ClassLoaderUtils.isClassVisible(proxyClassLoader, objectInterface)) { |
| 119 | 0 | if (LOG.isDebugEnabled()) { |
| 120 | 0 | LOG.debug("The interface " + objectInterface + " was not visible from the proxy ClassLoader when attempting to proxy: " + object); |
| 121 | |
} |
| 122 | 0 | iterator.remove(); |
| 123 | 0 | continue outer; |
| 124 | |
} |
| 125 | |
} |
| 126 | 0 | } |
| 127 | 0 | Class[] interfaceArray = new Class[interfaces.size()]; |
| 128 | 0 | return (Class[]) interfaces.toArray(interfaceArray); |
| 129 | |
} |
| 130 | |
|
| 131 | |
public static Class getClass(String className) { |
| 132 | 0 | if (StringUtils.isEmpty(className)) { |
| 133 | 0 | return null; |
| 134 | |
} |
| 135 | |
try { |
| 136 | 0 | return ClassUtils.getClass(getDefaultClassLoader(), className); |
| 137 | 0 | } catch (ClassNotFoundException e) { |
| 138 | 0 | throw new RiceRuntimeException(e); |
| 139 | |
} |
| 140 | |
} |
| 141 | |
|
| 142 | |
} |