User: pra     
  Date: 00/12/06 05:00:58

  Added:       src/main/org/jboss/ejb/plugins
                        MessageDrivenInstanceInterceptor.java
                        MessageDrivenInstancePool.java
                        MessageDrivenTxInterceptorBMT.java
  Log:
  Added MessageDriven bean classes in ejb hierarchy. ContainerInvoker is changed to 
support MessageDriven Beans
  
  Revision  Changes    Path
  1.1                  
jboss/src/main/org/jboss/ejb/plugins/MessageDrivenInstanceInterceptor.java
  
  Index: MessageDrivenInstanceInterceptor.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.ejb.plugins;
  
  import java.rmi.RemoteException;
  
  import org.jboss.ejb.Container;
  import org.jboss.ejb.MessageDrivenContainer;
  import org.jboss.ejb.MethodInvocation;
  import org.jboss.logging.Logger;
  /**
   *   This container acquires the given instance. This must be used after
   *   the EnvironmentInterceptor, since acquiring instances requires a proper
   *   JNDI environment to be set
   * For MessageDriven Beans, we inherit the StatelessSession for now,
   * since message driven beans is much like them
   *
   *   @see <related>
   *   @author Peter Antman ([EMAIL PROTECTED])
   *   @author Rickard �berg ([EMAIL PROTECTED])
   *   @version $Revision: 1.1 $
   */
  public class MessageDrivenInstanceInterceptor
     extends StatelessSessionInstanceInterceptor
  {
      protected MessageDrivenContainer container;
  
      public void setContainer(Container container) 
     { 
        this.container = (MessageDrivenContainer)container; 
     }
  
      // Overriden here, since these bastards don't have homes
      public Object invokeHome(MethodInvocation mi)
        throws Exception
     {
         throw new Error("Not valid for MessageDriven beans");
     }
      // Interceptor implementation --------------------------------------
  
  
     public Object invoke(MethodInvocation mi)
        throws Exception
     {
        // Get context
        mi.setEnterpriseContext(container.getInstancePool().get());
        
          // There is no need for synchronization since the instance is always fresh 
also there should
          // never be a tx associated with the instance.
         
        try
        {
           // Invoke through interceptors
           return getNext().invoke(mi);
        } catch (RuntimeException e) // Instance will be GC'ed at MI return
        {
                mi.setEnterpriseContext(null);
                throw e;
        } catch (RemoteException e) // Instance will be GC'ed at MI return
        {
        mi.setEnterpriseContext(null);
        throw e;
        } catch (Error e) // Instance will be GC'ed at MI return
        {
            mi.setEnterpriseContext(null);
            throw e;
        } finally
        {
                // Return context
                if (mi.getEnterpriseContext() != null)
                        container.getInstancePool().free(mi.getEnterpriseContext());
        }
     }
  }
  
  
  
  1.1                  
jboss/src/main/org/jboss/ejb/plugins/MessageDrivenInstancePool.java
  
  Index: MessageDrivenInstancePool.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.ejb.plugins;
  
  import java.rmi.RemoteException;
  
  import org.jboss.ejb.Container;
  import org.jboss.ejb.EnterpriseContext;
  import org.jboss.ejb.MessageDrivenEnterpriseContext;
  
  /**
   *    <description> 
   *      Stolen from StatelessSessionInstancePool
   *    @see <related>
   *    @author Rickard �berg ([EMAIL PROTECTED])
   *      @author Peter Antman ([EMAIL PROTECTED])
   *    @version $Revision: 1.1 $
   */
  public class MessageDrivenInstancePool
     extends AbstractInstancePool
  {
     // Constants -----------------------------------------------------
      
     // Attributes ----------------------------------------------------
     
     // Static --------------------------------------------------------
     
     // Constructors --------------------------------------------------
     
     // Public --------------------------------------------------------
     
     // Z implementation ----------------------------------------------
     public void init()
        throws Exception
     {
     }
      
     // Package protected ---------------------------------------------
      
     // Protected -----------------------------------------------------
     protected EnterpriseContext create(Object instance)
        throws Exception
     {
        return new MessageDrivenEnterpriseContext(instance, getContainer());
     }
      
     // Private -------------------------------------------------------
  
     // Inner classes -------------------------------------------------
  
  }
  
  
  
  1.1                  
jboss/src/main/org/jboss/ejb/plugins/MessageDrivenTxInterceptorBMT.java
  
  Index: MessageDrivenTxInterceptorBMT.java
  ===================================================================
  /*
  * jBoss, the OpenSource EJB server
  *
  * Distributable under GPL license.
  * See terms of license at gnu.org.
  */
  package org.jboss.ejb.plugins;
  
  import java.rmi.RemoteException;
  import java.rmi.ServerException;
  
  import javax.transaction.Status;
  import javax.transaction.Transaction;
  import javax.transaction.RollbackException;
  import javax.transaction.UserTransaction;
  
  import org.jboss.ejb.MessageDrivenEnterpriseContext;
  import org.jboss.ejb.MethodInvocation;
  
  import org.jboss.logging.Logger;
  /**
  *   <description>
  *
  *   Stolen from TxInterceptorBMP
  *   @see <related>
  *   @author <a href="mailto:[EMAIL PROTECTED]">Marc Fleury</a>
  *   @author <a href="mailto:[EMAIL PROTECTED]">Sebastien Alborini</a>
  *   @author Peter Antman ([EMAIL PROTECTED])
  *   @version $Revision: 1.1 $
  */
  public class MessageDrivenTxInterceptorBMT
  extends TxInterceptorBMT
  {
  
  
  /**
      *   This method does invocation interpositioning of tx management
      *
      * MessageDriven specialication
      * @param   id
      * @param   m
      * @param   args
      * @return
      * @exception   Exception
      */
      public Object invoke(MethodInvocation mi) throws Exception {
  
                // Store old UserTX
          Object oldUserTx = userTransaction.get();
                
        
        
        // retrieve the real userTransaction
        
userTransaction.set(((MessageDrivenEnterpriseContext)mi.getEnterpriseContext()).getMessageDrivenContext().getUserTransaction());
                        
          
                
          // t1 refers to the client transaction (spec ejb1.1, 11.6.1, p174)
                // this is necessary for optimized (inVM) calls: threads come 
associated with the client transaction
          Transaction t1 = tm.disassociateThread();
                
  //DEBUG               Logger.debug("TxInterceptorBMT disassociate" + ((t1==null) ? 
"null": Integer.toString(t1.hashCode())));
        
          // t2 refers to the instance transaction (spec ejb1.1, 11.6.1, p174) 
          Transaction t2 = mi.getEnterpriseContext().getTransaction();
        
        // This is BMT so the transaction is dictated by the Bean, the 
MethodInvocation follows
        mi.setTransaction(t2);
          
        //DEBUG Logger.debug("TxInterceptorBMT t2 in context" + ((t2==null) ? "null": 
Integer.toString(t2.hashCode())));
                
          try {
            
            if (t2 != null) {
                
                                // associate the transaction to the thread
                tm.associateThread(t2);
                
            }
            
            return getNext().invoke(mi);
            
        } catch (RuntimeException e)
            {
                // EJB 2.0 17.3, table 16
                if (mi.getEnterpriseContext().getTransaction() != null) {
                    try {
                                        
mi.getEnterpriseContext().getTransaction().setRollbackOnly();
                    } catch (IllegalStateException ex) {
                    }
                }
                
                throw new ServerException("Transaction rolled back", e);        
            } catch (RemoteException e)
                {
                    // EJB 2.0 17.3, table 16
                    if (mi.getEnterpriseContext().getTransaction() != null) {
                        try {
                            
mi.getEnterpriseContext().getTransaction().setRollbackOnly();
                        } catch (IllegalStateException ex) {
                        }
                    }
                    
                    throw new ServerException("Transaction rolled back", e);    
                } catch (Error e)
                    {
                        // EJB 2.0 17.3, table 16
                        if (mi.getEnterpriseContext().getTransaction() != null) {
                            try {
                                
mi.getEnterpriseContext().getTransaction().setRollbackOnly();
                            } catch (IllegalStateException ex) {
                            }
                        }
                        
                        throw new ServerException("Transaction rolled 
back:"+e.getMessage());   
                    } finally {
                        
                        // Reset user Tx
                        userTransaction.set(oldUserTx);
                        
                        if (t1 != null) {
                            
                                // DEBUG Logger.debug("TxInterceptorBMT reassociating 
client tx " + t1.hashCode());
                            //DEBUG                             
Logger.debug("TxInterceptorBMT reassociating client tx " + t1.hashCode());
                            
                                // reassociate the previous transaction before 
returning
                            tm.associateThread(t1);
                            
                        }
                        
                        
                        
                                // t3 is the transaction associated with the context 
at the end of the call
                        Transaction t3 = mi.getEnterpriseContext().getTransaction();
                        
                        //DEBUG                         Logger.debug("in TxIntBMT " + 
t3);
                        
                                // for a stateless sessionbean the transaction should 
be completed at the end of the call
                        if (t3 != null) switch (t3.getStatus()) {
                        case Status.STATUS_ACTIVE: 
                            case Status.STATUS_COMMITTING: 
                        case Status.STATUS_MARKED_ROLLBACK: 
                        case Status.STATUS_PREPARING: 
                        case Status.STATUS_ROLLING_BACK:
                            
                            t3.rollback();
                            
                        case Status.STATUS_PREPARED: 
                            
                                // cf ejb1.1 11.6.1
                            Logger.error("Application error: BMT stateless bean " + 
container.getBeanMetaData().getEjbName() + " should complete transactions before 
returning (ejb1.1 spec, 11.6.1)");
                            
                            // the instance interceptor will discard the instance
                            throw new RemoteException("Application error: BMT 
stateless bean " + container.getBeanMetaData().getEjbName() + " should complete 
transactions before returning (ejb1.1 spec, 11.6.1)");
                        }
                        
                    }
      }
  }
  
  
  
  

Reply via email to