KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TraceInterceptor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 import org.jboss.aop.joinpoint.Invocation;
23 import org.jboss.aop.joinpoint.MethodInvocation;
24 import org.jboss.aop.joinpoint.ConstructorInvocation;
25 import org.jboss.aop.joinpoint.FieldInvocation;
26 import org.jboss.aop.joinpoint.FieldReadInvocation;
27 import org.jboss.aop.joinpoint.FieldWriteInvocation;
28 import org.jboss.aop.advice.Interceptor;
29 import org.jboss.aop.annotation.AnnotationElement;
30 /**
31  *
32  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
33  * @version $Revision: 37406 $
34  */

35 public class TraceInterceptor implements Interceptor
36 {
37    public String JavaDoc getName() { return "TraceInterceptor"; }
38
39    private void printSingle(single s)
40    {
41       System.out.println("@single (\"" + s.value() + "\")");
42    }
43
44    private void printComplex(complex c)
45    {
46       System.out.print("@complex (ch='" + c.ch() + "', ");
47       System.out.print("\"" + c.string() + "\", ");
48       System.out.print("flt=" + c.flt() + ", ");
49       System.out.println("dbl=" + c.dbl() + ", ...blah blah blah YOU GET THE IDEA...");
50       
51    }
52
53    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
54    {
55       String JavaDoc msg = "";
56       if (invocation instanceof MethodInvocation)
57       {
58          msg = "executing method " + ((MethodInvocation)invocation).getMethod().toString();
59          single s = (single)AnnotationElement.getAnyAnnotation(((MethodInvocation)invocation).getMethod(), single.class);
60          printSingle(s);
61          complex c = (complex)AnnotationElement.getAnyAnnotation(((MethodInvocation)invocation).getMethod(), complex.class);
62          printComplex(c);
63       }
64       else if (invocation instanceof ConstructorInvocation)
65       {
66          msg = "executing constructor " + ((ConstructorInvocation)invocation).getConstructor();
67          single s = (single)AnnotationElement.getAnyAnnotation(((ConstructorInvocation)invocation).getConstructor(), single.class);
68          printSingle(s);
69          complex c = (complex)AnnotationElement.getAnyAnnotation(((ConstructorInvocation)invocation).getConstructor(), complex.class);
70          printComplex(c);
71       }
72       else if (invocation instanceof FieldReadInvocation)
73       {
74          msg = "read field name: " + ((FieldReadInvocation)invocation).getField();
75          single s = (single)AnnotationElement.getAnyAnnotation(((FieldInvocation)invocation).getField(), single.class);
76          printSingle(s);
77          complex c = (complex)AnnotationElement.getAnyAnnotation(((FieldInvocation)invocation).getField(), complex.class);
78          printComplex(c);
79       }
80       else if (invocation instanceof FieldWriteInvocation)
81       {
82          msg = "write field name: " + ((FieldWriteInvocation)invocation).getField();
83          single s = (single)AnnotationElement.getAnyAnnotation(((FieldInvocation)invocation).getField(), single.class);
84          printSingle(s);
85          complex c = (complex)AnnotationElement.getAnyAnnotation(((FieldInvocation)invocation).getField(), complex.class);
86          printComplex(c);
87       }
88
89       try
90       {
91          System.out.println("<<< Trace : " + msg);
92          return invocation.invokeNext();
93       }
94       finally
95       {
96          System.out.println(">>> Leaving Trace");
97       }
98    }
99 }
100
Popular Tags