ch.elca.el4j.services.exceptionhandler
Class ContextExceptionHandlerInterceptor

java.lang.Object
  extended by ch.elca.el4j.services.exceptionhandler.AbstractExceptionHandlerInterceptor
      extended by ch.elca.el4j.services.exceptionhandler.ContextExceptionHandlerInterceptor
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"
       class="ch.elca.el4j.services.exceptionhandler.ContextExceptionHandlerInterceptor">
       <property name="policies">
           <map>
               <entry key="gui">
                   <list>
                       <ref local="guiExceptionConfiguration"/>
                   </list>
               </entry>
               <entry key="batch">
                   <list>
                       <ref local="batchExceptionConfiguration"/>
                   </list>
               </entry>
           </map>
       </property>
   </bean>
And the Java fragment:
 ContextExceptionHandlerInterceptor.setContext("gui");
 foo(); // an exception is handled by the gui policy

 ContextExceptionHandlerInterceptor.setContext("batch");
 foo(); // an exception is handled by the batch policy

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

Author:
Andreas Bur (ABU)
See Also:
ContextExceptionHandlerFactoryBean
File-location:
ContextExceptionHandlerInterceptor
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 ch.elca.el4j.services.exceptionhandler.AbstractExceptionHandlerInterceptor
s_retries
 
Constructor Summary
ContextExceptionHandlerInterceptor()
          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 ch.elca.el4j.services.exceptionhandler.AbstractExceptionHandlerInterceptor
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

ContextExceptionHandlerInterceptor

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

setContext

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

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

setPolicies

public void setPolicies(Map policies)
Sets the policies.

Parameters:
policies - The policies to set.

handleException

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
Parameters:
t - The exception to handle.
invocation - The method invocation that threw the exception.
Returns:
Returns an object which is treated as the original invocation's result.
Throws:
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.