aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From timothyjw...@apache.org
Subject svn commit: r1750848 - in /aries/trunk/tx-control/tx-control-service-xa/src: main/java/org/apache/aries/tx/control/service/xa/impl/ test/java/org/apache/aries/tx/control/service/xa/impl/
Date Thu, 30 Jun 2016 17:54:55 GMT
Author: timothyjward
Date: Thu Jun 30 17:54:55 2016
New Revision: 1750848

URL: http://svn.apache.org/viewvc?rev=1750848&view=rev
Log:
[tx-control] Make local resource support configurable, and permit users to enforce a single
last participant

Added:
    aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/Config.java
    aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/LocalResourceSupport.java
Modified:
    aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/Activator.java
    aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextImpl.java
    aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlImpl.java
    aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextTest.java

Modified: aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/Activator.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/Activator.java?rev=1750848&r1=1750847&r2=1750848&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/Activator.java
(original)
+++ aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/Activator.java
Thu Jun 30 17:54:55 2016
@@ -31,32 +31,14 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.cm.ManagedService;
-import org.osgi.service.metatype.annotations.AttributeDefinition;
-import org.osgi.service.metatype.annotations.ObjectClassDefinition;
 import org.osgi.service.transaction.control.TransactionControl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class Activator implements BundleActivator {
 	
-	private static final String PID = "org.apache.aries.tx.control.service.xa";
+	static final String PID = "org.apache.aries.tx.control.service.xa";
 
-	/**
-	 * This will be more useful once the OSGi converter exists, for now it just
-	 * generates a metatype for this service.
-	 */
-	@ObjectClassDefinition(pid=PID, description="Apache Aries Transaction Control Service (XA)")
-	@interface Config {
-		@AttributeDefinition(name="Enable recovery", required=false, description="Enable recovery")
-		boolean recovery_enabled() default false;
-
-		@AttributeDefinition(name="Recovery Log storage folder", required=false, description="Transaction
Recovery Log directory")
-		boolean recovery_log_dir();
-		
-		@AttributeDefinition(name="Transaction Timeout", required=false, description="Transaction
Timeout in seconds")
-		int transaction_timeout() default 300;
-	}
-	
 	private static final Logger logger = LoggerFactory.getLogger(Activator.class);
 
 	private ServiceRegistration<ManagedService> msReg;

Added: aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/Config.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/Config.java?rev=1750848&view=auto
==============================================================================
--- aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/Config.java
(added)
+++ aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/Config.java
Thu Jun 30 17:54:55 2016
@@ -0,0 +1,25 @@
+package org.apache.aries.tx.control.service.xa.impl;
+
+import static org.apache.aries.tx.control.service.xa.impl.LocalResourceSupport.ENFORCE_SINGLE;
+
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+/**
+ * This will be more useful once the OSGi converter exists, for now it just
+ * generates a metatype for this service.
+ */
+@ObjectClassDefinition(pid=Activator.PID, description="Apache Aries Transaction Control Service
(XA)")
+@interface Config {
+	@AttributeDefinition(name="Enable recovery", required=false, description="Enable recovery")
+	boolean recovery_enabled() default false;
+
+	@AttributeDefinition(name="Recovery Log storage folder", required=false, description="Transaction
Recovery Log directory")
+	boolean recovery_log_dir();
+	
+	@AttributeDefinition(name="Transaction Timeout", required=false, description="Transaction
Timeout in seconds")
+	int transaction_timeout() default 300;
+	
+	@AttributeDefinition(name="Local Resources", required=false, description="Allow Local Resources
to participate in transactions")
+	LocalResourceSupport local_resources() default ENFORCE_SINGLE;
+}
\ No newline at end of file

Added: aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/LocalResourceSupport.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/LocalResourceSupport.java?rev=1750848&view=auto
==============================================================================
--- aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/LocalResourceSupport.java
(added)
+++ aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/LocalResourceSupport.java
Thu Jun 30 17:54:55 2016
@@ -0,0 +1,6 @@
+package org.apache.aries.tx.control.service.xa.impl;
+
+public enum LocalResourceSupport {
+
+	DISABLED, ENFORCE_SINGLE, ENABLED;
+}

Modified: aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextImpl.java?rev=1750848&r1=1750847&r2=1750848&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextImpl.java
(original)
+++ aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextImpl.java
Thu Jun 30 17:54:55 2016
@@ -22,6 +22,7 @@ import static java.util.Optional.ofNulla
 import static javax.transaction.xa.XAException.XA_HEURMIX;
 import static javax.transaction.xa.XAException.XA_RBOTHER;
 import static javax.transaction.xa.XAException.XA_RBPROTO;
+import static org.apache.aries.tx.control.service.xa.impl.LocalResourceSupport.DISABLED;
 import static org.osgi.service.transaction.control.TransactionStatus.ACTIVE;
 import static org.osgi.service.transaction.control.TransactionStatus.COMMITTED;
 import static org.osgi.service.transaction.control.TransactionStatus.COMMITTING;
@@ -68,10 +69,13 @@ public class TransactionContextImpl exte
 
 	private final boolean readOnly;
 
+	private LocalResourceSupport localResourceSupport;
+
 	public TransactionContextImpl(GeronimoTransactionManager transactionManager, 
-			boolean readOnly) {
+			boolean readOnly, LocalResourceSupport localResourceSupport) {
 		this.transactionManager = transactionManager;
 		this.readOnly = readOnly;
+		this.localResourceSupport = localResourceSupport;
 		Transaction tmp = null;
 		try {
 			tmp = transactionManager.suspend();
@@ -228,7 +232,22 @@ public class TransactionContextImpl exte
 		if (status.compareTo(MARKED_ROLLBACK) > 0) {
 			throw new IllegalStateException("The current transaction is in state " + status);
 		}
-		resources.add(resource);
+		
+		switch (localResourceSupport) {
+			case ENFORCE_SINGLE:
+				if(!resources.isEmpty()) {
+					throw new TransactionException(
+							"Only one local resource may be added. Adding multiple local resources increases the
risk of inconsistency on failure.");
+				}
+			case ENABLED:
+				resources.add(resource);
+				break;
+			case DISABLED:
+				throw new TransactionException(
+						"This Transaction Control Service does not support local resources");
+			default :
+				throw new IllegalArgumentException("Unknown local resources configuration option");
+		}
 	}
 
 	@Override
@@ -238,7 +257,7 @@ public class TransactionContextImpl exte
 
 	@Override
 	public boolean supportsLocal() {
-		return true;
+		return localResourceSupport != DISABLED;
 	}
 
 	@Override

Modified: aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlImpl.java?rev=1750848&r1=1750847&r2=1750848&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlImpl.java
(original)
+++ aries/trunk/tx-control/tx-control-service-xa/src/main/java/org/apache/aries/tx/control/service/xa/impl/TransactionControlImpl.java
Thu Jun 30 17:54:55 2016
@@ -20,6 +20,7 @@ package org.apache.aries.tx.control.serv
 
 import static org.apache.aries.tx.control.service.xa.impl.Activator.ChangeType.RECREATE;
 import static org.apache.aries.tx.control.service.xa.impl.Activator.ChangeType.SERVICE_PROPS;
+import static org.apache.aries.tx.control.service.xa.impl.LocalResourceSupport.ENFORCE_SINGLE;
 
 import java.io.File;
 import java.util.Dictionary;
@@ -43,11 +44,13 @@ public class TransactionControlImpl exte
 	private final XidFactory xidFactory;
 	private final HOWLLog log;
 	private final GeronimoTransactionManager transactionManager;
+	private final LocalResourceSupport localResourceSupport;
 
 	public TransactionControlImpl(BundleContext ctx, Map<String, Object> config) throws
Exception {
 		
 		try {
 			this.config = config;
+			this.localResourceSupport = getLocalResourceSupport();
 			xidFactory = new XidFactoryImpl();
 			log = getLog(ctx);
 			
@@ -55,7 +58,7 @@ public class TransactionControlImpl exte
 				log.doStart();
 			}
 			
-			transactionManager = new GeronimoTransactionManager(getTimeout(config),
+			transactionManager = new GeronimoTransactionManager(getTimeout(),
 					xidFactory, log);
 		} catch (Exception e) {
 			destroy();
@@ -63,6 +66,12 @@ public class TransactionControlImpl exte
 		}
 	}
 
+	private LocalResourceSupport getLocalResourceSupport() {
+		Object o = config.getOrDefault("local.resources", ENFORCE_SINGLE);
+		return o instanceof LocalResourceSupport ? (LocalResourceSupport) o : 
+			LocalResourceSupport.valueOf(o.toString());
+	}
+
 	private HOWLLog getLog(BundleContext ctx) throws Exception {
 		Object recovery = config.getOrDefault("recovery.enabled", false);
 		
@@ -107,8 +116,8 @@ public class TransactionControlImpl exte
 		return null;
 	}
 	
-	private int getTimeout(Map<String, Object> newConfig) {
-		Object o = newConfig.getOrDefault("transaction.timeout", 300);
+	private int getTimeout() {
+		Object o = config.getOrDefault("transaction.timeout", 300);
 		return o instanceof Integer ? (Integer) o : Integer.valueOf(o.toString());
 	}
 	
@@ -165,6 +174,7 @@ public class TransactionControlImpl exte
 		copy(raw, filtered, "transaction.timeout");
 		copy(raw, filtered, "recovery.enabled");
 		copy(raw, filtered, "recovery.log.dir");
+		copy(raw, filtered, "local.resources");
 		
 		return filtered;
 	}
@@ -177,7 +187,7 @@ public class TransactionControlImpl exte
 	
 	@Override
 	protected AbstractTransactionContextImpl startTransaction(boolean readOnly) {
-		return new TransactionContextImpl(transactionManager, readOnly);
+		return new TransactionContextImpl(transactionManager, readOnly, localResourceSupport);
 	}
 	
 }

Modified: aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextTest.java?rev=1750848&r1=1750847&r2=1750848&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextTest.java
(original)
+++ aries/trunk/tx-control/tx-control-service-xa/src/test/java/org/apache/aries/tx/control/service/xa/impl/TransactionContextTest.java
Thu Jun 30 17:54:55 2016
@@ -18,12 +18,16 @@
  */
 package org.apache.aries.tx.control.service.xa.impl;
 
+import static org.apache.aries.tx.control.service.xa.impl.LocalResourceSupport.DISABLED;
+import static org.apache.aries.tx.control.service.xa.impl.LocalResourceSupport.ENABLED;
+import static org.apache.aries.tx.control.service.xa.impl.LocalResourceSupport.ENFORCE_SINGLE;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import static org.osgi.service.transaction.control.TransactionStatus.ACTIVE;
 import static org.osgi.service.transaction.control.TransactionStatus.COMMITTED;
 import static org.osgi.service.transaction.control.TransactionStatus.COMMITTING;
@@ -62,7 +66,7 @@ public class TransactionContextTest {
 	
 	@Before
 	public void setUp() throws XAException {
-		ctx = new TransactionContextImpl(new GeronimoTransactionManager(), false);
+		ctx = new TransactionContextImpl(new GeronimoTransactionManager(), false, ENFORCE_SINGLE);
 	}
 	
 	@Test
@@ -83,7 +87,7 @@ public class TransactionContextTest {
 
 	@Test
 	public void testisReadOnlyTrue() throws XAException {
-		ctx = new TransactionContextImpl(new GeronimoTransactionManager(), true);
+		ctx = new TransactionContextImpl(new GeronimoTransactionManager(), true, ENFORCE_SINGLE);
 		assertTrue(ctx.isReadOnly());
 	}
 
@@ -108,6 +112,18 @@ public class TransactionContextTest {
 	}
 
 	@Test
+	public void testLocalResourceSupportEnabled() throws XAException {
+		ctx = new TransactionContextImpl(new GeronimoTransactionManager(), true, ENABLED);
+		assertTrue(ctx.supportsLocal());
+	}
+
+	@Test
+	public void testLocalResourceSupportDisabled() throws XAException {
+		ctx = new TransactionContextImpl(new GeronimoTransactionManager(), true, DISABLED);
+		assertFalse(ctx.supportsLocal());
+	}
+
+	@Test
 	public void testXAResourceSupport() {
 		assertTrue(ctx.supportsXA());
 	}
@@ -320,7 +336,34 @@ public class TransactionContextTest {
 	}
 
 	@Test
-	public void testLocalResourcesFirstFailsSoRollback() throws Exception {
+	public void testSecondLocalResourceCannotBeAdded() throws Exception {
+		
+		ctx.registerLocalResource(localResource);
+		
+		LocalResource localResource2 = Mockito.mock(LocalResource.class);
+		try {
+			ctx.registerLocalResource(localResource2);
+			fail("A second local resource should trigger a failure");
+		} catch (TransactionException te) {
+			
+		}
+	}
+
+	@Test
+	public void testNoLocalResourceCanBeAddedWhenDisabled() throws Exception {
+		ctx = new TransactionContextImpl(new GeronimoTransactionManager(), true, DISABLED);
+		
+		try {
+			ctx.registerLocalResource(localResource);
+			fail("A local resource should trigger a failure");
+		} catch (TransactionException te) {
+			
+		}
+	}
+	
+	@Test
+	public void testMultipleLocalResourcesFirstFailsSoRollback() throws Exception {
+		ctx = new TransactionContextImpl(new GeronimoTransactionManager(), true, ENABLED);
 		
 		ctx.registerLocalResource(localResource);
 



Mime
View raw message