You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by br...@apache.org on 2004/06/10 16:40:08 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/odmg ObjectEnvelopeTable.java TransactionImpl.java

brianm      2004/06/10 07:40:08

  Modified:    src/java/org/apache/ojb/broker/core/proxy
                        CollectionProxyDefaultImpl.java ProxyHelper.java
               src/java/org/apache/ojb/odmg ObjectEnvelopeTable.java
                        TransactionImpl.java
  Added:       src/java/org/apache/ojb/broker/core/proxy
                        CollectionProxy.java
  Log:
  ODMG wasn't respecting Colelction proxies. Added an interface to CollectionproxyDefaultImpl and added utility methods to ProxyHelper for dealing with that interface. TransactionImpl now treats CollectionProxy instances analogous to how it treats ReferenceProxy instances -- it registers them when they are loaded.
  
  Revision  Changes    Path
  1.7       +2 -2      db-ojb/src/java/org/apache/ojb/broker/core/proxy/CollectionProxyDefaultImpl.java
  
  Index: CollectionProxyDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/proxy/CollectionProxyDefaultImpl.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- CollectionProxyDefaultImpl.java	30 Apr 2004 20:14:24 -0000	1.6
  +++ CollectionProxyDefaultImpl.java	10 Jun 2004 14:40:08 -0000	1.7
  @@ -38,7 +38,7 @@
    * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
    * @version $Id$
    */
  -public class CollectionProxyDefaultImpl implements Collection, ManageableCollection
  +public class CollectionProxyDefaultImpl implements Collection, ManageableCollection, CollectionProxy
   {
       /** Reference to the used PersistenceBroker */
       private transient PersistenceBroker _broker;
  
  
  
  1.2       +20 -1     db-ojb/src/java/org/apache/ojb/broker/core/proxy/ProxyHelper.java
  
  Index: ProxyHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/proxy/ProxyHelper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ProxyHelper.java	9 Apr 2004 13:22:29 -0000	1.1
  +++ ProxyHelper.java	10 Jun 2004 14:40:08 -0000	1.2
  @@ -299,4 +299,23 @@
               return object.toString();
           }
       }
  +
  +    /**
  +     * Return CollectionProxy for item is item is a CollectionProxy, otherwise return null
  +     */
  +    public static CollectionProxy getCollectionProxy(Object item)
  +    {
  +        if (isCollectionProxy(item)) return (CollectionProxy) item;
  +        else return null;
  +    }
  +
  +    /**
  +     * Reports if item is a CollectionProxy.
  +     *
  +     * @todo Provide handling for pluggable  collection proxy implementations
  +     */
  +    public static boolean isCollectionProxy(Object item)
  +    {
  +        return (item instanceof CollectionProxy);
  +    }
   }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/core/proxy/CollectionProxy.java
  
  Index: CollectionProxy.java
  ===================================================================
  package org.apache.ojb.broker.core.proxy;
  
  /**
   * Interface which Collection proxies need to implement to be
   * treated like collection proxies in ODMG.
   * <p> 
   * Presently the collection proxy impl class can be plugged in and
   * not implement this interface, but those implementations will
   * *not* be treated as proxies by OJB
   */
  public interface CollectionProxy
  {
      /**
       * Adds a listener to this collection.
       *
       * @param listener The listener to add
       */
      void addListener(CollectionProxyListener listener);
  
      /**
       * Removes the given listener from this collecton.
       *
       * @param listener The listener to remove
       */
      void removeListener(CollectionProxyListener listener);
  
      /**
       * Determines whether the collection data already has been loaded from the database.
       *
       * @return <code>true</code> if the data is already loaded
       */
      public boolean isLoaded();
  }
  
  
  
  1.31      +7 -1      db-ojb/src/java/org/apache/ojb/odmg/ObjectEnvelopeTable.java
  
  Index: ObjectEnvelopeTable.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/ObjectEnvelopeTable.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- ObjectEnvelopeTable.java	4 Apr 2004 23:53:38 -0000	1.30
  +++ ObjectEnvelopeTable.java	10 Jun 2004 14:40:08 -0000	1.31
  @@ -22,6 +22,7 @@
   import java.util.Iterator;
   import java.util.List;
   import java.util.Map;
  +import java.util.Collections;
   
   import org.apache.commons.lang.builder.ToStringBuilder;
   import org.apache.commons.lang.builder.ToStringStyle;
  @@ -31,6 +32,7 @@
   import org.apache.ojb.broker.OptimisticLockException;
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceBrokerFactory;
  +import org.apache.ojb.broker.core.proxy.ProxyHelper;
   import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
  @@ -521,7 +523,11 @@
               if (col != null)
               {
                   Iterator colIterator;
  -                if (col instanceof ManageableCollection)
  +                if (ProxyHelper.isCollectionProxy(col) && !ProxyHelper.getCollectionProxy(col).isLoaded())
  +                {
  +                    colIterator = Collections.EMPTY_LIST.iterator(); 
  +                }
  +                else if (col instanceof ManageableCollection)
                   {
                       colIterator = ((ManageableCollection) col).ojbIterator();
                   }
  
  
  
  1.58      +75 -2     db-ojb/src/java/org/apache/ojb/odmg/TransactionImpl.java
  
  Index: TransactionImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/TransactionImpl.java,v
  retrieving revision 1.57
  retrieving revision 1.58
  diff -u -r1.57 -r1.58
  --- TransactionImpl.java	9 Apr 2004 13:22:30 -0000	1.57
  +++ TransactionImpl.java	10 Jun 2004 14:40:08 -0000	1.58
  @@ -37,6 +37,9 @@
   import org.apache.ojb.broker.core.proxy.IndirectionHandler;
   import org.apache.ojb.broker.core.proxy.MaterializationListener;
   import org.apache.ojb.broker.core.proxy.ProxyHelper;
  +import org.apache.ojb.broker.core.proxy.CollectionProxy;
  +import org.apache.ojb.broker.core.proxy.CollectionProxyListener;
  +import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
  @@ -64,11 +67,12 @@
    * @author     Thomas Mahler & David Dixon-Peugh
    * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
    * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
  + * @author <a href="mailto:brianm@apache.org">Brian McCallister</a>
    * @version $Id$
    *
    */
   public class TransactionImpl
  -        implements Transaction, MaterializationListener, Configurable, TransactionExt
  +        implements Transaction, MaterializationListener, Configurable, CollectionProxyListener, TransactionExt
   {
       private Logger log = LoggerFactory.getLogger(TransactionImpl.class);
       private Hashtable myNrm = null;
  @@ -102,6 +106,13 @@
        * tx from their List of Listeners.
        */
       private ArrayList registeredIndirectionHandlers = new ArrayList();
  +
  +    /**
  +     * Unloaded collection proxies which will be registered with tx when
  +     * collection is loaded
  +     */
  +    private ArrayList registeredCollectionProxies = new ArrayList();
  +
       /**
        * list of proxy objects that were locked, but haven't been materialized yet.
        * This is necessary so the locks can be released on closing the transaction
  @@ -200,10 +211,12 @@
       public void lock(Object obj, int lockMode) throws LockNotGrantedException
       {
           if (log.isDebugEnabled()) log.debug("lock object was called on tx " + this);
  +        if (log.isDebugEnabled()) log.debug("object is " + obj.toString());
           checkOpen();
   
           if (!ProxyHelper.isProxy(obj))
           {
  +            if (log.isDebugEnabled()) log.debug("object is a proxy");
               ClassDescriptor cld = this.getBroker().getClassDescriptor(obj.getClass());
               if (!cld.isAcceptLocks())
               {
  @@ -245,6 +258,7 @@
           }
           try
           {
  +            if (log.isDebugEnabled()) log.debug("registering lock on object at " + System.currentTimeMillis());
               register(obj, lockMode);
           }
           catch (Throwable t)
  @@ -355,6 +369,7 @@
   
               // this tx is no longer interested in materialization callbacks
               unRegisterFromAllIndirectionHandlers();
  +            unRegisterFromAllCollectionProxies();
           }
           finally
           {
  @@ -913,6 +928,16 @@
               Object col = cds.getPersistentField().get(newTxObject);
               if (col != null)
               {
  +                CollectionProxy proxy = ProxyHelper.getCollectionProxy(col);
  +                if (proxy != null)
  +                {
  +                    if (!proxy.isLoaded()) ;
  +                    {
  +                        if (log.isDebugEnabled()) log.debug("adding self as listener to collection proxy");
  +                        proxy.addListener(this);
  +                        continue;
  +                    }
  +                }
                   Iterator colIterator;
                   if (col instanceof ManageableCollection)
                   {
  @@ -958,6 +983,8 @@
                                   }
                                   else
                                   {
  +                                    // @todo consider registering to hear when this is
  +                                    // derefernced instead of just loading here -bmc
                                       item = handler.getRealSubject();
                                   }
                               }
  @@ -1131,6 +1158,20 @@
           }
       }
   
  +    protected synchronized void unRegisterFromAllCollectionProxies()
  +    {
  +        for (int i = registeredCollectionProxies.size() - 1; i >= 0; i--)
  +        {
  +            unregisterFromCollectionProxy((CollectionProxy) registeredCollectionProxies.get(i));
  +        }
  +    }
  +
  +    protected synchronized void unregisterFromCollectionProxy(CollectionProxy handler)
  +    {
  +        handler.removeListener(this);
  +        registeredCollectionProxies.remove(handler);
  +    }
  +
       protected synchronized void unregisterFromIndirectionHandler(IndirectionHandler handler)
       {
           handler.removeListener(this);
  @@ -1200,5 +1241,37 @@
       public synchronized void setImplicitLocking(boolean value)
       {
           useImplicitLocking = value;
  +    }
  +
  +    /**
  +     * noop -- here for interface
  +     */
  +    public void beforeLoading(CollectionProxyDefaultImpl colProxy)
  +    {
  +        // noop
  +    }
  +
  +    /**
  +     * Remove colProxy from list of pending collections and
  +     * register its contents with the transaction.
  +     */
  +    public void afterLoading(CollectionProxyDefaultImpl colProxy)
  +    {
  +        if (log.isDebugEnabled()) log.debug("loading a proxied collection a collection: " + colProxy);
  +        if (this.registeredCollectionProxies.contains(colProxy))
  +        {
  +            this.registeredCollectionProxies.remove(colProxy);
  +            colProxy.removeListener(this);
  +        }
  +        Collection data = colProxy.getData();
  +        for (Iterator iterator = data.iterator(); iterator.hasNext();)
  +        {
  +            Object o = iterator.next();
  +            if (useImplicitLocking && this.isOpen())
  +            {
  +                int lock = useWriteLocks ? Transaction.WRITE : Transaction.READ;
  +                this.register(o, lock);
  +            }
  +        }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org