KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > proxy > tracing > TraceMe1


1 package examples.proxy.tracing;
2
3 import org.codehaus.aspectwerkz.proxy.Proxy;
4 import org.codehaus.aspectwerkz.intercept.Advisable;
5 import org.codehaus.aspectwerkz.intercept.AroundAdvice;
6 import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
7
8 public class TraceMe1 {
9
10     public void step1() {
11         System.out.println(" - TraceMe1.step1");
12         step2();
13     }
14
15     protected void step2() {
16         System.out.println(" - TraceMe1.step2");
17         step3();
18     }
19
20     void step3() {
21         System.out.println(" - TraceMe1.step3");
22     }
23
24     public static void main(String JavaDoc[] args) {
25
26         // get a proxy instance of the TraceMe1 class
27
// this class will have been weaved with all AW aspects on the classpath
28
System.out.println("\nINFO:: ------ get a weaved proxy to the TraceMe1 class -----");
29         TraceMe1 traceMe1 = (TraceMe1) Proxy.newInstance(TraceMe1.class, false, true);
30
31         // invoke the method - trigger deployed matched aspects (one around, one before and one after)
32
System.out.println("\nINFO:: ------ run method chain with only regular AW aspects -----");
33         traceMe1.step1();
34
35         // adding around interceptor using Advisble API
36
System.out.println("\nINFO:: ------ adding an around per instance interceptor on the fly -----");
37         ((Advisable) traceMe1).aw_addAdvice(
38                 "execution(* *.step3())",
39                 new AroundAdvice() {
40                     public Object JavaDoc invoke(JoinPoint jp) throws Throwable JavaDoc {
41                         System.out.println("Interceptor::ENTERING - step3()");
42                         Object JavaDoc result = jp.proceed();
43                         System.out.println("Interceptor::EXITING - step3()");
44                         return result;
45                     }
46                 }
47         );
48
49         // invoking the method chain with the added around interceptor
50
System.out.println(
51                 "\nINFO:: ------ run method chain with the added per instance runtime added around interceptor -----"
52         );
53         traceMe1.step1();
54     }
55 }
56
Popular Tags