KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > logging > AbstractLoggingAspect


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 examples.logging;
9
10 import org.codehaus.aspectwerkz.joinpoint.MemberSignature;
11 import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
12
13 /**
14  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
15  */

16 public abstract class AbstractLoggingAspect {
17
18     private int m_level = 0;
19
20     /**
21      * @Around methodsToLog
22      */

23     public Object JavaDoc logMethod(StaticJoinPoint joinPoint) throws Throwable JavaDoc {
24         MemberSignature signature = (MemberSignature) joinPoint.getSignature();
25         indent();
26         System.out.println(
27                 "--> "
28                 + joinPoint.getCalleeClass().getName()
29                 + "::"
30                 + signature.getName()
31         );
32         m_level++;
33         final Object JavaDoc result = joinPoint.proceed();
34         m_level--;
35         indent();
36         System.out.println(
37                 "<-- "
38                 + joinPoint.getCalleeClass().getName()
39                 + "::"
40                 + signature.getName()
41         );
42         return result;
43     }
44
45     /**
46      * @Before methodsToLog
47      */

48     public void logBefore(final StaticJoinPoint joinPoint) throws Throwable JavaDoc {
49         MemberSignature signature = (MemberSignature) joinPoint.getSignature();
50         System.out.println(
51                 "BEFORE: "
52                 + joinPoint.getCalleeClass().getName()
53                 + "::"
54                 + signature.getName()
55         );
56     }
57
58     /**
59      * @AfterReturning(type="java.lang.String", pointcut="methodsToLog")
60      */

61     public void logAfterReturning(final StaticJoinPoint joinPoint) throws Throwable JavaDoc {
62         MemberSignature signature = (MemberSignature) joinPoint.getSignature();
63         System.out.println(
64                 "AFTER RETURNING: "
65                 + joinPoint.getCalleeClass().getName()
66                 + "::"
67                 + signature.getName()
68         );
69     }
70
71     /**
72      * @AfterThrowing(type="java.lang.RuntimeException", pointcut="methodsToLog")
73      */

74     public void logAfterThrowingRE(final StaticJoinPoint joinPoint) throws Throwable JavaDoc {
75         MemberSignature signature = (MemberSignature) joinPoint.getSignature();
76         System.out.println(
77                 "AFTER THROWING RE: "
78                 + joinPoint.getCalleeClass().getName()
79                 + "::"
80                 + signature.getName()
81         );
82     }
83
84     /**
85      * @AfterThrowing(type="java.lang.IllegalArgumentException", pointcut="methodsToLog")
86      */

87     public void logAfterThrowingIAE(final StaticJoinPoint joinPoint) throws Throwable JavaDoc {
88         MemberSignature signature = (MemberSignature) joinPoint.getSignature();
89         System.out.println(
90                 "AFTER THROWING IAE: "
91                 + joinPoint.getCalleeClass().getName()
92                 + "::"
93                 + signature.getName()
94         );
95     }
96
97     /**
98      * @AfterFinally methodsToLog
99      */

100     public void logAfterFinally(final StaticJoinPoint joinPoint) throws Throwable JavaDoc {
101         MemberSignature signature = (MemberSignature) joinPoint.getSignature();
102         System.out.println(
103                 "AFTER FINALLY: "
104                 + joinPoint.getCalleeClass().getName()
105                 + "::"
106                 + signature.getName()
107         );
108     }
109
110     private void indent() {
111         for (int i = 0; i < m_level; i++) {
112             System.out.print(" ");
113         }
114     }
115 }
Popular Tags