KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > tracing > TracingAspect


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package samples.tracing;
9
10 import org.codehaus.aspectwerkz.definition.Pointcut;
11 import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
12 import org.codehaus.aspectwerkz.joinpoint.MemberSignature;
13
14 /**
15  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
16  */

17 public class TracingAspect {
18
19     private int m_level = 0;
20
21     public Object JavaDoc logMethod(StaticJoinPoint joinPoint) throws Throwable JavaDoc {
22         MemberSignature signature = (MemberSignature) joinPoint.getSignature();
23         indent();
24         System.out.println(
25                 "--> "
26                 + joinPoint.getCalleeClass().getName()
27                 + "::"
28                 + signature.getName()
29         );
30         m_level++;
31         final Object JavaDoc result = joinPoint.proceed();
32         m_level--;
33         indent();
34         System.out.println(
35                 "<-- "
36                 + joinPoint.getCalleeClass().getName()
37                 + "::"
38                 + signature.getName()
39         );
40         return result;
41     }
42
43     public void logBeforeArgs(final StaticJoinPoint joinPoint, Trace.Target t, Trace.Target arg) throws Throwable JavaDoc {
44         System.out.println("BEFORE: ARGS: I am [" + t + "] and args is [" + arg + "]") ;
45     }
46     public void logBefore(final StaticJoinPoint joinPoint) throws Throwable JavaDoc {
47         MemberSignature signature = (MemberSignature) joinPoint.getSignature();
48         System.out.println(
49                 "BEFORE: "
50                 + joinPoint.getCalleeClass().getName()
51                 + "::"
52                 + signature.getName()
53         );
54     }
55
56     public void logAfterReturning(final StaticJoinPoint joinPoint) throws Throwable JavaDoc {
57         MemberSignature signature = (MemberSignature) joinPoint.getSignature();
58         System.out.println(
59                 "AFTER RETURNING: "
60                 + joinPoint.getCalleeClass().getName()
61                 + "::"
62                 + signature.getName()
63         );
64     }
65
66     public void logAfterThrowingRE(final StaticJoinPoint joinPoint) throws Throwable JavaDoc {
67         MemberSignature signature = (MemberSignature) joinPoint.getSignature();
68         System.out.println(
69                 "AFTER THROWING RE: "
70                 + joinPoint.getCalleeClass().getName()
71                 + "::"
72                 + signature.getName()
73         );
74     }
75
76     public void logAfterThrowingIAE(final StaticJoinPoint joinPoint) throws Throwable JavaDoc {
77         MemberSignature signature = (MemberSignature) joinPoint.getSignature();
78         System.out.println(
79                 "AFTER THROWING IAE: "
80                 + joinPoint.getCalleeClass().getName()
81                 + "::"
82                 + signature.getName()
83         );
84     }
85
86     public void logAfter(final StaticJoinPoint joinPoint) throws Throwable JavaDoc {
87         MemberSignature signature = (MemberSignature) joinPoint.getSignature();
88         System.out.println(
89                 "AFTER: "
90                 + joinPoint.getCalleeClass().getName()
91                 + "::"
92                 + signature.getName()
93         );
94     }
95
96     private void indent() {
97         for (int i = 0; i < m_level; i++) {
98             System.out.print(" ");
99         }
100     }
101
102     /**
103      * @Expression execution(* examples.logging.Target.toLog*(..))
104      */

105     Pointcut methodsToLog() {
106         return null;
107     };
108 }
Popular Tags