KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > yapbaop > demo > YapbaopDemo


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 yapbaop.demo;
9
10 import org.codehaus.aspectwerkz.proxy.Proxy;
11 import org.codehaus.aspectwerkz.intercept.Advisable;
12 import org.codehaus.aspectwerkz.intercept.Advice;
13 import org.codehaus.aspectwerkz.intercept.AfterThrowingAdvice;
14 import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
15 import yapbaop.core.Yapbaop;
16
17 /**
18  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
19  */

20 public class YapbaopDemo {
21
22     static int COUNT = 0;
23
24     String JavaDoc m_name;
25
26     public YapbaopDemo() {
27         m_name = "YapbaopDemo-" + COUNT++;
28     }
29
30     public void method() {
31         System.out.println(m_name + " .method");
32     }
33
34     public void canThrow(boolean doThrow) {
35         System.out.println(m_name + " .canThrow");
36         if (doThrow)
37             throw new RuntimeException JavaDoc("Was asked to throw");
38     }
39
40     public static void main(String JavaDoc args[]) throws Throwable JavaDoc {
41
42         // no aspect
43
System.out.println(" ( no aspect )");
44         YapbaopDemo me0 = new YapbaopDemo();
45         me0.method();
46
47         System.out.println(" ( bind a new aspect )");
48         Yapbaop.Handle handle = Yapbaop.bindAspect(DemoAspect.class, "* yapbaop.demo.YapbaopDemo.*(..)");
49         YapbaopDemo me1 = (YapbaopDemo) Proxy.newInstance(YapbaopDemo.class);
50         me1.method();
51
52         handle.unbind();
53
54         // get a new one but not using the proxy cache then..
55
System.out.println(" ( unbind it and get a new proxy YapbaopDemo-2)");
56         YapbaopDemo me2 = (YapbaopDemo) Proxy.newInstance(YapbaopDemo.class, false, false/*not advisable*/);
57         me1.method();// still has advice
58
me2.method();// no advice
59

60         // lets add some per instance interceptor now
61
// don't use the cache, and ensure we have an advisable version
62
System.out.println(" ( real per instance interception, lets add an afterThrowing on YapbaopDemo-3..)");
63         YapbaopDemo me3 = (YapbaopDemo) Proxy.newInstance(YapbaopDemo.class, false, true/*IS advisable*/);
64         me3.method();// nothing happen
65

66         // note here that composition is not allowed for now since only execution pointcut are valid
67
((Advisable)me3).aw_addAdvice(
68                 "execution(* *.canThrow(..))",
69                 new AfterThrowingAdvice() {
70                     public void invoke(JoinPoint joinPoint, Throwable JavaDoc throwable) throws Throwable JavaDoc {
71                         System.out.print("afterThrowing on ");
72                         System.out.print(((YapbaopDemo)joinPoint.getTarget()).m_name);
73                         System.out.println(" : afterThrowing on " + joinPoint.getSignature().toString());
74                         System.out.println(" exception is " + throwable.getClass().getName()
75                                 + " / " + throwable.getMessage());
76                     }
77                 }
78         );
79         me3.canThrow(false);// nothing
80
System.out.println(" ( nothing happen on YapbaopDemo-2 off course)");
81         try {
82             me2.canThrow(true);// after throwing NOT triggered !! this is me2
83
} catch (Throwable JavaDoc t) {
84             System.out.println("got " + t.getClass().getName() + " / " + t.getMessage());
85         }
86         System.out.println(" ( after throwing per instance on YapbaopDemo-3 happens)");
87         try {
88             me3.canThrow(true);// after throwing IS triggered !! this is me3
89
} catch (Throwable JavaDoc t) {
90             System.out.println("got " + t.getClass().getName() + " / " + t.getMessage());
91         }
92
93     }
94
95 }
96
Popular Tags