⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proxymethodinvocation.java

📁 一个非常好的FRAMWRK!是一个外国组织做的!不!
💻 JAVA
字号:
/**
 * Copyright 2003-2005 the original author or authors.
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0

 * 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.
 */

package com.jdon.aop.reflection;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.List;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import com.jdon.bussinessproxy.TargetMetaDef;
import com.jdon.bussinessproxy.target.TargetServiceFactory;
import com.jdon.container.access.TargetMetaRequest;
import com.jdon.util.Debug;

/**
 * MethodInvocation Implemention by this class, Interceptor will action.
 * 
 * @author banq
 */
public class ProxyMethodInvocation implements MethodInvocation,
        java.io.Serializable {
    private final static String module = ProxyMethodInvocation.class.getName();

    protected TargetServiceFactory targetServiceFactory;

    private TargetMetaRequest targetMetaRequest;

    private Method method;

    private Object[] args;

    private Object target;

    protected List interceptors;

    protected MethodInvokerUtil mUtil;
    
    protected int currentInterceptorInt = -1;
 
    public ProxyMethodInvocation(List interceptors,
            TargetMetaRequest targetMetaRequest,
            TargetServiceFactory targetServiceFactory, Method method,
            Object[] args) {
        Debug.logVerbose(" method.getName() :" + method.getName(), module);
        this.targetMetaRequest = targetMetaRequest;
        this.interceptors = interceptors;
        this.targetServiceFactory = targetServiceFactory;
        this.mUtil = new MethodInvokerUtil();
        this.method = method;
        this.args = args;
    }

    /**
     * Invokes next interceptor/proxy target. now there is no mixin
     */
    public Object proceed() throws Throwable {
        Debug.logVerbose(" <-----> enter ProxyMethodInvocation proceed() for "
                         + currentInterceptorInt, module);
        if (currentInterceptorInt == interceptors.size() - 1) {
            Debug.logVerbose(" finish call all inteceptors", module);
            return methodInvoke();
        }

        Object interceptor = interceptors.get(++currentInterceptorInt);
        if (interceptor != null) {
            MethodInterceptor methodInterceptor = (MethodInterceptor) interceptor;
            Debug.logVerbose(" now call inteceptor : "
                    + methodInterceptor.getClass().getName(), module);
            return methodInterceptor.invoke(this);
        } else {
            Debug.logVerbose(" null finish call all inteceptors", module);
            return methodInvoke();
        }
    }

    private Object methodInvoke() throws Throwable {
        Debug.logVerbose("enter method reflection ", module);
        Object result = null;
        try {
            if (target == null){//interceptor not set target
                Debug.logVerbose(" all interceptors not set this target object, now create it", module);
                target = mUtil.createTargetObject(targetServiceFactory, targetMetaRequest);
            }

            Debug.logVerbose(" target:"+ target.getClass().getName() +" service's method:"
                		+ method.getName()+ " running.. ", module);

            if (targetMetaRequest.getTargetMetaDef().isEJB()) {
                Debug.logVerbose(" it is ejb target service",  module);
                result = mUtil.execute(method, target, mUtil.narrowArgs(args));
            } else {
                Debug.logVerbose(" it is pojo target service", module);
                result = mUtil.execute(method, target, args);
            }
        } catch (Exception ex) {
            Debug.logError("run error: " + ex, module);
            throw new Throwable(ex);
        } catch (Throwable tex) {
            throw new Throwable(tex);
        }
        return result;
    }

    public TargetMetaDef getTargetMetaDef() {
        return targetMetaRequest.getTargetMetaDef();
    }


    public Object[] getArguments() {
        return this.args;
    }

    public Object getThis() {
        /**
        try {
            if (target == null)
                target = mUtil.createTargetObject(targetServiceFactory, targetMetaRequest);   
        } catch (Exception e) {
            Debug.logError("createTargetObject error :" + e, module);
        }
        **/
        return target;
    }

    public void setThis(Object target) {
        this.target = target;
    }

    public AccessibleObject getStaticPart() {
        return null;
    }

    public Method getMethod() {
        return method;
    }

    
    /**
     * @return Returns the targetMetaRequest.
     */
    public TargetMetaRequest getTargetMetaRequest() {
        return targetMetaRequest;
    }
    /**
     * @param targetMetaRequest The targetMetaRequest to set.
     */
    public void setTargetMetaRequest(TargetMetaRequest targetMetaRequest) {
        this.targetMetaRequest = targetMetaRequest;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -