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

📄 sessioncontextinterceptor.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.data.SessionContext;
import com.jdon.container.visitor.data.SessionContextAcceptable;
import com.jdon.util.Debug;

/**
 * interceptor the SessionContextAcceptable concrete services,
 * and inject the SessionContext object into them, so these application 
 * service can got the datas from session that saved by framework.
 * 
 * this interceptor must be after the targetservice creating interceptors
 * such as StatefulInterceptor or PoolInterceptor,
 *  
 * because this interceptor will inject SessionContext object into the existed 
 * targetservice object.
 * 
 * @author <a href="mailto:banqiao@jdon.com">banq</a>
 *
 */

public class SessionContextInterceptor implements MethodInterceptor {
    private final static String module = SessionContextInterceptor.class.getName();
    
    private List isSessionContextAcceptables = new ArrayList();
    
    private List unSessionContextAcceptables = new ArrayList();
    
    private ContainerCallback containerCallback;
    
    public SessionContextInterceptor(ContainerCallback containerCallback) {
        this.containerCallback = containerCallback;
    }
    
    /* 
     * inject SessionContext into targetObject.
     * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
     */
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Debug.logVerbose(" enter SessionContextInterceptor", module);
        ProxyMethodInvocation pmi = (ProxyMethodInvocation)invocation;
        TargetMetaRequest targetMetaRequest = pmi.getTargetMetaRequest();
        TargetMetaDef targetMetaDef = targetMetaRequest.getTargetMetaDef();
        if (targetMetaDef.isEJB())
            return invocation.proceed();

        if (!isSessionContextAcceptable(targetMetaDef)){
            Debug.logVerbose(" target service is not SessionContextAcceptable: "
                    + targetMetaDef.getClassName() + " SessionContextInterceptor unactiive", module);
            return invocation.proceed();
        }
        Object result = null;
        try {            
            Object targetObject = pmi.getThis();
            Debug.logVerbose(" targetObject should be SessionContextAcceptable: " + targetObject.getClass().getName(), module);
            SessionContextAcceptable myResult = (SessionContextAcceptable)targetObject;
            targetMetaRequest.setVisitableName(ComponentKeys.SESSIONCONTEXT_FACTORY);        
            SessionContext sessionContext = targetMetaRequest.getComponentVisitor().visitSessionContext(targetMetaRequest);
            myResult.setSessionContext(sessionContext);
            
            result = invocation.proceed();
            
        }catch(Exception ex){
            Debug.logError("SessionContextInterceptor error: " + ex, module);
        }
        return result;
    }

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

}

⌨️ 快捷键说明

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