Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » stateless » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.openejb.core.stateless;
   18   
   19   import org.apache.openejb.ApplicationException;
   20   import org.apache.openejb.ContainerType;
   21   import org.apache.openejb.SystemException;
   22   import org.apache.openejb.core.transaction.TransactionContainer;
   23   import org.apache.openejb.core.transaction.TransactionContext;
   24   import org.apache.openejb.core.transaction.TransactionPolicy;
   25   
   26   import javax.transaction.Status;
   27   import java.rmi.RemoteException;
   28   
   29   public class StatelessBeanManagedTxPolicy extends TransactionPolicy {
   30   
   31       public StatelessBeanManagedTxPolicy(TransactionContainer container) {
   32           super(Type.BeanManaged, container);
   33           if (container.getContainerType() != ContainerType.STATELESS) {
   34               throw new IllegalArgumentException();
   35           }
   36       }
   37   
   38       public String policyToString() {
   39           return "TX_BeanManaged: ";
   40       }
   41   
   42       public void beforeInvoke(Object instance, TransactionContext context) throws SystemException, ApplicationException {
   43           context.callContext.set(Type.class, getPolicyType());
   44   
   45           context.clientTx = suspendTransaction(context);
   46       }
   47   
   48       public void afterInvoke(Object instance, TransactionContext context) throws ApplicationException, SystemException {
   49           try {
   50               /*
   51               * The Container must detect the case in which a transaction was started, but
   52               * not completed, in the business method, and handle it as follows:
   53               */
   54               context.currentTx = context.getTransactionManager().getTransaction();
   55   
   56               if (context.currentTx == null) return;
   57   
   58               if (context.currentTx.getStatus() != Status.STATUS_ROLLEDBACK && context.currentTx.getStatus() != Status.STATUS_COMMITTED) {
   59                   String message = "The stateless session bean started a transaction but did not complete it.";
   60   
   61                   /* [1] Log this as an application error ********/
   62                   logger.error(message);
   63   
   64                   /* [2] Roll back the started transaction *******/
   65                   try {
   66                       rollbackTransaction(context, context.currentTx);
   67                   } catch (Throwable t) {
   68   
   69                   }
   70   
   71                   /* [3] Throw the RemoteException to the client */
   72                   throwAppExceptionToServer(new RemoteException(message));
   73               }
   74   
   75           } catch (javax.transaction.SystemException e) {
   76               throw new SystemException(e);
   77           } finally {
   78               resumeTransaction(context, context.clientTx);
   79           }
   80       }
   81   
   82       public void handleApplicationException(Throwable appException, boolean rollback, TransactionContext context) throws ApplicationException, SystemException {
   83           if (rollback && context.currentTx != null) markTxRollbackOnly(context.currentTx);
   84   
   85           throw new ApplicationException(appException);
   86       }
   87   
   88       public void handleSystemException(Throwable sysException, Object instance, TransactionContext context) throws ApplicationException, SystemException {
   89           try {
   90               context.currentTx = context.getTransactionManager().getTransaction();
   91           } catch (javax.transaction.SystemException e) {
   92               context.currentTx = null;
   93           }
   94   
   95           logSystemException(sysException, context);
   96   
   97           if (context.currentTx != null) markTxRollbackOnly(context.currentTx);
   98   
   99           discardBeanInstance(instance, context.callContext);
  100   
  101           throwExceptionToServer(sysException);
  102   
  103       }
  104   
  105   }
  106   

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