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

📄 statefulinterceptor.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.interceptor;

import java.util.ArrayList;
import java.util.List;

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

import com.jdon.aop.reflection.ProxyMethodInvocation;
import com.jdon.bussinessproxy.TargetMetaDef;
import com.jdon.container.ContainerWrapper;
import com.jdon.container.access.TargetMetaRequest;
import com.jdon.container.finder.ComponentKeys;
import com.jdon.container.finder.ContainerCallback;
import com.jdon.container.visitor.ComponentVisitor;
import com.jdon.controller.service.Stateful;
import com.jdon.util.Debug;

/**
 * StatefulInterceptor is a Interceptor of
 * creating target object.
 * must be the last in Interceptors.
 * 
 * it active for all pojoServices that implements 
 * com.jdon.controller.service.StatefulPOJOService
 * 
 * this class stateful function is using ComponentVisitor's
 * cache, such as HttpSession.
 * 
 * @author <a href="mailto:banqiao@jdon.com">banq</a>
 *
 */
public class StatefulInterceptor implements MethodInterceptor {
    private final static String module = StatefulInterceptor.class.getName();
    
    private List isStatefulCache = new ArrayList();
    
    private List unStatefulCache = new ArrayList();
    
    private ContainerCallback containerCallback;
    
    
    /**
     * @param containerCallback
     */
    public StatefulInterceptor(ContainerCallback containerCallback) {
        super();
        this.containerCallback = containerCallback;
    }
    
    /* 
     * check the pojoService if implements StatefulPOJOService
     * using ComponentVisitor, we can get targetObjRef from httpsession.
     * 
     * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
     */
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Debug.logVerbose(" enter StatefulInterceptor", module);
        ProxyMethodInvocation pmi = (ProxyMethodInvocation)invocation;
        TargetMetaRequest targetMetaRequest = pmi.getTargetMetaRequest();
        TargetMetaDef targetMetaDef = targetMetaRequest.getTargetMetaDef();
        if (targetMetaDef.isEJB())
            return invocation.proceed();

        if (!isStateful(targetMetaDef)){
            Debug.logVerbose(" target service is not Stateful: "
                    + targetMetaDef.getClassName() + " StatefulInterceptor unactiive", module);
            return invocation.proceed();
        }
        Object result = null;
        try {            
            ComponentVisitor cm = targetMetaRequest.getComponentVisitor();
            targetMetaRequest.setVisitableName(ComponentKeys.TARGETSERVICE_FACTORY);
            Debug.logVerbose(ComponentKeys.TARGETSERVICE_FACTORY
                    + " in action (Stateful)", module);
            Object targetObjRef = cm.visit(targetMetaRequest);
            pmi.setThis(targetObjRef);     
            result = invocation.proceed();
        }catch(Exception ex){
            Debug.logError("StatefulInterceptor error: " + ex, module);
        }
        return result;
    }

    public boolean isStateful(TargetMetaDef targetMetaDef) {        
        boolean found = false;
        if (isStatefulCache.contains(targetMetaDef.getName())) {
            found = true;
        }else if(!unStatefulCache.contains(targetMetaDef.getName())){
            Debug.logVerbose(" check if it is a isStateful", module);
            ContainerWrapper containerWrapper = containerCallback.getContainerWrapper();
            Class thisCLass = containerWrapper.getComponentClass(targetMetaDef.getName());
            if (Stateful.class.isAssignableFrom(thisCLass)) {
                found = true;
                isStatefulCache.add(targetMetaDef.getName());
            }else{
                unStatefulCache.add(targetMetaDef.getName());
            }
        }
        return found;
    }
    
    
}

⌨️ 快捷键说明

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