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.JoinPoint;
6   import org.springframework.core.Ordered;
7   
8   /**
9    * Interceptor class to inject logging / tracing.
10   */
11  public class LoggingAspect implements Ordered {
12  
13      /**
14       * Logs the entry of an advised method.
15       * 
16       * @param call The intercepted {@link JoinPoint}.
17       */
18      public void logBefore(final JoinPoint call) {
19          final Log log = getLog(call);
20  
21          if (log.isTraceEnabled()) {
22              log.trace("Entering method [" + call.getSignature().getName() + "]");
23          }
24      }
25  
26      /**
27       * Logs the exit (regardless of successful return or exception, aka 'finally') of an advised method.
28       * 
29       * @param call The intercepted {@link JoinPoint}.
30       */
31      public void logAfter(final JoinPoint call) {
32          final Log log = getLog(call);
33  
34          if (log.isTraceEnabled()) {
35              log.trace("Exiting method [" + call.getSignature().getName() + "]");
36          }
37      }
38  
39      /**
40       * Get the {@link Log} for the advised method. Note that commons-logging caches the returned object, making this a
41       * cheap operation.
42       * 
43       * @param call Used to determine the advised method.
44       * @return The {@link Log} for the advised method.
45       */
46      private Log getLog(final JoinPoint call) {
47          return LogFactory.getLog(call.getTarget().getClass());
48      }
49  
50      private int order;
51  
52      /**
53       * {@inheritDoc}
54       */
55      public int getOrder() {
56          return order;
57      }
58  
59      /**
60       * Allows the order of this aspect to be set.
61       * 
62       * @param order the order to set.
63       */
64      public void setOrder(final int order) {
65          this.order = order;
66      }
67  
68  }