com.emarsys.ecommon.exceptions.handling
Class ExceptionHandlingProxy

java.lang.Object
  extended by com.emarsys.ecommon.exceptions.handling.ExceptionHandlingProxy
All Implemented Interfaces:
java.lang.reflect.InvocationHandler

public class ExceptionHandlingProxy
extends java.lang.Object
implements java.lang.reflect.InvocationHandler

An ExceptionHandlingProxy creates dynamic Proxys that serve two purposes:

.

If an Exception occurs during a method call on a proxy it will look for a ExceptionHandler registered with the ExceptionScenario described

NOTE: the whole pluggable exception handling (PEH) module is highly experimental and still under heavy development, don't use it in production code unless you know what you're doing!

Author:
Michael "kULO" Kulovits

See Also:
ExceptionHandling,

TODO what about methods with more than one exception? - specify a common super class for all declared exceptions as handled exception type and dispatch the handling behavior it in the handler class according to the concrete exception type to be handled - maybe create something like a ChainedExceptionHandler implementing a chain-of-responsibility TODO not fully tested


Constructor Summary
protected ExceptionHandlingProxy(java.lang.Object target)
           
 
Method Summary
protected  java.lang.Object doInvoke(java.lang.Object invokee, java.lang.reflect.Method method, java.lang.Object[] args)
           
static ExceptionHandling getExceptionHandling(java.lang.Object obj)
           
static
<P> P
getInstance(java.lang.Object target, java.lang.Class<P> type)
           
protected  ExceptionScenario<?,?> getScenario(java.lang.Object invokee, java.lang.reflect.Method method)
          Note: HandleExceptionss annotations are inherited so so we don't query method.getDeclaringClass() but pass the actual object being invoked.
protected
<H extends java.lang.Exception,I extends java.lang.Exception>
void
handle(ExceptionScenario<H,I> scenario, java.lang.Throwable th)
           
 java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
          Dispatches calls between target and handling and delegates the actual invocation to doInvoke(Object, Method, Object[]).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExceptionHandlingProxy

protected ExceptionHandlingProxy(java.lang.Object target)
Parameters:
target -
Method Detail

getInstance

public static <P> P getInstance(java.lang.Object target,
                                java.lang.Class<P> type)
Type Parameters:
E -
P -
Parameters:
target -
type -
Returns:

getExceptionHandling

public static ExceptionHandling getExceptionHandling(java.lang.Object obj)
Parameters:
obj -
Returns:

invoke

public java.lang.Object invoke(java.lang.Object proxy,
                               java.lang.reflect.Method method,
                               java.lang.Object[] args)
                        throws java.lang.Throwable
Dispatches calls between target and handling and delegates the actual invocation to doInvoke(Object, Method, Object[]).

Specified by:
invoke in interface java.lang.reflect.InvocationHandler
Throws:
java.lang.Throwable
See Also:
InvocationHandler.invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])

doInvoke

protected java.lang.Object doInvoke(java.lang.Object invokee,
                                    java.lang.reflect.Method method,
                                    java.lang.Object[] args)
                             throws java.lang.Throwable
Parameters:
proxy -
method -
args -
Returns:
Throws:
java.lang.Throwable

getScenario

protected ExceptionScenario<?,?> getScenario(java.lang.Object invokee,
                                             java.lang.reflect.Method method)
Note: HandleExceptionss annotations are inherited so so we don't query method.getDeclaringClass() but pass the actual object being invoked.

Parameters:
method -
Returns:
a new ExceptionScenario instance described by the HandleExceptions annotation on the passed method of the passed invokee object's Class or if not present on the invokee's Class itself or null if neither the method nor the class level annotation is present.

handle

protected <H extends java.lang.Exception,I extends java.lang.Exception> void handle(ExceptionScenario<H,I> scenario,
                                                                                    java.lang.Throwable th)
               throws java.lang.Throwable
Type Parameters:
H -
I -
Parameters:
scenario -
th -
Throws:
java.lang.Throwable


Copyright © 2010 emarsys AG. All Rights Reserved.