KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > remoting > support > RemoteInvocationTraceInterceptor


1 /*
2  * Copyright 2002-2006 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 JavaDoc;
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 JavaDoc exporterName;
51
52
53     /**
54      * Create a new RemoteInvocationTraceInterceptor.
55      * @param protocolName the name of the remoting protocol
56      * (to be used as context information in log messages)
57      */

58     public RemoteInvocationTraceInterceptor(String JavaDoc protocolName) {
59         this.exporterName = protocolName;
60     }
61
62
63     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
64         Method JavaDoc method = invocation.getMethod();
65         if (logger.isDebugEnabled()) {
66             logger.debug("Incoming " + this.exporterName + " remote call: " +
67                     ClassUtils.getQualifiedMethodName(method));
68         }
69         try {
70             Object JavaDoc retVal = invocation.proceed();
71             if (logger.isDebugEnabled()) {
72                 logger.debug("Finished processing of " + this.exporterName + " remote call: " +
73                         ClassUtils.getQualifiedMethodName(method));
74             }
75             return retVal;
76         }
77         catch (Throwable JavaDoc ex) {
78             if (ex instanceof RuntimeException JavaDoc || ex instanceof Error JavaDoc) {
79                 if (logger.isWarnEnabled()) {
80                     logger.warn("Processing of " + this.exporterName + " remote call resulted in fatal exception: " +
81                             ClassUtils.getQualifiedMethodName(method), ex);
82                 }
83             }
84             else {
85                 if (logger.isInfoEnabled()) {
86                     logger.info("Processing of " + this.exporterName + " remote call resulted in exception: " +
87                             ClassUtils.getQualifiedMethodName(method), ex);
88                 }
89             }
90             throw ex;
91         }
92     }
93
94 }
95
Popular Tags