KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > container > LogInterceptor


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.jms.container;
8
9 import java.util.Arrays JavaDoc;
10
11 import org.jboss.aop.advice.Interceptor;
12 import org.jboss.aop.joinpoint.Invocation;
13 import org.jboss.aop.joinpoint.MethodInvocation;
14 import org.jboss.logging.Logger;
15
16 /**
17  * An interceptor for logging invocations.
18  *
19  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
20  * @version $Revision: 1.4 $
21  */

22 public class LogInterceptor
23    implements Interceptor
24 {
25    // Constants -----------------------------------------------------
26

27    private static final Logger log = Logger.getLogger(LogInterceptor.class);
28
29    // Attributes ----------------------------------------------------
30

31    // Static --------------------------------------------------------
32

33    public static LogInterceptor singleton = new LogInterceptor();
34
35    // Constructors --------------------------------------------------
36

37    // Public --------------------------------------------------------
38

39    // Interceptor implementation -----------------------------------
40

41    public String JavaDoc getName()
42    {
43       return "LogInterceptor";
44    }
45
46    public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc
47    {
48       StringBuffer JavaDoc desc = getDescription(invocation);
49       log.info("invoke:" + desc);
50       Object JavaDoc result = null;
51       try
52       {
53          result = invocation.invokeNext();
54          log.info("result: " + result + " of invoke:" + desc);
55          return result;
56       }
57       catch (Throwable JavaDoc t)
58       {
59          log.info("error in invoke:" + desc, t);
60          throw t;
61       }
62    }
63
64    // Protected ------------------------------------------------------
65

66    protected StringBuffer JavaDoc getDescription(Invocation invocation)
67    {
68       MethodInvocation mi = (MethodInvocation) invocation;
69       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50);
70       buffer.append(" method=").append(mi.getMethod().getName());
71       buffer.append(" params=");
72       if (mi.getArguments() == null)
73          buffer.append("[]");
74       else
75          buffer.append(Arrays.asList(mi.getArguments()));
76       buffer.append(" object=").append(Container.getProxy(invocation));
77       return buffer;
78    }
79    
80    // Package Private ------------------------------------------------
81

82    // Private --------------------------------------------------------
83

84    // Inner Classes --------------------------------------------------
85

86 }
87
Popular Tags