aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1689632 - in /aries/trunk/transaction: transaction-blueprint/ transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/ transaction-itests/src/test/java/org/apache/aries/transaction/itests/ transaction-testbundle/ transacti...
Date Tue, 07 Jul 2015 11:52:29 GMT
Author: cschneider
Date: Tue Jul  7 11:52:28 2015
New Revision: 1689632

URL: http://svn.apache.org/r1689632
Log:
ARIES-628: enable jta transactional annotation

Added:
    aries/trunk/transaction/transaction-itests/src/test/java/org/apache/aries/transaction/itests/MandatoryJtaAnnotatedTranAttributeTest.java
    aries/trunk/transaction/transaction-testbundle/src/main/java/org/apache/aries/transaction/test/impl/JtaAnnotatedTestBeanImpl.java
Modified:
    aries/trunk/transaction/transaction-blueprint/pom.xml
    aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/AnnotationParser.java
    aries/trunk/transaction/transaction-itests/src/test/java/org/apache/aries/transaction/itests/AbstractIntegrationTest.java
    aries/trunk/transaction/transaction-testbundle/pom.xml
    aries/trunk/transaction/transaction-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml

Modified: aries/trunk/transaction/transaction-blueprint/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-blueprint/pom.xml?rev=1689632&r1=1689631&r2=1689632&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-blueprint/pom.xml (original)
+++ aries/trunk/transaction/transaction-blueprint/pom.xml Tue Jul  7 11:52:28 2015
@@ -96,7 +96,12 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
-
+        <dependency>
+            <groupId>javax.transaction</groupId>
+            <artifactId>javax.transaction-api</artifactId>
+            <version>1.2</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

Modified: aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/AnnotationParser.java
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/AnnotationParser.java?rev=1689632&r1=1689631&r2=1689632&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/AnnotationParser.java
(original)
+++ aries/trunk/transaction/transaction-blueprint/src/main/java/org/apache/aries/transaction/parsing/AnnotationParser.java
Tue Jul  7 11:52:28 2015
@@ -18,6 +18,7 @@
  */
 package org.apache.aries.transaction.parsing;
 
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 
 import org.apache.aries.blueprint.BeanProcessor;
@@ -26,8 +27,11 @@ import org.apache.aries.blueprint.Interc
 import org.apache.aries.transaction.Constants;
 import org.apache.aries.transaction.TxComponentMetaDataHelper;
 import org.apache.aries.transaction.annotations.Transaction;
+import org.apache.aries.transaction.annotations.TransactionPropagationType;
 import org.osgi.service.blueprint.reflect.BeanMetadata;
 
+import javax.transaction.Transactional;
+
 /**
  * Adds the transactional interceptor if Transaction annotation is present
  * on bean class or superclasses.
@@ -59,14 +63,20 @@ public class AnnotationParser implements
         boolean interceptorAssigned = isInterceptorAssigned(beanData);
         while (c != Object.class) {
             for (Method m : c.getDeclaredMethods()) {
-                Transaction t = m.getAnnotation(Transaction.class);
-                if (t != null && helper.getComponentMethodTxAttribute(beanData, m.getName())
== null) {
-                    assertAllowedModifier(m);
-                    helper.setComponentTransactionData(cdr, beanData, t.value(), m.getName());
-                    if (!interceptorAssigned) {
-                        cdr.registerInterceptorWithComponent(beanData, interceptor);
-                        interceptorAssigned = true;
+                try {
+                    if (beanData != null && helper.getComponentMethodTxAttribute(beanData,
m.getName()) == null) {
+                        Transaction t = tryGetTransaction(m);
+                        if (t != null) {
+                            assertAllowedModifier(m);
+                            helper.setComponentTransactionData(cdr, beanData, t.value(),
m.getName());
+                            if (!interceptorAssigned) {
+                                cdr.registerInterceptorWithComponent(beanData, interceptor);
+                                interceptorAssigned = true;
+                            }
+                        }
                     }
+                } catch(IllegalStateException e) {
+                    // don't break bean creation due to invalid transaction attribute
                 }
 
             }
@@ -75,6 +85,45 @@ public class AnnotationParser implements
         return bean;
     }
 
+    private Transaction tryGetTransaction(Method m) {
+        Transaction t = m.getAnnotation(Transaction.class);
+        if (t == null) {
+            final Transactional jtaT = m.getAnnotation(Transactional.class);
+            if (jtaT != null) {
+                t = new Transaction() {
+                    @Override
+                    public Class<? extends Annotation> annotationType() {
+                        return Transactional.class;
+                    }
+
+                    @Override
+                    public TransactionPropagationType value() {
+                        return convert(jtaT.value());
+                    }
+                };
+            }
+        }
+        return t;
+    }
+
+    private static TransactionPropagationType convert(Transactional.TxType type) {
+        switch(type) {
+            case MANDATORY:
+                return TransactionPropagationType.Mandatory;
+            case REQUIRED:
+                return TransactionPropagationType.Required;
+            case REQUIRES_NEW:
+                return TransactionPropagationType.RequiresNew;
+            case SUPPORTS:
+                return TransactionPropagationType.Supports;
+            case NOT_SUPPORTED:
+                return TransactionPropagationType.NotSupported;
+            case NEVER:
+                return TransactionPropagationType.Never;
+        }
+        throw new IllegalArgumentException(Constants.MESSAGES.getMessage("unknown.jta.transaction.type",
type.name()));
+    }
+
     private boolean isInterceptorAssigned(BeanMetadata beanData) {
         for (Interceptor i : cdr.getInterceptors(beanData)) {
             if (i == interceptor) {

Modified: aries/trunk/transaction/transaction-itests/src/test/java/org/apache/aries/transaction/itests/AbstractIntegrationTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-itests/src/test/java/org/apache/aries/transaction/itests/AbstractIntegrationTest.java?rev=1689632&r1=1689631&r2=1689632&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-itests/src/test/java/org/apache/aries/transaction/itests/AbstractIntegrationTest.java
(original)
+++ aries/trunk/transaction/transaction-itests/src/test/java/org/apache/aries/transaction/itests/AbstractIntegrationTest.java
Tue Jul  7 11:52:28 2015
@@ -73,6 +73,11 @@ public abstract class AbstractIntegratio
                 mavenBundle("org.apache.aries.blueprint", "org.apache.aries.blueprint.core"),
                 mavenBundle("org.apache.aries.proxy", "org.apache.aries.proxy.api"),
                 mavenBundle("org.apache.aries.proxy", "org.apache.aries.proxy.impl"),
+                mavenBundle("javax.interceptor", "javax.interceptor-api", "1.2"),
+                mavenBundle("org.apache.servicemix.bundles", "org.apache.servicemix.bundles.javax-inject",
"1_2"),
+                mavenBundle("javax.el", "javax.el-api", "3.0.0"),
+                mavenBundle("javax.enterprise", "cdi-api", "1.2"),
+                mavenBundle("javax.transaction", "javax.transaction-api", "1.2"),
                 mavenBundle("org.apache.aries.transaction", "org.apache.aries.transaction.manager").versionAsInProject(),
                 mavenBundle("org.apache.aries.transaction", "org.apache.aries.transaction.blueprint").versionAsInProject(),
                 mavenBundle("org.apache.aries.transaction", "org.apache.aries.transaction.jdbc").versionAsInProject(),

Added: aries/trunk/transaction/transaction-itests/src/test/java/org/apache/aries/transaction/itests/MandatoryJtaAnnotatedTranAttributeTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-itests/src/test/java/org/apache/aries/transaction/itests/MandatoryJtaAnnotatedTranAttributeTest.java?rev=1689632&view=auto
==============================================================================
--- aries/trunk/transaction/transaction-itests/src/test/java/org/apache/aries/transaction/itests/MandatoryJtaAnnotatedTranAttributeTest.java
(added)
+++ aries/trunk/transaction/transaction-itests/src/test/java/org/apache/aries/transaction/itests/MandatoryJtaAnnotatedTranAttributeTest.java
Tue Jul  7 11:52:28 2015
@@ -0,0 +1,93 @@
+/*  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.aries.transaction.itests;
+
+import org.apache.aries.transaction.test.TestBean;
+import org.junit.Test;
+
+import javax.transaction.RollbackException;
+import javax.transaction.UserTransaction;
+import java.sql.SQLException;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class MandatoryJtaAnnotatedTranAttributeTest extends AbstractIntegrationTest {
+  
+  @Test
+  public void testMandatory() throws Exception {
+	  String prefix = "MTAT";
+      TestBean bean = context().getService(TestBean.class, "(tranAttribute=MandatoryJtaAnnotated)");
+      UserTransaction tran = context().getService(UserTransaction.class);
+      
+      //Test with client transaction - the user transaction is used to insert a row
+      int initialRows = bean.countRows();
+      
+      tran.begin();
+      bean.insertRow(prefix + "testWithClientTran", 1);
+      tran.commit();
+      
+      int finalRows = bean.countRows();
+      assertTrue("Initial rows: " + initialRows + ", Final rows: " + finalRows, finalRows
- initialRows == 1);
+  
+      //Test with client transaction and application exception - the user transaction is
not rolled back
+      initialRows = bean.countRows();
+      
+      tran.begin();
+      bean.insertRow(prefix + "testWithClientTranAndWithAppException", 1);
+      
+      try {
+          bean.insertRow(prefix + "testWithClientTranAndWithAppException", 2, new SQLException("Dummy
exception"));
+      } catch (SQLException e) {
+          e.printStackTrace();
+      }
+      
+      tran.commit();
+      
+      finalRows = bean.countRows();
+      assertTrue("Initial rows: " + initialRows + ", Final rows: " + finalRows, finalRows
- initialRows == 2);
+      
+      //Test with client transaction and runtime exception - the user transaction is rolled
back
+      initialRows = bean.countRows();
+      
+      tran.begin();
+      bean.insertRow(prefix + "testWithClientTranAndWithRuntimeException", 1);
+      
+      try {
+          bean.insertRow(prefix + "testWithClientTranAndWithRuntimeException", 2, new RuntimeException("Dummy
exception"));
+      } catch (RuntimeException e) {
+          e.printStackTrace();
+      }
+      
+      try {
+          tran.commit();
+          fail("RollbackException not thrown");
+      } catch (RollbackException e) {
+          e.printStackTrace();
+      }
+      
+      finalRows = bean.countRows();
+      assertTrue("Initial rows: " + initialRows + ", Final rows: " + finalRows, finalRows
- initialRows == 0);
+      
+      //Test without client transaction - an exception is thrown because a transaction is
mandatory
+      try {
+          bean.insertRow(prefix + "testWithoutClientTran", 1);
+          fail("IllegalStateException not thrown");
+      } catch (IllegalStateException e) {
+          e.printStackTrace();
+      }
+  }
+}

Modified: aries/trunk/transaction/transaction-testbundle/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-testbundle/pom.xml?rev=1689632&r1=1689631&r2=1689632&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-testbundle/pom.xml (original)
+++ aries/trunk/transaction/transaction-testbundle/pom.xml Tue Jul  7 11:52:28 2015
@@ -58,6 +58,12 @@
             <scope>provided</scope>
             <version>1.0.3-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>javax.transaction</groupId>
+            <artifactId>javax.transaction-api</artifactId>
+            <version>1.2</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 
 </project>

Added: aries/trunk/transaction/transaction-testbundle/src/main/java/org/apache/aries/transaction/test/impl/JtaAnnotatedTestBeanImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-testbundle/src/main/java/org/apache/aries/transaction/test/impl/JtaAnnotatedTestBeanImpl.java?rev=1689632&view=auto
==============================================================================
--- aries/trunk/transaction/transaction-testbundle/src/main/java/org/apache/aries/transaction/test/impl/JtaAnnotatedTestBeanImpl.java
(added)
+++ aries/trunk/transaction/transaction-testbundle/src/main/java/org/apache/aries/transaction/test/impl/JtaAnnotatedTestBeanImpl.java
Tue Jul  7 11:52:28 2015
@@ -0,0 +1,34 @@
+package org.apache.aries.transaction.test.impl;
+
+import javax.transaction.Transactional;
+import java.sql.SQLException;
+
+/**
+ * Created by Maxim Becker on 31.05.15.
+ */
+public class JtaAnnotatedTestBeanImpl extends TestBeanImpl {
+
+    @Override
+    @Transactional(Transactional.TxType.MANDATORY)
+    public void insertRow(String name, int value) throws SQLException {
+        super.insertRow(name, value);
+    }
+
+    @Override
+    @Transactional(Transactional.TxType.MANDATORY)
+    public void insertRow(String name, int value, Exception e) throws SQLException {
+        super.insertRow(name, value, e);
+    }
+
+    @Override
+    @Transactional(Transactional.TxType.MANDATORY)
+    public void insertRow(String name, int value, boolean delegate) throws SQLException {
+        super.insertRow(name, value, delegate);
+    }
+
+    @Override
+    @Transactional(Transactional.TxType.NOT_SUPPORTED)
+    public int countRows() throws SQLException {
+        return super.countRows();
+    }
+}

