Save This Page
Home » xwork-2.1.1-src » com.opensymphony.xwork2.interceptor » [javadoc | source]
    1   /*
    2    * Copyright (c) 2002-2006 by OpenSymphony
    3    * All rights reserved.
    4    */
    5   package com.opensymphony.xwork2.interceptor;
    6   
    7   import java.util.Iterator;
    8   import java.util.Map;
    9   
   10   import com.opensymphony.xwork2.ActionContext;
   11   import com.opensymphony.xwork2.ActionInvocation;
   12   import com.opensymphony.xwork2.config.entities.ActionConfig;
   13   import com.opensymphony.xwork2.config.entities.Parameterizable;
   14   import com.opensymphony.xwork2.util.TextParseUtil;
   15   import com.opensymphony.xwork2.util.ValueStack;
   16   import com.opensymphony.xwork2.util.logging.Logger;
   17   import com.opensymphony.xwork2.util.logging.LoggerFactory;
   18   
   19   
   20   /**
   21    * <!-- START SNIPPET: description -->
   22    *
   23    * This interceptor populates the action with the static parameters defined in the action configuration. If the action
   24    * implements {@link Parameterizable}, a map of the static parameters will be also be passed directly to the action.
   25    *
   26    * <p/> Parameters are typically defined with &lt;param&gt; elements within xwork.xml.
   27    *
   28    * <!-- END SNIPPET: description -->
   29    *
   30    * <p/> <u>Interceptor parameters:</u>
   31    *
   32    * <!-- START SNIPPET: parameters -->
   33    *
   34    * <ul>
   35    *
   36    * <li>None</li>
   37    *
   38    * </ul>
   39    *
   40    * <!-- END SNIPPET: parameters -->
   41    *
   42    * <p/> <u>Extending the interceptor:</u>
   43    *
   44    * <!-- START SNIPPET: extending -->
   45    *
   46    * <p/>There are no extension points to this interceptor.
   47    *
   48    * <!-- END SNIPPET: extending -->
   49    *
   50    * <p/> <u>Example code:</u>
   51    *
   52    * <pre>
   53    * <!-- START SNIPPET: example -->
   54    * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
   55    *     &lt;interceptor-ref name="staticParams"&gt;
   56    *          &lt;param name="parse"&gt;true&lt;/param&gt;
   57    *     &lt;/interceptor-ref&gt;
   58    *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
   59    * &lt;/action&gt;
   60    * <!-- END SNIPPET: example -->
   61    * </pre>
   62    *
   63    * @author Patrick Lightbody
   64    */
   65   public class StaticParametersInterceptor extends AbstractInterceptor {
   66   
   67       private boolean parse;
   68       
   69       private static final Logger LOG = LoggerFactory.getLogger(StaticParametersInterceptor.class);
   70   
   71       public void setParse(String value) {
   72           this.parse = Boolean.valueOf(value).booleanValue();
   73       }
   74   
   75       public String intercept(ActionInvocation invocation) throws Exception {
   76           ActionConfig config = invocation.getProxy().getConfig();
   77           Object action = invocation.getAction();
   78   
   79           final Map parameters = config.getParams();
   80   
   81           if (LOG.isDebugEnabled()) {
   82               LOG.debug("Setting static parameters " + parameters);
   83           }
   84   
   85           // for actions marked as Parameterizable, pass the static parameters directly
   86           if (action instanceof Parameterizable) {
   87               ((Parameterizable) action).setParams(parameters);
   88           }
   89   
   90           if (parameters != null) {
   91               final ValueStack stack = ActionContext.getContext().getValueStack();
   92   
   93               for (Iterator iterator = parameters.entrySet().iterator();
   94                    iterator.hasNext();) {
   95                   Map.Entry entry = (Map.Entry) iterator.next();
   96                   stack.setValue(entry.getKey().toString(), entry.getValue());
   97                   Object val = entry.getValue();
   98                   if (parse && val instanceof String) {
   99                       val = TextParseUtil.translateVariables((String) val, stack);
  100                   }
  101                   stack.setValue(entry.getKey().toString(), val);
  102               }
  103           }
  104           return invocation.invoke();
  105       }
  106   }

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