org.mentawai.filter
Class ExceptionFilter

java.lang.Object
  extended by org.mentawai.filter.ExceptionFilter
All Implemented Interfaces:
Filter

public class ExceptionFilter
extends Object
implements Filter

A filter for handling exceptions that may occour during the action's execution. The filter catchs all the exceptions throwed by the action and call the method handleException. The default implementation of the handleException method put in the action's output the following attributes:

"message" - The message of the Exception (e.getMessage())
"stacktrace" - The Exception's stacktrace (e.getStackTrace())
"stackheader" - The Exception's first stacktrace (e.getStackTrace()[0])

Also, the method e.printStackTrace() is called if the attribute "trace" is setted to true.

After that, the filter return "exception" as the action's result. A Consequence must be mapped for this result.

The behavior of this filter can be changed by extending it's class and overriding the handleException method.

Author:
Rubem Azenha (rubem.azenha@gmail.com)

Field Summary
static String EXCEPTION
          Attribute EXCEPTION of ExceptionFilter.
static String EXCEPTION_KEY
          Attribute EXCEPTION_KEY of ExceptionFilter.
static String MESSAGE_KEY
          Attribute MESSAGE_KEY of ExceptionFilter.
static String STACK_HEADER_KEY
          Attribute STACK_HEADER_KEY of ExceptionFilter.
static String STACK_TRACE_KEY
          Attribute STACK_TRACE_KEY of ExceptionFilter.
 
Constructor Summary
ExceptionFilter()
          Default constructor.
ExceptionFilter(boolean trace)
          Parametric constructor.
 
Method Summary
 void destroy()
          Gives a chance to the filter to deallocalte any resources before it is destroyed.
 String filter(InvocationChain chain)
          Execute the chain and cacth any exception that occours, delegating to the handleException() method the resposability to handle the exception throwed by the chain's execution.
protected static Throwable getRootCause(Throwable throwable)
          Gets the root cause of exception.
protected  String handleException(Action a, Throwable throwable)
          Handle the exception, putting in the action's output the message of the exception, all the StackTrace elements and the first StackTrace element.
protected  String prepareForHtml(String s)
           
protected  String prepareStackTrace(StackTraceElement[] stacktrace)
           
protected  String prepareStackTrace(String[] stacktrace)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MESSAGE_KEY

public static String MESSAGE_KEY
Attribute MESSAGE_KEY of ExceptionFilter.


EXCEPTION_KEY

public static String EXCEPTION_KEY
Attribute EXCEPTION_KEY of ExceptionFilter.


STACK_TRACE_KEY

public static String STACK_TRACE_KEY
Attribute STACK_TRACE_KEY of ExceptionFilter.


STACK_HEADER_KEY

public static String STACK_HEADER_KEY
Attribute STACK_HEADER_KEY of ExceptionFilter.


EXCEPTION

public static String EXCEPTION
Attribute EXCEPTION of ExceptionFilter.

Constructor Detail

ExceptionFilter

public ExceptionFilter()
Default constructor.


ExceptionFilter

public ExceptionFilter(boolean trace)
Parametric constructor.

Parameters:
trace - boolean
Method Detail

filter

public String filter(InvocationChain chain)
              throws Exception
Execute the chain and cacth any exception that occours, delegating to the handleException() method the resposability to handle the exception throwed by the chain's execution.

Specified by:
filter in interface Filter
Parameters:
chain - The InvocationChain for the action this filter is being applied to.
Returns:
the result of the action's execution.
Throws:
Exception

getRootCause

protected static Throwable getRootCause(Throwable throwable)
Gets the root cause of exception.

Parameters:
throwable - Throwable
Returns:
Throwable

handleException

protected String handleException(Action a,
                                 Throwable throwable)
Handle the exception, putting in the action's output the message of the exception, all the StackTrace elements and the first StackTrace element.
This method can be overrided to change the behavior of the ExceptionFilter.

Parameters:
a - the action that has throwed the exception.
throwable - the exception throwed by the action's execution.
Returns:
the result of the action's execution.

prepareStackTrace

protected String prepareStackTrace(String[] stacktrace)

prepareStackTrace

protected String prepareStackTrace(StackTraceElement[] stacktrace)

prepareForHtml

protected String prepareForHtml(String s)

destroy

public void destroy()
Gives a chance to the filter to deallocalte any resources before it is destroyed. This is called when the web application is stopped, in other words, this has nothing to do with garbage collection.

Specified by:
destroy in interface Filter


Copyright © 2013. All Rights Reserved.