All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tinygroup.tinyioc.DynamicProxy Maven / Gradle / Ivy

The newest version!
/**
 *  Copyright (c) 1997-2013, tinygroup.org ([email protected]).
 *
 *  Licensed under the GPL, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * --------------------------------------------------------------------------
 *  版权 (c) 1997-2013, tinygroup.org ([email protected]).
 *
 *  本开源软件遵循 GPL 3.0 协议;
 *  如果您不遵循此协议,则不被允许使用此文件。
 *  你可以从下面的地址获取完整的协议文本
 *
 *       http://www.gnu.org/licenses/gpl.html
 */
package org.tinygroup.tinyioc;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.tinygroup.tinyioc.AopDefine;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

/**
 * Created by luoguo on 13-12-30.
 */
public class DynamicProxy implements MethodInterceptor {
    List aopDefineList = new ArrayList();

    public void addAop(AopDefine aopDefine) {
        aopDefineList.add(aopDefine);
    }

    public  boolean isMatchClassName(String className) {
        className = className.split("[$][$]")[0];
        for (AopDefine aopDefine : aopDefineList) {
            Matcher matcher = aopDefine.getClassPattern().matcher(className);
            if (matcher.find() && matcher.group().length() == className.length()) {
                return true;
            }
        }
        return false;
    }


    public  T getProxyObject(Class cls) {
        return (T) Enhancer.create(cls, this);
    }

    public Object intercept(Object target, Method method, Object[] args, MethodProxy methodProxy)
            throws Throwable {
        before(target, method, args);
        Object result = null;
        try {
            result = methodProxy.invokeSuper(target, args);
        } catch (Throwable e) {
            exception(target, method, e, args);
            throw e;
        }
        after(target, method, args);
        return result;
    }


    private void exception(Object target, Method method, Throwable e, Object[] args) {
        if (!isMatchClassName(target.getClass().getName())) {
            return;
        }
        for (AopDefine aopDefine : aopDefineList) {
            String exceptionClassName = e.getClass().getName();
            Matcher matcher = aopDefine.getExceptionPattern().matcher(exceptionClassName);
            Object interceptor = BeanContainerFactory.getBeanContainer().getBeanByType(aopDefine.getInterceptorTypeName());
            if ((interceptor instanceof InterceptorException) && (matcher.find()
                    && matcher.group().length() == exceptionClassName.length())) {
                InterceptorException interceptorException = (InterceptorException) interceptor;
                interceptorException.exception(target, method, e, args);
            }
        }
    }

    private void after(Object target, Method method, Object[] args) {
        if (!isMatchClassName(target.getClass().getName())) {
            return;
        }
        for (AopDefine aopDefine : aopDefineList) {
            String methodName = method.getName();
            Matcher matcher = aopDefine.getMethodPattern().matcher(methodName);
            Object interceptor = BeanContainerFactory.getBeanContainer().getBeanByType(aopDefine.getInterceptorTypeName());
            if ((interceptor instanceof InterceptorAfter) && (matcher.find()
                    && matcher.group().length() == methodName.length())) {
                InterceptorAfter interceptorAfter = (InterceptorAfter) interceptor;
                interceptorAfter.after(target, method, args);
            }
        }
    }


    private void before(Object target, Method method, Object[] args) {
        if (!isMatchClassName(target.getClass().getName())) {
            return;
        }
        for (AopDefine aopDefine : aopDefineList) {
            String methodName = method.getName();
            Matcher matcher = aopDefine.getMethodPattern().matcher(methodName);
            Object interceptor = BeanContainerFactory.getBeanContainer().getBeanByType(aopDefine.getInterceptorTypeName());
            if ((interceptor instanceof InterceptorBefore) && (matcher.find()
                    && matcher.group().length() == methodName.length())) {
                InterceptorBefore interceptorBefore = (InterceptorBefore) interceptor;
                interceptorBefore.before(target, method, args);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy