User: patriot1burke
  Date: 01/08/06 09:26:28

  Modified:    src/main/org/jboss/ejb TxEntityMap.java EntityContainer.java
                        Application.java
  Added:       src/main/org/jboss/ejb ApplicationTxEntityMap.java
  Log:
  - put TxEntityMap back into EntityContainer
  - added ApplicationTxEntityMap to Application.  This class returns a list of entites
  involved with a transaction in the specific order in which they were accessed.
  
  Revision  Changes    Path
  1.4       +45 -28    jboss/src/main/org/jboss/ejb/TxEntityMap.java
  
  Index: TxEntityMap.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/ejb/TxEntityMap.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TxEntityMap.java  2001/08/03 17:15:43     1.3
  +++ TxEntityMap.java  2001/08/06 16:26:27     1.4
  @@ -8,6 +8,9 @@
   
   import java.util.HashMap;
   import javax.transaction.Transaction;
  +import javax.transaction.RollbackException;
  +import javax.transaction.SystemException;
  +import javax.transaction.Synchronization;
   
   /**
    * This class provides a way to find out what entities are contained in
  @@ -16,7 +19,16 @@
    * Used in EntitySynchronizationInterceptor.
    * 
    * @author <a href="[EMAIL PROTECTED]">Bill Burke</a>
  - * @version $Revision: 1.3 $
  + * @version $Revision: 1.4 $
  + *
  + * Revisions:
  + *
  + * <p><b>Revisions:</b><br>
  + * <p><b>2001/08/06: marcf</b>
  + * <ol>
  + *   <li>Got rid of disassociate and added a javax.transaction.Synchronization.  
The sync will clean up the map now.
  + *   <li>This class now interacts with ApplicationTxEntityMap available in 
Application
  + * </ol>
    */
   public class TxEntityMap
   {
  @@ -26,50 +38,55 @@
       * associate entity with transaction
       */
      public synchronized void associate(Transaction tx,
  -                                      EntityEnterpriseContext entity)
  +                                      EntityEnterpriseContext entity) throws 
