1 | package com.hammurapi.common; |
2 | |
3 | import java.lang.reflect.InvocationHandler; |
4 | import java.lang.reflect.Method; |
5 | import java.lang.reflect.Proxy; |
6 | import java.util.Arrays; |
7 | import java.util.LinkedList; |
8 | import java.util.List; |
9 | |
10 | /** |
11 | * This class creates a proxy which record all invocations of |
12 | * the methods of the target object with stack traces, calling threads, timestamps, and arguments. |
13 | * These records can be used for troubleshooing of concurrency problems. |
14 | * @author Pavel Vlasov |
15 | * |
16 | */ |
17 | public class InvocationRecordingProxyFactory { |
18 | |
19 | /** |
20 | * Callback interface to receive problem notifications. |
21 | * @author Pavel Vlasov |
22 | * |
23 | */ |
24 | public interface ProblemListener { |
25 | |
26 | void onProblem(Throwable th, List<InvocationRecord> records); |
27 | } |
28 | |
29 | public static class InvocationRecord { |
30 | |
31 | @Override |
32 | public String toString() { |
33 | return "InvocationRecord("+method.getName()+") [thread=" + thread + ", method=" + method |
34 | + ", timestamp=" + timestamp + ", arguments=" |
35 | + Arrays.toString(arguments) + ", target=" + target + "]"; |
36 | } |
37 | |
38 | private Thread thread; |
39 | private StackTraceElement[] stackTrace; |
40 | private Method method; |
41 | private long timestamp; |
42 | private Object[] arguments; |
43 | private Object target; |
44 | |
45 | InvocationRecord(Thread thread, StackTraceElement[] stackTrace, Method method, long timestamp, Object[] arguments, Object target) { |
46 | super(); |
47 | this.thread = thread; |
48 | this.stackTrace = stackTrace; |
49 | this.method = method; |
50 | this.timestamp = timestamp; |
51 | if (arguments != null) { |
52 | this.arguments = Arrays.copyOf(arguments, arguments.length); |
53 | } |
54 | this.target = target; |
55 | } |
56 | |
57 | public Thread getThread() { |
58 | return thread; |
59 | } |
60 | |
61 | public StackTraceElement[] getStackTrace() { |
62 | return stackTrace; |
63 | } |
64 | |
65 | public Method getMethod() { |
66 | return method; |
67 | } |
68 | |
69 | public long getTimestamp() { |
70 | return timestamp; |
71 | } |
72 | |
73 | Object[] getArguments() { |
74 | return arguments; |
75 | } |
76 | |
77 | Object getTarget() { |
78 | return target; |
79 | } |
80 | |
81 | } |
82 | |
83 | private static class RecordingInvocationHandler implements InvocationHandler { |
84 | |
85 | private ProblemListener problemListener; |
86 | |
87 | RecordingInvocationHandler(Object target, ProblemListener problemListener, LinkedList<InvocationRecord> records) { |
88 | this.target = target; |
89 | this.problemListener = problemListener; |
90 | this.records = records; |
91 | } |
92 | |
93 | private Object target; |
94 | |
95 | private LinkedList<InvocationRecord> records; |
96 | |
97 | @Override |
98 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
99 | |
100 | try { |
101 | synchronized (records) { |
102 | Thread currentThread = Thread.currentThread(); |
103 | records.addFirst(new InvocationRecord(currentThread, currentThread.getStackTrace(), method, System.currentTimeMillis(), args, target)); |
104 | } |
105 | Object ret = method.invoke(target, args); |
106 | Class<?> returnType = method.getReturnType(); |
107 | if (returnType.isInterface()) { |
108 | return Proxy.newProxyInstance(returnType.getClass().getClassLoader(), new Class[] {returnType}, new RecordingInvocationHandler(ret, problemListener, records)); |
109 | } |
110 | return ret; |
111 | } catch (Throwable th) { |
112 | if (problemListener!=null) { |
113 | problemListener.onProblem(th, records); |
114 | } |
115 | throw th; |
116 | } |
117 | } |
118 | |
119 | List<InvocationRecord> getRecords() { |
120 | return records; |
121 | } |
122 | |
123 | } |
124 | |
125 | /** |
126 | * Wraps target into a proxy for method call recording. |
127 | * @param <I> |
128 | * @param <T> |
129 | * @param target |
130 | * @param problemListener |
131 | * @return |
132 | */ |
133 | @SuppressWarnings("unchecked") |
134 | public static <I, T extends I> I wrap(Class<I> proxyType, T target, ProblemListener problemListener) { |
135 | return (I) Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class[] {proxyType}, new RecordingInvocationHandler(target, problemListener, new LinkedList<InvocationRecord>())); |
136 | } |
137 | |
138 | public static List<InvocationRecord> getInvocationRecords(Object proxy) { |
139 | return ((RecordingInvocationHandler) Proxy.getInvocationHandler(proxy)).getRecords(); |
140 | } |
141 | |
142 | } |