01: // Copyright 2004, 2005 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.examples.impl;
16:
17: import java.lang.reflect.InvocationHandler;
18: import java.lang.reflect.InvocationTargetException;
19: import java.lang.reflect.Method;
20:
21: import org.apache.commons.logging.Log;
22: import org.apache.hivemind.service.impl.LoggingUtils;
23:
24: /**
25: * An invocation handler used by {@link org.apache.examples.impl.ProxyLoggingInterceptorFactory}.
26: * Logs all method invocations, return values and exceptions. Note that, unlike the real
27: * LoggingInterceptor, <code>toString()</code> will just pass through to the delegate service object
28: * (typically, the core service implementation).
29: *
30: * @author Howard Lewis Ship
31: */
32: public class ProxyLoggingInvocationHandler implements InvocationHandler {
33: private Log _log;
34: private Object _delegate;
35:
36: public ProxyLoggingInvocationHandler(Log log, Object delegate) {
37: _log = log;
38: _delegate = delegate;
39: }
40:
41: public Object invoke(Object proxy, Method method, Object[] args)
42: throws Throwable {
43: boolean debug = _log.isDebugEnabled();
44:
45: if (debug)
46: LoggingUtils.entry(_log, method.getName(), args);
47:
48: try {
49: Object result = method.invoke(_delegate, args);
50:
51: if (debug) {
52: if (method.getReturnType() == void.class)
53: LoggingUtils.voidExit(_log, method.getName());
54: else
55: LoggingUtils.exit(_log, method.getName(), result);
56: }
57:
58: return result;
59: } catch (InvocationTargetException ex) {
60: Throwable targetException = ex.getTargetException();
61:
62: if (debug)
63: LoggingUtils.exception(_log, method.getName(),
64: targetException);
65:
66: throw targetException;
67: }
68: }
69:
70: }
|