Modified: aries/trunk/transaction/transaction-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml
URL: http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml?rev=1689632&r1=1689631&r2=1689632&view=diff
==============================================================================
--- aries/trunk/transaction/transaction-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml
(original)
+++ aries/trunk/transaction/transaction-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml
Tue Jul  7 11:52:28 2015
@@ -125,24 +125,37 @@
        <property name="password" value="password"/>
    </bean>
 
-   <bean id="mandatoryAnnotatedBeanImpl" class="org.apache.aries.transaction.test.impl.AnnotatedTestBeanImpl"
init-method="initialize">
-       <property name="enlistingDataSource" ref="enlistingDataSource"/>
-       <property name="dataSource" ref="dataSource"/>
-       <property name="user" value="user"/>
-       <property name="password" value="password"/>
-   </bean>
-
-   <service id="mandatoryAnnotatedBean" ref="mandatoryAnnotatedBeanImpl" interface="org.apache.aries.transaction.test.TestBean">
-       <service-properties>
-           <entry key="tranAttribute" value="MandatoryAnnotated"/>
-       </service-properties>
-   </service>
-
    <service id="invalidBean" ref="invalidBeanImpl" interface="org.apache.aries.transaction.test.TestBean">
        <service-properties>
            <entry key="tranAttribute" value="Invalid"/>
        </service-properties>
    </service>
