001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork.interceptor;
006:
007: import com.opensymphony.xwork.ActionContext;
008: import com.opensymphony.xwork.ActionInvocation;
009: import com.opensymphony.xwork.config.entities.ActionConfig;
010: import com.opensymphony.xwork.config.entities.Parameterizable;
011: import com.opensymphony.xwork.util.OgnlValueStack;
012: import com.opensymphony.xwork.util.TextParseUtil;
013:
014: import java.util.Iterator;
015: import java.util.Map;
016:
017: /**
018: * <!-- START SNIPPET: description -->
019: *
020: * This interceptor populates the action with the static parameters defined in the action configuration. If the action
021: * implements {@link Parameterizable}, a map of the static parameters will be also be passed directly to the action.
022: *
023: * <p/> Parameters are typically defined with <param> elements within xwork.xml.
024: *
025: * <!-- END SNIPPET: description -->
026: *
027: * <p/> <u>Interceptor parameters:</u>
028: *
029: * <!-- START SNIPPET: parameters -->
030: *
031: * <ul>
032: *
033: * <li>None</li>
034: *
035: * </ul>
036: *
037: * <!-- END SNIPPET: parameters -->
038: *
039: * <p/> <u>Extending the interceptor:</u>
040: *
041: * <!-- START SNIPPET: extending -->
042: *
043: * <p/>There are no extension points to this interceptor.
044: *
045: * <!-- END SNIPPET: extending -->
046: *
047: * <p/> <u>Example code:</u>
048: *
049: * <pre>
050: * <!-- START SNIPPET: example -->
051: * <action name="someAction" class="com.examples.SomeAction">
052: * <interceptor-ref name="static-params">
053: * <param name="parse">true</param>
054: * </interceptor-ref>
055: * <result name="success">good_result.ftl</result>
056: * </action>
057: * <!-- END SNIPPET: example -->
058: * </pre>
059: *
060: * @author Patrick Lightbody
061: * @author tmjee
062: *
063: * @version $Date: 2007-02-02 13:29:36 +0100 (Fr, 02 Feb 2007) $ $Id: StaticParametersInterceptor.java 1325 2007-02-02 12:29:36Z tm_jee $
064: */
065: public class StaticParametersInterceptor extends AroundInterceptor {
066:
067: private static final long serialVersionUID = -5364822129178790799L;
068:
069: private boolean parse;
070:
071: public void setParse(String value) {
072: this .parse = Boolean.valueOf(value).booleanValue();
073: }
074:
075: protected void after(ActionInvocation invocation, String result)
076: throws Exception {
077: }
078:
079: protected void before(ActionInvocation invocation) throws Exception {
080: ActionConfig config = invocation.getProxy().getConfig();
081: Object action = invocation.getAction();
082:
083: final Map parameters = config.getParams();
084:
085: if (log.isDebugEnabled()) {
086: log.debug("Setting static parameters " + parameters);
087: }
088:
089: // for actions marked as Parameterizable, pass the static parameters directly
090: if (action instanceof Parameterizable) {
091: ((Parameterizable) action).setParams(parameters);
092: }
093:
094: if (parameters != null) {
095: final OgnlValueStack stack = ActionContext.getContext()
096: .getValueStack();
097:
098: for (Iterator iterator = parameters.entrySet().iterator(); iterator
099: .hasNext();) {
100: Map.Entry entry = (Map.Entry) iterator.next();
101: Object val = entry.getValue();
102: if (parse && val instanceof String) {
103: val = TextParseUtil.translateVariables(
104: (String) val, stack);
105: }
106: stack.setValue(entry.getKey().toString(), val);
107: }
108: }
109: }
110: }
|