KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > aop > MetricsInterceptor


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.aop;
8
9 //import org.jboss.aop.InvocationResponse;
10

11 import org.jboss.aop.advice.Interceptor;
12 import org.jboss.aop.joinpoint.Invocation;
13 import org.jboss.aop.joinpoint.MethodInvocation;
14
15 import java.lang.reflect.Method JavaDoc;
16
17 /**
18  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
19  * @version $Revision: 1.5 $
20  */

21 public class MetricsInterceptor implements org.jboss.aop.advice.Interceptor
22 {
23
24    public MetricsInterceptor()
25    {
26       System.out.println("### MetricsInterceptor was created ####");
27    }
28
29    public String JavaDoc getName()
30    {
31       return "MetricsInterceptor";
32    }
33
34    public Object JavaDoc invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable JavaDoc
35    {
36       if (invocation == null) {
37          System.err.println("\n-- MetricsInterceptor: invocation is null !");
38          return null;
39       }
40
41       try {
42
43          System.out.println("*** MetricsInterceptor: inv=" + invocation);
44          org.jboss.aop.joinpoint.MethodInvocation methodInvocation = (org.jboss.aop.joinpoint.MethodInvocation) invocation;
45          Object JavaDoc[] args = methodInvocation.getArguments();
46          Method JavaDoc meth = methodInvocation.getMethod();
47          String JavaDoc method_name = meth != null ? meth.getName() : null;
48
49          System.out.println("\n\n-- [MetricsInterceptor]: type=" + invocation.getClass().getName());
50          if (method_name != null)
51             System.out.println("-- [MetricsInterceptor]: method_name("
52                   + printArgs(args)
53                   + ")");
54       } catch (Throwable JavaDoc t) {
55          System.err.println("\n-- [MetricsInterceptor]: error " + t);
56          throw t;
57       }
58
59       return invocation.invokeNext();
60    }
61
62    String JavaDoc printArgs(Object JavaDoc[] args)
63    {
64       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
65       if (args != null) {
66          for (int i = 0; i < args.length; i++) {
67             Object JavaDoc arg = args[i];
68             sb.append(arg).append(" ");
69          }
70       }
71       return sb.toString();
72    }
73
74 }
75
Popular Tags