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

📄 jworkspicoactiondispatcher.java

📁 jworks program
💻 JAVA
字号:
/**
 * $RCSfile: JWorksPicoActionDispatcher.java,v $
 * $Revision: 1.2 $
 * $Date: 2004/08/26 11:10:26 $
 *
 * Copyright (C) 2002-2004 softme.org (Softme Studio). All rights reserved.
 * ===================================================================
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL COOLSERVLETS INC. OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 */

package org.softme.jworks.dispatcher;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.turbine.util.RunData;
import org.nanocontainer.nanowar.ServletRequestContainerLauncher;
import org.softme.jworks.ActionDispatcher;
import org.softme.jworks.util.parser.JWorksParameterParser;

import com.opensymphony.webwork.dispatcher.ApplicationMap;
import com.opensymphony.webwork.dispatcher.ServletDispatcher;
import com.opensymphony.xwork.ActionProxy;
import com.opensymphony.xwork.ActionProxyFactory;
import com.opensymphony.xwork.config.ConfigurationException;

/**
 * JWorks 使用PicoContainer的ActionDispatcher实现类
 * @author <a href="mailto:brokendoor@sina.com">boon xiong</a>
 * @version CVS $Revision: 1.2 $ $Date: 2004/08/26 11:10:26 $
 */
public class JWorksPicoActionDispatcher extends ServletDispatcher implements ActionDispatcher {
    
	
    private ServletContext context;
	private ServletConfig config;
	private JWorksParameterParser pp;
	

	public JWorksPicoActionDispatcher () {
        super();
        ActionProxyFactory.setFactory(new JWorksPicoActionProxyFactory());
    }
    
	/* (non-Javadoc)
	 * @see javax.servlet.Servlet#getServletConfig()
	 */
	public ServletConfig getServletConfig() {
		return this.config;
	}
	/* (non-Javadoc)
	 * @see javax.servlet.ServletConfig#getServletContext()
	 */
	public ServletContext getServletContext() {

		return this.context;
	}
	
	/* (non-Javadoc)
	 * @see com.opensymphony.webwork.dispatcher.ServletDispatcher#getApplicationMap()
	 */
	protected Map getApplicationMap() {
		return new ApplicationMap(context);
	}
	
    public void init(RunData data) throws ServletException {
    	this.context = data.getServletContext();
    	this.config = data.getServletConfig();
    	
    	// ParameterParser 
    	this.pp = new JWorksParameterParser();
    	pp.setRequest(data.getRequest());
    	
        // init the servlet using webwork2
        super.init(data.getServletConfig());

    }

	/**
	 * 覆盖WebWork ServletDispatcher 的Action路径解析
	 * 
	 * @see com.opensymphony.webwork.dispatcher.ServletDispatcher#getActionName(javax.servlet.http.HttpServletRequest)
	 */
	protected String getActionName(HttpServletRequest request) {
		return pp.getActionName(pp.getPathInfo(request));
	}
	
	/**
	 * 覆盖WebWork ServletDispatcher 的路径解析
	 * 
	 */
	protected String getNameSpace(HttpServletRequest request) {
		
		return getNamespaceFromServletPath(request.getPathInfo());
	}
	
    public HashMap createMap(RunData data) throws Exception {
        HttpServletRequest request = data.getRequest();
        HttpServletResponse response = data.getResponse();
        request = wrapRequest(request);

        HashMap extraContext = createContextMap( 
                getRequestMap(request), getParameterMap(request), getSessionMap(request), getApplicationMap(),
                request, response, getServletConfig());
        extraContext.put(SERLVET_DISPATCHER, this);       
        extraContext.put(JWORKS_RUNDATA, data);
        return extraContext;    
    }
    
    
    public void serviceAction(RunData data, HashMap extraContext) throws Exception {
        ServletRequestContainerLauncher containerLauncher = new ServletRequestContainerLauncher(data.getServletContext(), data.getRequest());
        
        HttpServletRequest request = data.getRequest();
        HttpServletResponse response = data.getResponse();
        try {
            containerLauncher.startContainer();
            
//            request = wrapRequest(request);
            
            ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(getNameSpace(request), getActionName(request), extraContext);
            request.setAttribute("webwork.valueStack",
                    proxy.getInvocation().getStack());
            proxy.execute();
        } catch (IOException e) {
            String message = "Could not wrap servlet request with MultipartRequestWrapper!";
            log.error(message, e);
            throw new ServletException(message, e);
        } catch (ConfigurationException e) {
            log.error("Could not find action", e);
            throw e;
        } catch (Exception e) {
            log.error("Could not execute action", e);
            throw e;
        } finally {
            containerLauncher.killContainer();
        }
}
}

⌨️ 快捷键说明

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