Home » xwork-2.1.5 » 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 com.opensymphony.xwork2.ActionInvocation;
    8   import com.opensymphony.xwork2.util.logging.Logger;
    9   import com.opensymphony.xwork2.util.logging.LoggerFactory;
   10   
   11   
   12   /**
   13    * <!-- START SNIPPET: description -->
   14    * This interceptor logs the start and end of the execution an action (in English-only, not internationalized).
   15    * <br/>
   16    * <b>Note:</b>: This interceptor will log at <tt>INFO</tt> level.
   17    * <p/>
   18    * <!-- END SNIPPET: description -->
   19    *
   20    * <!-- START SNIPPET: parameters -->
   21    * There are no parameters for this interceptor.
   22    * <!-- END SNIPPET: parameters -->
   23    *
   24    * <!-- START SNIPPET: extending -->
   25    * There are no obvious extensions to the existing interceptor.
   26    * <!-- END SNIPPET: extending -->
   27    *
   28    * <pre>
   29    * <!-- START SNIPPET: example -->
   30    * &lt;!-- prints out a message before and after the immediate action execution --&gt;
   31    * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
   32    *     &lt;interceptor-ref name="completeStack"/&gt;
   33    *     &lt;interceptor-ref name="logger"/&gt;
   34    *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
   35    * &lt;/action&gt;
   36    *
   37    * &lt;!-- prints out a message before any more interceptors continue and after they have finished --&gt;
   38    * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
   39    *     &lt;interceptor-ref name="logger"/&gt;
   40    *     &lt;interceptor-ref name="completeStack"/&gt;
   41    *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
   42    * &lt;/action&gt;
   43    * <!-- END SNIPPET: example -->
   44    * </pre>
   45    *
   46    * @author Jason Carreira
   47    */
   48   public class LoggingInterceptor extends AbstractInterceptor {
   49       private static final Logger LOG = LoggerFactory.getLogger(LoggingInterceptor.class);
   50       private static final String FINISH_MESSAGE = "Finishing execution stack for action ";
   51       private static final String START_MESSAGE = "Starting execution stack for action ";
   52   
   53       @Override
   54       public String intercept(ActionInvocation invocation) throws Exception {
   55           logMessage(invocation, START_MESSAGE);
   56           String result = invocation.invoke();
   57           logMessage(invocation, FINISH_MESSAGE);
   58           return result;
   59       }
   60   
   61       private void logMessage(ActionInvocation invocation, String baseMessage) {
   62           if (LOG.isInfoEnabled()) {
   63               StringBuilder message = new StringBuilder(baseMessage);
   64               String namespace = invocation.getProxy().getNamespace();
   65   
   66               if ((namespace != null) && (namespace.trim().length() > 0)) {
   67                   message.append(namespace).append("/");
   68               }
   69   
   70               message.append(invocation.getProxy().getActionName());
   71               LOG.info(message.toString());
   72           }
   73       }
   74   
   75   }

Home » xwork-2.1.5 » com.opensymphony » xwork2 » interceptor » [javadoc | source]