001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005:
006: package com.opensymphony.xwork.interceptor;
007:
008: import com.opensymphony.xwork.ActionContext;
009: import com.opensymphony.xwork.ActionInvocation;
010: import com.opensymphony.xwork.config.entities.ActionConfig;
011: import com.opensymphony.xwork.util.OgnlValueStack;
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014:
015: import java.util.Iterator;
016: import java.util.Map;
017:
018: /**
019: * <!-- START SNIPPET: description -->
020: *
021: * The aim of this Interceptor is to alias a named parameter to a different named parameter. By acting as the glue
022: * between actions sharing similiar parameters (but with different names), it can help greatly with action chaining.
023: *
024: * <p/> Action's alias expressions should be in the form of #{ "name1" : "alias1", "name2" : "alias2" }. This means
025: * that assuming an action (or something else in the stack) has a value for the expression named <i>name1</i> and the
026: * action this interceptor is applied to has a setter named <i>alias1</i>, <i>alias1</i> will be set with the value from
027: * <i>name1</i>.
028: *
029: * <!-- END SNIPPET: description -->
030: *
031: * <p/> <u>Interceptor parameters:</u>
032: *
033: * <!-- START SNIPPET: parameters -->
034: *
035: * <ul>
036: *
037: * <li>aliasesKey (optional) - the name of the action parameter to look for the alias map (by default this is
038: * <i>aliases</i>).</li>
039: *
040: * </ul>
041: *
042: * <!-- END SNIPPET: parameters -->
043: *
044: * <p/> <u>Extending the interceptor:</u>
045: *
046: * <p/>
047: *
048: * <!-- START SNIPPET: extending -->
049: *
050: * This interceptor does not have any known extension points.
051: *
052: * <!-- END SNIPPET: extending -->
053: *
054: * <p/> <u>Example code:</u>
055: *
056: * <pre>
057: * <!-- START SNIPPET: example -->
058: * <action name="someAction" class="com.examples.SomeAction">
059: * <!-- The value for the foo parameter will be applied as if it were named bar -->
060: * <param name="aliases">#{ 'foo' : 'bar' }</param>
061: *
062: * <!-- note: the alias interceptor is included with the defaultStack in webwork-default.xml -->
063: * <interceptor-ref name="alias"/>
064: * <interceptor-ref name="basicStack"/>
065: * <result name="success">good_result.ftl</result>
066: * </action>
067: * <!-- END SNIPPET: example -->
068: * </pre>
069: *
070: * @author Matthew Payne
071: */
072: public class AliasInterceptor extends AroundInterceptor {
073: private static final Log log = LogFactory
074: .getLog(AliasInterceptor.class);
075: private static final String DEFAULT_ALIAS_KEY = "aliases";
076:
077: protected String aliasesKey = DEFAULT_ALIAS_KEY;
078:
079: public void setAliasesKey(String aliasesKey) {
080: this .aliasesKey = aliasesKey;
081: }
082:
083: public void destroy() {
084: }
085:
086: public void init() {
087: }
088:
089: protected void before(ActionInvocation invocation) throws Exception {
090: ActionConfig config = invocation.getProxy().getConfig();
091: ActionContext ac = invocation.getInvocationContext();
092:
093: // get the action's parameters
094: final Map parameters = config.getParams();
095:
096: if (parameters.containsKey(aliasesKey)) {
097:
098: String aliasExpression = (String) parameters
099: .get(aliasesKey);
100: OgnlValueStack stack = ac.getValueStack();
101: Object obj = stack.findValue(aliasExpression);
102:
103: if (obj != null && obj instanceof Map) {
104: // override
105: Map aliases = (Map) obj;
106: Iterator itr = aliases.entrySet().iterator();
107: while (itr.hasNext()) {
108: Map.Entry entry = (Map.Entry) itr.next();
109: String name = entry.getKey().toString();
110: String alias = (String) entry.getValue();
111: Object value = stack.findValue(name);
112: if (null == value) {
113: // workaround
114: Map contextParameters = (Map) stack
115: .getContext().get("parameters");
116:
117: if (null != contextParameters) {
118: value = contextParameters.get(name);
119: }
120: }
121: if (null != value) {
122: stack.setValue(alias, value);
123: }
124: }
125: } else {
126: log.debug("invalid alias expression:" + aliasesKey);
127: }
128: }
129: }
130:
131: protected void after(ActionInvocation invocation, String result)
132: throws Exception {
133: }
134: }
|