Save This Page
Home » xwork-2.1.1-src » com.opensymphony.xwork2.interceptor » [javadoc | source]
    1   /*
    2    * Copyright (c) 2002-2007 by OpenSymphony
    3    * All rights reserved.
    4    */
    5   package com.opensymphony.xwork2.interceptor;
    6   
    7   import java.util.ArrayList;
    8   import java.util.Collection;
    9   import java.util.Collections;
   10   import java.util.Iterator;
   11   import java.util.List;
   12   import java.util.Map;
   13   
   14   import com.opensymphony.xwork2.ActionInvocation;
   15   import com.opensymphony.xwork2.Unchainable;
   16   import com.opensymphony.xwork2.inject.Inject;
   17   import com.opensymphony.xwork2.util.CompoundRoot;
   18   import com.opensymphony.xwork2.util.ValueStack;
   19   import com.opensymphony.xwork2.util.logging.Logger;
   20   import com.opensymphony.xwork2.util.logging.LoggerFactory;
   21   import com.opensymphony.xwork2.util.reflection.ReflectionProvider;
   22   
   23   
   24   /**
   25    * <!-- START SNIPPET: description -->
   26    *
   27    * An interceptor that copies all the properties of every object in the value stack to the currently executing object,
   28    * except for any object that implements {@link Unchainable}. A collection of optional <i>includes</i> and
   29    * <i>excludes</i> may be provided to control how and which parameters are copied. Only includes or excludes may be
   30    * specified. Specifying both results in undefined behavior. See the javadocs for {@link ReflectionProvider#copy(Object, Object,
   31    * java.util.Map, java.util.Collection, java.util.Collection)} for more information.
   32    *
   33    * <p/>
   34    * <b>Note:</b> It is important to remember that this interceptor does nothing if there are no objects already on the stack.
   35    * <br/>This means two things:
   36    * <br/><b>One</b>, you can safely apply it to all your actions without any worry of adverse affects.
   37    * <br/><b/>Two</b>, it is up to you to ensure an object exists in the stack prior to invoking this action. The most typical way this is done
   38    * is through the use of the <b>chain</b> result type, which combines with this interceptor to make up the action
   39    * chaining feature.
   40    *
   41    * <!-- END SNIPPET: description -->
   42    *
   43    * <p/> <u>Interceptor parameters:</u>
   44    *
   45    * <!-- START SNIPPET: parameters -->
   46    *
   47    * <ul>
   48    *
   49    * <li>excludes (optional) - the list of parameter names to exclude from copying (all others will be included).</li>
   50    *
   51    * <li>includes (optional) - the list of parameter names to include when copying (all others will be excluded).</li>
   52    *
   53    * </ul>
   54    *
   55    * <!-- END SNIPPET: parameters -->
   56    *
   57    * <p/> <u>Extending the interceptor:</u>
   58    *
   59    * <p/>
   60    *
   61    * <!-- START SNIPPET: extending -->
   62    *
   63    * There are no known extension points to this interceptor.
   64    *
   65    * <!-- END SNIPPET: extending -->
   66    *
   67    * <p/> <u>Example code:</u>
   68    *
   69    * <pre>
   70    * <!-- START SNIPPET: example -->
   71    * 
   72    * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
   73    *     &lt;interceptor-ref name="basicStack"/&gt;
   74    *     &lt;result name="success" type="chain"&gt;otherAction&lt;/result&gt;
   75    * &lt;/action&gt;
   76    *
   77    * &lt;action name="otherAction" class="com.examples.OtherAction"&gt;
   78    *     &lt;interceptor-ref name="chain"/&gt;
   79    *     &lt;interceptor-ref name="basicStack"/&gt;
   80    *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
   81    * &lt;/action&gt;
   82    * 
   83    * <!-- END SNIPPET: example -->
   84    * </pre>
   85    *
   86    * @see com.opensymphony.xwork2.ActionChainResult
   87    * @author mrdon
   88    * @author tm_jee ( tm_jee(at)yahoo.co.uk )
   89    */
   90   public class ChainingInterceptor extends AbstractInterceptor {
   91   	
   92   	private static final Logger LOG = LoggerFactory.getLogger(ChainingInterceptor.class);
   93   	
   94       protected Collection excludes;
   95       protected Collection includes;
   96       
   97       protected ReflectionProvider reflectionProvider;
   98       
   99       @Inject
  100       public void setReflectionProvider(ReflectionProvider prov) {
  101           this.reflectionProvider = prov;
  102       }
  103   
  104       public String intercept(ActionInvocation invocation) throws Exception {
  105           ValueStack stack = invocation.getStack();
  106           CompoundRoot root = stack.getRoot();
  107   
  108           if (root.size() > 1) {
  109               List list = new ArrayList(root);
  110               list.remove(0);
  111               Collections.reverse(list);
  112   
  113               Map ctxMap = invocation.getInvocationContext().getContextMap();
  114               Iterator iterator = list.iterator();
  115               int index = 1; // starts with 1, 0 has been removed
  116               while (iterator.hasNext()) {
  117               	index = index + 1;
  118                   Object o = iterator.next();
  119                   if (o != null) {
  120                   	if (!(o instanceof Unchainable)) {
  121                   		reflectionProvider.copy(o, invocation.getAction(), ctxMap, excludes, includes);
  122                   	}
  123                   }
  124                   else {
  125                   	LOG.warn("compound root element at index "+index+" is null");
  126                   }
  127               }
  128           }
  129           
  130           return invocation.invoke();
  131       }
  132       
  133       /**
  134        * Gets list of parameter names to exclude
  135        *
  136        * @return the exclude list
  137        */
  138       public Collection getExcludes() {
  139           return excludes;
  140       }
  141   
  142       /**
  143        * Sets the list of parameter names to exclude from copying (all others will be included).
  144        *
  145        * @param excludes  the excludes list
  146        */
  147       public void setExcludes(Collection excludes) {
  148           this.excludes = excludes;
  149       }
  150   
  151       /**
  152        * Gets list of parameter names to include
  153        *
  154        * @return the include list
  155        */
  156       public Collection getIncludes() {
  157           return includes;
  158       }
  159   
  160       /**
  161        * Sets the list of parameter names to include when copying (all others will be excluded).
  162        *
  163        * @param includes  the includes list
  164        */
  165       public void setIncludes(Collection includes) {
  166           this.includes = includes;
  167       }
  168   
  169   }

Save This Page
Home » xwork-2.1.1-src » com.opensymphony.xwork2.interceptor » [javadoc | source]