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

📄 jsr168dispatcher.java

📁 struts 2 核心包 的源码 有错误是难免的
💻 JAVA
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   Jsr168Dispatcher.java

package org.apache.struts2.portlet.dispatcher;

import com.opensymphony.xwork2.*;
import com.opensymphony.xwork2.config.*;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.util.*;
import java.io.IOException;
import java.util.*;
import javax.portlet.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.StrutsException;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.portlet.*;
import org.apache.struts2.portlet.servlet.*;
import org.apache.struts2.util.AttributeMap;

public class Jsr168Dispatcher extends GenericPortlet
	implements StrutsStatics, PortletActionConstants
{

	private static final Log LOG = LogFactory.getLog(org/apache/struts2/portlet/dispatcher/Jsr168Dispatcher);
	private ActionProxyFactory factory;
	private Map modeMap;
	private Map actionMap;
	private String portletNamespace;
	private Dispatcher dispatcherUtils;
	private ActionMapper actionMapper;

	public Jsr168Dispatcher()
	{
		factory = null;
		modeMap = new HashMap(3);
		actionMap = new HashMap(3);
		portletNamespace = null;
	}

	public void init(PortletConfig cfg)
		throws PortletException
	{
		super.init(cfg);
		LOG.debug((new StringBuilder()).append("Initializing portlet ").append(getPortletName()).toString());
		Map params = new HashMap();
		String name;
		String value;
		for (Enumeration e = cfg.getInitParameterNames(); e.hasMoreElements(); params.put(name, value))
		{
			name = (String)e.nextElement();
			value = cfg.getInitParameter(name);
		}

		Dispatcher.setPortletSupportActive(true);
		dispatcherUtils = new Dispatcher(new PortletServletContext(cfg.getPortletContext()), params);
		dispatcherUtils.init();
		if (factory == null)
			factory = (ActionProxyFactory)dispatcherUtils.getConfigurationManager().getConfiguration().getContainer().getInstance(com/opensymphony/xwork2/ActionProxyFactory);
		portletNamespace = cfg.getInitParameter("portletNamespace");
		LOG.debug((new StringBuilder()).append("PortletNamespace: ").append(portletNamespace).toString());
		parseModeConfig(cfg, PortletMode.VIEW, "viewNamespace", "defaultViewAction");
		parseModeConfig(cfg, PortletMode.EDIT, "editNamespace", "defaultEditAction");
		parseModeConfig(cfg, PortletMode.HELP, "helpNamespace", "defaultHelpAction");
		parseModeConfig(cfg, new PortletMode("config"), "configNamespace", "defaultConfigAction");
		parseModeConfig(cfg, new PortletMode("about"), "aboutNamespace", "defaultAboutAction");
		parseModeConfig(cfg, new PortletMode("print"), "printNamespace", "defaultPrintAction");
		parseModeConfig(cfg, new PortletMode("preview"), "previewNamespace", "defaultPreviewAction");
		parseModeConfig(cfg, new PortletMode("edit_defaults"), "editDefaultsNamespace", "defaultEditDefaultsAction");
		if (!TextUtils.stringSet(portletNamespace))
			portletNamespace = "";
		LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
		Container container = dispatcherUtils.getContainer();
		if ("true".equalsIgnoreCase((String)container.getInstance(java/lang/String, "struts.configuration.xml.reload")))
			FileManager.setReloadingConfigs(true);
		actionMapper = (ActionMapper)container.getInstance(org/apache/struts2/dispatcher/mapper/ActionMapper);
	}

	private void parseModeConfig(PortletConfig portletConfig, PortletMode portletMode, String nameSpaceParam, String defaultActionParam)
	{
		String namespace = portletConfig.getInitParameter(nameSpaceParam);
		if (!TextUtils.stringSet(namespace))
			namespace = "";
		modeMap.put(portletMode, namespace);
		String defaultAction = portletConfig.getInitParameter(defaultActionParam);
		if (!TextUtils.stringSet(defaultAction))
			defaultAction = "default";
		StringBuffer fullPath = new StringBuffer();
		if (TextUtils.stringSet(portletNamespace))
			fullPath.append(portletNamespace);
		if (TextUtils.stringSet(namespace))
			fullPath.append(namespace).append("/");
		else
			fullPath.append("/");
		fullPath.append(defaultAction);
		ActionMapping mapping = new ActionMapping();
		mapping.setName(getActionName(fullPath.toString()));
		mapping.setNamespace(getNamespace(fullPath.toString()));
		actionMap.put(portletMode, mapping);
	}

	public void processAction(ActionRequest request, ActionResponse response)
		throws PortletException, IOException
	{
		LOG.debug("Entering processAction");
		resetActionContext();
		serviceAction(request, response, getActionMapping(request), getRequestMap(request), getParameterMap(request), getSessionMap(request), getApplicationMap(), portletNamespace, EVENT_PHASE);
		LOG.debug("Leaving processAction");
		ActionContext.setContext(null);
		break MISSING_BLOCK_LABEL_75;
		Exception exception;
		exception;
		ActionContext.setContext(null);
		throw exception;
	}

	public void render(RenderRequest request, RenderResponse response)
		throws PortletException, IOException
	{
		LOG.debug("Entering render");
		resetActionContext();
		response.setTitle(getTitle(request));
		if (request.getWindowState().equals(WindowState.MINIMIZED))
			break MISSING_BLOCK_LABEL_101;
		serviceAction(request, response, getActionMapping(request), getRequestMap(request), getParameterMap(request), getSessionMap(request), getApplicationMap(), portletNamespace, RENDER_PHASE);
		LOG.debug("Leaving render");
		resetActionContext();
		break MISSING_BLOCK_LABEL_101;
		Exception exception;
		exception;
		resetActionContext();
		throw exception;
	}

	private void resetActionContext()
	{
		ActionContext.setContext(null);
	}

	public HashMap createContextMap(Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap, PortletRequest request, PortletResponse response, PortletConfig portletConfig, 
			Integer phase)
		throws IOException
	{
		javax.servlet.http.HttpServletResponse dummyResponse = new PortletServletResponse(response);
		javax.servlet.http.HttpServletRequest dummyRequest = new PortletServletRequest(request, getPortletContext());
		javax.servlet.ServletContext dummyServletContext = new PortletServletContext(getPortletContext());
		if (EVENT_PHASE.equals(phase))
			dummyRequest = dispatcherUtils.wrapRequest(dummyRequest, dummyServletContext);
		HashMap extraContext = new HashMap();
		extraContext.put("com.opensymphony.xwork2.dispatcher.HttpServletRequest", dummyRequest);
		extraContext.put("com.opensymphony.xwork2.dispatcher.HttpServletResponse", dummyResponse);
		extraContext.put("com.opensymphony.xwork2.dispatcher.ServletContext", dummyServletContext);
		extraContext.put("com.opensymphony.xwork2.ActionContext.parameters", parameterMap);
		extraContext.put("com.opensymphony.xwork2.ActionContext.session", sessionMap);
		extraContext.put("com.opensymphony.xwork2.ActionContext.application", applicationMap);
		String defaultLocale = (String)dispatcherUtils.getContainer().getInstance(java/lang/String, "struts.locale");
		java.util.Locale locale = null;
		if (defaultLocale != null)
			locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());
		else
			locale = request.getLocale();
		extraContext.put("com.opensymphony.xwork2.ActionContext.locale", locale);
		extraContext.put("struts.portlet.context", getPortletContext());
		extraContext.put("struts.portlet.request", request);
		extraContext.put("struts.portlet.response", response);
		extraContext.put("struts.portlet.config", portletConfig);
		extraContext.put("struts.portlet.portletNamespace", portletNamespace);
		extraContext.put("struts.portlet.defaultActionForMode", actionMap.get(request.getPortletMode()));
		extraContext.put("request", requestMap);
		extraContext.put("session", sessionMap);
		extraContext.put("application", applicationMap);
		extraContext.put("parameters", parameterMap);
		extraContext.put("struts.portlet.modeNamespaceMap", modeMap);
		extraContext.put("struts.portlet.phase", phase);
		AttributeMap attrMap = new AttributeMap(extraContext);
		extraContext.put("attr", attrMap);
		return extraContext;
	}

	public void serviceAction(PortletRequest request, PortletResponse response, ActionMapping mapping, Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap, 
			String portletNamespace, Integer phase)
		throws PortletException
	{
		Exception exception;
		LOG.debug("serviceAction");
		String actionName = mapping.getName();
		String namespace = mapping.getNamespace();
		Dispatcher.setInstance(dispatcherUtils);
		try
		{
			HashMap extraContext = createContextMap(requestMap, parameterMap, sessionMap, applicationMap, request, response, getPortletConfig(), phase);
			LOG.debug((new StringBuilder()).append("Creating action proxy for name = ").append(actionName).append(", namespace = ").append(namespace).toString());
			ActionProxy proxy = factory.createActionProxy(namespace, actionName, extraContext);
			proxy.setMethod(mapping.getMethod());
			request.setAttribute("struts.valueStack", proxy.getInvocation().getStack());
			proxy.execute();
		}
		catch (ConfigurationException e)
		{
			LOG.error("Could not find action", e);
			throw new PortletException((new StringBuilder()).append("Could not find action ").append(actionName).toString(), e);
		}
		catch (Exception e)
		{
			LOG.error("Could not execute action", e);
			throw new PortletException((new StringBuilder()).append("Error executing action ").append(actionName).toString(), e);
		}
		finally
		{
			Dispatcher.setInstance(null);
		}
		Dispatcher.setInstance(null);
		break MISSING_BLOCK_LABEL_249;
		throw exception;
	}

	protected Map getApplicationMap()
	{
		return new PortletApplicationMap(getPortletContext());
	}

	protected ActionMapping getActionMapping(PortletRequest request)
	{
		ActionMapping mapping = null;
		String actionPath = null;
		if (resetAction(request))
		{
			mapping = (ActionMapping)actionMap.get(request.getPortletMode());
		} else
		{
			actionPath = request.getParameter("struts.portlet.action");
			if (!TextUtils.stringSet(actionPath))
			{
				mapping = (ActionMapping)actionMap.get(request.getPortletMode());
			} else
			{
				PortletServletRequest httpRequest = new PortletServletRequest(request, getPortletContext());
				mapping = actionMapper.getMapping(httpRequest, dispatcherUtils.getConfigurationManager());
			}
		}
		if (mapping == null)
			throw new StrutsException((new StringBuilder()).append("Unable to locate action mapping for request, probably due to an invalid action path: ").append(actionPath).toString());
		else
			return mapping;
	}

	String getNamespace(String actionPath)
	{
		int idx = actionPath.lastIndexOf('/');
		String namespace = "";
		if (idx >= 0)
			namespace = actionPath.substring(0, idx);
		return namespace;
	}

	String getActionName(String actionPath)
	{
		int idx = actionPath.lastIndexOf('/');
		String action = actionPath;
		if (idx >= 0)
			action = actionPath.substring(idx + 1);
		return action;
	}

	protected Map getParameterMap(PortletRequest request)
		throws IOException
	{
		return new HashMap(request.getParameterMap());
	}

	protected Map getRequestMap(PortletRequest request)
	{
		return new PortletRequestMap(request);
	}

	protected Map getSessionMap(PortletRequest request)
	{
		return new PortletSessionMap(request);
	}

	protected void setActionProxyFactory(ActionProxyFactory factory)
	{
		this.factory = factory;
	}

	private boolean resetAction(PortletRequest request)
	{
		boolean reset = false;
		Map paramMap = request.getParameterMap();
		String modeParam[] = (String[])(String[])paramMap.get("struts.portlet.mode");
		if (modeParam != null && modeParam.length == 1)
		{
			String originatingMode = modeParam[0];
			String currentMode = request.getPortletMode().toString();
			if (!currentMode.equals(originatingMode))
				reset = true;
		}
		if (reset)
			request.setAttribute("struts.portlet.actionReset", Boolean.TRUE);
		else
			request.setAttribute("struts.portlet.actionReset", Boolean.FALSE);
		return reset;
	}

	public void destroy()
	{
		if (dispatcherUtils == null)
			LOG.warn("something is seriously wrong, DispatcherUtil is not initialized (null) ");
		else
			dispatcherUtils.cleanup();
	}

	public void setActionMapper(ActionMapper actionMapper)
	{
		this.actionMapper = actionMapper;
	}

}

⌨️ 快捷键说明

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