org.nuiton.web.filter
Class TopiaTransactionFilter

java.lang.Object
  extended by org.nuiton.web.filter.TopiaTransactionFilter
All Implemented Interfaces:
javax.servlet.Filter

public abstract class TopiaTransactionFilter
extends Object
implements javax.servlet.Filter

Purpose of this filter

This filter purpose is to inject in the request a transaction from TopiaContext and deal with the complete lifecycle of a topia transaction while a request.

The injected transaction will be closed (if was really opened) at the end of the request.

Configuration of the filter

The filter accepts two configuration parameters:

Obtain the transaction

The (proxied) transaction is pushed as an attribute in the servlet request.

The attribute name is defined by field requestAttributeName (default value is TOPIA_TRANSACTION_REQUEST_ATTRIBUTE) and can be changed.

A convience method is created here to obtain the transaction getTransaction(ServletRequest) :

 TopiaContext tx = TopiaTransactionFilter.getTransaction(ServletRequest);
 

If you prefer to not use this nice method, you can also do this:

 TopiaContext tx = (TopiaContext) request.getAttribute(TopiaTransactionFilter#TOPIA_TRANSACTION_REQUEST_ATTRIBUTE);
 

Or

 TopiaContext tx = (TopiaContext) request.getAttribute(modifiedAttributeName);
 

Since:
1.6
Author:
tchemit

Nested Class Summary
 class TopiaTransactionFilter.TopiaTransactionProxyInvocationHandler
          Handler of a proxy on a TopiaContext.
 
Field Summary
static String[] DEFAULT_EXCLUDE_METHODS
           
static String[] DEFAULT_UNUSED_METHODS
           
protected  Set<String> excludeMethods
          names of methods to forbid access while using proxy.
protected  String requestAttributeName
          Name of the request attribute where to push the transaction.
static String TOPIA_TRANSACTION_REQUEST_ATTRIBUTE
           
protected  Set<String> unusedMethods
          names of methods to by-pass if no transaction opened on proxy.
 
Constructor Summary
TopiaTransactionFilter()
           
 
Method Summary
protected abstract  org.nuiton.topia.TopiaContext beginTransaction(javax.servlet.ServletRequest request)
          Method to open a new transaction.
 void destroy()
           
 void doFilter(javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, javax.servlet.FilterChain chain)
           
 Set<String> getExcludeMethods()
           
static org.nuiton.topia.TopiaContext getTransaction(javax.servlet.ServletRequest request)
           
 Set<String> getUnusedMethods()
           
 void init(javax.servlet.FilterConfig filterConfig)
           
protected  void onCloseTransaction(org.nuiton.topia.TopiaContext transaction)
          Hook method to close the topia transaction of the request at the end of the request when all filter has been consumed.
protected  Object onExcludeMethod(Object proxy, Method method, Object[] args)
          Hook method called when a method with his name in excludeMethods was invoked on the proxied transaction.
protected  Object onUnusedMethod(Object proxy, Method method, Object[] args)
          Hook method called when a method with his name in unusedMethods was invoked on the proxied transaction while the underlying transaction is still not opened.
 void setRequestAttributeName(String requestAttributeName)
          to change the requestAttributeName.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TOPIA_TRANSACTION_REQUEST_ATTRIBUTE

public static final String TOPIA_TRANSACTION_REQUEST_ATTRIBUTE
See Also:
Constant Field Values

DEFAULT_EXCLUDE_METHODS

public static final String[] DEFAULT_EXCLUDE_METHODS

DEFAULT_UNUSED_METHODS

public static final String[] DEFAULT_UNUSED_METHODS

excludeMethods

protected Set<String> excludeMethods
names of methods to forbid access while using proxy.


unusedMethods

protected Set<String> unusedMethods
names of methods to by-pass if no transaction opened on proxy.


requestAttributeName

protected String requestAttributeName
Name of the request attribute where to push the transaction.

By default will use value of TOPIA_TRANSACTION_REQUEST_ATTRIBUTE.

Since:
1.10
Constructor Detail

TopiaTransactionFilter

public TopiaTransactionFilter()
Method Detail

getExcludeMethods

public Set<String> getExcludeMethods()

getUnusedMethods

public Set<String> getUnusedMethods()

setRequestAttributeName

public void setRequestAttributeName(String requestAttributeName)
to change the requestAttributeName.

Parameters:
requestAttributeName - new name of the request attribute where to push the transaction.
Since:
1.10

getTransaction

public static org.nuiton.topia.TopiaContext getTransaction(javax.servlet.ServletRequest request)

beginTransaction

protected abstract org.nuiton.topia.TopiaContext beginTransaction(javax.servlet.ServletRequest request)
                                                           throws org.nuiton.topia.TopiaRuntimeException
Method to open a new transaction.

Parameters:
request - incoming request
Returns:
the new freshly opened transaction
Throws:
org.nuiton.topia.TopiaRuntimeException - if any problem while opening a new transaction

destroy

public void destroy()
Specified by:
destroy in interface javax.servlet.Filter

init

public void init(javax.servlet.FilterConfig filterConfig)
          throws javax.servlet.ServletException
Specified by:
init in interface javax.servlet.Filter
Throws:
javax.servlet.ServletException

doFilter

public void doFilter(javax.servlet.ServletRequest request,
                     javax.servlet.ServletResponse response,
                     javax.servlet.FilterChain chain)
              throws IOException,
                     javax.servlet.ServletException
Specified by:
doFilter in interface javax.servlet.Filter
Throws:
IOException
javax.servlet.ServletException

onExcludeMethod

protected Object onExcludeMethod(Object proxy,
                                 Method method,
                                 Object[] args)
                          throws Throwable
Hook method called when a method with his name in excludeMethods was invoked on the proxied transaction.

Parameters:
proxy - proxied transaction
method - method invoked
args - arguments of the invoked method
Returns:
the return code of the method
Throws:
Throwable - if any error to do.

onCloseTransaction

protected void onCloseTransaction(org.nuiton.topia.TopiaContext transaction)
Hook method to close the topia transaction of the request at the end of the request when all filter has been consumed.

Parameters:
transaction - the transaction to close (can be null if transaction was not required while the current request)
Since:
1.9.1

onUnusedMethod

protected Object onUnusedMethod(Object proxy,
                                Method method,
                                Object[] args)
                         throws Throwable
Hook method called when a method with his name in unusedMethods was invoked on the proxied transaction while the underlying transaction is still not opened.

Parameters:
proxy - the proxy itself
method - method invoked
args - arguments of the invoked method
Returns:
the return code of the method
Throws:
Throwable - if any error to do.


Copyright © 2011-2012 CodeLutin. All Rights Reserved.