1 /*
2 * Copyright 2002-2008 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.remoting.support;
18
19 import java.lang.reflect.Method;
20
21 import org.aopalliance.intercept.MethodInterceptor;
22 import org.aopalliance.intercept.MethodInvocation;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.springframework.util.ClassUtils;
27
28 /**
29 * AOP Alliance MethodInterceptor for tracing remote invocations.
30 * Automatically applied by RemoteExporter and its subclasses.
31 *
32 * <p>Logs an incoming remote call as well as the finished processing of a remote call
33 * at DEBUG level. If the processing of a remote call results in a checked exception,
34 * the exception will get logged at INFO level; if it results in an unchecked
35 * exception (or error), the exception will get logged at WARN level.
36 *
37 * <p>The logging of exceptions is particularly useful to save the stacktrace
38 * information on the server-side rather than just propagating the exception
39 * to the client (who might or might not log it properly).
40 *
41 * @author Juergen Hoeller
42 * @since 1.2
43 * @see RemoteExporter#setRegisterTraceInterceptor
44 * @see RemoteExporter#getProxyForService
45 */
46 public class RemoteInvocationTraceInterceptor implements MethodInterceptor {
47
48 protected static final Log logger = LogFactory.getLog(RemoteInvocationTraceInterceptor.class);
49
50 private final String exporterNameClause;
51
52
53 /**
54 * Create a new RemoteInvocationTraceInterceptor.
55 */
56 public RemoteInvocationTraceInterceptor() {
57 this.exporterNameClause = "";
58 }
59
60 /**
61 * Create a new RemoteInvocationTraceInterceptor.
62 * @param exporterName the name of the remote exporter
63 * (to be used as context information in log messages)
64 */
65 public RemoteInvocationTraceInterceptor(String exporterName) {
66 this.exporterNameClause = exporterName + " ";
67 }
68
69
70 public Object invoke(MethodInvocation invocation) throws Throwable {
71 Method method = invocation.getMethod();
72 if (logger.isDebugEnabled()) {
73 logger.debug("Incoming " + this.exporterNameClause + "remote call: " +
74 ClassUtils.getQualifiedMethodName(method));
75 }
76 try {
77 Object retVal = invocation.proceed();
78 if (logger.isDebugEnabled()) {
79 logger.debug("Finished processing of " + this.exporterNameClause + "remote call: " +
80 ClassUtils.getQualifiedMethodName(method));
81 }
82 return retVal;
83 }
84 catch (Throwable ex) {
85 if (ex instanceof RuntimeException || ex instanceof Error) {
86 if (logger.isWarnEnabled()) {
87 logger.warn("Processing of " + this.exporterNameClause + "remote call resulted in fatal exception: " +
88 ClassUtils.getQualifiedMethodName(method), ex);
89 }
90 }
91 else {
92 if (logger.isInfoEnabled()) {
93 logger.info("Processing of " + this.exporterNameClause + "remote call resulted in exception: " +
94 ClassUtils.getQualifiedMethodName(method), ex);
95 }
96 }
97 throw ex;
98 }
99 }
100
101 }