com.ht.halo.dorado.util.proxy.ProxyBeanUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.ht.halo.dorado.util.proxy.ProxyBeanUtils.java

Source

/*
 * This file is part of Dorado 7.x (http://dorado7.bsdn.org).
 * 
 * Copyright (c) 2002-2012 BSTEK Corp. All rights reserved.
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * and BSDN commercial (http://www.bsdn.org/licenses) licenses.
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at http://www.bstek.com/contact.
 */

package com.ht.halo.dorado.util.proxy;

import java.lang.reflect.Method;

import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;
import net.sf.cglib.proxy.Enhancer;

import com.ht.halo.dorado.util.clazz.ClassUtils;

/**
 * ??
 * 
 * @author Benny Bao (mailto:benny.bao@bstek.com)
 * @since Dec 28, 2007
 */
public abstract class ProxyBeanUtils {
    public static final int JAVASSIST = 0;
    public static final int CGLIB = 1;

    private static int defaultByteCodeProvider = JAVASSIST;
    private static boolean extraEnhancersInited;
    private static Method springCglibIsEnhancerMethod;

    // private static Map<Class<?>, Class<?>> javassistProxyClassCache = new
    // Hashtable<Class<?>, Class<?>>();
    // private static Map<Class<?>, Enhancer> cglibEnhancerCache = new
    // Hashtable<Class<?>, Enhancer>();

    public static int getDefaultByteCodeProvider() {
        return defaultByteCodeProvider;
    }

    public static void setDefaultByteCodeProvider(int defaultByteCodeProvider) {
        ProxyBeanUtils.defaultByteCodeProvider = defaultByteCodeProvider;
    }

    public static boolean isProxy(Class<?> cl) {
        boolean b = Enhancer.isEnhanced(cl) || ProxyFactory.isProxyClass(cl);
        if (!b) {
            if (!extraEnhancersInited) {
                extraEnhancersInited = true;
                try {
                    Class<?> enhancerType = ClassUtils.forName("org.springframework.cglib.proxy.Enhancer");
                    springCglibIsEnhancerMethod = enhancerType.getMethod("isEnhanced", new Class[] { Class.class });
                } catch (Exception e) {
                    // do nothing
                }
            }

            if (springCglibIsEnhancerMethod != null) {
                try {
                    b = (Boolean) springCglibIsEnhancerMethod.invoke(null, new Object[] { cl });
                } catch (Exception e) {
                    // do nothing
                }
            }
        }
        return b;
    }

    public static boolean isProxy(Object bean) {
        if (bean == null) {
            return false;
        }

        boolean b = (bean instanceof ProxyObject || bean instanceof net.sf.cglib.proxy.Factory);
        if (!b) {
            b = isProxy(bean.getClass());
        }
        return b;
    }

    public static Class<?> getProxyTargetType(Object bean) {
        Class<?> cl = bean.getClass();
        while (ProxyBeanUtils.isProxy(cl)) {
            cl = cl.getSuperclass();
        }
        return cl;
    }

    public static Class<?> getProxyTargetType(Class<?> cl) {
        while (ProxyBeanUtils.isProxy(cl)) {
            cl = cl.getSuperclass();
        }
        return cl;
    }

}