find flow scope varibles in spring web flow

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.webflow.context.ExternalContext;
import org.springframework.webflow.context.ExternalContextHolder;
import org.springframework.webflow.context.servlet.ServletExternalContext;
import org.springframework.webflow.core.collection.MutableAttributeMap;
import org.springframework.webflow.execution.FlowExecution;
import org.springframework.webflow.execution.FlowExecutionKey;
import org.springframework.webflow.execution.FlowSession;
import org.springframework.webflow.execution.repository.FlowExecutionLock;
import org.springframework.webflow.execution.repository.FlowExecutionRepository;
import org.springframework.webflow.executor.FlowExecutorImpl;
import org.springframework.webflow.mvc.servlet.FlowHandlerAdapter;
/**
 * Util class to find flow scope variables in SWF
 *
 */
public class WebflowUtil {
   
    private static FlowHandlerAdapter flowHandler = null ;

    public void setFlowHandler(FlowHandlerAdapter flowHandler) {
        this.flowHandler = flowHandler;
    }
    
    /**
     * Find the flow scope variables
     * @param request
     * @param response
     * @param excutionKey
     * @param variableName
     * @return
     */
    public static Object findFlowScopeVariable(HttpServletRequest request,HttpServletResponse response,
            String excutionKey,String variableName){
        
        if (flowHandler != null)
        {
            ExternalContext externalContext = new ServletExternalContext(request.getSession().getServletContext(),
                    request, response);
            ExternalContextHolder.setExternalContext(externalContext);
            FlowExecutionRepository repository =
                ((FlowExecutorImpl)flowHandler.getFlowExecutor()).getExecutionRepository() ;
            FlowExecutionKey flowExecutionKey =
                repository.parseFlowExecutionKey(excutionKey) ;
            
            FlowExecutionLock lock = repository.getLock(flowExecutionKey);
             lock.lock();
             try {
                 FlowExecution execution = repository.getFlowExecution(flowExecutionKey);
                 FlowSession flowSession = execution.getActiveSession();
                MutableAttributeMap map = flowSession.getScope() ;
                Object obj = map.get(variableName);
                return obj ;
             } finally {
                 lock.unlock();
             }
        }
        return null ;
    }
}
posted on 2011-10-12 02:29  sunliho  阅读(801)  评论(0编辑  收藏  举报