View Javadoc

1   package org.mod4j.runtime.aspects;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.aspectj.lang.ProceedingJoinPoint;
6   import org.springframework.core.Ordered;
7   
8   /**
9    * Aspect to trace the time a method call has taken to complete.
10   * 
11   * @author Philippe Tjon-a-Hen
12   */
13  public class TimingAspect implements Ordered {
14  
15      /**
16       * Logs the delta between entry and exit of an advised method.
17       * 
18       * @param call The intercepted {@link ProceedingJoinPoint}.
19       * @return The object the intercepted method call returns.
20       * @throws Throwable Any {@link Throwable} thrown by the intercepted method call.
21       */
22      public Object time(final ProceedingJoinPoint call) throws Throwable {
23          final long startTimer = System.currentTimeMillis();
24  
25          try {
26              return call.proceed();
27          } finally {
28              final Log log = LogFactory.getLog(call.getTarget().getClass());
29              if (log.isTraceEnabled()) {
30                  final long delta = System.currentTimeMillis() - startTimer;
31                  log.trace("Execution of method [" + call.getSignature().getName() + "] in " + delta + " milliseconds");
32              }
33          }
34      }
35  
36      private int order;
37  
38      /**
39       * {@inheritDoc}
40       */
41      public int getOrder() {
42          return order;
43      }
44  
45      /**
46       * Allows the order of this aspect to be set.
47       * 
48       * @param order the order to set.
49       */
50      public void setOrder(final int order) {
51          this.order = order;
52      }
53  
54  }