01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.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 edu.iu.uis.eden.util;
18:
19: import java.lang.reflect.InvocationHandler;
20: import java.lang.reflect.Proxy;
21:
22: import org.kuali.rice.proxy.TargetedInvocationHandler;
23:
24: /**
25: * Provides common utility methods for dealing with Classloaders.
26: *
27: * @author ewestfal
28: */
29: public class ClassLoaderUtils {
30:
31: /**
32: * Returns the default class loader within the current context. If there is a context classloader
33: * it is returned, otherwise the classloader which loaded the ClassLoaderUtil Class is returned.
34: *
35: * @return the appropriate default classloader which is guaranteed to be non-null
36: */
37: public static ClassLoader getDefaultClassLoader() {
38: ClassLoader classLoader = Thread.currentThread()
39: .getContextClassLoader();
40: if (classLoader == null) {
41: classLoader = ClassLoaderUtils.class.getClassLoader();
42: }
43: return classLoader;
44: }
45:
46: /**
47: * Checks if the given object is an instance of the given class, unwrapping any proxies if
48: * necessary to get to the underlying object.
49: */
50: public static boolean isInstanceOf(Object object,
51: Class instanceClass) {
52: if (object == null) {
53: return false;
54: }
55: if (instanceClass.isInstance(object)) {
56: return true;
57: }
58: return isInstanceOf(unwrapFromProxyOnce(object), instanceClass);
59: }
60:
61: public static Object unwrapFromProxy(Object proxy) {
62: Object unwrapped = unwrapFromProxyOnce(proxy);
63: if (unwrapped == null) {
64: return proxy;
65: }
66: return unwrapFromProxy(unwrapped);
67: }
68:
69: /**
70: * Unwraps the underlying object from the given proxy (which may itself be a proxy). If the
71: * given object is not a valid proxy, then null is returned.
72: */
73: private static Object unwrapFromProxyOnce(Object proxy) {
74: if (proxy != null && Proxy.isProxyClass(proxy.getClass())) {
75: InvocationHandler invocationHandler = Proxy
76: .getInvocationHandler(proxy);
77: if (invocationHandler instanceof TargetedInvocationHandler) {
78: return ((TargetedInvocationHandler) invocationHandler)
79: .getTarget();
80: }
81: }
82: return null;
83: }
84:
85: }
|