
com.opensymphony.xwork.ActionChainResult Maven / Gradle / Ivy
/*
* Copyright (c) 2002-2006 by OpenSymphony
* All rights reserved.
*/
package com.opensymphony.xwork;
import com.opensymphony.xwork.interceptor.component.ComponentInterceptor;
import com.opensymphony.xwork.util.OgnlValueStack;
import com.opensymphony.xwork.util.TextParseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
/**
*
*
* This result invokes an entire other action, complete with it's own interceptor stack and result.
*
*
*
* This result type takes the following parameters:
*
*
*
*
*
* - actionName (default) - the name of the action that will be chained to
*
* - namespace - used to determine which namespace the Action is in that we're chaining. If namespace is null,
* this defaults to the current namespace
*
* - method - used to specify another method on target action to be invoked.
* If null, this defaults to execute method
*
*
*
*
*
* Example:
*
*
* <package name="public" extends="webwork-default">
* <!-- Chain creatAccount to login, using the default parameter -->
* <action name="createAccount" class="...">
* <result type="chain">login</result>
* </action>
*
* <action name="login" class="...">
* <!-- Chain to another namespace -->
* <result type="chain">
* <param name="actionName">dashboard</param>
* <param name="namespace">/secure</param>
* </result>
* </action>
* </package>
*
* <package name="secure" extends="webwork-default" namespace="/secure">
* <action name="dashboard" class="...">
* <result>dashboard.jsp</result>
* </action>
* </package>
*
*
* @author Alexandru Popescu
*/
public class ActionChainResult implements Result {
private static final Log log = LogFactory.getLog(ActionChainResult.class);
public static final String DEFAULT_PARAM = "actionName";
private static final String CHAIN_HISTORY = "CHAIN_HISTORY";
private ActionProxy proxy;
private String actionName;
private String namespace;
private String methodName;
public void setActionName(String actionName) {
this.actionName = actionName;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public void setMethod(String method) {
this.methodName = method;
}
public ActionProxy getProxy() {
return proxy;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ActionChainResult)) {
return false;
}
final ActionChainResult actionChainResult = (ActionChainResult) o;
if ((actionName != null) ? (!actionName.equals(actionChainResult.actionName)) : (actionChainResult.actionName != null))
{
return false;
}
return true;
}
/**
* @param invocation the DefaultActionInvocation calling the action call stack
*/
public void execute(ActionInvocation invocation) throws Exception {
// if the finalNamespace wasn't explicitly defined, assume the current one
if (this.namespace == null) {
this.namespace = invocation.getProxy().getNamespace();
}
OgnlValueStack stack = ActionContext.getContext().getValueStack();
String finalNamespace = TextParseUtil.translateVariables(namespace, stack);
String finalActionName = TextParseUtil.translateVariables(actionName, stack);
String finalMethodName = this.methodName != null
? TextParseUtil.translateVariables(this.methodName, stack)
: null;
if (isInChainHistory(finalNamespace, finalActionName, finalMethodName)) {
throw new XworkException("infinite recursion detected");
}
addToHistory(finalNamespace, finalActionName, finalMethodName);
HashMap extraContext = new HashMap();
extraContext.put(ActionContext.VALUE_STACK, ActionContext.getContext().getValueStack());
extraContext.put(ActionContext.PARAMETERS, ActionContext.getContext().getParameters());
extraContext.put(ComponentInterceptor.COMPONENT_MANAGER, ActionContext.getContext().get(ComponentInterceptor.COMPONENT_MANAGER));
extraContext.put(CHAIN_HISTORY, ActionContext.getContext().get(CHAIN_HISTORY));
if (log.isDebugEnabled()) {
log.debug("Chaining to action " + finalActionName);
}
proxy = ActionProxyFactory.getFactory().createActionProxy(finalNamespace, finalActionName, extraContext);
if (null != finalMethodName) {
proxy.setMethod(finalMethodName);
}
proxy.execute();
}
public int hashCode() {
return ((actionName != null) ? actionName.hashCode() : 0);
}
private boolean isInChainHistory(String namespace, String actionName, String methodName) {
Set chainHistory = (Set) ActionContext.getContext().get(CHAIN_HISTORY);
if (chainHistory == null) {
return false;
} else {
return chainHistory.contains(makeKey(namespace, actionName, methodName));
}
}
private void addToHistory(String namespace, String actionName, String methodName) {
Set chainHistory = (Set) ActionContext.getContext().get(CHAIN_HISTORY);
if (chainHistory == null) {
chainHistory = new HashSet();
}
ActionContext.getContext().put(CHAIN_HISTORY, chainHistory);
chainHistory.add(makeKey(namespace, actionName, methodName));
}
private String makeKey(String namespace, String actionName, String methodName) {
if (null == methodName) {
return namespace + "/" + actionName;
}
return namespace + "/" + actionName + "!" + methodName;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy