From e5b7380c874745c989d1816b8f552504f038e1bc Mon Sep 17 00:00:00 2001 From: lresende Date: Thu, 26 Sep 2013 20:33:20 +0000 Subject: 2.0 branch for possible maintenance release git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1526672 13f79535-47bb-0310-9956-ffa450edef68 --- .../runtime/TransactionManagerHelper.java | 252 +++++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 sca-java-2.x/branches/2.0/modules/policy-transaction-runtime/src/main/java/org/apache/tuscany/sca/policy/transaction/runtime/TransactionManagerHelper.java (limited to 'sca-java-2.x/branches/2.0/modules/policy-transaction-runtime/src/main/java/org/apache/tuscany/sca/policy/transaction/runtime/TransactionManagerHelper.java') diff --git a/sca-java-2.x/branches/2.0/modules/policy-transaction-runtime/src/main/java/org/apache/tuscany/sca/policy/transaction/runtime/TransactionManagerHelper.java b/sca-java-2.x/branches/2.0/modules/policy-transaction-runtime/src/main/java/org/apache/tuscany/sca/policy/transaction/runtime/TransactionManagerHelper.java new file mode 100644 index 0000000000..6d67e51bd7 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/policy-transaction-runtime/src/main/java/org/apache/tuscany/sca/policy/transaction/runtime/TransactionManagerHelper.java @@ -0,0 +1,252 @@ +/* + * 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.tuscany.sca.policy.transaction.runtime; + +import java.lang.reflect.InvocationTargetException; + +import javax.transaction.HeuristicMixedException; +import javax.transaction.HeuristicRollbackException; +import javax.transaction.InvalidTransactionException; +import javax.transaction.NotSupportedException; +import javax.transaction.RollbackException; +import javax.transaction.Status; +import javax.transaction.SystemException; +import javax.transaction.Transaction; +import javax.transaction.TransactionManager; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; + +/** + * @version $Rev$ $Date$ + */ +public class TransactionManagerHelper { + // private static final Logger logger = Logger.getLogger(TransactionManagerHelper.class.getName()); + + private TransactionManager tm; + + public TransactionManagerHelper(ExtensionPointRegistry registry) { + super(); + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + this.tm = utilities.getUtility(TransactionManager.class); + } + + public TransactionManagerHelper(TransactionManager tm) { + super(); + this.tm = tm; + } + + public Transaction managedGlobalTransactionPreInvoke() throws SystemException, NotSupportedException { + int status = tm.getStatus(); + if (status == Status.STATUS_COMMITTED || status == Status.STATUS_NO_TRANSACTION + || status == Status.STATUS_ROLLEDBACK) { + tm.begin(); + return tm.getTransaction(); + } + return null; + } + + public void managedGlobalTransactionPostInvoke(Transaction created, boolean rollback) + throws InvalidTransactionException, IllegalStateException, SystemException, SecurityException, + HeuristicMixedException, HeuristicRollbackException, RollbackException { + if (created != null) { + int status = created.getStatus(); + if (status == Status.STATUS_MARKED_ROLLBACK) { + created.rollback(); + } else if (status == Status.STATUS_ACTIVE) { + if (rollback) { + created.rollback(); + } else { + created.commit(); + } + } + } + } + + public Transaction suspendsTransactionPreInvoke() throws SystemException { + if (tm.getTransaction() != null) { + return tm.suspend(); + } else { + return null; + } + } + + public void suspendsTransactionPostInvoke(Transaction suspended) throws InvalidTransactionException, + IllegalStateException, SystemException { + if (suspended != null) { + tm.resume(suspended); + } + } + + public TransactionManager getTransactionManager() { + return tm; + } + + public void setTransactionManager(TransactionManager tm) { + this.tm = tm; + } + + public void validateOneway(TransactionIntent onewayIntent, TransactionIntent implIntent) + throws IncompatibleIntentException { + if (onewayIntent == TransactionIntent.transactedOneWay) { + if (implIntent != TransactionIntent.managedTransactionGlobal) { + throw new IncompatibleIntentException(onewayIntent + "<-X->" + implIntent); + } + } + } + + public void validateInbound(TransactionIntent serviceIntent, TransactionIntent implIntent) + throws IncompatibleIntentException { + if (serviceIntent == TransactionIntent.propagatesTransacton) { + if (implIntent != TransactionIntent.managedTransactionGlobal) { + throw new IncompatibleIntentException(serviceIntent + "<-X->" + implIntent); + } + } + } + + public void validateOutbound(TransactionIntent referenceIntent, TransactionIntent implIntent) + throws IncompatibleIntentException { + if (referenceIntent == TransactionIntent.propagatesTransacton) { + if (implIntent != TransactionIntent.managedTransactionGlobal) { + throw new IncompatibleIntentException(referenceIntent + "<-X->" + implIntent); + } + } + } + + public T handlesOutbound(TransactionIntent referenceIntent, + TransactionIntent implIntent, + TransactionalAction action) throws Exception { + + if (implIntent == null) { + implIntent = TransactionIntent.noManagedTransaction; + } + + if (referenceIntent == TransactionIntent.propagatesTransacton) { + if (implIntent != TransactionIntent.managedTransactionGlobal) { + throw new IncompatibleIntentException(referenceIntent + "<-X->" + implIntent); + } else { + // propagates the current TX + return run(action); + } + } else if (referenceIntent == TransactionIntent.suspendsTransaction) { + Transaction tx = suspendsTransactionPreInvoke(); + try { + return run(action); + } finally { + suspendsTransactionPostInvoke(tx); + } + } else { + return run(action); + } + } + + private T run(TransactionalAction action) throws Exception { + // Make sure a global TX is in place + Transaction tx = managedGlobalTransactionPreInvoke(); + boolean rollback = false; + try { + return action.run(); + } catch (InvocationTargetException e) { + throw e; + } catch (Throwable e) { + rollback = true; + if (e instanceof Error) { + throw (Error)e; + } else if (e instanceof RuntimeException) { + throw (RuntimeException)e; + } else { + throw (Exception)e; + } + } finally { + managedGlobalTransactionPostInvoke(tx, rollback); + } + } + + public T handlesInbound(TransactionIntent serviceIntent, + TransactionIntent implIntent, + TransactionalAction action) throws Exception { + if (serviceIntent == null && implIntent == null) { + return run(action); + } + + if (implIntent == null) { + implIntent = TransactionIntent.noManagedTransaction; + } + + if (serviceIntent == TransactionIntent.propagatesTransacton) { + if (implIntent != TransactionIntent.managedTransactionGlobal) { + throw new IncompatibleIntentException(serviceIntent + "<-X->" + implIntent); + } else { + return run(action); + } + } else if (serviceIntent == TransactionIntent.suspendsTransaction) { + Transaction tx1 = suspendsTransactionPreInvoke(); + try { + if (implIntent == TransactionIntent.managedTransactionGlobal) { + return run(action); + } else { + return action.run(); + } + } finally { + suspendsTransactionPostInvoke(tx1); + } + } else { + if (implIntent == TransactionIntent.managedTransactionGlobal) { + // Start a new TX + return run(action); + } else { + return action.run(); + } + } + } + + public void handlesOneWay(TransactionIntent onewayIntent, + TransactionIntent implIntent, + TransactionalAction action) throws Exception { + if (implIntent == null) { + implIntent = TransactionIntent.noManagedTransaction; + } + + if (onewayIntent == null) { + // Assume transactedOneWay + run(action); + return; + } + + if (onewayIntent == TransactionIntent.transactedOneWay) { + if (implIntent != TransactionIntent.managedTransactionGlobal) { + throw new IncompatibleIntentException(onewayIntent + "<-X->" + implIntent); + } else { + run(action); + return; + } + } else { + // TransactionIntent.immediateOneWay + Transaction tx = suspendsTransactionPreInvoke(); + try { + run(action); + return; + } finally { + suspendsTransactionPostInvoke(tx); + } + } + } + +} -- cgit v1.2.3