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.ActionInvocation;
008: import com.opensymphony.xwork.Unchainable;
009: import com.opensymphony.xwork.util.CompoundRoot;
010: import com.opensymphony.xwork.util.OgnlUtil;
011: import com.opensymphony.xwork.util.OgnlValueStack;
012:
013: import java.util.*;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017:
018: /**
019: * <!-- START SNIPPET: description -->
020: *
021: * An interceptor that copies all the properties of every object in the value stack to the currently executing object,
022: * except for any object that implements {@link Unchainable}. A collection of optional <i>includes</i> and
023: * <i>excludes</i> may be provided to control how and which parameters are copied. Only includes or excludes may be
024: * specified. Specifying both results in undefined behavior. See the javadocs for {@link OgnlUtil#copy(Object, Object,
025: * java.util.Map, java.util.Collection, java.util.Collection)} for more information.
026: *
027: * <p/> It is important to remember that this interceptor does nothing if there are no objects already on the stack.
028: * This means two things: One, you can safely apply it to all your actions without any worry of adverse affects. Two, it
029: * is up to you to ensure an object exists in the stack prior to invoking this action. The most typical way this is done
030: * is through the use of the <b>chain</b> result type, which combines with this interceptor to make up the action
031: * chaining feature.
032: *
033: * <!-- END SNIPPET: description -->
034: *
035: * <p/> <u>Interceptor parameters:</u>
036: *
037: * <!-- START SNIPPET: parameters -->
038: *
039: * <ul>
040: *
041: * <li>excludes (optional) - the list of parameter names to exclude from copying (all others will be included).</li>
042: *
043: * <li>includes (optional) - the list of parameter names to include when copying (all others will be excluded).</li>
044: *
045: * </ul>
046: *
047: * <!-- END SNIPPET: parameters -->
048: *
049: * <p/> <u>Extending the interceptor:</u>
050: *
051: * <p/>
052: *
053: * <!-- START SNIPPET: extending -->
054: *
055: * There are no known extension points to this interceptor.
056: *
057: * <!-- END SNIPPET: extending -->
058: *
059: * <p/> <u>Example code:</u>
060: *
061: * <pre>
062: * <!-- START SNIPPET: example -->
063: *
064: * <action name="someAction" class="com.examples.SomeAction">
065: * <interceptor-ref name="basicStack"/>
066: * <result name="success" type="chain">otherAction</result>
067: * </action>
068: *
069: * <action name="otherAction" class="com.examples.OtherAction">
070: * <interceptor-ref name="chain"/>
071: * <interceptor-ref name="basicStack"/>
072: * <result name="success">good_result.ftl</result>
073: * </action>
074: *
075: * <!-- END SNIPPET: example -->
076: * </pre>
077: *
078: * @author $Author: rainerh $
079: * @author tm_jee ( tm_jee(at)yahoo.co.uk )
080: * @version $Revision: 860 $
081: */
082: public class ChainingInterceptor extends AroundInterceptor {
083:
084: private static final Log _log = LogFactory
085: .getLog(ChainingInterceptor.class);
086:
087: Collection excludes;
088: Collection includes;
089:
090: protected void after(ActionInvocation invocation, String result)
091: throws Exception {
092: }
093:
094: protected void before(ActionInvocation invocation) throws Exception {
095: OgnlValueStack stack = invocation.getStack();
096: CompoundRoot root = stack.getRoot();
097:
098: if (root.size() > 1) {
099: List list = new ArrayList(root);
100: list.remove(0);
101: Collections.reverse(list);
102:
103: Map ctxMap = invocation.getInvocationContext()
104: .getContextMap();
105: Iterator iterator = list.iterator();
106: int index = 1; // starts with 1, 0 has been removed
107: while (iterator.hasNext()) {
108: index = index + 1;
109: Object o = iterator.next();
110: if (o != null) {
111: if (!(o instanceof Unchainable)) {
112: OgnlUtil.copy(o, invocation.getAction(),
113: ctxMap, excludes, includes);
114: }
115: } else {
116: _log.warn("compound root element at index " + index
117: + " is null");
118: }
119: }
120: }
121: }
122:
123: public Collection getExcludes() {
124: return excludes;
125: }
126:
127: public void setExcludes(Collection excludes) {
128: this .excludes = excludes;
129: }
130:
131: public Collection getIncludes() {
132: return includes;
133: }
134:
135: public void setIncludes(Collection includes) {
136: this.includes = includes;
137: }
138: }
|