View Javadoc
1   /**
2    * Copyright 2005-2015 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      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ClassLoaderUtils.class);
36      
37  	private ClassLoaderUtils() {
38  		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  		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
49  		if (classLoader == null) {
50  			classLoader = ClassLoaderUtils.class.getClassLoader();
51  		}
52  		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  		if (object == null) {
61  			return false;
62  		}
63  		if (instanceClass.isInstance(object)) {
64  			return true;
65  		}
66  		return isInstanceOf(unwrapFromProxyOnce(object), instanceClass);
67  	}
68  
69  	public static Object unwrapFromProxy(Object proxy) {
70  		Object unwrapped = unwrapFromProxyOnce(proxy);
71  		if (unwrapped == null) {
72  			return proxy;
73  		}
74  		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  		if (proxy != null && Proxy.isProxyClass(proxy.getClass())) {
83  			InvocationHandler invocationHandler = Proxy.getInvocationHandler(proxy);
84  			if (invocationHandler instanceof TargetedInvocationHandler) {
85  				return ((TargetedInvocationHandler)invocationHandler).getTarget();
86  			}
87  		}
88  		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  		Class<?> classFound = classLoader.loadClass(classToCheck.getName());
97  		return classFound.equals(classToCheck);
98  	    } catch (ClassNotFoundException e) {
99  		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 	    List interfaces = ClassUtils.getAllInterfaces(object.getClass());
108 	    outer:for (Iterator iterator = interfaces.iterator(); iterator.hasNext();) {
109 		Class objectInterface = (Class) iterator.next();
110 		// check package name filters
111 		if (packageNamesToFilter != null) {
112 		    for (String packageNames : packageNamesToFilter) {
113 			if (objectInterface.getName().startsWith(packageNames)) {
114 			    iterator.remove();
115 			    continue outer;
116 			}
117 		    }
118 		}
119 		// check that the interface is visible from the given classloader
120 		if (proxyClassLoader != null) {
121 		    if (!ClassLoaderUtils.isClassVisible(proxyClassLoader, objectInterface)) {
122 			if (LOG.isDebugEnabled()) {
123 			    LOG.debug("The interface " + objectInterface + " was not visible from the proxy ClassLoader when attempting to proxy: " + object);
124 			}
125 			iterator.remove();
126 			continue outer;
127 		    }
128 		}
129 	    }
130 	    Class[] interfaceArray = new Class[interfaces.size()];
131 	    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 		if (StringUtils.isEmpty(className)) {
138 			return null;
139 		}
140 		try {
141 			return ClassUtils.getClass(getDefaultClassLoader(), className);
142 		} catch (ClassNotFoundException e) {
143 			throw new RiceRuntimeException(e);
144 		}
145 	}
146 	
147 	public static <T> Class<? extends T> getClass(String className, Class<T> type) throws ClassNotFoundException {
148 		Class<?> theClass = ClassUtils.getClass(getDefaultClassLoader(), className);
149 		return theClass.asSubclass(type);
150 	}
151 	
152 }