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 public class TraceCallback implements MethodInterceptor {
16 private static final Logger logger = LoggerFactory.getLogger(TraceCallback.class);
17 private static final Object MUTEX = new Object();
18 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 return intercept(new CallbackContext(object, method, args, methodProxy));
25 }
26
27 public Object intercept(CallbackContext context) throws Throwable {
28 long interceptCount = -1;
29 synchronized (MUTEX) {
30 interceptCount = this.counter++;
31 }
32 if (logger.isTraceEnabled()) {
33 logger.trace("Method Invocation Id=" + interceptCount + " Invoking {} parameters={}",
34 getString(context.getMethod()), context.getArgs());
35 }
36 Object result = context.getMethodProxy().invokeSuper(context.getObject(), context.getArgs());
37 if (logger.isTraceEnabled()) {
38 logger.trace("Method Invocation Id={} Result=[{}]", interceptCount, result);
39 }
40 return result;
41 }
42
43 protected String getTypeName(Class<?> type) {
44 if (type.isArray()) {
45 try {
46 Class<?> cl = type;
47 int dimensions = 0;
48 while (cl.isArray()) {
49 dimensions++;
50 cl = cl.getComponentType();
51 }
52 StringBuffer sb = new StringBuffer();
53 sb.append(cl.getName());
54 for (int i = 0; i < dimensions; i++) {
55 sb.append("[]");
56 }
57 return sb.toString();
58 } catch (Throwable e) {
59 }
60 }
61 return type.getSimpleName();
62 }
63
64 protected String getString(Method method) {
65 try {
66 StringBuffer sb = new StringBuffer();
67 int mod = method.getModifiers() & LANGUAGE_MODIFIERS;
68 if (mod != 0) {
69 sb.append(Modifier.toString(mod) + " ");
70 }
71 sb.append(getTypeName(method.getReturnType()) + " ");
72 sb.append(getTypeName(method.getDeclaringClass()) + ".");
73 sb.append(method.getName() + "(");
74 Class<?>[] params = method.getParameterTypes();
75 for (int j = 0; j < params.length; j++) {
76 sb.append(getTypeName(params[j]));
77 if (j < (params.length - 1))
78 sb.append(",");
79 }
80 sb.append(")");
81 Class<?>[] exceptions = method.getExceptionTypes();
82 if (exceptions.length > 0) {
83 sb.append(" throws ");
84 for (int k = 0; k < exceptions.length; k++) {
85 sb.append(exceptions[k].getName());
86 if (k < (exceptions.length - 1))
87 sb.append(",");
88 }
89 }
90 return sb.toString();
91 } catch (Exception e) {
92 return "<" + e + ">";
93 }
94 }
95
96 }