
com.opensymphony.xwork.interceptor.TimerInterceptor Maven / Gradle / Ivy
/*
* Copyright (c) 2002-2003 by OpenSymphony
* All rights reserved.
*/
package com.opensymphony.xwork.interceptor;
import com.opensymphony.xwork.ActionInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* 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.
*
*
*
* TODO: Describe the paramters for this Interceptor.
*
*
*
* TODO: Discuss some possible extension of the Interceptor.
*
*
*
*
* <!-- 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>
*
*
*
* @author Jason Carreira
*/
public class TimerInterceptor implements Interceptor {
private static final Log log = LogFactory.getLog(TimerInterceptor.class);
public void destroy() {
}
public void init() {
}
public String intercept(ActionInvocation invocation) throws Exception {
if (log.isInfoEnabled()) {
long startTime = System.currentTimeMillis();
String result = invocation.invoke();
long executionTime = System.currentTimeMillis() - startTime;
String namespace = invocation.getProxy().getNamespace();
StringBuffer message = new StringBuffer("Processed action ");
if ((namespace != null) && (namespace.trim().length() > 0)) {
message.append(namespace).append("/");
}
message.append(invocation.getProxy().getActionName());
message.append(" in ").append(executionTime).append("ms.");
log.info(message.toString());
return result;
} else {
return invocation.invoke();
}
}
public int hashCode() {
return 11;
}
public boolean equals(Object obj) {
return obj instanceof TimerInterceptor;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy