net.sf.sasl.aop.exception.handling
Class ServiceExceptionFirewallAspect

java.lang.Object
  extended by net.sf.sasl.aop.AbstractRootAspect
      extended by net.sf.sasl.aop.exception.handling.ServiceExceptionFirewallAspect
All Implemented Interfaces:
org.aopalliance.aop.Advice, org.aopalliance.intercept.Interceptor, org.aopalliance.intercept.MethodInterceptor, org.springframework.beans.factory.InitializingBean, org.springframework.core.Ordered, org.springframework.core.PriorityOrdered

public class ServiceExceptionFirewallAspect
extends AbstractRootAspect
implements org.aopalliance.intercept.MethodInterceptor, org.springframework.beans.factory.InitializingBean

The task that this aspect addresses is to prune exceptions that have been thrown by join points (target methods). It could be configured via a list of configurations to return the well known return type of a target method, instead of the thrown exceptions. In the instantiated return type of a target method it can set some properties so that a caller of a target method can later determine that his call did not succeeded. To know which properties must be set with which values for a return type the aspect needs a list of configurations. If the aspect can not find a configuration for a return type or a configuration for one of the return types super classes, the aspect fails and will just rethrow the exception. The aspect also supports a special "error detail message" property in which it can fills the string representation of the stack trace of an exception.

The environment in which this aspect may be useful is for example a web service landscape.
Normally a web service must forfill a declared "contract". On the other hand some aspects only work correctly (like for example a transactional aspect) if service methods can throw any exceptions. This behavior breaks the contract. Secondly transforming such exceptions into declared service exceptions might not always work very well in a heterogeneous technology environment. Thats why exceptions get sometimes transformed from the sender to the receiver into something very strange that was not expected and never declared. Some older formal languages - like C - are also not supporting the concept of an exception natively. So all in all it should be avoided to throw exceptions in the communication between heterogeneous systems.

The aspect could be also used on the callers side to protect versus exceptions.

Since:
0.0.1 (sasl-common-aspect-library)
Author:
Philipp Förmer
See Also:
ServiceExceptionFirewallAspectConfigBean

Field Summary
 
Fields inherited from class net.sf.sasl.aop.AbstractRootAspect
logger
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
ServiceExceptionFirewallAspect()
          Default constructor, setting the order to the highest priority.
 
Method Summary
 void afterPropertiesSet()
          Throws an illegal state exception if no config beans have been set.
protected  Object constructReturnValue(org.aopalliance.intercept.MethodInvocation invocation, Throwable throwable)
          See the see-tag for more informations what this method does.
protected  ReflectionHelper getReflectionHelper()
          Returns the reflection helper which will be used by this class for reflection operations.
protected  StackTraceHelper getStackTraceHelper()
          Returns the stack trace helper which will be used by this class for stack trace transformation operations.
 Object invoke(org.aopalliance.intercept.MethodInvocation invocation)
          Around aspect method for a method invocation.
protected  void logPropertySetFailure(Class<?> targetClass, String propertyName)
          Logs a property set failure over the logger.
protected  ServiceExceptionFirewallAspectConfigBean lookupConfigForClass(Class<?> clazz)
          Tries to find a config bean for the given class in the setted config bean list.
 void setConfigBeanList(List<ServiceExceptionFirewallAspectConfigBean> configBeanList)
          Sets a list of configurations which will be used by this "firewall".
protected  void setReflectionHelper(ReflectionHelper reflectionHelper)
          Sets the reflection helper which will be used by this class for reflection operations.
protected  void setStackTraceHelper(StackTraceHelper stackTraceHelper)
          Sets the stack trace helper which will be used by this class for stack trace transformation operations.
 
Methods inherited from class net.sf.sasl.aop.AbstractRootAspect
getLogger, getOrder, setLogger, setLoggerByName, setOrder
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServiceExceptionFirewallAspect

public ServiceExceptionFirewallAspect()
Default constructor, setting the order to the highest priority.

Since:
0.0.1 (sasl-common-aspect-library)
Method Detail

invoke

public Object invoke(org.aopalliance.intercept.MethodInvocation invocation)
              throws Throwable
Around aspect method for a method invocation. Please see { setConfigBeanList(List) and the introduction of the class for more informations what this around aspect does and how it could be configured.

Specified by:
invoke in interface org.aopalliance.intercept.MethodInterceptor
Throws:
Throwable
Since:
0.0.1 (sasl-common-aspect-library)
See Also:
MethodInterceptor

constructReturnValue

protected Object constructReturnValue(org.aopalliance.intercept.MethodInvocation invocation,
                                      Throwable throwable)
See the see-tag for more informations what this method does.

Parameters:
invocation - non null
Returns:
null or non null
Since:
0.0.1 (sasl-common-aspect-library)
See Also:
setConfigBeanList(List)

logPropertySetFailure

protected void logPropertySetFailure(Class<?> targetClass,
                                     String propertyName)
Logs a property set failure over the logger.

Parameters:
targetClass - non null
propertyName - non null
Since:
0.0.1 (sasl-common-aspect-library)

lookupConfigForClass

protected ServiceExceptionFirewallAspectConfigBean lookupConfigForClass(Class<?> clazz)
Tries to find a config bean for the given class in the setted config bean list. Firstly it tries to find a configuration for the class. It no configuration is found it tries to find a configuration for one of the class super classes. If this also fails, null is returned. Else the first configuration which could be resolved via the class or its super classes.

Parameters:
clazz - null or non null
Returns:
null or non null
Since:
0.0.1 (sasl-common-aspect-library)

setConfigBeanList

public void setConfigBeanList(List<ServiceExceptionFirewallAspectConfigBean> configBeanList)
                       throws IllegalArgumentException
Sets a list of configurations which will be used by this "firewall". A configuration will be used for the scenario that a target method throwed a throwable. In this case a configuration for the target methods return type will be searched in the list of configurations. If one is found (could be also a configuration for a super class of the return type) it will be used to set the defined properties of the configuration to the defined values in a new instantiated object of the target methods return type. The throwable is pruned in this case and replaced by the instantiated object, which is the normal and definitely known return value of the target method.

Parameters:
configBeanList - null or non null
Throws:
IllegalArgumentException -
(1)if multiple configuration were found for the same class,
(2)if the class object for class name could not be resolved or
(3)if the defined property names in a configuration do not exist as setter methods or fields in the defined class or its super classes.
Since:
0.0.1 (sasl-common-aspect-library)

setReflectionHelper

protected void setReflectionHelper(ReflectionHelper reflectionHelper)
Sets the reflection helper which will be used by this class for reflection operations. Should be normally only relevant for test cases.

Parameters:
reflectionHelper - non null

getReflectionHelper

protected ReflectionHelper getReflectionHelper()
Returns the reflection helper which will be used by this class for reflection operations.

Returns:
non null

setStackTraceHelper

protected void setStackTraceHelper(StackTraceHelper stackTraceHelper)
Sets the stack trace helper which will be used by this class for stack trace transformation operations. Should be normally only relevant for test cases.

Parameters:
stackTraceHelper - non null

getStackTraceHelper

protected StackTraceHelper getStackTraceHelper()
Returns the stack trace helper which will be used by this class for stack trace transformation operations.

Returns:
non null.

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Throws an illegal state exception if no config beans have been set. In this case the aspect is meaningless. Thats just a simple warn heuristic for the user.

Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
Exception
See Also:
InitializingBean.afterPropertiesSet()


Copyright © 2010. All Rights Reserved.