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 yapbaop.demo;
008:
009: import org.codehaus.aspectwerkz.proxy.Proxy;
010: import org.codehaus.aspectwerkz.intercept.Advisable;
011: import org.codehaus.aspectwerkz.intercept.Advice;
012: import org.codehaus.aspectwerkz.intercept.AfterThrowingAdvice;
013: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
014: import yapbaop.core.Yapbaop;
015:
016: /**
017: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
018: */
019: public class YapbaopDemo {
020:
021: static int COUNT = 0;
022:
023: String m_name;
024:
025: public YapbaopDemo() {
026: m_name = "YapbaopDemo-" + COUNT++;
027: }
028:
029: public void method() {
030: System.out.println(m_name + " .method");
031: }
032:
033: public void canThrow(boolean doThrow) {
034: System.out.println(m_name + " .canThrow");
035: if (doThrow)
036: throw new RuntimeException("Was asked to throw");
037: }
038:
039: public static void main(String args[]) throws Throwable {
040:
041: // no aspect
042: System.out.println(" ( no aspect )");
043: YapbaopDemo me0 = new YapbaopDemo();
044: me0.method();
045:
046: System.out.println(" ( bind a new aspect )");
047: Yapbaop.Handle handle = Yapbaop.bindAspect(DemoAspect.class,
048: "* yapbaop.demo.YapbaopDemo.*(..)");
049: YapbaopDemo me1 = (YapbaopDemo) Proxy
050: .newInstance(YapbaopDemo.class);
051: me1.method();
052:
053: handle.unbind();
054:
055: // get a new one but not using the proxy cache then..
056: System.out
057: .println(" ( unbind it and get a new proxy YapbaopDemo-2)");
058: YapbaopDemo me2 = (YapbaopDemo) Proxy.newInstance(
059: YapbaopDemo.class, false, false/*not advisable*/);
060: me1.method();// still has advice
061: me2.method();// no advice
062:
063: // lets add some per instance interceptor now
064: // don't use the cache, and ensure we have an advisable version
065: System.out
066: .println(" ( real per instance interception, lets add an afterThrowing on YapbaopDemo-3..)");
067: YapbaopDemo me3 = (YapbaopDemo) Proxy.newInstance(
068: YapbaopDemo.class, false, true/*IS advisable*/);
069: me3.method();// nothing happen
070:
071: // note here that composition is not allowed for now since only execution pointcut are valid
072: ((Advisable) me3).aw_addAdvice("execution(* *.canThrow(..))",
073: new AfterThrowingAdvice() {
074: public void invoke(JoinPoint joinPoint,
075: Throwable throwable) throws Throwable {
076: System.out.print("afterThrowing on ");
077: System.out.print(((YapbaopDemo) joinPoint
078: .getTarget()).m_name);
079: System.out.println(" : afterThrowing on "
080: + joinPoint.getSignature().toString());
081: System.out.println(" exception is "
082: + throwable.getClass().getName()
083: + " / " + throwable.getMessage());
084: }
085: });
086: me3.canThrow(false);// nothing
087: System.out
088: .println(" ( nothing happen on YapbaopDemo-2 off course)");
089: try {
090: me2.canThrow(true);// after throwing NOT triggered !! this is me2
091: } catch (Throwable t) {
092: System.out.println("got " + t.getClass().getName() + " / "
093: + t.getMessage());
094: }
095: System.out
096: .println(" ( after throwing per instance on YapbaopDemo-3 happens)");
097: try {
098: me3.canThrow(true);// after throwing IS triggered !! this is me3
099: } catch (Throwable t) {
100: System.out.println("got " + t.getClass().getName() + " / "
101: + t.getMessage());
102: }
103:
104: }
105:
106: }
|