RollbackException, SystemException
      {
         HashMap entityMap = (HashMap)m_map.get(tx);
         if (entityMap == null)
         {
            entityMap = new HashMap();
            m_map.put(tx, entityMap);
  +         tx.registerSynchronization(new TxEntityMapCleanup(this, tx));
         }
  +      entity.getContainer().getApplication().getTxEntityMap().associate(tx, entity);
         entityMap.put(entity.getCacheKey(), entity);
      }
   
  -   /**
  -    * Disassociate entity with transaction.  When the transaction has no
  -    * more entities.  it is removed from this class's internal HashMap.
  -    */
  -   public synchronized void disassociate(Transaction tx,
  -                                         EntityEnterpriseContext ctx)
  +   public synchronized EntityEnterpriseContext getCtx(Transaction tx,
  +                                                      CacheKey key)
      {
         HashMap entityMap = (HashMap)m_map.get(tx);
  -      if (entityMap == null)
  +      return (EntityEnterpriseContext)entityMap.get(key);
  +   }
  +
  +   private class TxEntityMapCleanup implements Synchronization
  +   {
  +      TxEntityMap map;
  +      Transaction tx;
  +
  +      public TxEntityMapCleanup(TxEntityMap map,
  +                                Transaction tx)
         {
  -         return;
  +         this.map = map;
  +         this.tx = tx;
         }
  -      entityMap.remove(ctx.getCacheKey());
  -      
  -      // When all entities are gone, cleanup!
  -      // cleanup involves removing the transaction
  -      // from the map
  -      if (entityMap.size() <= 0)
  +
  +      // Synchronization implementation -----------------------------
  +  
  +      public void beforeCompletion()
         {
  -         m_map.remove(tx);
  +         /* complete */
         }
  -   }
  -     
  -   /**
  -    * get all EntityEnterpriseContext that are involved with a transaction.
  -    */
  -   public synchronized Object[] getEntities(Transaction tx)
  -   {
  -      HashMap entityMap = (HashMap)m_map.get(tx);
  -      if (entityMap == null) // there are no entities associated
  +  
  +      public void afterCompletion(int status)
         {
  -         return new Object[0];
  +         synchronized(map)
  +         {
  +            HashMap entityMap = (HashMap)m_map.remove(tx);
  +            if (entityMap != null)
  +            {
  +               entityMap.clear();
  +            }
  +         }
         }
  -      return entityMap.values().toArray();
      }
   }
  
  
  
  1.50      +11 -4     jboss/src/main/org/jboss/ejb/EntityContainer.java
  
  Index: EntityContainer.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/ejb/EntityContainer.java,v
  retrieving revision 1.49
  retrieving revision 1.50
  diff -u -r1.49 -r1.50
  --- EntityContainer.java      2001/08/05 19:32:19     1.49
  +++ EntityContainer.java      2001/08/06 16:26:27     1.50
  @@ -46,7 +46,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel OConnor</a>
    * @author <a href="[EMAIL PROTECTED]">Bill Burke</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Andreas Schaefer</a>
  - * @version $Revision: 1.49 $
  + * @version $Revision: 1.50 $
    *
    * <p><b>Revisions:</b>
    *
  @@ -97,6 +97,8 @@
      /** This is the instancepool that is to be used */
      protected InstancePool instancePool;
   
  +   protected TxEntityMap txEntityMap = new TxEntityMap();
  +
      /**
       * This is the first interceptor in the chain. The last interceptor must
       * be provided by the container itself.
  @@ -142,6 +144,11 @@
         return instancePool;
      }
   
  +   public TxEntityMap getTxEntityMap()
  +   {
  +      return txEntityMap;
  +   }
  +
      public void setInstanceCache(InstanceCache ic)
      {
         if (ic == null)
  @@ -385,9 +392,9 @@
      public void remove(MethodInvocation mi)
         throws RemoteException, RemoveException
      {
  -             // synchronize entities with the datastore before the bean is removed
  -             // this will write queued updates so datastore will be consistent 
before removal
  -             
getApplication().synchronizeEntitiesWithinTransaction(mi.getTransaction());
  +      // synchronize entities with the datastore before the bean is removed
  +      // this will write queued updates so datastore will be consistent before 
removal
  +      getApplication().synchronizeEntitiesWithinTransaction(mi.getTransaction());
                
         // Get the persistence manager to do the dirty work
         
getPersistenceManager().removeEntity((EntityEnterpriseContext)mi.getEnterpriseContext());
  
  
  
  1.15      +123 -115  jboss/src/main/org/jboss/ejb/Application.java
  
  Index: Application.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/ejb/Application.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Application.java  2001/08/05 19:32:19     1.14
  +++ Application.java  2001/08/06 16:26:28     1.15
  @@ -25,10 +25,16 @@
    *   @see Container
    *   @see ContainerFactory
    *   @author <a href="mailto:[EMAIL PROTECTED]";>Rickard Öberg</a>
  - *   @version $Revision: 1.14 $
  + *   @version $Revision: 1.15 $
  + *
  + * <p><b>Revisions:</b><br>
  + * <p><b>2001/08/06: dain</b>
  + * <ol>
  + *   <li>Moved synchronizeEntitiesWithinTransaction to this class.
  + * </ol>
    */
   public class Application
  -     implements Service
  +   implements Service
   {
      // Constants -----------------------------------------------------
       
  @@ -52,7 +58,7 @@
       * transaction EntitySynchronizationInterceptor and InstanceSynchronization
       * manage this instance.
       */
  -   private TxEntityMap txEntityMap = new TxEntityMap();
  +   private ApplicationTxEntityMap txEntityMap = new ApplicationTxEntityMap();
   
      // Static --------------------------------------------------------
   
  @@ -66,16 +72,16 @@
         */
      public void addContainer(Container con)
      {
  -       containers.put(con.getBeanMetaData().getEjbName(), con);
  -        con.setApplication(this);
  +      containers.put(con.getBeanMetaData().getEjbName(), con);
  +      con.setApplication(this);
      }
      
   
  -     /**
  -      * Remove a container from this application.
  -      *
  -      * @param   con  
  -      */
  +   /**
  +    * Remove a container from this application.
  +    *
  +    * @param   con  
  +    */
      public void removeContainer(Container con)
      {
         containers.remove(con.getBeanMetaData().getEjbName());
  @@ -97,95 +103,95 @@
      }
      
   
  -     /**
  -      * Get a container from this Application that corresponds to a given name
  -      *
  -      * @param   name  ejb-name name defined in ejb-jar.xml
  -     *
  -      * @return  container for the named bean, or null if the container was not 
found   
  -      */
  +   /**
  +    * Get a container from this Application that corresponds to a given name
  +    *
  +    * @param   name  ejb-name name defined in ejb-jar.xml
  +    *
  +    * @return  container for the named bean, or null if the container was not found 
  
  +    */
      public Container getContainer(String name)
      {
         return (Container)containers.get(name);
      }
      
   
  -     /**
  -      * Get all containers in this Application.
  -      *
  -      * @return  a collection of containers for each enterprise bean in this 
application
  -     *          unit.
  -      */
  +   /**
  +    * Get all containers in this Application.
  +    *
  +    * @return  a collection of containers for each enterprise bean in this 
application
  +    *          unit.
  +    */
      public Collection getContainers()
      {
         return containers.values();
      }
      
   
  -     /**
  -      *      Get the class loader of this Application. 
  -      *
  -      * @return     
  -      */
  +   /**
  +    *        Get the class loader of this Application. 
  +    *
  +    * @return     
  +    */
      public ClassLoader getClassLoader()
      {
         return classLoader;
      }
      
   
  -     /**
  -      *      Set the class loader of this Application
  -      *
  -      * @param   name  
  -      */
  +   /**
  +    *        Set the class loader of this Application
  +    *
  +    * @param   name  
  +    */
      public void setClassLoader(ClassLoader cl)
      {
         this.classLoader = cl;
      }
      
   
  -     /**
  -      *      Get the name of this Application. 
  -      *
  -      * @return     
  -      */
  +   /**
  +    *        Get the name of this Application. 
  +    *
  +    * @return     
  +    */
      public String getName()
      {
         return name;
      }
      
   
  -     /**
  -      *      Set the name of this Application
  -      *
  -      * @param   name  
  -      */
  +   /**
  +    *        Set the name of this Application
  +    *
  +    * @param   name  
  +    */
      public void setName(String name)
      {
         this.name = name;
      }
      
   
  -     /**
  -      *      Get the URL from which this Application was deployed
  -      *
  -      * @return     
  -      */
  +   /**
  +    *        Get the URL from which this Application was deployed
  +    *
  +    * @return     
  +    */
      public URL getURL()
      {
         return url;
      }
      
   
  -     /**
  -      *      Set the URL that was used to deploy this Application
  -      *
  -      * @param   url  
  -      */
  +   /**
  +    *        Set the URL that was used to deploy this Application
  +    *
  +    * @param   url  
  +    */
      public void setURL(URL url)
      {
  -             if (url == null)
  -                     throw new IllegalArgumentException("Null URL");
  +      if (url == null)
  +         throw new IllegalArgumentException("Null URL");
        
         this.url = url;
         
  @@ -194,89 +200,91 @@
            name = url.toString();
      }
        
  -     /**
  -      * Gets the transaction to entity map object, which contains a map from 
  -      * a transaction to every entity used in that transaction.
  -      * @return the transaction to entity map for this application
  -      */
  -   public TxEntityMap getTxEntityMap() {
  +   /**
  +    * Gets the transaction to entity map object, which contains a map from 
  +    * a transaction to every entity used in that transaction.
  +    * @return the transaction to entity map for this application
  +    */
  +   public ApplicationTxEntityMap getTxEntityMap() {
         return txEntityMap;
      }
   
      /**
       * Stores all of the entities associated with the specified transaction.
  -      * @param tx the transaction that associated entites will be stored
  -      * @throws Exception if an problem occures while storing the entities
  +    * @param tx the transaction that associated entites will be stored
  +    * @throws Exception if an problem occures while storing the entities
       */
      public void synchronizeEntitiesWithinTransaction(Transaction tx) throws 
RemoteException {
  -             // If there is no transaction, there is nothing to synchronize.
  -             if(tx != null) {
  -                     Object[] entities = getTxEntityMap().getEntities(tx);
  -                     for (int i = 0; i < entities.length; i++) {
  -                             EntityEnterpriseContext ctx = 
(EntityEnterpriseContext)entities[i];
  -                             EntityContainer container = 
(EntityContainer)ctx.getContainer();
  -                             container.getPersistenceManager().storeEntity(ctx);
  -                     }
  -             }
  +      // If there is no transaction, there is nothing to synchronize.
  +      if(tx != null) 
  +      {
  +         EntityEnterpriseContext[] entities = getTxEntityMap().getEntities(tx);
  +         for (int i = 0; i < entities.length; i++) 
  +         {
  +            EntityEnterpriseContext ctx = entities[i];
  +            EntityContainer container = (EntityContainer)ctx.getContainer();
  +            container.getPersistenceManager().storeEntity(ctx);
  +         }
  +      }
      }
      
  -     // Service implementation ----------------------------------------
  +   // Service implementation ----------------------------------------
       
       /**
        * Initializes all the containers of this application.
        *
        * @exception Exception
        */
  -     public void init()
  -        throws Exception
  -     {
  -             Iterator enum = containers.values().iterator();
  -             while (enum.hasNext())
  -             {
  -                     Container con = (Container)enum.next();
  -                     con.init();
  -             }
  -     }
  -     
  -    /**
  +   public void init()
  +      throws Exception
  +   {
  +      Iterator enum = containers.values().iterator();
  +      while (enum.hasNext())
  +      {
  +         Container con = (Container)enum.next();
  +         con.init();
  +      }
  +   }
  +   
  +   /**
        * Starts all the containers of this application.
        *
        * @exception Exception
        */
  -     public void start()
  -        throws Exception
  -     {
  -             Iterator enum = containers.values().iterator();
  -             while (enum.hasNext())
  -             {
  -                     Container con = (Container)enum.next();
  -            con.start();        
  -             }
  -     }
  +   public void start()
  +      throws Exception
  +   {
  +      Iterator enum = containers.values().iterator();
  +      while (enum.hasNext())
  +      {
  +         Container con = (Container)enum.next();
  +         con.start();        
  +      }
  +   }
        
  -    /**
  +   /**
        * Stops all the containers of this application.
        */
  -     public void stop()
  -     {
  -             Iterator enum = containers.values().iterator();
  -             while (enum.hasNext())
  -             {
  -                     Container con = (Container)enum.next();
  -            con.stop();
  -             }
  -     }
  +   public void stop()
  +   {
  +      Iterator enum = containers.values().iterator();
  +      while (enum.hasNext())
  +      {
  +         Container con = (Container)enum.next();
  +         con.stop();
  +      }
  +   }
        
  -    /**
  +   /**
        * Destroys all the containers of this application.
        */
  -     public void destroy()
  -     {
  -             Iterator enum = containers.values().iterator();
  -             while (enum.hasNext())
  -             {
  -                     Container con = (Container)enum.next();
  -                     con.destroy();
  -             }
  -     }
  +   public void destroy()
  +   {
  +      Iterator enum = containers.values().iterator();
  +      while (enum.hasNext())
  +      {
  +         Container con = (Container)enum.next();
  +         con.destroy();
  +      }
  +   }
   }
  
  
  
  1.1                  jboss/src/main/org/jboss/ejb/ApplicationTxEntityMap.java
  
  Index: ApplicationTxEntityMap.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.ejb;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import javax.transaction.Transaction;
  import javax.transaction.RollbackException;
  import javax.transaction.SystemException;
  import javax.transaction.Synchronization;
  
  
  /**
   * This class provides a way to find out what entities are contained in
   * what transaction.  It is used, to find which entities to call ejbStore()
   * on when a ejbFind() method is called within a transaction. EJB 2.0- 9.6.4
   * also, it is used to synchronize on a remove.
   * Used in EntitySynchronizationInterceptor, EntityContainer
   *
   * Entities are stored in an ArrayList to ensure specific ordering. 
   *
   * @author <a href="[EMAIL PROTECTED]">Bill Burke</a>
   * @version $Revision: 1.1 $
   */
  public class ApplicationTxEntityMap
  {
     protected HashMap m_map = new HashMap();
      
     /**
      * associate entity with transaction
      */
     public synchronized void associate(Transaction tx,
                                        EntityEnterpriseContext entity) throws 
RollbackException, SystemException
     {
        ArrayList entityList = (ArrayList)m_map.get(tx);
        if (entityList == null)
        {
           entityList = new ArrayList();
           m_map.put(tx, entityList);
           tx.registerSynchronization(new ApplicationTxEntityMapCleanup(this, tx));
        }
        entityList.add(entity);
     }
  
     /**
      * get all EntityEnterpriseContext that are involved with a transaction.
      */
     public synchronized EntityEnterpriseContext[] getEntities(Transaction tx)
     {
        ArrayList entityList = (ArrayList)m_map.get(tx);
        if (entityList == null) // there are no entities associated
        {
           return new EntityEnterpriseContext[0];
        }
        return (EntityEnterpriseContext[])entityList.toArray(new 
EntityEnterpriseContext[0]);
     }
  
     private class ApplicationTxEntityMapCleanup implements Synchronization
     {
        ApplicationTxEntityMap map;
        Transaction tx;
  
        public ApplicationTxEntityMapCleanup(ApplicationTxEntityMap map,
                                             Transaction tx)
        {
           this.map = map;
           this.tx = tx;
        }
  
        // Synchronization implementation -----------------------------
    
        public void beforeCompletion()
        {
           /* complete */
        }
    
        public void afterCompletion(int status)
        {
           synchronized(map)
           {
              ArrayList entityList = (ArrayList)m_map.remove(tx);
              if (entityList != null)
              {
                 entityList.clear();
              }
           }
        }
     }
  
     
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to