Class ContextExceptionHandlerInterceptor

  extended by
      extended by
All Implemented Interfaces:
org.aopalliance.aop.Advice, org.aopalliance.intercept.Interceptor, org.aopalliance.intercept.MethodInterceptor

public class ContextExceptionHandlerInterceptor
extends AbstractExceptionHandlerInterceptor

This class allows to handle exceptions centrally. Realized as an interceptor, it can be added to an arbitrary bean using a proxy. The handler supports different contexts which allows to handle a particular exception in different fashions, depending on the current context.

This class is configured with different policies, one for each context. A policy is an <context, ExceptionConfiguration> pair.

Important: The context is stored in a ThreadLocal, allowing to use this class in multi threading environments. Since it's considered cumbersome to set the context before each invocation it isn't reset. The programmer is responsible to set the context appropriately.

Don't use this interceptor directly. Instead use the ContextExceptionHandlerFactoryBean.

Example: Let's assume the exception configurations are initialized properly.

<bean id="contextExceptionHandlerInterceptor"
       <property name="policies">
               <entry key="gui">
                       <ref local="guiExceptionConfiguration"/>
               <entry key="batch">
                       <ref local="batchExceptionConfiguration"/>
And the Java fragment:
 foo(); // an exception is handled by the gui policy

 foo(); // an exception is handled by the batch policy

 foo(); // an exception is handled by the batch policy

Andreas Bur (ABU)
See Also:
Last check-in date:
2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) by swismer for revision 3873

Field Summary
Fields inherited from class
Constructor Summary
          Default constructor.
Method Summary
protected  Object handleException(Throwable t, org.aopalliance.intercept.MethodInvocation invocation)
          Handles the given exception that was thrown in the given method invocation's execution.
static Object setContext(Object context)
          Sets the current thread's context.
 void setPolicies(Map policies)
          Sets the policies.
Methods inherited from class
doHandleException, doInvoke, getRetries, handleInterfaceExceptions, invoke, setDefaultBehaviourConsume, setForwardSignatureExceptions, setHandleRTSignatureExceptions, setRetries
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public ContextExceptionHandlerInterceptor()
Default constructor. Configures the interceptor to handle only those exceptions that are not defined in the signature (excluding unchecked exceptions, which are handled always).

Method Detail


public static Object setContext(Object context)
Sets the current thread's context.

context - The context to set.
Returns the previous context.


public void setPolicies(Map policies)
Sets the policies.

policies - The policies to set.


protected Object handleException(Throwable t,
                                 org.aopalliance.intercept.MethodInvocation invocation)
                          throws Throwable
Handles the given exception that was thrown in the given method invocation's execution.

Specified by:
handleException in class AbstractExceptionHandlerInterceptor
t - The exception to handle.
invocation - The method invocation that threw the exception.
Returns an object which is treated as the original invocation's result.
RetryException - Signals that the complete invocation has to be rerun.
Throwable - Any exception thrown by a exception handler.

Copyright © 2005-2011 ELCA. All Rights Reserved.