1 | |
package org.kuali.spring.proxy; |
2 | |
|
3 | |
import java.lang.reflect.Method; |
4 | |
import java.lang.reflect.Modifier; |
5 | |
|
6 | |
import org.slf4j.Logger; |
7 | |
import org.slf4j.LoggerFactory; |
8 | |
|
9 | |
import net.sf.cglib.proxy.MethodInterceptor; |
10 | |
import net.sf.cglib.proxy.MethodProxy; |
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | 1 | public class TraceCallback implements MethodInterceptor { |
16 | 1 | private static final Logger logger = LoggerFactory.getLogger(TraceCallback.class); |
17 | 1 | private static final Object MUTEX = new Object(); |
18 | 1 | long counter = 0; |
19 | |
private static final int LANGUAGE_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
20 | |
| Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | Modifier.SYNCHRONIZED | Modifier.NATIVE; |
21 | |
|
22 | |
@Override |
23 | |
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { |
24 | 4317 | return intercept(new CallbackContext(object, method, args, methodProxy)); |
25 | |
} |
26 | |
|
27 | |
public Object intercept(CallbackContext context) throws Throwable { |
28 | 4317 | long interceptCount = -1; |
29 | 4317 | synchronized (MUTEX) { |
30 | 4317 | interceptCount = this.counter++; |
31 | 4317 | } |
32 | 4317 | if (logger.isTraceEnabled()) { |
33 | 0 | logger.trace("Method Invocation Id=" + interceptCount + " Invoking {} parameters={}", |
34 | |
getString(context.getMethod()), context.getArgs()); |
35 | |
} |
36 | 4317 | Object result = context.getMethodProxy().invokeSuper(context.getObject(), context.getArgs()); |
37 | 4317 | if (logger.isTraceEnabled()) { |
38 | 0 | logger.trace("Method Invocation Id={} Result=[{}]", interceptCount, result); |
39 | |
} |
40 | 4317 | return result; |
41 | |
} |
42 | |
|
43 | |
protected String getTypeName(Class<?> type) { |
44 | 0 | if (type.isArray()) { |
45 | |
try { |
46 | 0 | Class<?> cl = type; |
47 | 0 | int dimensions = 0; |
48 | 0 | while (cl.isArray()) { |
49 | 0 | dimensions++; |
50 | 0 | cl = cl.getComponentType(); |
51 | |
} |
52 | 0 | StringBuffer sb = new StringBuffer(); |
53 | 0 | sb.append(cl.getName()); |
54 | 0 | for (int i = 0; i < dimensions; i++) { |
55 | 0 | sb.append("[]"); |
56 | |
} |
57 | 0 | return sb.toString(); |
58 | 0 | } catch (Throwable e) { |
59 | |
} |
60 | |
} |
61 | 0 | return type.getSimpleName(); |
62 | |
} |
63 | |
|
64 | |
protected String getString(Method method) { |
65 | |
try { |
66 | 0 | StringBuffer sb = new StringBuffer(); |
67 | 0 | int mod = method.getModifiers() & LANGUAGE_MODIFIERS; |
68 | 0 | if (mod != 0) { |
69 | 0 | sb.append(Modifier.toString(mod) + " "); |
70 | |
} |
71 | 0 | sb.append(getTypeName(method.getReturnType()) + " "); |
72 | 0 | sb.append(getTypeName(method.getDeclaringClass()) + "."); |
73 | 0 | sb.append(method.getName() + "("); |
74 | 0 | Class<?>[] params = method.getParameterTypes(); |
75 | 0 | for (int j = 0; j < params.length; j++) { |
76 | 0 | sb.append(getTypeName(params[j])); |
77 | 0 | if (j < (params.length - 1)) |
78 | 0 | sb.append(","); |
79 | |
} |
80 | 0 | sb.append(")"); |
81 | 0 | Class<?>[] exceptions = method.getExceptionTypes(); |
82 | 0 | if (exceptions.length > 0) { |
83 | 0 | sb.append(" throws "); |
84 | 0 | for (int k = 0; k < exceptions.length; k++) { |
85 | 0 | sb.append(exceptions[k].getName()); |
86 | 0 | if (k < (exceptions.length - 1)) |
87 | 0 | sb.append(","); |
88 | |
} |
89 | |
} |
90 | 0 | return sb.toString(); |
91 | 0 | } catch (Exception e) { |
92 | 0 | return "<" + e + ">"; |
93 | |
} |
94 | |
} |
95 | |
|
96 | |
} |