public abstract class TypedTopiaTransactionFilter<PersistenceContext extends org.nuiton.topia.persistence.TopiaPersistenceContext> extends Object implements javax.servlet.Filter
TopiaPersistenceContext 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.
excludeMethods: This parameters configure a set of method names
which should never be called on the proxied transaction.
When a such method is called on the transaction then the filter will pass in
the hook onExcludeMethod(Object, Method, Object[]).
Default implementation of this hook is to throw an exception.
unusedMethods: This parameters configure a set of method names
which should be by-pass when the proxied transaction was not still open (via a
TopiaApplicationContext.newPersistenceContext().
When a such method is called on the transaction then the filter will pass in
the hook onUnusedMethod(Object, Method, Object[]).
Default implementation of this hook is to not return null values.
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 getPersistenceContext(ServletRequest) :
PersistenceContext tx = TypedTopiaTransactionFilter.getPersistenceContext(ServletRequest);
If you prefer to not use this nice method, you can also do this:
PersistenceContext tx = (PersistenceContext) request.getAttribute(TopiaTransactionFilter#TOPIA_TRANSACTION_REQUEST_ATTRIBUTE);
Or
PersistenceContext tx = (PersistenceContext) request.getAttribute(modifiedAttributeName);
| Modifier and Type | Class and Description |
|---|---|
class |
TypedTopiaTransactionFilter.TopiaTransactionProxyInvocationHandler
Handler of a proxy on a
TopiaPersistenceContext. |
| Modifier and Type | Field and Description |
|---|---|
static String[] |
DEFAULT_EXCLUDE_METHODS |
static String[] |
DEFAULT_UNUSED_METHODS |
protected Set<String> |
excludeMethods
names of methods to forbid access while using proxy.
|
protected Class<PersistenceContext> |
persistenceContextType |
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.
|
| Modifier | Constructor and Description |
|---|---|
protected |
TypedTopiaTransactionFilter(Class<PersistenceContext> persistenceContextType) |
| Modifier and Type | Method and Description |
|---|---|
protected abstract PersistenceContext |
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 <PersistenceContext extends org.nuiton.topia.persistence.TopiaPersistenceContext> |
getPersistenceContext(javax.servlet.ServletRequest request) |
static <PersistenceContext extends org.nuiton.topia.persistence.TopiaPersistenceContext> |
getTransaction(javax.servlet.ServletRequest request)
Deprecated.
since 1.16, use now
getPersistenceContext(ServletRequest) |
Set<String> |
getUnusedMethods() |
void |
init(javax.servlet.FilterConfig filterConfig) |
protected void |
onCloseTransaction(PersistenceContext 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. |
public static final String TOPIA_TRANSACTION_REQUEST_ATTRIBUTE
public static final String[] DEFAULT_EXCLUDE_METHODS
public static final String[] DEFAULT_UNUSED_METHODS
protected Set<String> excludeMethods
protected Set<String> unusedMethods
protected final Class<PersistenceContext extends org.nuiton.topia.persistence.TopiaPersistenceContext> persistenceContextType
protected String requestAttributeName
By default will use value of
TOPIA_TRANSACTION_REQUEST_ATTRIBUTE.
protected TypedTopiaTransactionFilter(Class<PersistenceContext> persistenceContextType)
public static <PersistenceContext extends org.nuiton.topia.persistence.TopiaPersistenceContext> PersistenceContext getPersistenceContext(javax.servlet.ServletRequest request)
@Deprecated public static <PersistenceContext extends org.nuiton.topia.persistence.TopiaPersistenceContext> PersistenceContext getTransaction(javax.servlet.ServletRequest request)
getPersistenceContext(ServletRequest)public void setRequestAttributeName(String requestAttributeName)
requestAttributeName.requestAttributeName - new name of the request attribute
where to push the transaction.protected abstract PersistenceContext beginTransaction(javax.servlet.ServletRequest request) throws org.nuiton.topia.persistence.TopiaException
request - incoming requestorg.nuiton.topia.persistence.TopiaException - if any problem while opening a new transactionpublic void destroy()
destroy in interface javax.servlet.Filterpublic void init(javax.servlet.FilterConfig filterConfig)
throws javax.servlet.ServletException
init in interface javax.servlet.Filterjavax.servlet.ServletExceptionpublic void doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain chain)
throws IOException,
javax.servlet.ServletException
doFilter in interface javax.servlet.FilterIOExceptionjavax.servlet.ServletExceptionprotected Object onExcludeMethod(Object proxy, Method method, Object[] args) throws Throwable
excludeMethods was invoked on the proxied transaction.proxy - proxied transactionmethod - method invokedargs - arguments of the invoked methodThrowable - if any error to do.protected void onCloseTransaction(PersistenceContext transaction)
transaction - the transaction to close (can be null if transaction
was not required while the current request)protected Object onUnusedMethod(Object proxy, Method method, Object[] args) throws Throwable
unusedMethods was invoked on the proxied transaction
while the underlying transaction is still not opened.proxy - the proxy itselfmethod - method invokedargs - arguments of the invoked methodThrowable - if any error to do.Copyright © 2011–2017 CodeLutin. All rights reserved.