User: fleury  
  Date: 00/09/07 22:15:48

  Added:       src/java/org/jboss/zol/testbean/bean TxSessionBean.java
  Log:
  new bean for testing transaction behaviour
  
  Revision  Changes    Path
  1.1                  zola/src/java/org/jboss/zol/testbean/bean/TxSessionBean.java
  
  Index: TxSessionBean.java
  ===================================================================
  /******************************************************
   * File: TxSessionBean.java
   * created 07-Sep-00 7:39:53 PM by Administrator
   */
  
  
  package org.jboss.zol.testbean.bean;
  
  import java.rmi.*;
  import javax.ejb.*;
  import javax.naming.InitialContext;
  import javax.naming.Context;
  import org.jboss.zol.testbean.interfaces.TxSession;
  
  
  public class TxSessionBean implements SessionBean {
    private SessionContext sessionContext;
  
    public void ejbCreate() throws RemoteException, CreateException {
    System.out.println("TxSessionBean.ejbCreate() called");
    }
  
    public void ejbActivate() throws RemoteException {
      System.out.println("TxSessionBean.ejbActivate() called");
    }
  
    public void ejbPassivate() throws RemoteException {
        System.out.println("TxSessionBean.ejbPassivate() called");
    }
  
    public void ejbRemove() throws RemoteException {
       System.out.println("TxSessionBean.ejbRemove() called");
    }
  
    public void setSessionContext(SessionContext context) throws RemoteException {
      sessionContext = context;
      //Exception e = new Exception("in set Session context");
      //e.printStackTrace();
    }
  
    /*
    * This method is defined with "Required"
    */
    public String txRequired() {
   
        System.out.println("TxSessionBean.txRequired() called");  
        
        try {
           Object tx =getDaTransaction();
           if (tx == null) 
             throw new Error("Required sees no transaction");
           else
             return ("required sees a transaction "+tx.hashCode());
        }
        catch (Exception e) {e.printStackTrace(); return e.getMessage();}
     }
    
    
    /*
    * This method is defined with "Requires_new"
    */
    public String txRequiresNew() {
   
        System.out.println("TxSessionBean.txRequiresNew() called");  
        
        try {
           Object tx =getDaTransaction();
           if (tx == null) 
             throw new Error("RequiresNew sees no transaction");
           else
             return ("requiresNew sees a transaction "+tx.hashCode());
        }
        catch (Exception e) {e.printStackTrace();return e.getMessage();}
     }
    
     /*
    * testSupports is defined with Supports
    */
     
    public String txSupports() {
        
        System.out.println("TxSessionBean.txSupports() called");  
        
        try {
            
          Object tx =getDaTransaction();
          
          if (tx == null) 
             return "supports sees no transaction";
          else
             return "supports sees a transaction "+tx.hashCode();
      }
      catch (Exception e) {e.printStackTrace();return e.getMessage();}
   }  
   
   
    /*
    * This method is defined with "Mandatory"
    */
    public String txMandatory() {
   
        System.out.println("TxSessionBean.txMandatory() called");  
        
        try {
           Object tx =getDaTransaction();
           if (tx == null) 
             throw new Error("Mandatory sees no transaction");
           else
             return ("mandatory sees a transaction "+tx.hashCode());
        }
        catch (Exception e) {e.printStackTrace();return e.getMessage();}
     }
    
     /*
    * This method is defined with "Never"
    */
    public String txNever() {
   
        System.out.println("TxSessionBean.txNever() called");  
        
        try {
           Object tx =getDaTransaction();
           if (tx == null) 
             return "never sees no transaction";
           else
               throw new Error("txNever sees a transaction");
        }
        catch (Exception e) {e.printStackTrace();return e.getMessage();}
     }
      /*
    * This method is defined with "TxNotSupported"
    */
    
    public String txNotSupported() {
   
        System.out.println("TxSessionBean.txNotSupported() called");  
        
        try {
           Object tx =getDaTransaction();
           if (tx == null) 
             return "notSupported sees no transaction";
           else
               throw new Error("txNotSupported sees a transaction");
        }
        catch (Exception e) {e.printStackTrace();return e.getMessage();}
     }
   
    /*
    * This method is defined with "Required" and it passes it to a Supports Tx
    */
    public String requiredToSupports() throws RemoteException {
        
        System.out.println("TxSessionBean.requiredToSupports() called");
        
        String message;        
        Object tx =getDaTransaction();
           if (tx == null) 
             throw new Error("Required doesn't see a transaction");
           else
             message = "Required sees a transaction "+tx.hashCode()+ " Supports should 
see the same ";
       
        message = message + ((TxSession) sessionContext.getEJBObject()).txSupports();
    
         // And after invocation we should have the same transaction
         tx =getDaTransaction();
           if (tx == null) 
             throw new Error("Required doesn't see a transaction COMING BACK");
           else
             return message + " on coming back Required sees a transaction 
"+tx.hashCode() ;
   
    }   
    
      /*
    * This method is defined with "Required" and it passes it to a NotSupported Tx
    */
    public String requiredToNotSupported() throws RemoteException {
        
        System.out.println("TxSessionBean.requiredToNotSupported() called");
        
        String message;        
        Object tx =getDaTransaction();
           if (tx == null) 
             throw new Error("Required doesn't see a transaction");
           else
             message = "Required sees a transaction "+tx.hashCode()+ " NotSupported 
should see the same ";
      
          
        message = message + ((TxSession) 
sessionContext.getEJBObject()).txNotSupported();
        
        // And after invocation we should have the same transaction
         tx =getDaTransaction();
           if (tx == null) 
             throw new Error("Required doesn't see a transaction COMING BACK");
           else
             return message + " on coming back Required sees a transaction 
"+tx.hashCode() ;
    }
    
    public String requiredToRequiresNew() throws RemoteException{
        
        System.out.println("TxSessionBean.requiredToRequiresNew() called");
        
        String message;        
        Object tx =getDaTransaction();
           if (tx == null) 
             throw new Error("Required doesn't see a transaction");
           else
             message = "Required sees a transaction "+tx.hashCode()+ " Requires new 
should see a new transaction ";
      
      message =  message + ((TxSession) sessionContext.getEJBObject()).txRequiresNew();
        
       // And after invocation we should have the same transaction
         tx =getDaTransaction();
           if (tx == null) 
             throw new Error("Required doesn't see a transaction COMING BACK");
           else
             return message + " on coming back Required sees a transaction 
"+tx.hashCode() ;
    }
        
  
    
    private Object getDaTransaction() {
    
       try { 
          
          return ((javax.transaction.TransactionManager) new 
InitialContext().lookup("TransactionManager")).getTransaction();
      }
      catch (Exception e) { return null;}
    }
               
  }
  
  
  

Reply via email to