Coverage Report - org.kuali.rice.core.api.util.ClassLoaderUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassLoaderUtils
0%
0/49
0%
0/30
3.889
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.api.util;
 17  
 
 18  
 import org.apache.commons.lang.ClassUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 21  
 import org.kuali.rice.core.api.util.reflect.TargetedInvocationHandler;
 22  
 
 23  
 import java.lang.reflect.InvocationHandler;
 24  
 import java.lang.reflect.Proxy;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 
 28  
 /**
 29  
  * Provides common utility methods for dealing with Classloaders.
 30  
  *
 31  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 32  
  */
 33  
 public final class ClassLoaderUtils {
 34  
 
 35  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ClassLoaderUtils.class);
 36  
     
 37  0
         private ClassLoaderUtils() {
 38  0
                 throw new UnsupportedOperationException("do not call");
 39  
         }
 40  
 
 41  
         /**
 42  
          * Returns the default class loader within the current context.  If there is a context classloader
 43  
          * it is returned, otherwise the classloader which loaded the ClassLoaderUtil Class is returned.
 44  
          *
 45  
          * @return the appropriate default classloader which is guaranteed to be non-null
 46  
          */
 47  
         public static ClassLoader getDefaultClassLoader() {
 48  0
                 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
 49  0
                 if (classLoader == null) {
 50  0
                         classLoader = ClassLoaderUtils.class.getClassLoader();
 51  
                 }
 52  0
                 return classLoader;
 53  
         }
 54  
 
 55  
         /**
 56  
          * Checks if the given object is an instance of the given class, unwrapping any proxies if
 57  
          * necessary to get to the underlying object.
 58  
          */
 59  
         public static boolean isInstanceOf(Object object, Class<?> instanceClass) {
 60  0
                 if (object == null) {
 61  0
                         return false;
 62  
                 }
 63  0
                 if (instanceClass.isInstance(object)) {
 64  0
                         return true;
 65  
                 }
 66  0
                 return isInstanceOf(unwrapFromProxyOnce(object), instanceClass);
 67  
         }
 68  
 
 69  
         public static Object unwrapFromProxy(Object proxy) {
 70  0
                 Object unwrapped = unwrapFromProxyOnce(proxy);
 71  0
                 if (unwrapped == null) {
 72  0
                         return proxy;
 73  
                 }
 74  0
                 return unwrapFromProxy(unwrapped);
 75  
         }
 76  
 
 77  
         /**
 78  
          * Unwraps the underlying object from the given proxy (which may itself be a proxy).  If the
 79  
          * given object is not a valid proxy, then null is returned.
 80  
          */
 81  
         private static Object unwrapFromProxyOnce(Object proxy) {
 82  0
                 if (proxy != null && Proxy.isProxyClass(proxy.getClass())) {
 83  0
                         InvocationHandler invocationHandler = Proxy.getInvocationHandler(proxy);
 84  0
                         if (invocationHandler instanceof TargetedInvocationHandler) {
 85  0
                                 return ((TargetedInvocationHandler)invocationHandler).getTarget();
 86  
                         }
 87  
                 }
 88  0
                 return null;
 89  
         }
 90  
 
 91  
         /**
 92  
          * Checks if the given Class is visible to the given ClassLoader.
 93  
          */
 94  
         public static boolean isClassVisible(ClassLoader classLoader, Class<?> classToCheck) {
 95  
             try {
 96  0
                 Class<?> classFound = classLoader.loadClass(classToCheck.getName());
 97  0
                 return classFound.equals(classToCheck);
 98  0
             } catch (ClassNotFoundException e) {
 99  0
                 return false;
 100  
             }
 101  
         }
 102  
 
 103  
         /**
 104  
          * Determines the interfaces which need to be proxied and are visible to the given proxy ClassLoader.
 105  
          */
 106  
         public static Class[] getInterfacesToProxy(Object object, ClassLoader proxyClassLoader, String[] packageNamesToFilter) {
 107  0
             List interfaces = ClassUtils.getAllInterfaces(object.getClass());
 108  0
             outer:for (Iterator iterator = interfaces.iterator(); iterator.hasNext();) {
 109  0
                 Class objectInterface = (Class) iterator.next();
 110  
                 // check package name filters
 111  0
                 if (packageNamesToFilter != null) {
 112  0
                     for (String packageNames : packageNamesToFilter) {
 113  0
                         if (objectInterface.getName().startsWith(packageNames)) {
 114  0
                             iterator.remove();
 115  0
                             continue outer;
 116  
                         }
 117  
                     }
 118  
                 }
 119  
                 // check that the interface is visible from the given classloader
 120  0
                 if (proxyClassLoader != null) {
 121  0
                     if (!ClassLoaderUtils.isClassVisible(proxyClassLoader, objectInterface)) {
 122  0
                         if (LOG.isDebugEnabled()) {
 123  0
                             LOG.debug("The interface " + objectInterface + " was not visible from the proxy ClassLoader when attempting to proxy: " + object);
 124  
                         }
 125  0
                         iterator.remove();
 126  0
                         continue outer;
 127  
                     }
 128  
                 }
 129  0
             }
 130  0
             Class[] interfaceArray = new Class[interfaces.size()];
 131  0
             return (Class[]) interfaces.toArray(interfaceArray);
 132  
         }
 133  
         
 134  
         // TODO why not make this so that it throws ClassNoteFoundException and doesn't return null?
 135  
         // this is more standard behavior...
 136  
         public static Class<?> getClass(String className) {
 137  0
                 if (StringUtils.isEmpty(className)) {
 138  0
                         return null;
 139  
                 }
 140  
                 try {
 141  0
                         return ClassUtils.getClass(getDefaultClassLoader(), className);
 142  0
                 } catch (ClassNotFoundException e) {
 143  0
                         throw new RiceRuntimeException(e);
 144  
                 }
 145  
         }
 146  
         
 147  
         public static <T> Class<? extends T> getClass(String className, Class<T> type) throws ClassNotFoundException {
 148  0
                 Class<?> theClass = ClassUtils.getClass(getDefaultClassLoader(), className);
 149  0
                 return theClass.asSubclass(type);
 150  
         }
 151  
         
 152  
 }