com.opensymphony.xwork.interceptor
Class TimerInterceptor

java.lang.Object
  extended bycom.opensymphony.xwork.interceptor.TimerInterceptor
All Implemented Interfaces:
Interceptor, Serializable

public class TimerInterceptor
extends Object
implements Interceptor

This interceptor logs the amount of time in milliseconds. In order for this interceptor to work properly, the logging framework must be set to at least the INFO level. This interceptor relies on the Commons Logging API to report its execution-time value.

The parameters above enables us to log all action execution times in our own logfile. This interceptor can be extended to provide custom message format. Users should override the invokeUnderTiming method.
 
 <!-- records only the action's execution time -->
 <action name="someAction" class="com.examples.SomeAction">
     <interceptor-ref name="completeStack"/>
     <interceptor-ref name="timer"/>
     <result name="success">good_result.ftl</result>
 </action>

 <!-- records action's execution time as well as other interceptors-->
 <action name="someAction" class="com.examples.SomeAction">
     <interceptor-ref name="timer"/>
     <interceptor-ref name="completeStack"/>
     <result name="success">good_result.ftl</result>
 </action>
 
 
This second example uses our own log category at debug level.
 
 <!-- records only the action's execution time -->
 <action name="someAction" class="com.examples.SomeAction">
     <interceptor-ref name="completeStack"/>
     <interceptor-ref name="timer">
         <param name="logLevel">debug</param>
         <param name="logCategory">com.mycompany.myapp.actiontime</param>
     <interceptor-ref/>
     <result name="success">good_result.ftl</result>
 </action>

 <!-- records action's execution time as well as other interceptors-->
 <action name="someAction" class="com.examples.SomeAction">
     <interceptor-ref name="timer"/>
     <interceptor-ref name="completeStack"/>
     <result name="success">good_result.ftl</result>
 </action>
 
 

Author:
Jason Carreira, Claus Ibsen
See Also:
Serialized Form

Field Summary
protected  org.apache.commons.logging.Log categoryLogger
           
protected static org.apache.commons.logging.Log log
           
protected  String logCategory
           
protected  String logLevel
           
 
Constructor Summary
TimerInterceptor()
           
 
Method Summary
 void destroy()
          Called to let an interceptor clean up any resources it has allocated.
protected  void doLog(org.apache.commons.logging.Log logger, String message)
          Performs the actual logging.
 String getLogCategory()
           
protected  org.apache.commons.logging.Log getLoggerToUse()
          Get's the logger to use.
 String getLogLevel()
           
 void init()
          Called after an interceptor is created, but before any requests are processed using intercept , giving the Interceptor a chance to initialize any needed resources.
 String intercept(ActionInvocation invocation)
          Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the request by the ActionInvocation or to short-circuit the processing and just return a String return code.
protected  String invokeUnderTiming(ActionInvocation invocation)
          Is called to invoke the action invocation and time the execution time.
 void setLogCategory(String logCatgory)
           
 void setLogLevel(String logLevel)
           
protected  boolean shouldLog()
          Determines if we should log the time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.apache.commons.logging.Log log

categoryLogger

protected org.apache.commons.logging.Log categoryLogger

logCategory

protected String logCategory

logLevel

protected String logLevel
Constructor Detail

TimerInterceptor

public TimerInterceptor()
Method Detail

getLogCategory

public String getLogCategory()

setLogCategory

public void setLogCategory(String logCatgory)

getLogLevel

public String getLogLevel()

setLogLevel

public void setLogLevel(String logLevel)

destroy

public void destroy()
Description copied from interface: Interceptor
Called to let an interceptor clean up any resources it has allocated.

Specified by:
destroy in interface Interceptor

init

public void init()
Description copied from interface: Interceptor
Called after an interceptor is created, but before any requests are processed using intercept , giving the Interceptor a chance to initialize any needed resources.

Specified by:
init in interface Interceptor

intercept

public String intercept(ActionInvocation invocation)
                 throws Exception
Description copied from interface: Interceptor
Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the request by the ActionInvocation or to short-circuit the processing and just return a String return code.

Specified by:
intercept in interface Interceptor
Returns:
the return code, either returned from ActionInvocation.invoke(), or from the interceptor itself.
Throws:
Exception - any system-level error, as defined in Action.execute().

invokeUnderTiming

protected String invokeUnderTiming(ActionInvocation invocation)
                            throws Exception
Is called to invoke the action invocation and time the execution time.

Parameters:
invocation - the action invocation.
Returns:
the result of the action execution.
Throws:
Exception - can be thrown from the action.

shouldLog

protected boolean shouldLog()
Determines if we should log the time.

Returns:
true to log, false to not log.

getLoggerToUse

protected org.apache.commons.logging.Log getLoggerToUse()
Get's the logger to use.

Returns:
the logger to use.

doLog

protected void doLog(org.apache.commons.logging.Log logger,
                     String message)
Performs the actual logging.

Parameters:
logger - the provided logger to use.
message - the message to log.

XWork Project Page