You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by ti...@apache.org on 2016/02/25 14:36:16 UTC

svn commit: r1732301 - in /aries/trunk/tx-control/tx-control-api: ./ src/main/java/org/osgi/service/transaction/control/ src/main/java/org/osgi/service/transaction/control/recovery/

Author: timothyjward
Date: Thu Feb 25 13:36:15 2016
New Revision: 1732301

URL: http://svn.apache.org/viewvc?rev=1732301&view=rev
Log:
[tx-control] Update to the latest API from the RFC

Added:
    aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/ScopedWorkException.java
    aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/
    aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/TransactionRecovery.java
    aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/package-info.java
    aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/packageinfo
Modified:
    aries/trunk/tx-control/tx-control-api/pom.xml
    aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionRolledBackException.java
    aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionStarter.java

Modified: aries/trunk/tx-control/tx-control-api/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-api/pom.xml?rev=1732301&r1=1732300&r2=1732301&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-api/pom.xml (original)
+++ aries/trunk/tx-control/tx-control-api/pom.xml Thu Feb 25 13:36:15 2016
@@ -35,13 +35,15 @@
 		<aries.osgi.export.pkg>
 			org.osgi.service.transaction.control,
 			org.osgi.service.transaction.control.jdbc,
-			org.osgi.service.transaction.control.jpa
+			org.osgi.service.transaction.control.jpa,
+			org.osgi.service.transaction.control.recovery
 		</aries.osgi.export.pkg>
 		<aries.osgi.private.pkg />
 		<aries.osgi.import.pkg>
 			org.osgi.service.transaction.control,
 			org.osgi.service.transaction.control.jdbc,
 			org.osgi.service.transaction.control.jpa,
+			org.osgi.service.transaction.control.recovery,
 			*
 		</aries.osgi.import.pkg>
 		<lastReleaseVersion>0.0.1-SNAPSHOT</lastReleaseVersion>

