r125 - trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/interceptor
Author: tchemit Date: 2011-10-28 22:54:21 +0200 (Fri, 28 Oct 2011) New Revision: 125 Url: http://nuiton.org/repositories/revision/nuiton-web/125 Log: Evolution #1790: Add OpenTopiaTransactionInterceptor Modified: trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/interceptor/OpenTopiaTransactionInterceptor.java trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/interceptor/TopiaTransactionInterceptor.java Modified: trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/interceptor/OpenTopiaTransactionInterceptor.java =================================================================== --- trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/interceptor/OpenTopiaTransactionInterceptor.java 2011-10-28 17:05:28 UTC (rev 124) +++ trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/interceptor/OpenTopiaTransactionInterceptor.java 2011-10-28 20:54:21 UTC (rev 125) @@ -10,11 +10,8 @@ import org.nuiton.topia.TopiaException; import org.nuiton.topia.framework.TopiaContextImplementor; import org.nuiton.topia.framework.TopiaTransactionAware; +import org.nuiton.web.struts2.filter.CloseTopiaTransactionFilter; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; @@ -40,7 +37,7 @@ * <p/> * Note also that the transaction is <strong>not</strong> commited nor closed. * If you want the transaction to be closed, you may use - * {@link org.nuiton.web.struts2.filter.CloseTopiaTransactionFilter} by adding + * {@link CloseTopiaTransactionFilter} by adding * it to your web.xml file. * <p/> * This interceptor, as it provides connection to database should be in the @@ -63,16 +60,11 @@ * <!-- END SNIPPET: parameters --> * * @author tchemit <chemit@codelutin.com> + * @author bleny <leny@codelutin.com> * @since 1.5 */ public abstract class OpenTopiaTransactionInterceptor extends AbstractInterceptor { - /** To specify on your action that you never want any commit. */ - @Retention(RetentionPolicy.RUNTIME) - @Target({ElementType.TYPE, ElementType.METHOD}) - public @interface NoCommit { - } - /** Logger. */ private static final Log log = LogFactory.getLog(OpenTopiaTransactionInterceptor.class); @@ -103,7 +95,7 @@ * Method to open a new transaction. * * @return the new freshly opened transaction - * @throws org.nuiton.topia.TopiaException if any problem while opening a new transaction + * @throws TopiaException if any problem while opening a new transaction */ protected abstract TopiaContext beginTransaction() throws TopiaException; @@ -120,15 +112,6 @@ } } - /** - * @deprecated to be removed in the same time as TopiaTransactionInterceptor - */ - @Deprecated - protected void closeTransaction(TopiaContext transaction, Object action, - ActionInvocation invocation) throws TopiaException { - // do nothing by contract - } - @Override public String intercept(ActionInvocation invocation) throws Exception { @@ -161,15 +144,7 @@ // set the transaction in the action transactionAware.setTransaction(proxy); - try { - - return invocation.invoke(); - - } finally { - - closeTransaction(proxyInvocationHandler.getTransaction(), action, invocation); - - } + return invocation.invoke(); } /** Modified: trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/interceptor/TopiaTransactionInterceptor.java =================================================================== --- trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/interceptor/TopiaTransactionInterceptor.java 2011-10-28 17:05:28 UTC (rev 124) +++ trunk/nuiton-struts2/src/main/java/org/nuiton/web/struts2/interceptor/TopiaTransactionInterceptor.java 2011-10-28 20:54:21 UTC (rev 125) @@ -29,24 +29,84 @@ import org.apache.commons.logging.LogFactory; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.topia.framework.TopiaTransactionAware; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import java.lang.reflect.Method; +import java.lang.reflect.Proxy; -/** Same interceptor as {@link OpenTopiaTransactionInterceptor} but close +/** + * Same interceptor as {@link OpenTopiaTransactionInterceptor} but close * the transaction after action is called. The transaction will be commited * (unless asked otherwise) and closed. * * @author tchemit <chemit@codelutin.com> * @since 1.2 + * @deprecated since 1.5, will be removed soon */ @Deprecated public abstract class TopiaTransactionInterceptor extends OpenTopiaTransactionInterceptor { + /** To specify on your action or method that you never want any commit. */ + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.TYPE, ElementType.METHOD}) + public @interface NoCommit { + } + + private static final long serialVersionUID = 1L; + /** Logger. */ - private static final Log log = LogFactory.getLog(TopiaTransactionInterceptor.class); + private static final Log log = + LogFactory.getLog(TopiaTransactionInterceptor.class); @Override + public String intercept(ActionInvocation invocation) throws Exception { + + TopiaTransactionAware transactionAware = null; + + Object action = invocation.getProxy().getAction(); + + if (action instanceof TopiaTransactionAware) { + transactionAware = (TopiaTransactionAware) action; + } + + if (transactionAware == null) { + + // not a transaction aware action, direct skip this interceptor + return invocation.invoke(); + } + + // creates a proxy of a lazy transaction + + TopiaTransactionProxyInvocationHandler proxyInvocationHandler = + new TopiaTransactionProxyInvocationHandler(); + + TopiaContext proxy = (TopiaContext) Proxy.newProxyInstance( + getClass().getClassLoader(), + new Class<?>[]{TopiaContext.class, + TopiaContextImplementor.class}, + proxyInvocationHandler + ); + + // set the transaction in the action + transactionAware.setTransaction(proxy); + + try { + + return invocation.invoke(); + + } finally { + + closeTransaction(proxyInvocationHandler.getTransaction(), action, invocation); + + } + } + protected void closeTransaction(TopiaContext transaction, Object action, ActionInvocation invocation) throws TopiaException {
participants (1)
-
tchemit@users.nuiton.org