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

📄 messagestoreinterceptor.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:   MessageStoreInterceptor.java

package org.apache.struts2.interceptor;

import com.opensymphony.xwork2.*;
import com.opensymphony.xwork2.interceptor.Interceptor;
import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MessageStoreInterceptor
	implements Interceptor
{

	private static final long serialVersionUID = 0x3e56c7f08ca35974L;
	private static final Log _log = LogFactory.getLog(org/apache/struts2/interceptor/MessageStoreInterceptor);
	public static final String STORE_MODE = "STORE";
	public static final String RETRIEVE_MODE = "RETRIEVE";
	public static final String NONE = "NONE";
	private boolean allowRequestParameterSwitch;
	private String requestParameterSwitch;
	private String operationMode;
	public static String fieldErrorsSessionKey = "__MessageStoreInterceptor_FieldErrors_SessionKey";
	public static String actionErrorsSessionKey = "__MessageStoreInterceptor_ActionErrors_SessionKey";
	public static String actionMessagesSessionKey = "__MessageStoreInterceptor_ActionMessages_SessionKey";

	public MessageStoreInterceptor()
	{
		allowRequestParameterSwitch = true;
		requestParameterSwitch = "operationMode";
		operationMode = "NONE";
	}

	public void setAllowRequestParameterSwitch(boolean allowRequestParameterSwitch)
	{
		this.allowRequestParameterSwitch = allowRequestParameterSwitch;
	}

	public boolean getAllowRequestParameterSwitch()
	{
		return allowRequestParameterSwitch;
	}

	public void setRequestParameterSwitch(String requestParameterSwitch)
	{
		this.requestParameterSwitch = requestParameterSwitch;
	}

	public String getRequestParameterSwitch()
	{
		return requestParameterSwitch;
	}

	public void setOperationMode(String operationMode)
	{
		this.operationMode = operationMode;
	}

	public String getOperationModel()
	{
		return operationMode;
	}

	public void destroy()
	{
	}

	public void init()
	{
	}

	public String intercept(ActionInvocation invocation)
		throws Exception
	{
		_log.debug("entering MessageStoreInterceptor ...");
		before(invocation);
		String result = invocation.invoke();
		after(invocation, result);
		_log.debug("exit executing MessageStoreInterceptor");
		return result;
	}

	protected void before(ActionInvocation invocation)
		throws Exception
	{
		String reqOperationMode = getRequestOperationMode(invocation);
		if ("RETRIEVE".equalsIgnoreCase(reqOperationMode) || "RETRIEVE".equalsIgnoreCase(operationMode))
		{
			Object action = invocation.getAction();
			if (action instanceof ValidationAware)
			{
				Map session = (Map)invocation.getInvocationContext().get("com.opensymphony.xwork2.ActionContext.session");
				ValidationAware validationAwareAction = (ValidationAware)action;
				_log.debug((new StringBuilder()).append("retrieve error / message from session to populate into action [").append(action).append("]").toString());
				Collection actionErrors = (Collection)session.get(actionErrorsSessionKey);
				Collection actionMessages = (Collection)session.get(actionMessagesSessionKey);
				Map fieldErrors = (Map)session.get(fieldErrorsSessionKey);
				if (actionErrors != null && actionErrors.size() > 0)
				{
					Collection mergedActionErrors = mergeCollection(validationAwareAction.getActionErrors(), actionErrors);
					validationAwareAction.setActionErrors(mergedActionErrors);
				}
				if (actionMessages != null && actionMessages.size() > 0)
				{
					Collection mergedActionMessages = mergeCollection(validationAwareAction.getActionMessages(), actionMessages);
					validationAwareAction.setActionMessages(mergedActionMessages);
				}
				if (fieldErrors != null && fieldErrors.size() > 0)
				{
					Map mergedFieldErrors = mergeMap(validationAwareAction.getFieldErrors(), fieldErrors);
					validationAwareAction.setFieldErrors(mergedFieldErrors);
				}
				session.remove(actionErrorsSessionKey);
				session.remove(actionMessagesSessionKey);
				session.remove(fieldErrorsSessionKey);
			}
		}
	}

	protected void after(ActionInvocation invocation, String result)
		throws Exception
	{
		String reqOperationMode = getRequestOperationMode(invocation);
		if ("STORE".equalsIgnoreCase(reqOperationMode) || "STORE".equalsIgnoreCase(operationMode))
		{
			Object action = invocation.getAction();
			if (action instanceof ValidationAware)
			{
				Map session = (Map)invocation.getInvocationContext().get("com.opensymphony.xwork2.ActionContext.session");
				_log.debug((new StringBuilder()).append("store action [").append(action).append("] error/messages into session ").toString());
				ValidationAware validationAwareAction = (ValidationAware)action;
				session.put(actionErrorsSessionKey, validationAwareAction.getActionErrors());
				session.put(actionMessagesSessionKey, validationAwareAction.getActionMessages());
				session.put(fieldErrorsSessionKey, validationAwareAction.getFieldErrors());
			} else
			{
				_log.debug((new StringBuilder()).append("Action [").append(action).append("] is not ValidationAware, no message / error that are storeable").toString());
			}
		}
	}

	protected String getRequestOperationMode(ActionInvocation invocation)
	{
		String reqOperationMode = "NONE";
		if (allowRequestParameterSwitch)
		{
			Map reqParams = (Map)invocation.getInvocationContext().get("com.opensymphony.xwork2.ActionContext.parameters");
			boolean containsParameter = reqParams.containsKey(requestParameterSwitch);
			if (containsParameter)
			{
				String reqParamsArr[] = (String[])(String[])reqParams.get(requestParameterSwitch);
				if (reqParamsArr != null && reqParamsArr.length > 0)
					reqOperationMode = reqParamsArr[0];
			}
		}
		return reqOperationMode;
	}

	protected Collection mergeCollection(Collection col1, Collection col2)
	{
		Collection _col1 = ((Collection) (col1 != null ? col1 : ((Collection) (new ArrayList()))));
		Collection _col2 = ((Collection) (col2 != null ? col2 : ((Collection) (new ArrayList()))));
		_col1.addAll(_col2);
		return _col1;
	}

	protected Map mergeMap(Map map1, Map map2)
	{
		Map _map1 = ((Map) (map1 != null ? map1 : ((Map) (new LinkedHashMap()))));
		Map _map2 = ((Map) (map2 != null ? map2 : ((Map) (new LinkedHashMap()))));
		_map1.putAll(_map2);
		return _map1;
	}

}

⌨️ 快捷键说明

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