01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.aop.interceptor;
18:
19: import org.aopalliance.intercept.MethodInvocation;
20: import org.apache.commons.logging.Log;
21:
22: /**
23: * Simple AOP Alliance <code>MethodInterceptor</code> that can be introduced
24: * in a chain to display verbose trace information about intercepted method
25: * invocations, with method entry and method exit info.
26: *
27: * <p>Consider using <code>CustomizableTraceInterceptor</code> for more
28: * advanced needs. Note that <code>CustomizableTraceInterceptor</code>
29: * requires JDK 1.4 or later.
30: *
31: * @author Dmitriy Kopylenko
32: * @author Juergen Hoeller
33: * @since 1.2
34: * @see CustomizableTraceInterceptor
35: */
36: public class SimpleTraceInterceptor extends AbstractTraceInterceptor {
37:
38: /**
39: * Create a new SimpleTraceInterceptor with a static logger.
40: */
41: public SimpleTraceInterceptor() {
42: }
43:
44: /**
45: * Create a new SimpleTraceInterceptor with dynamic or static logger,
46: * according to the given flag.
47: * @param useDynamicLogger whether to use a dynamic logger or a static logger
48: * @see #setUseDynamicLogger
49: */
50: public SimpleTraceInterceptor(boolean useDynamicLogger) {
51: setUseDynamicLogger(useDynamicLogger);
52: }
53:
54: protected Object invokeUnderTrace(MethodInvocation invocation,
55: Log logger) throws Throwable {
56: String invocationDescription = getInvocationDescription(invocation);
57: logger.trace("Entering " + invocationDescription);
58: try {
59: Object rval = invocation.proceed();
60: logger.trace("Exiting " + invocationDescription);
61: return rval;
62: } catch (Throwable ex) {
63: logger.trace(
64: "Exception thrown in " + invocationDescription, ex);
65: throw ex;
66: }
67: }
68:
69: /**
70: * Return a description for the given method invocation.
71: * @param invocation the invocation to describe
72: * @return the description
73: */
74: protected String getInvocationDescription(
75: MethodInvocation invocation) {
76: return "method '" + invocation.getMethod().getName()
77: + "' of class ["
78: + invocation.getThis().getClass().getName() + "]";
79: }
80:
81: }
|