001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.examples.impl;
016
017 import java.lang.reflect.InvocationHandler;
018 import java.lang.reflect.InvocationTargetException;
019 import java.lang.reflect.Method;
020
021 import org.apache.commons.logging.Log;
022 import org.apache.hivemind.service.impl.LoggingUtils;
023
024 /**
025 * An invocation handler used by {@link org.apache.examples.impl.ProxyLoggingInterceptorFactory}.
026 * Logs all method invocations, return values and exceptions. Note that, unlike the real
027 * LoggingInterceptor, <code>toString()</code> will just pass through to the delegate service object
028 * (typically, the core service implementation).
029 *
030 * @author Howard Lewis Ship
031 */
032 public class ProxyLoggingInvocationHandler implements InvocationHandler
033 {
034 private Log _log;
035 private Object _delegate;
036
037 public ProxyLoggingInvocationHandler(Log log, Object delegate)
038 {
039 _log = log;
040 _delegate = delegate;
041 }
042
043 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
044 {
045 boolean debug = _log.isDebugEnabled();
046
047 if (debug)
048 LoggingUtils.entry(_log, method.getName(), args);
049
050 try
051 {
052 Object result = method.invoke(_delegate, args);
053
054 if (debug)
055 {
056 if (method.getReturnType() == void.class)
057 LoggingUtils.voidExit(_log, method.getName());
058 else
059 LoggingUtils.exit(_log, method.getName(), result);
060 }
061
062 return result;
063 }
064 catch (InvocationTargetException ex)
065 {
066 Throwable targetException = ex.getTargetException();
067
068 if (debug)
069 LoggingUtils.exception(_log, method.getName(), targetException);
070
071 throw targetException;
072 }
073 }
074
075 }