public ProxyHandler(AbstractProxyManager abstractProxyManager, Callable<Object> dispatcher, InvocationListener listener)
{
target = dispatcher;
proxyManager = abstractProxyManager;
final InvocationListener nonNullListener;
if (listener == null) {
nonNullListener = new DefaultWrapper();
} else {
nonNullListener = listener;
}
core = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
Object result = null;
Object token = null;
boolean inInvoke = false;
try {
token = nonNullListener.preInvoke(proxy, method, args);
inInvoke = true;
result = method.invoke(target.call(), args);
inInvoke = false;
nonNullListener.postInvoke(token, proxy, method, result);
} catch (Throwable e) {
// whether the the exception is an error is an application decision
// if we catch an exception we decide carefully which one to
// throw onwards
Throwable exceptionToRethrow = null;
// if the exception came from a precall or postcall
// we will rethrow it
if (!inInvoke) {
exceptionToRethrow = e;
}
// if the exception didn't come from precall or postcall then it
// came from invoke
// we will rethrow this exception if it is not a runtime
// exception, but we must unwrap InvocationTargetExceptions
else {
if (e instanceof InvocationTargetException) {
e = ((InvocationTargetException) e).getTargetException();
}
if (!(e instanceof RuntimeException)) {
exceptionToRethrow = e;
}
}
try {
nonNullListener.postInvokeExceptionalReturn(token, proxy, method, e);
} catch (Exception f) {
// we caught an exception from
// postInvokeExceptionalReturn
// if we haven't already chosen an exception to rethrow then
// we will throw this exception