Save This Page
Home » openejb-3.1.2-src » org.apache.openejb.spring » [javadoc | source]
    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing, software
   13    *  distributed under the License is distributed on an "AS IS" BASIS,
   14    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    *  See the License for the specific language governing permissions and
   16    *  limitations under the License.
   17    */
   18   package org.apache.openejb.spring;
   19   
   20   import javax.transaction.RollbackException;
   21   import javax.transaction.HeuristicMixedException;
   22   import javax.transaction.HeuristicRollbackException;
   23   import javax.transaction.xa.XAResource;
   24   
   25   import org.apache.openejb.core.transaction.TransactionPolicy;
   26   import org.apache.openejb.core.transaction.TransactionType;
   27   import org.apache.openejb.core.transaction.TransactionPolicy.TransactionSynchronization.Status;
   28   import org.apache.openejb.ApplicationException;
   29   import org.apache.openejb.SystemException;
   30   import org.springframework.transaction.PlatformTransactionManager;
   31   import org.springframework.transaction.TransactionStatus;
   32   import org.springframework.transaction.TransactionException;
   33   import org.springframework.transaction.HeuristicCompletionException;
   34   import org.springframework.transaction.UnexpectedRollbackException;
   35   import org.springframework.transaction.support.DefaultTransactionStatus;
   36   import org.springframework.transaction.support.DefaultTransactionDefinition;
   37   import org.springframework.transaction.support.TransactionSynchronizationManager;
   38   import org.springframework.transaction.support.TransactionSynchronizationAdapter;
   39   
   40   public class SpringTransactionPolicy implements TransactionPolicy {
   41       protected final PlatformTransactionManager transactionManager;
   42       protected final TransactionType type;
   43       private final DefaultTransactionStatus transactionStatus;
   44   
   45       public SpringTransactionPolicy(PlatformTransactionManager transactionManager, TransactionType type) {
   46           this.transactionManager = transactionManager;
   47           this.type = type;
   48   
   49           DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
   50           switch (type) {
   51               case BeanManaged:
   52                   transactionDefinition.setPropagationBehavior(org.springframework.transaction.TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
   53                   break;
   54               case Mandatory:
   55                   transactionDefinition.setPropagationBehavior(org.springframework.transaction.TransactionDefinition.PROPAGATION_MANDATORY);
   56                   break;
   57               case Never:
   58                   transactionDefinition.setPropagationBehavior(org.springframework.transaction.TransactionDefinition.PROPAGATION_NEVER);
   59                   break;
   60               case NotSupported:
   61                   transactionDefinition.setPropagationBehavior(org.springframework.transaction.TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
   62                   break;
   63               case Required:
   64                   transactionDefinition.setPropagationBehavior(org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRED);
   65                   break;
   66               case RequiresNew:
   67                   transactionDefinition.setPropagationBehavior(org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRES_NEW);
   68                   break;
   69               case Supports:
   70                   transactionDefinition.setPropagationBehavior(org.springframework.transaction.TransactionDefinition.PROPAGATION_SUPPORTS);
   71                   break;
   72           }
   73   
   74           TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
   75           if (!(transactionManager instanceof DefaultTransactionStatus)) {
   76               throw new IllegalArgumentException("SpringTransactionPolicy only works with a PlatformTransactionManager that uses DefaultTransactionStatus");
   77           }
   78           this.transactionStatus = (DefaultTransactionStatus) transactionStatus;
   79       }
   80   
   81       public TransactionType getTransactionType() {
   82           return type;
   83       }
   84   
   85       public boolean isNewTransaction() {
   86           return getTransactionStatus().isNewTransaction();
   87       }
   88   
   89       public boolean isClientTransaction() {
   90           DefaultTransactionStatus status = getTransactionStatus();
   91           return status.hasTransaction() && !status.isNewSynchronization();
   92       }
   93   
   94       public boolean isTransactionActive() {
   95           return !getTransactionStatus().isCompleted() && getTransactionStatus().hasTransaction();
   96       }
   97   
   98       public boolean isRollbackOnly() {
   99           return getTransactionStatus().isRollbackOnly();
  100       }
  101   
  102       public void setRollbackOnly() {
  103           getTransactionStatus().setRollbackOnly();
  104       }
  105   
  106       public void commit() throws ApplicationException, SystemException {
  107           try {
  108               transactionManager.commit(getTransactionStatus());
  109           } catch (TransactionException e) {
  110               // check if exception is simply wrapping a supported JTA exception
  111               Throwable cause = e.getCause();
  112               if (cause instanceof RollbackException) {
  113                   throw new ApplicationException(cause);
  114               } else if (cause instanceof HeuristicMixedException) {
  115                   throw new ApplicationException(cause);
  116               } else if (cause instanceof HeuristicRollbackException) {
  117                   throw new ApplicationException(cause);
  118               } else if (cause instanceof IllegalStateException) {
  119                   throw new SystemException(cause);
  120               } else if (cause instanceof SecurityException) {
  121                   throw new SystemException(cause);
  122               } else if (cause instanceof javax.transaction.SystemException) {
  123                   throw new SystemException(cause);
  124               }
  125   
  126               // wrap with application or system exception based on type
  127               if (e instanceof HeuristicCompletionException) {
  128                   throw new ApplicationException(e);
  129               } else if (e instanceof UnexpectedRollbackException) {
  130                   throw new ApplicationException(e);
  131               }
  132               throw new SystemException(e);
  133           }
  134       }
  135   
  136       public Object getResource(Object key) {
  137           Object resource = TransactionSynchronizationManager.getResource(key);
  138           return resource;
  139       }
  140   
  141       public void putResource(Object key, Object value) {
  142           TransactionSynchronizationManager.bindResource(key, value);
  143       }
  144   
  145       public Object removeResource(Object key) {
  146           Object resource = TransactionSynchronizationManager.unbindResource(key);
  147           return resource;
  148       }
  149   
  150       public void registerSynchronization(final TransactionSynchronization synchronization) {
  151           TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
  152               public void beforeCompletion() {
  153                   synchronization.beforeCompletion();
  154               }
  155   
  156               public void afterCompletion(int status) {
  157                   Status s = null;
  158                   if (status == STATUS_COMMITTED) {
  159                       s = Status.COMMITTED;
  160                   } else if (status == STATUS_ROLLED_BACK) {
  161                       s = Status.ROLLEDBACK;
  162                   } else if (status == STATUS_UNKNOWN) {
  163                       s = Status.UNKNOWN;
  164                   }
  165                   synchronization.afterCompletion(s);
  166               }
  167           });
  168       }
  169   
  170       public void enlistResource(XAResource xaResource) throws SystemException {
  171           throw new SystemException(new UnsupportedOperationException("SpringTransactionPolicy does not support XAResource enlistment"));
  172       }
  173   
  174       protected DefaultTransactionStatus getTransactionStatus() {
  175           return transactionStatus;
  176       }
  177   }

Save This Page
Home » openejb-3.1.2-src » org.apache.openejb.spring » [javadoc | source]