001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package examples.logging;
008:
009: import org.codehaus.aspectwerkz.joinpoint.MemberSignature;
010: import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
011:
012: /**
013: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
014: */
015: public abstract class AbstractLoggingAspect {
016:
017: private int m_level = 0;
018:
019: /**
020: * @Around methodsToLog
021: */
022: public Object logMethod(StaticJoinPoint joinPoint) throws Throwable {
023: MemberSignature signature = (MemberSignature) joinPoint
024: .getSignature();
025: indent();
026: System.out.println("--> "
027: + joinPoint.getCalleeClass().getName() + "::"
028: + signature.getName());
029: m_level++;
030: final Object result = joinPoint.proceed();
031: m_level--;
032: indent();
033: System.out.println("<-- "
034: + joinPoint.getCalleeClass().getName() + "::"
035: + signature.getName());
036: return result;
037: }
038:
039: /**
040: * @Before methodsToLog
041: */
042: public void logBefore(final StaticJoinPoint joinPoint)
043: throws Throwable {
044: MemberSignature signature = (MemberSignature) joinPoint
045: .getSignature();
046: System.out.println("BEFORE: "
047: + joinPoint.getCalleeClass().getName() + "::"
048: + signature.getName());
049: }
050:
051: /**
052: * @AfterReturning(type="java.lang.String", pointcut="methodsToLog")
053: */
054: public void logAfterReturning(final StaticJoinPoint joinPoint)
055: throws Throwable {
056: MemberSignature signature = (MemberSignature) joinPoint
057: .getSignature();
058: System.out.println("AFTER RETURNING: "
059: + joinPoint.getCalleeClass().getName() + "::"
060: + signature.getName());
061: }
062:
063: /**
064: * @AfterThrowing(type="java.lang.RuntimeException", pointcut="methodsToLog")
065: */
066: public void logAfterThrowingRE(final StaticJoinPoint joinPoint)
067: throws Throwable {
068: MemberSignature signature = (MemberSignature) joinPoint
069: .getSignature();
070: System.out.println("AFTER THROWING RE: "
071: + joinPoint.getCalleeClass().getName() + "::"
072: + signature.getName());
073: }
074:
075: /**
076: * @AfterThrowing(type="java.lang.IllegalArgumentException", pointcut="methodsToLog")
077: */
078: public void logAfterThrowingIAE(final StaticJoinPoint joinPoint)
079: throws Throwable {
080: MemberSignature signature = (MemberSignature) joinPoint
081: .getSignature();
082: System.out.println("AFTER THROWING IAE: "
083: + joinPoint.getCalleeClass().getName() + "::"
084: + signature.getName());
085: }
086:
087: /**
088: * @AfterFinally methodsToLog
089: */
090: public void logAfterFinally(final StaticJoinPoint joinPoint)
091: throws Throwable {
092: MemberSignature signature = (MemberSignature) joinPoint
093: .getSignature();
094: System.out.println("AFTER FINALLY: "
095: + joinPoint.getCalleeClass().getName() + "::"
096: + signature.getName());
097: }
098:
099: private void indent() {
100: for (int i = 0; i < m_level; i++) {
101: System.out.print(" ");
102: }
103: }
104: }
|