KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > LoggingInvocationHandler


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import org.apache.commons.lang.exception.ExceptionUtils;
7
8 import java.io.IOException JavaDoc;
9 import java.io.Writer JavaDoc;
10 import java.lang.reflect.InvocationHandler JavaDoc;
11 import java.lang.reflect.InvocationTargetException JavaDoc;
12 import java.lang.reflect.Method JavaDoc;
13 import java.lang.reflect.Proxy JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 /**
18  * An {@link InvocationHandler}that logs every call made through it, along with the thread that made it. This can be
19  * very useful for debugging in certain situations.
20  * </p>
21  * <p>
22  * This class will also usefully create proxy wrappers around any objects returned from any calls that implement any of
23  * the interfaces supplied in the <code>furtherProxies</code> argument to the constructor. This allows you to
24  * 'capture' an entire object graph resulting from a series of calls pretty easily.
25  */

26 public class LoggingInvocationHandler implements InvocationHandler JavaDoc {
27
28   private final Object JavaDoc delegate;
29   private final Writer JavaDoc dest;
30   private final Class JavaDoc[] furtherProxies;
31
32   public LoggingInvocationHandler(Object JavaDoc delegate, Writer JavaDoc dest, Class JavaDoc[] furtherProxies) {
33     Assert.assertNotNull(delegate);
34     Assert.assertNotNull(dest);
35     Assert.assertNoNullElements(furtherProxies);
36     this.delegate = delegate;
37     this.dest = dest;
38     this.furtherProxies = furtherProxies;
39   }
40
41   public Object JavaDoc invoke(Object JavaDoc arg0, Method JavaDoc arg1, Object JavaDoc[] arg2) throws Throwable JavaDoc {
42     Thread JavaDoc theThread = Thread.currentThread();
43     StringBuffer JavaDoc message = new StringBuffer JavaDoc();
44
45     message.append("[" + theThread.getName() + "]: " + delegate + "." + arg1 + "(" + describeArguments(arg2) + ")");
46     try {
47       Object JavaDoc result = arg1.invoke(delegate, arg2);
48       message.append(" ==> " + result);
49       process(message.toString());
50       return reproxify(result);
51     } catch (InvocationTargetException JavaDoc ite) {
52       message.append(" => THROWABLE: ");
53       message.append(ExceptionUtils.getStackTrace(ite.getCause()));
54       message.append(".");
55       process(message.toString());
56       throw ite.getCause();
57     }
58   }
59
60   private Object JavaDoc reproxify(Object JavaDoc out) {
61     if (out == null) return out;
62     if (Proxy.isProxyClass(out.getClass())) return out;
63
64     List JavaDoc implementedClasses = new ArrayList JavaDoc();
65
66     for (int i = 0; i < furtherProxies.length; ++i) {
67       if (furtherProxies[i].isInstance(out)) {
68         implementedClasses.add(furtherProxies[i]);
69       }
70     }
71
72     if (implementedClasses.size() > 0) {
73       return Proxy.newProxyInstance(getClass().getClassLoader(), (Class JavaDoc[]) implementedClasses
74           .toArray(new Class JavaDoc[implementedClasses.size()]), new LoggingInvocationHandler(out, this.dest,
75                                                                                        this.furtherProxies));
76     } else {
77       return out;
78     }
79   }
80
81   private String JavaDoc describeArguments(Object JavaDoc[] arguments) {
82     if (arguments == null) return "";
83
84     StringBuffer JavaDoc out = new StringBuffer JavaDoc();
85
86     for (int i = 0; i < arguments.length; ++i) {
87       if (i > 0) out.append(", ");
88       out.append(arguments[i]);
89     }
90
91     return out.toString();
92   }
93
94   private void process(String JavaDoc message) {
95     try {
96       dest.write("\n\n" + message + "\n");
97       dest.flush();
98     } catch (IOException JavaDoc ioe) {
99       throw Assert.failure("Got an IOException when writing.", ioe);
100     }
101   }
102
103 }
104
Popular Tags