[jboss-svn-commits] JBL Code SVN: r38113 - in labs/jbossesb/branches/JBESB_4_11_CP/product/rosetta: tests/src/org/jboss/internal/soa/esb/couriers/transport and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed May 30 13:42:17 EDT 2012


Author: tcunning
Date: 2012-05-30 13:42:16 -0400 (Wed, 30 May 2012)
New Revision: 38113

Added:
   labs/jbossesb/branches/JBESB_4_11_CP/product/rosetta/src/org/jboss/internal/soa/esb/couriers/transport/InVMResourceManager.java
   labs/jbossesb/branches/JBESB_4_11_CP/product/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/transport/InVMResourceManagerUnitTest.java
Log:
JBESB-3743
Commit added files from Kevin's patch to make sure we only use a single XAResource in a transaction..


Added: labs/jbossesb/branches/JBESB_4_11_CP/product/rosetta/src/org/jboss/internal/soa/esb/couriers/transport/InVMResourceManager.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_11_CP/product/rosetta/src/org/jboss/internal/soa/esb/couriers/transport/InVMResourceManager.java	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_11_CP/product/rosetta/src/org/jboss/internal/soa/esb/couriers/transport/InVMResourceManager.java	2012-05-30 17:42:16 UTC (rev 38113)
@@ -0,0 +1,193 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.internal.soa.esb.couriers.transport;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import org.jboss.internal.soa.esb.couriers.tx.InVMXAResource;
+import org.jboss.soa.esb.common.TransactionStrategy;
+import org.jboss.soa.esb.common.TransactionStrategyException;
+
+/**
+ * Provide support for the transactional InVM operations.
+ * 
+ * @author kevin
+ */
+public class InVMResourceManager
+{
+    /**
+     * Mapping of transactions to transactional resource entries.
+     */
+    private final Map<Object, InVMXAResource> transactionToXAResource = new HashMap<Object, InVMXAResource>() ;
+    /**
+     * The lock guarding access and modification to the structures.
+     */
+    private ReadWriteLock lock = new ReentrantReadWriteLock() ;
+
+    /**
+     * Factory singleton instance.
+     */
+    private static InVMResourceManager instance = new InVMResourceManager() ;
+
+    public InVMXAResource getXAResource()
+        throws InVMException
+    {
+        final TransactionStrategy txStrategy = TransactionStrategy.getTransactionStrategy(true) ;
+        final Object txHandle ;
+        final boolean isActive ;
+        try
+        {
+            txHandle = txStrategy.getTransaction() ;
+            isActive = txStrategy.isActive() ;
+        }
+        catch (final TransactionStrategyException tse)
+        {
+            throw new InVMException("Failed to obtain details about current transaction", tse) ;
+        }
+
+        if (!isActive)
+        {
+            throw new InVMException("Associated transaction is no longer active!") ;
+        }
+
+        acquireReadLock() ;
+        try
+        {
+            final InVMXAResource current = transactionToXAResource.get(txHandle) ;
+            if (current != null)
+            {
+                return current ;
+            }
+        }
+        finally
+        {
+            releaseReadLock() ;
+        }
+        
+        boolean enlist = false ;
+        final InVMXAResource result ;
+        
+        acquireWriteLock() ;
+        try
+        {
+            final InVMXAResource newResource = new InVMXAResource() ;
+            final InVMXAResource oldResource = transactionToXAResource.put(txHandle, newResource) ;
+            if (oldResource != null)
+            {
+                transactionToXAResource.put(txHandle,  oldResource) ;
+                result = oldResource ;
+            }
+            else
+            {
+                result = newResource ;
+                enlist = true ;
+            }
+        }
+        finally
+        {
+            releaseWriteLock() ;
+        }
+        
+        if (enlist)
+        {
+            try
+            {
+                txStrategy.enlistResource(result) ;
+            }
+            catch (final TransactionStrategyException tse)
+            {
+                throw new InVMException("Failed to enlist XAResource in current transaction", tse) ;
+            }
+        }
+        
+        return result ;
+    }
+
+    public void removeXAResource()
+        throws InVMException
+    {
+        final TransactionStrategy txStrategy = TransactionStrategy.getTransactionStrategy(true) ;
+        final Object txHandle ;
+        try
+        {
+            txHandle = txStrategy.getTransaction() ;
+        }
+        catch (final TransactionStrategyException tse)
+        {
+            throw new InVMException("Failed to obtain details about current transaction", tse) ;
+        }
+        
+        acquireWriteLock() ;
+        try
+        {
+            transactionToXAResource.remove(txHandle) ;
+        }
+        finally
+        {
+            releaseWriteLock() ;
+        }
+    }
+
+    /**
+     * Get the InVM Resource Manager.
+     * @return The InVM Resource Manager instance.
+     */
+    public static InVMResourceManager getInstance()
+    {
+        return instance;
+    }
+
+    /**
+     * Acquire a read lock for accessing the data.
+     */
+    private void acquireReadLock()
+    {
+        lock.readLock().lock() ;
+    }
+    
+    /**
+     * Release a read lock for accessing the data.
+     */
+    private void releaseReadLock()
+    {
+        lock.readLock().unlock() ;
+    }
+    
+    /**
+     * Acquire a write lock for accessing the data.
+     */
+    private void acquireWriteLock()
+    {
+        lock.writeLock().lock() ;
+    }
+    
+    /**
+     * Release a write lock for accessing the data.
+     */
+    private void releaseWriteLock()
+    {
+        lock.writeLock().unlock() ;
+    }
+}

Added: labs/jbossesb/branches/JBESB_4_11_CP/product/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/transport/InVMResourceManagerUnitTest.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_11_CP/product/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/transport/InVMResourceManagerUnitTest.java	                        (rev 0)
+++ labs/jbossesb/branches/JBESB_4_11_CP/product/rosetta/tests/src/org/jboss/internal/soa/esb/couriers/transport/InVMResourceManagerUnitTest.java	2012-05-30 17:42:16 UTC (rev 38113)
@@ -0,0 +1,360 @@
+package org.jboss.internal.soa.esb.couriers.transport;
+
+import java.net.URI;
+
+import javax.transaction.Synchronization;
+import javax.transaction.xa.XAException;
+import javax.transaction.xa.XAResource;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertNull;
+import static junit.framework.Assert.assertTrue;
+import junit.framework.JUnit4TestAdapter;
+
+import org.jboss.soa.esb.addressing.eprs.InVMEpr;
+import org.jboss.soa.esb.common.TransactionStrategy;
+import org.jboss.soa.esb.common.TransactionStrategyException;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * InVMResourceManager unit tests.
+ * 
+ * @author <a href="mailto:kevin.conner at jboss.com">Kevin Conner</a>
+ */
+public class InVMResourceManagerUnitTest
+{
+    private static final String TEST_CATEGORY1 = "test category1" ;
+    private static final String TEST_CATEGORY2 = "test category2" ;
+    private static final String TEST_NAME1 = "service name 1" ;
+    private static final String TEST_NAME2 = "service name 2" ;
+    
+    private InVMEpr epr1 ;
+    private InVMEpr epr2 ;
+    private InVMEpr tempEPR1 ;
+    private InVMEpr tempEPR2 ;
+    private TransactionStrategy transactionStrategy ;
+    private TestTransactionStrategy testTransactionStrategy ;
+
+    @Before
+    public void setup()
+        throws Exception
+    {
+        epr1 = new InVMEpr(URI.create(InVMEpr.INVM_PROTOCOL + "://" + InVMEpr.createEncodedServiceId(TEST_CATEGORY1, TEST_NAME1))) ;
+        epr2 = new InVMEpr(URI.create(InVMEpr.INVM_PROTOCOL + "://" + InVMEpr.createEncodedServiceId(TEST_CATEGORY2, TEST_NAME2))) ;
+        tempEPR1 = new InVMEpr(URI.create(InVMEpr.INVM_PROTOCOL + "://" + InVMEpr.createEncodedServiceId(TEST_CATEGORY1, TEST_NAME1))) ;
+        tempEPR1.setTemporaryEPR(true) ;
+        tempEPR2 = new InVMEpr(URI.create(InVMEpr.INVM_PROTOCOL + "://" + InVMEpr.createEncodedServiceId(TEST_CATEGORY2, TEST_NAME2))) ;
+        tempEPR2.setTemporaryEPR(true) ;
+        
+        final InVMTransport transport = InVMTransport.getInstance() ;
+        transport.registerEPR(TEST_CATEGORY1, TEST_NAME1, epr1) ;
+        transport.registerEPR(TEST_CATEGORY2, TEST_NAME2, epr2) ;
+        
+        transactionStrategy = TransactionStrategy.getTransactionStrategy(true) ;
+        testTransactionStrategy = new TestTransactionStrategy() ;
+        TransactionStrategy.setTransactionStrategy(testTransactionStrategy) ;
+    }
+
+    @After
+    public void teardown()
+        throws Exception
+    {
+        
+        final InVMTransport transport = InVMTransport.getInstance() ;
+        transport.unRegisterEPR(TEST_CATEGORY1, TEST_NAME1, epr1) ;
+        transport.unRegisterEPR(TEST_CATEGORY2, TEST_NAME2, epr2) ;
+
+        final InVMTemporaryTransport tempTransport = InVMTemporaryTransport.getInstance() ;
+        while(tempTransport.pickup(tempEPR1, 0) != null) ;
+        while(tempTransport.pickup(tempEPR2, 0) != null) ;
+        
+        TransactionStrategy.setTransactionStrategy(transactionStrategy) ;
+    }
+    
+    @Test
+    public void testInsertCommit()
+        throws Exception
+    {
+        final Message message1 = MessageFactory.getInstance().getMessage() ;
+        final Message message2 = MessageFactory.getInstance().getMessage() ;
+        
+        final InVMTransport transport = InVMTransport.getInstance() ;
+        
+        testTransactionStrategy.begin();
+        
+        transport.deliver(epr1, message1) ;
+        transport.deliver(epr2, message2) ;
+        
+        assertTrue("Enlisted", testTransactionStrategy.isResourceEnlisted()) ;
+        testTransactionStrategy.terminate() ;
+        
+        assertNotNull("Service 1 delivery", transport.pickup(epr1, 0)) ;
+        assertNotNull("Service 2 delivery", transport.pickup(epr2, 0)) ;
+    }
+
+    @Test
+    public void testInsertRollback()
+        throws Exception
+    {
+        final Message message1 = MessageFactory.getInstance().getMessage() ;
+        final Message message2 = MessageFactory.getInstance().getMessage() ;
+        
+        final InVMTransport transport = InVMTransport.getInstance() ;
+        
+        testTransactionStrategy.begin();
+        
+        transport.deliver(epr1, message1) ;
+        transport.deliver(epr2, message2) ;
+        
+        assertTrue("Enlisted", testTransactionStrategy.isResourceEnlisted()) ;
+        testTransactionStrategy.rollbackOnly() ;
+        testTransactionStrategy.terminate() ;
+        
+        assertNull("Service 1 delivery", transport.pickup(epr1, 0)) ;
+        assertNull("Service 2 delivery", transport.pickup(epr2, 0)) ;
+    }
+    
+    @Test
+    public void testRemoveCommit()
+        throws Exception
+    {
+        final Message message1 = MessageFactory.getInstance().getMessage() ;
+        final Message message2 = MessageFactory.getInstance().getMessage() ;
+        
+        final InVMTransport transport = InVMTransport.getInstance() ;
+        
+        transport.deliver(epr1, message1) ;
+        transport.deliver(epr2, message2) ;
+        
+        testTransactionStrategy.begin();
+        
+        assertNotNull("Service 1 during transaction pickup", transport.pickup(epr1, 0)) ;
+        assertNotNull("Service 2 during transaction pickup", transport.pickup(epr2, 0)) ;
+        
+        assertTrue("Enlisted", testTransactionStrategy.isResourceEnlisted()) ;
+        testTransactionStrategy.terminate() ;
+        
+        assertNull("Service 1 pickup", transport.pickup(epr1, 0)) ;
+        assertNull("Service 2 pickup", transport.pickup(epr2, 0)) ;
+    }
+
+    @Test
+    public void testRemoveRollback()
+        throws Exception
+    {
+        final Message message1 = MessageFactory.getInstance().getMessage() ;
+        final Message message2 = MessageFactory.getInstance().getMessage() ;
+        
+        final InVMTransport transport = InVMTransport.getInstance() ;
+        
+        transport.deliver(epr1, message1) ;
+        transport.deliver(epr2, message2) ;
+        
+        testTransactionStrategy.begin();
+        
+        assertNotNull("Service 1 during transaction pickup", transport.pickup(epr1, 0)) ;
+        assertNotNull("Service 2 during transaction pickup", transport.pickup(epr2, 0)) ;
+        
+        assertTrue("Enlisted", testTransactionStrategy.isResourceEnlisted()) ;
+        testTransactionStrategy.rollbackOnly() ;
+        testTransactionStrategy.terminate() ;
+        
+        assertNotNull("Service 1 pickup", transport.pickup(epr1, 0)) ;
+        assertNotNull("Service 2 pickup", transport.pickup(epr2, 0)) ;
+    }
+    
+    @Test
+    public void testTemporaryInsertCommit()
+        throws Exception
+    {
+        final Message message1 = MessageFactory.getInstance().getMessage() ;
+        final Message message2 = MessageFactory.getInstance().getMessage() ;
+        
+        final InVMTemporaryTransport transport = InVMTemporaryTransport.getInstance() ;
+        
+        testTransactionStrategy.begin();
+        
+        transport.deliver(tempEPR1, message1) ;
+        transport.deliver(tempEPR2, message2) ;
+        
+        assertTrue("Enlisted", testTransactionStrategy.isResourceEnlisted()) ;
+        testTransactionStrategy.terminate() ;
+        
+        assertNotNull("Service 1 delivery", transport.pickup(tempEPR1, 0)) ;
+        assertNotNull("Service 2 delivery", transport.pickup(tempEPR2, 0)) ;
+    }
+
+    @Test
+    public void testTemporaryInsertRollback()
+        throws Exception
+    {
+        final Message message1 = MessageFactory.getInstance().getMessage() ;
+        final Message message2 = MessageFactory.getInstance().getMessage() ;
+        
+        final InVMTemporaryTransport transport = InVMTemporaryTransport.getInstance() ;
+        
+        testTransactionStrategy.begin();
+        
+        transport.deliver(tempEPR1, message1) ;
+        transport.deliver(tempEPR2, message2) ;
+        
+        assertTrue("Enlisted", testTransactionStrategy.isResourceEnlisted()) ;
+        testTransactionStrategy.rollbackOnly() ;
+        testTransactionStrategy.terminate() ;
+        
+        assertNull("Service 1 delivery", transport.pickup(tempEPR1, 0)) ;
+        assertNull("Service 2 delivery", transport.pickup(tempEPR2, 0)) ;
+    }
+    
+    @Test
+    public void testTemporaryRemoveCommit()
+        throws Exception
+    {
+        final Message message1 = MessageFactory.getInstance().getMessage() ;
+        final Message message2 = MessageFactory.getInstance().getMessage() ;
+        
+        final InVMTemporaryTransport transport = InVMTemporaryTransport.getInstance() ;
+        
+        transport.deliver(tempEPR1, message1) ;
+        transport.deliver(tempEPR2, message2) ;
+        
+        testTransactionStrategy.begin();
+        
+        assertNotNull("Service 1 during transaction pickup", transport.pickup(tempEPR1, 0)) ;
+        assertNotNull("Service 2 during transaction pickup", transport.pickup(tempEPR2, 0)) ;
+        
+        assertTrue("Enlisted", testTransactionStrategy.isResourceEnlisted()) ;
+        testTransactionStrategy.terminate() ;
+        
+        assertNull("Service 1 pickup", transport.pickup(tempEPR1, 0)) ;
+        assertNull("Service 2 pickup", transport.pickup(tempEPR2, 0)) ;
+    }
+
+    @Test
+    public void testTemporaryRemoveRollback()
+        throws Exception
+    {
+        final Message message1 = MessageFactory.getInstance().getMessage() ;
+        final Message message2 = MessageFactory.getInstance().getMessage() ;
+        
+        final InVMTemporaryTransport transport = InVMTemporaryTransport.getInstance() ;
+        
+        transport.deliver(tempEPR1, message1) ;
+        transport.deliver(tempEPR2, message2) ;
+        
+        testTransactionStrategy.begin();
+        
+        assertNotNull("Service 1 during transaction pickup", transport.pickup(tempEPR1, 0)) ;
+        assertNotNull("Service 2 during transaction pickup", transport.pickup(tempEPR2, 0)) ;
+        
+        assertTrue("Enlisted", testTransactionStrategy.isResourceEnlisted()) ;
+        testTransactionStrategy.rollbackOnly() ;
+        testTransactionStrategy.terminate() ;
+        
+        assertNotNull("Service 1 pickup", transport.pickup(tempEPR1, 0)) ;
+        assertNotNull("Service 2 pickup", transport.pickup(tempEPR2, 0)) ;
+    }
+
+    public static junit.framework.Test suite()
+    {
+        return new JUnit4TestAdapter(InVMResourceManagerUnitTest.class);
+    }
+    
+    private static class TestTransactionStrategy extends TransactionStrategy
+    {
+        private XAResource resource ;
+        private boolean rollback ;
+        private boolean active ;
+        
+        @Override
+        public void begin() throws TransactionStrategyException
+        {
+            active = true ;
+        }
+
+        @Override
+        public void terminate() throws TransactionStrategyException
+        {
+            try
+            {
+                if (resource != null)
+                {
+                    if (rollback)
+                    {
+                        resource.rollback(null) ;
+                    }
+                    else
+                    {
+                        resource.commit(null, true) ;
+                    }
+                }
+            }
+            catch (final XAException xae)
+            {
+                throw new TransactionStrategyException("Unexpected exception during commit", xae) ;
+            }
+            finally
+            {
+                resource = null ;
+                rollback = false ;
+                active = false ;
+            }
+        }
+
+        @Override
+        public void rollbackOnly() throws TransactionStrategyException
+        {
+            rollback = true ;
+        }
+
+        @Override
+        public Object getTransaction() throws TransactionStrategyException
+        {
+            return (active ? this : null) ;
+        }
+
+        @Override
+        public Object suspend() throws TransactionStrategyException
+        {
+            return null ;
+        }
+
+        @Override
+        public boolean isActive() throws TransactionStrategyException
+        {
+            return active ;
+        }
+
+        @Override
+        public void resume(Object tx) throws TransactionStrategyException
+        {
+        }
+
+        @Override
+        public void registerSynchronization(Synchronization sync)
+                throws TransactionStrategyException
+        {
+        }
+
+        @Override
+        public void enlistResource(XAResource resource)
+                throws TransactionStrategyException
+        {
+            if (this.resource != null)
+            {
+                throw new TransactionStrategyException("resource enlisted multiple times") ;
+            }
+            this.resource = resource ;
+        }
+        
+        public boolean isResourceEnlisted()
+        {
+            return resource != null ;
+        }
+    }
+}



More information about the jboss-svn-commits mailing list