Coverage Report - org.kuali.rice.core.util.ClassLoaderUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassLoaderUtils
0%
0/46
0%
0/30
4.571
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 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  
  * Provides common utility methods for dealing with Classloaders.
 31  
  *
 32  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
          * Returns the default class loader within the current context.  If there is a context classloader
 40  
          * it is returned, otherwise the classloader which loaded the ClassLoaderUtil Class is returned.
 41  
          *
 42  
          * @return the appropriate default classloader which is guaranteed to be non-null
 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  
          * Checks if the given object is an instance of the given class, unwrapping any proxies if
 54  
          * necessary to get to the underlying object.
 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  
          * Unwraps the underlying object from the given proxy (which may itself be a proxy).  If the
 76  
          * given object is not a valid proxy, then null is returned.
 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  
          * Checks if the given Class is visible to the given ClassLoader.
 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  
          * Determines the interfaces which need to be proxied and are visible to the given proxy ClassLoader.
 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  
                 // check package name filters
 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  
                 // check that the interface is visible from the given classloader
 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  
 }