Save This Page
Home » openejb-3.1.2-src » org.apache » openejb » core » stateful » [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.stateful;
   18   
   19   import org.apache.openejb.ApplicationException;
   20   import org.apache.openejb.InvalidateReferenceException;
   21   import org.apache.openejb.ContainerType;
   22   import org.apache.openejb.SystemException;
   23   import org.apache.openejb.core.transaction.TransactionContext;
   24   import org.apache.openejb.core.transaction.TransactionPolicy;
   25   import org.apache.openejb.core.Operation;
   26   
   27   import javax.ejb.SessionSynchronization;
   28   
   29   public class SessionSynchronizationTxPolicy extends TransactionPolicy {
   30   
   31       protected TransactionPolicy policy;
   32   
   33       public SessionSynchronizationTxPolicy(TransactionPolicy policy) {
   34           super(policy.getPolicyType(), policy.getContainer());
   35           this.policy = policy;
   36           if (container.getContainerType() != ContainerType.STATEFUL ||
   37                   getPolicyType() == TransactionPolicy.Type.Never ||
   38                   getPolicyType() == TransactionPolicy.Type.NotSupported) {
   39               throw new IllegalArgumentException();
   40           }
   41   
   42       }
   43   
   44       public void beforeInvoke(Object instance, TransactionContext context) throws SystemException, ApplicationException {
   45           policy.beforeInvoke(instance, context);
   46   
   47           if (context.currentTx == null) return;
   48   
   49           if (context.callContext.getCurrentOperation() == Operation.CREATE) return;
   50           
   51           try {
   52               StatefulInstanceManager.Instance instance2 = (StatefulInstanceManager.Instance) instance;
   53               SessionSynchronizationCoordinator.registerSessionSynchronization(instance2, context);
   54           } catch (javax.transaction.RollbackException e) {
   55               logger.error("Cannot register the SessionSynchronization bean with the transaction, the transaction has been rolled back");
   56               handleSystemException(e, instance, context);
   57           } catch (javax.transaction.SystemException e) {
   58               logger.error("Cannot register the SessionSynchronization bean with the transaction, received an unknown system exception from the transaction manager: " + e.getMessage());
   59               handleSystemException(e, instance, context);
   60           } catch (Throwable e) {
   61               logger.error("Cannot register the SessionSynchronization bean with the transaction, received an unknown exception: " + e.getClass().getName() + " " + e.getMessage());
   62               handleSystemException(e, instance, context);
   63           }
   64       }
   65   
   66       public void afterInvoke(Object instance, TransactionContext context) throws ApplicationException, SystemException {
   67           policy.afterInvoke(instance, context);
   68       }
   69   
   70       public void handleApplicationException(Throwable appException, boolean rollback, TransactionContext context) throws ApplicationException, SystemException {
   71           policy.handleApplicationException(appException, rollback, context);
   72       }
   73   
   74       public void handleSystemException(Throwable sysException, Object instance, TransactionContext context) throws ApplicationException, SystemException {
   75           try {
   76               policy.handleSystemException(sysException, instance, context);
   77           } catch (ApplicationException e) {
   78               throw new InvalidateReferenceException(e.getRootCause());
   79           }
   80       }
   81   
   82   }
   83   

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