+
+    <bean id="mandatoryAnnotatedBeanImpl" class="org.apache.aries.transaction.test.impl.AnnotatedTestBeanImpl"
init-method="initialize">
+        <property name="enlistingDataSource" ref="enlistingDataSource"/>
+        <property name="dataSource" ref="dataSource"/>
+        <property name="user" value="user"/>
+        <property name="password" value="password"/>
+    </bean>
+
+    <service id="mandatoryAnnotatedBean" ref="mandatoryAnnotatedBeanImpl" interface="org.apache.aries.transaction.test.TestBean">
+        <service-properties>
+            <entry key="tranAttribute" value="MandatoryAnnotated"/>
+        </service-properties>
+    </service>
+
+    <bean id="mandatoryJtaAnnotatedBeanImpl" class="org.apache.aries.transaction.test.impl.JtaAnnotatedTestBeanImpl"
init-method="initialize">
+        <property name="enlistingDataSource" ref="enlistingDataSource"/>
+        <property name="dataSource" ref="dataSource"/>
+        <property name="user" value="user"/>
+        <property name="password" value="password"/>
+    </bean>
+
+    <service id="mandatoryJtaAnnotatedBean" ref="mandatoryJtaAnnotatedBeanImpl" interface="org.apache.aries.transaction.test.TestBean">
+        <service-properties>
+            <entry key="tranAttribute" value="MandatoryJtaAnnotated"/>
+        </service-properties>
+    </service>
    
    <reference id="enlistingDataSource" interface="javax.sql.DataSource" filter="(osgi.jndi.service.name=jdbc/xads)"/>
    



Mime
View raw message