1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.util.spring; |
17 | |
|
18 | |
import java.lang.reflect.Method; |
19 | |
|
20 | |
import org.aopalliance.intercept.MethodInterceptor; |
21 | |
import org.aopalliance.intercept.MethodInvocation; |
22 | |
import org.apache.commons.lang.StringUtils; |
23 | |
import org.apache.commons.logging.Log; |
24 | |
import org.apache.commons.logging.LogFactory; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | public class MethodLoggingInterceptor implements MethodInterceptor { |
32 | 0 | private static final Log LOG = LogFactory.getLog(MethodLoggingInterceptor.class); |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public Object invoke(MethodInvocation invocation) throws Throwable { |
43 | 0 | long startTime = System.currentTimeMillis(); |
44 | 0 | Object methodResult = null; |
45 | 0 | String invocationLabel = buildInvocationLabel(invocation); |
46 | |
try { |
47 | 0 | LOG.fatal("entering " + invocationLabel); |
48 | |
|
49 | 0 | methodResult = invocation.proceed(); |
50 | |
} |
51 | 0 | catch (Exception invocationException) { |
52 | 0 | String exceptionLabel = buildExceptionLabel(invocationException); |
53 | 0 | LOG.fatal("aborting " + invocationLabel + ": throwing " + exceptionLabel); |
54 | |
|
55 | 0 | throw invocationException; |
56 | 0 | } |
57 | 0 | LOG.fatal(new StringBuffer("leaving ").append(invocationLabel).append(" / took ").append(System.currentTimeMillis() - startTime).append(" ms")); |
58 | |
|
59 | 0 | return methodResult; |
60 | |
} |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
private String buildInvocationLabel(MethodInvocation invocation) { |
67 | 0 | StringBuffer invocationLabel = new StringBuffer(); |
68 | |
|
69 | 0 | Method method = invocation.getMethod(); |
70 | 0 | Class targetClass = invocation.getThis().getClass(); |
71 | 0 | Class declaringClass = method.getDeclaringClass(); |
72 | |
|
73 | |
|
74 | 0 | if (targetClass != declaringClass) { |
75 | 0 | invocationLabel.append("{"); |
76 | 0 | invocationLabel.append(declaringClass.getName()); |
77 | 0 | invocationLabel.append("} "); |
78 | |
} |
79 | 0 | invocationLabel.append(targetClass.getName() + "." + method.getName()); |
80 | |
|
81 | |
|
82 | |
|
83 | 0 | Class[] paramTypes = method.getParameterTypes(); |
84 | 0 | Object[] argValues = invocation.getArguments(); |
85 | |
|
86 | 0 | invocationLabel.append("("); |
87 | 0 | if (paramTypes != null) { |
88 | 0 | for (int i = 0; i < paramTypes.length; i++) { |
89 | 0 | if (i > 0) { |
90 | 0 | invocationLabel.append(","); |
91 | |
} |
92 | |
|
93 | 0 | invocationLabel.append(paramTypes[i].getName()); |
94 | 0 | invocationLabel.append("="); |
95 | |
|
96 | |
|
97 | 0 | if (argValues[i] == null) { |
98 | 0 | invocationLabel.append("<literal null>"); |
99 | |
} |
100 | |
else { |
101 | 0 | invocationLabel.append(argValues[i]); |
102 | |
} |
103 | |
} |
104 | |
} |
105 | 0 | invocationLabel.append(")"); |
106 | |
|
107 | 0 | return invocationLabel.toString(); |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
private String buildExceptionLabel(Exception e) { |
116 | 0 | String className = e.getClass().getName(); |
117 | |
|
118 | 0 | String exceptionLabel = StringUtils.substringAfterLast(className, "."); |
119 | 0 | if (StringUtils.isBlank(exceptionLabel)) { |
120 | 0 | exceptionLabel = className; |
121 | |
} |
122 | |
|
123 | 0 | return exceptionLabel; |
124 | |
} |
125 | |
} |