Added: aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/ScopedWorkException.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/ScopedWorkException.java?rev=1732301&view=auto
==============================================================================
--- aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/ScopedWorkException.java (added)
+++ aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/ScopedWorkException.java Thu Feb 25 13:36:15 2016
@@ -0,0 +1,203 @@
+/*
+ * Copyright (c) OSGi Alliance (2016). All Rights Reserved.
+ * 
+ * Licensed 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.osgi.service.transaction.control;
+
+/**
+ * An Exception that is thrown when a piece of scoped work exits with an
+ * {@link Exception}
+ */
+public class ScopedWorkException extends RuntimeException {
+
+	/**
+	 */
+	private static final long			serialVersionUID	= 4160254161503114842L;
+
+	private final TransactionContext context;
+
+	/**
+	 * Creates a new TransactionException with the supplied message and cause
+	 * 
+	 * @param message
+	 * @param cause
+	 * @param context 
+	 */
+	public ScopedWorkException(String message, Throwable cause, TransactionContext context) {
+		super(message, cause);
+		this.context = context;
+	}
+
+	/**
+	 * @return The ongoing transaction context if the scope is still active
+	 */
+	public TransactionContext ongoingContext() {
+		return context;
+	}
+
+	/**
+	 * @return The cause of this Exception as a {@link RuntimeException} if it
+	 *         is one, or this otherwise
+	 */
+	public RuntimeException asRuntimeException() {
+		return (RuntimeException) getCause();
+	}
+
+	/**
+	 * Throws the cause of this Exception as a RuntimeException the supplied
+	 * Exception type.
+	 * <p>
+	 * Usage is of the form:
+	 * 
+	 * <pre>
+	 * public void doStuff() throws IOException {
+	 *     try {
+	 *         ...
+	 *     } catch (ScopedWorkException swe) {
+	 *         throw swe.as(IOException.class);
+	 *     }
+	 * }
+	 * </pre>
+	 * 
+	 * @param throwable
+	 * @return This method will always throw an exception
+	 * @throws T
+	 */
+	public <T extends Throwable> T as(Class<T> throwable) throws T {
+		Throwable t = getCause();
+
+		if (t instanceof RuntimeException) {
+			throw (RuntimeException) t;
+		}
+
+		possibleThrow(throwable, t);
+
+		throw new IllegalArgumentException(
+				"The cause of this Exception is not an instance of "
+						+ throwable.getName(),
+				this);
+	}
+
+	/**
+	 * Throws the cause of this Exception as a RuntimeException or one of the
+	 * supplied Exception types.
+	 * <p>
+	 * Usage is of the form:
+	 * 
+	 * <pre>
+	 * public void doStuff() throws IOException, ClassNotFoundException {
+	 *     try {
+	 *         ...
+	 *     } catch (ScopedWorkException swe) {
+	 *         throw swe.asOneOf(IOException.class, ClassNotFoundException.class);
+	 *     }
+	 * }
+	 * </pre>
+	 * 
+	 * @param a
+	 * @param b
+	 * @return This method will always throw an exception
+	 * @throws A
+	 * @throws B
+	 */
+	public <A extends Throwable, B extends Throwable> RuntimeException asOneOf(
+			Class<A> a, Class<B> b) throws A, B {
+		Throwable t = getCause();
+
+		if (t instanceof RuntimeException) {
+			throw (RuntimeException) t;
+		}
+
+		possibleThrow(a, t);
+		possibleThrow(b, t);
+
+		throw new IllegalArgumentException(
+				"The cause of this Exception is not an instance of "
+						+ String.join(", ", a.getName(), b.getName()),
+				this);
+	}
+
+	/**
+	 * Throws the cause of this Exception as a RuntimeException or one of the
+	 * supplied Exception types.
+	 * 
+	 * @see #asOneOf(Class, Class)
+	 * @param a
+	 * @param b
+	 * @param c
+	 * @return This method will always throw an exception
+	 * @throws A
+	 * @throws B
+	 */
+	public <A extends Throwable, B extends Throwable, C extends Throwable> RuntimeException asOneOf(
+			Class<A> a, Class<B> b, Class<C> c) throws A, B, C {
+		Throwable t = getCause();
+
+		if (t instanceof RuntimeException) {
+			throw (RuntimeException) t;
+		}
+
+		possibleThrow(a, t);
+		possibleThrow(b, t);
+		possibleThrow(c, t);
+
+		throw new IllegalArgumentException(
+				"The cause of this Exception is not an instance of " + String
+						.join(", ", a.getName(), b.getName(), c.getName()),
+				this);
+	}
+
+	/**
+	 * Throws the cause of this Exception as a RuntimeException or one of the
+	 * supplied Exception types.
+	 * 
+	 * @see #asOneOf(Class, Class)
+	 * @param a
+	 * @param b
+	 * @param c
+	 * @param d
+	 * @return This method will always throw an exception
+	 * @throws A
+	 * @throws B
+	 * @throws C
+	 * @throws D
+	 */
+	public <A extends Throwable, B extends Throwable, C extends Throwable, D extends Throwable> RuntimeException asOneOf(
+			Class<A> a, Class<B> b, Class<C> c, Class<D> d) throws A, B, C, D {
+		Throwable t = getCause();
+
+		if (t instanceof RuntimeException) {
+			throw (RuntimeException) t;
+		}
+
+		possibleThrow(a, t);
+		possibleThrow(b, t);
+		possibleThrow(c, t);
+		possibleThrow(d, t);
+
+		throw new IllegalArgumentException(
+				"The cause of this Exception is not an instance of " + String
+						.join(", ", a.getName(), b.getName(), c.getName()),
+				this);
+	}
+
+	@SuppressWarnings("unchecked")
+	private <X extends Throwable> void possibleThrow(Class<X> x, Throwable t)
+			throws X {
+		if (x.isInstance(t)) {
+			throw (X) t;
+		}
+	}
+
+}

Modified: aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionRolledBackException.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionRolledBackException.java?rev=1732301&r1=1732300&r2=1732301&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionRolledBackException.java (original)
+++ aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionRolledBackException.java Thu Feb 25 13:36:15 2016
@@ -16,7 +16,8 @@
 package org.osgi.service.transaction.control;
 
 /**
- * An Exception indicating that the active transaction was rolled back
+ * An Exception indicating that the active transaction was unexpectedly rolled
+ * back
  */
 public class TransactionRolledBackException extends TransactionException {
 

Modified: aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionStarter.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionStarter.java?rev=1732301&r1=1732300&r2=1732301&view=diff
==============================================================================
--- aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionStarter.java (original)
+++ aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/TransactionStarter.java Thu Feb 25 13:36:15 2016
@@ -32,10 +32,13 @@ public interface TransactionStarter {
 	 * @throws TransactionException if there is an error starting or completing
 	 *             the transaction
 	 * @throws TransactionRolledBackException if the transaction rolled back due
-	 *             to a failure
+	 *             to a failure in one of the resources or an internal error in
+	 *             the TransactionControl service
+	 * @throws ScopedWorkException if the supplied work throws an
+	 *             {@link Exception}
 	 */
-	<T> T required(Callable<T> work)
-			throws TransactionException, TransactionRolledBackException;
+	<T> T required(Callable<T> work) throws TransactionException,
+			TransactionRolledBackException, ScopedWorkException;
 
 	/**
 	 * A new transaction is required to run the supplied piece of work. If an
@@ -50,9 +53,11 @@ public interface TransactionStarter {
 	 *             the transaction
 	 * @throws TransactionRolledBackException if the transaction rolled back due
 	 *             to a failure
+	 * @throws ScopedWorkException if the supplied work throws an
+	 *             {@link Exception}
 	 */
-	<T> T requiresNew(Callable<T> work)
-			throws TransactionException, TransactionRolledBackException;
+	<T> T requiresNew(Callable<T> work) throws TransactionException,
+			TransactionRolledBackException, ScopedWorkException;
 
 	/**
 	 * The supplied piece of work must be run outside the context of a
@@ -69,8 +74,11 @@ public interface TransactionStarter {
 	 * @return The value returned by the work
 	 * @throws TransactionException if there is an error starting or completing
 	 *             the transaction
+	 * @throws ScopedWorkException if the supplied work throws an
+	 *             {@link Exception}
 	 */
-	<T> T notSupported(Callable<T> work) throws TransactionException;
+	<T> T notSupported(Callable<T> work)
+			throws TransactionException, ScopedWorkException;
 
 	/**
 	 * The supplied piece of work may run inside or outside the context of a
@@ -88,8 +96,11 @@ public interface TransactionStarter {
 	 * @return The value returned by the work
 	 * @throws TransactionException if there is an error starting or completing
 	 *             the transaction
+	 * @throws ScopedWorkException if the supplied work throws an
+	 *             {@link Exception}
 	 */
-	<T> T supports(Callable<T> work) throws TransactionException;
+	<T> T supports(Callable<T> work)
+			throws TransactionException, ScopedWorkException;
 
 
 }

Added: aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/TransactionRecovery.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/TransactionRecovery.java?rev=1732301&view=auto
==============================================================================
--- aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/TransactionRecovery.java (added)
+++ aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/TransactionRecovery.java Thu Feb 25 13:36:15 2016
@@ -0,0 +1,21 @@
+package org.osgi.service.transaction.control.recovery;
+
+import javax.transaction.xa.XAResource;
+
+/**
+ * This service interface is published by Transaction control services that are
+ * able to support recovery. Any recoverable resources should register
+ * themselves with all available recovery services as they are created.
+ */
+public interface TransactionRecovery {
+
+	/**
+	 * Allow the {@link TransactionRecovery} service to attempt to recover any
+	 * incomplete XA transactions. Any recovery failures that occur must be
+	 * logged and not thrown to the caller of this service.
+	 * 
+	 * @param resource
+	 */
+	public void recover(XAResource resource);
+
+}

Added: aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/package-info.java
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/package-info.java?rev=1732301&view=auto
==============================================================================
--- aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/package-info.java (added)
+++ aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/package-info.java Thu Feb 25 13:36:15 2016
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) OSGi Alliance (2016). All Rights Reserved.
+ * 
+ * Licensed 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.
+ */
+
+/**
+ * Transaction Control Service Recovery Package Version 1.0.
+ * <p>
+ * Bundles wishing to use this package must list the package in the
+ * Import-Package header of the bundle's manifest. This package has two types of
+ * users: the consumers that use the API in this package and the providers that
+ * implement the API in this package.
+ * <p>
+ * Example import for consumers using the API in this package:
+ * <p>
+ * {@code  Import-Package: org.osgi.service.transaction.control.recovery; version="[1.0,2.0)"}
+ * <p>
+ * Example import for providers implementing the API in this package:
+ * <p>
+ * {@code  Import-Package: org.osgi.service.transaction.control.recovery; version="[1.0,1.1)"}
+ * 
+ * @version 1.0
+ * @author $Id:
+ */
+
+@org.osgi.annotation.versioning.Version("1.0.0")
+package org.osgi.service.transaction.control.recovery;
+

Added: aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/packageinfo
URL: http://svn.apache.org/viewvc/aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/packageinfo?rev=1732301&view=auto
==============================================================================
--- aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/packageinfo (added)
+++ aries/trunk/tx-control/tx-control-api/src/main/java/org/osgi/service/transaction/control/recovery/packageinfo Thu Feb 25 13:36:15 2016
@@ -0,0 +1 @@
+version 1.0