|  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.api.exception.RiceRuntimeException;  | 
  |  27 |     | 
   import org.kuali.rice.core.api.proxy.TargetedInvocationHandler;  | 
  |  28 |     | 
     | 
  |  29 |     | 
     | 
  |  30 |     | 
     | 
  |  31 |     | 
     | 
  |  32 |     | 
     | 
  |  33 |     | 
     | 
  |  34 |     | 
   public final class ClassLoaderUtils { | 
  |  35 |     | 
     | 
  |  36 |    0 |        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ClassLoaderUtils.class);  | 
  |  37 |     | 
         | 
  |  38 |    0 |            private ClassLoaderUtils() { | 
  |  39 |    0 |                    throw new UnsupportedOperationException("do not call"); | 
  |  40 |     | 
           }  | 
  |  41 |     | 
     | 
  |  42 |     | 
             | 
  |  43 |     | 
     | 
  |  44 |     | 
     | 
  |  45 |     | 
     | 
  |  46 |     | 
     | 
  |  47 |     | 
     | 
  |  48 |     | 
           public static ClassLoader getDefaultClassLoader() { | 
  |  49 |    0 |                    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();  | 
  |  50 |    0 |                    if (classLoader == null) { | 
  |  51 |    0 |                            classLoader = ClassLoaderUtils.class.getClassLoader();  | 
  |  52 |     | 
                   }  | 
  |  53 |    0 |                    return classLoader;  | 
  |  54 |     | 
           }  | 
  |  55 |     | 
     | 
  |  56 |     | 
             | 
  |  57 |     | 
     | 
  |  58 |     | 
     | 
  |  59 |     | 
     | 
  |  60 |     | 
           public static boolean isInstanceOf(Object object, Class instanceClass) { | 
  |  61 |    0 |                    if (object == null) { | 
  |  62 |    0 |                            return false;  | 
  |  63 |     | 
                   }  | 
  |  64 |    0 |                    if (instanceClass.isInstance(object)) { | 
  |  65 |    0 |                            return true;  | 
  |  66 |     | 
                   }  | 
  |  67 |    0 |                    return isInstanceOf(unwrapFromProxyOnce(object), instanceClass);  | 
  |  68 |     | 
           }  | 
  |  69 |     | 
     | 
  |  70 |     | 
           public static Object unwrapFromProxy(Object proxy) { | 
  |  71 |    0 |                    Object unwrapped = unwrapFromProxyOnce(proxy);  | 
  |  72 |    0 |                    if (unwrapped == null) { | 
  |  73 |    0 |                            return proxy;  | 
  |  74 |     | 
                   }  | 
  |  75 |    0 |                    return unwrapFromProxy(unwrapped);  | 
  |  76 |     | 
           }  | 
  |  77 |     | 
     | 
  |  78 |     | 
             | 
  |  79 |     | 
     | 
  |  80 |     | 
     | 
  |  81 |     | 
     | 
  |  82 |     | 
           private static Object unwrapFromProxyOnce(Object proxy) { | 
  |  83 |    0 |                    if (proxy != null && Proxy.isProxyClass(proxy.getClass())) { | 
  |  84 |    0 |                            InvocationHandler invocationHandler = Proxy.getInvocationHandler(proxy);  | 
  |  85 |    0 |                            if (invocationHandler instanceof TargetedInvocationHandler) { | 
  |  86 |    0 |                                    return ((TargetedInvocationHandler)invocationHandler).getTarget();  | 
  |  87 |     | 
                           }  | 
  |  88 |     | 
                   }  | 
  |  89 |    0 |                    return null;  | 
  |  90 |     | 
           }  | 
  |  91 |     | 
     | 
  |  92 |     | 
             | 
  |  93 |     | 
     | 
  |  94 |     | 
     | 
  |  95 |     | 
           public static boolean isClassVisible(ClassLoader classLoader, Class classToCheck) { | 
  |  96 |     | 
               try { | 
  |  97 |    0 |                    Class classFound = classLoader.loadClass(classToCheck.getName());  | 
  |  98 |    0 |                    return classFound.equals(classToCheck);  | 
  |  99 |    0 |                } catch (ClassNotFoundException e) { | 
  |  100 |    0 |                    return false;  | 
  |  101 |     | 
               }  | 
  |  102 |     | 
           }  | 
  |  103 |     | 
     | 
  |  104 |     | 
             | 
  |  105 |     | 
     | 
  |  106 |     | 
     | 
  |  107 |     | 
           public static Class[] getInterfacesToProxy(Object object, ClassLoader proxyClassLoader, String[] packageNamesToFilter) { | 
  |  108 |    0 |                List interfaces = ClassUtils.getAllInterfaces(object.getClass());  | 
  |  109 |    0 |                outer:for (Iterator iterator = interfaces.iterator(); iterator.hasNext();) { | 
  |  110 |    0 |                    Class objectInterface = (Class) iterator.next();  | 
  |  111 |     | 
                     | 
  |  112 |    0 |                    if (packageNamesToFilter != null) { | 
  |  113 |    0 |                        for (String packageNames : packageNamesToFilter) { | 
  |  114 |    0 |                            if (objectInterface.getName().startsWith(packageNames)) { | 
  |  115 |    0 |                                iterator.remove();  | 
  |  116 |    0 |                                continue outer;  | 
  |  117 |     | 
                           }  | 
  |  118 |     | 
                       }  | 
  |  119 |     | 
                   }  | 
  |  120 |     | 
                     | 
  |  121 |    0 |                    if (proxyClassLoader != null) { | 
  |  122 |    0 |                        if (!ClassLoaderUtils.isClassVisible(proxyClassLoader, objectInterface)) { | 
  |  123 |    0 |                            if (LOG.isDebugEnabled()) { | 
  |  124 |    0 |                                LOG.debug("The interface " + objectInterface + " was not visible from the proxy ClassLoader when attempting to proxy: " + object); | 
  |  125 |     | 
                           }  | 
  |  126 |    0 |                            iterator.remove();  | 
  |  127 |    0 |                            continue outer;  | 
  |  128 |     | 
                       }  | 
  |  129 |     | 
                   }  | 
  |  130 |    0 |                }  | 
  |  131 |    0 |                Class[] interfaceArray = new Class[interfaces.size()];  | 
  |  132 |    0 |                return (Class[]) interfaces.toArray(interfaceArray);  | 
  |  133 |     | 
           }  | 
  |  134 |     | 
             | 
  |  135 |     | 
             | 
  |  136 |     | 
             | 
  |  137 |     | 
           public static Class<?> getClass(String className) { | 
  |  138 |    0 |                    if (StringUtils.isEmpty(className)) { | 
  |  139 |    0 |                            return null;  | 
  |  140 |     | 
                   }  | 
  |  141 |     | 
                   try { | 
  |  142 |    0 |                            return ClassUtils.getClass(getDefaultClassLoader(), className);  | 
  |  143 |    0 |                    } catch (ClassNotFoundException e) { | 
  |  144 |    0 |                            throw new RiceRuntimeException(e);  | 
  |  145 |     | 
                   }  | 
  |  146 |     | 
           }  | 
  |  147 |     | 
             | 
  |  148 |     | 
           public static <T> Class<? extends T> getClass(String className, Class<T> type) throws ClassNotFoundException { | 
  |  149 |    0 |                    Class<?> theClass = ClassUtils.getClass(getDefaultClassLoader(), className);  | 
  |  150 |    0 |                    return theClass.asSubclass(type);  | 
  |  151 |     | 
           }  | 
  |  152 |     | 
             | 
  |  153 |     | 
   }  |