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 ar...@apache.org on 2006/09/26 14:15:47 UTC

svn commit: r450014 - in /db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb: broker/accesslayer/CollectionPrefetcher.java broker/accesslayer/MtoNCollectionPrefetcher.java broker/core/QueryReferenceBroker.java odmg/ImplementationImpl.java

Author: arminw
Date: Tue Sep 26 05:15:47 2006
New Revision: 450014

URL: http://svn.apache.org/viewvc?view=rev&rev=450014
Log:
add support for configurable collection implementation classes used for 1:n, m:n references and query results

Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/MtoNCollectionPrefetcher.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationImpl.java

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java?view=diff&rev=450014&r1=450013&r2=450014
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/CollectionPrefetcher.java Tue Sep 26 05:15:47 2006
@@ -24,25 +24,18 @@
 import java.util.List;
 
 import org.apache.ojb.broker.Identity;
+import org.apache.ojb.broker.IdentityFactory;
 import org.apache.ojb.broker.ManageableCollection;
-import org.apache.ojb.broker.OJBRuntimeException;
 import org.apache.ojb.broker.PersistenceBroker;
-import org.apache.ojb.broker.IdentityFactory;
 import org.apache.ojb.broker.core.PersistenceBrokerImpl;
 import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl;
-import org.apache.ojb.broker.core.proxy.ProxyHelper;
 import org.apache.ojb.broker.metadata.ClassDescriptor;
 import org.apache.ojb.broker.metadata.CollectionDescriptor;
 import org.apache.ojb.broker.metadata.FieldHelper;
-import org.apache.ojb.broker.metadata.MetadataException;
 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
 import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
 import org.apache.ojb.broker.query.Query;
 import org.apache.ojb.broker.query.QueryByCriteria;
-import org.apache.ojb.broker.util.BrokerHelper;
-import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
-import org.apache.ojb.broker.util.collections.RemovalAwareList;
-import org.apache.ojb.broker.util.collections.RemovalAwareSet;
 
 /**
  * Relationship Prefetcher for Collections.
@@ -73,7 +66,6 @@
     {
         ClassDescriptor cld = getOwnerClassDescriptor();
         Class topLevelClass = getBroker().getTopLevelClass(cld.getClassOfObject());
-        BrokerHelper helper = getBroker().serviceBrokerHelper();
         Collection queries = new ArrayList(owners.size());
         Collection idsSubset = new HashSet(owners.size());
         Object[] fkValues;
@@ -182,7 +174,7 @@
             }
             else
             {
-                ManageableCollection col = createCollection(cds, collectionClass);
+                ManageableCollection col = createCollection(cds);
                 for (Iterator it2 = list.iterator(); it2.hasNext();)
                 {
                     col.ojbAdd(it2.next());
@@ -208,56 +200,11 @@
      * on the field type. 
      * 
      * @param desc            The collection descriptor
-     * @param collectionClass The collection class specified in the collection-descriptor
      * @return The collection object
      */
-    protected ManageableCollection createCollection(CollectionDescriptor desc, Class collectionClass)
+    protected ManageableCollection createCollection(CollectionDescriptor desc)
     {
-        Class                fieldType = desc.getPersistentField().getType();
-        ManageableCollection col;
-
-        if (collectionClass == null)
-        {
-            if (ManageableCollection.class.isAssignableFrom(fieldType))
-            {
-                try
-                {
-                    col = (ManageableCollection)fieldType.newInstance();
-                }
-                catch (Exception e)
-                {
-                    throw new OJBRuntimeException("Cannot instantiate the default collection type "+fieldType.getName()+" of collection "+desc.getAttributeName()+" in type "+desc.getClassDescriptor().getClassNameOfObject());
-                }
-            }
-            else if (fieldType.isAssignableFrom(RemovalAwareCollection.class))
-            {
-                col = new RemovalAwareCollection();
-            }
-            else if (fieldType.isAssignableFrom(RemovalAwareList.class))
-            {
-                col = new RemovalAwareList();
-            }
-            else if (fieldType.isAssignableFrom(RemovalAwareSet.class))
-            {
-                col = new RemovalAwareSet();
-            }
-            else
-            {
-                throw new MetadataException("Cannot determine a default collection type for collection "+desc.getAttributeName()+" in type "+desc.getClassDescriptor().getClassNameOfObject());
-            }
-        }
-        else
-        {
-            try
-            {
-                col = (ManageableCollection)collectionClass.newInstance();
-            }
-            catch (Exception e)
-            {
-                throw new OJBRuntimeException("Cannot instantiate the collection class "+collectionClass.getName()+" of collection "+desc.getAttributeName()+" in type "+desc.getClassDescriptor().getClassNameOfObject());
-            }
-        }
-        return col;
+        return getBroker().getCollectionTypes().createCollectionClass(desc);
     }
 
     protected CollectionDescriptor getCollectionDescriptor()

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/MtoNCollectionPrefetcher.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/MtoNCollectionPrefetcher.java?view=diff&rev=450014&r1=450013&r2=450014
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/MtoNCollectionPrefetcher.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/MtoNCollectionPrefetcher.java Tue Sep 26 05:15:47 2006
@@ -485,7 +485,7 @@
             }
             else
             {
-                ManageableCollection col = createCollection(cds, collectionClass);
+                ManageableCollection col = createCollection(cds);
 
                 for (Iterator it2 = list.iterator(); it2.hasNext();)
                 {

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java?view=diff&rev=450014&r1=450013&r2=450014
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/QueryReferenceBroker.java Tue Sep 26 05:15:47 2006
@@ -23,6 +23,7 @@
 import java.util.List;
 import java.util.Map;
 
+import org.apache.commons.lang.SystemUtils;
 import org.apache.ojb.broker.Identity;
 import org.apache.ojb.broker.ManageableCollection;
 import org.apache.ojb.broker.PBLifeCycleEvent;
@@ -42,7 +43,6 @@
 import org.apache.ojb.broker.metadata.CollectionDescriptor;
 import org.apache.ojb.broker.metadata.FieldDescriptor;
 import org.apache.ojb.broker.metadata.FieldHelper;
-import org.apache.ojb.broker.metadata.MetadataException;
 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
 import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
 import org.apache.ojb.broker.query.Criteria;
@@ -50,14 +50,9 @@
 import org.apache.ojb.broker.query.QueryByCriteria;
 import org.apache.ojb.broker.query.QueryFactory;
 import org.apache.ojb.broker.util.BrokerHelper;
-import org.apache.ojb.broker.util.collections.ManageableArrayList;
-import org.apache.ojb.broker.util.collections.ManageableHashSet;
-import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
-import org.apache.ojb.broker.util.collections.RemovalAwareList;
-import org.apache.ojb.broker.util.collections.RemovalAwareSet;
+import org.apache.ojb.broker.util.ClassHelper;
 import org.apache.ojb.broker.util.logging.Logger;
 import org.apache.ojb.broker.util.logging.LoggerFactory;
-import org.apache.commons.lang.SystemUtils;
 
 /**
  * Encapsulates 1:1 and 1:n references and collection references stuff.
@@ -65,7 +60,6 @@
  * TODO: Should we made this class independend from PB implementation class
  * and only use PB interface methods?
  *
- * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
  * @version $Id$
  */
 public class QueryReferenceBroker
@@ -114,7 +108,7 @@
         pb.getInternalCache().enableMaterializationCache();
         try
         {
-            result = (ManageableCollection) collectionClass.newInstance();
+            result = (ManageableCollection) ClassHelper.newInstance(collectionClass);
 
             // now iterate over all elements and add them to the new collection
             // lifecycle events are disabled
@@ -254,7 +248,7 @@
             // BRJ: return empty Collection  for null query
             if (query == null)
             {
-                result = (ManageableCollection)collectionClass.newInstance();
+                result = (ManageableCollection) ClassHelper.newInstance(collectionClass);
             }
             else
             {
@@ -283,49 +277,6 @@
     }
 
     /**
-     * retrieve a collection of itemClass Objects matching the Query query
-     */
-    public Collection getCollectionByQuery(Query query, boolean lazy) throws PersistenceBrokerException
-    {
-        // thma: the following cast is safe because:
-        // 1. ManageableVector implements Collection (will be returned if lazy == false)
-        // 2. CollectionProxy implements Collection (will be returned if lazy == true)
-        return (Collection) getCollectionByQuery(RemovalAwareCollection.class, query, lazy);
-    }
-
-
-    private Class getCollectionTypeClass(CollectionDescriptor cds) throws PersistenceBrokerException{
-        // BRJ: do not use RemovalAwareCollection for m:n relationships
-        // see http://db.apache.org/ojb/docu/guides/basic-technique.html#Mapping+m%3An+associations
-
-        Class fieldType = cds.getPersistentField().getType();
-        Class collType;
-
-        if (fieldType.isArray() || fieldType.isAssignableFrom(RemovalAwareCollection.class))
-        {
-            collType = cds.isMtoNRelation() ? ManageableArrayList.class : RemovalAwareCollection.class;
-        }
-        else if (fieldType.isAssignableFrom(RemovalAwareList.class))
-        {
-            collType = cds.isMtoNRelation() ? ManageableArrayList.class : RemovalAwareList.class;
-        }
-        else if (fieldType.isAssignableFrom(RemovalAwareSet.class))
-        {
-            collType = cds.isMtoNRelation() ? ManageableHashSet.class : RemovalAwareSet.class;
-        }
-        else if (ManageableCollection.class.isAssignableFrom(fieldType))
-        {
-            collType = fieldType;
-        }
-        else
-        {
-            throw new MetadataException("Cannot determine a default collection type for collection "+cds.getAttributeName()+" in type "+cds.getClassDescriptor().getClassNameOfObject());
-        }
-        return collType;
-    }
-
-
-    /**
      * @return true if this is the first task for the given ObjectReferenceDescriptor
      */
     private boolean addRetrievalTask(Object obj, Object key)
@@ -723,7 +674,7 @@
      */
     public void retrieveCollection(Object obj, ClassDescriptor cld, CollectionDescriptor cds, boolean forced)
     {
-        doRetrieveCollection(obj, cld, cds, forced, cds.isLazy());
+        doRetrieveCollection(obj, cds, forced, cds.isLazy());
     }
 
     /**
@@ -739,10 +690,10 @@
      */
     public void retrieveProxyCollection(Object obj, ClassDescriptor cld, CollectionDescriptor cds, boolean forced)
     {
-        doRetrieveCollection(obj, cld, cds, forced, true);
+        doRetrieveCollection(obj, cds, forced, true);
     }
 
-    private void doRetrieveCollection(Object obj, ClassDescriptor cld, CollectionDescriptor cds, boolean forced, boolean lazyLoad)
+    private void doRetrieveCollection(Object obj, CollectionDescriptor cds, boolean forced, boolean lazyLoad)
     {
         if (forced || cds.getCascadeRetrieve())
         {
@@ -755,45 +706,39 @@
             else
             {
                 // this collection type will be used:
-                Class collectionClass = cds.getCollectionClass();
                 PersistentField collectionField = cds.getPersistentField();
-                Query fkQuery = getFKQuery(obj, cld, cds);
+                Query fkQuery = getFKQuery(obj, cds);
                 Object value;
 
                 pb.getInternalCache().enableMaterializationCache();
                 try
                 {
-                    if (collectionClass == null)
+                    // this collection type will be used:
+                    Class collectionClass = pb.getCollectionTypes().getCollectionClass(cds);
+                    ManageableCollection result = getCollectionByQuery(collectionClass, fkQuery, lazyLoad);
+                    // assign collection to objects attribute
+                    // if attribute has an array type build an array, else assign collection directly
+                    if (collectionField.getType().isArray() && (result instanceof Collection))
                     {
-                        Collection result = (Collection)getCollectionByQuery(getCollectionTypeClass(cds), fkQuery, lazyLoad);
+                        // if an array is used we expect a Collection result
+                        Collection coll = (Collection) result;
+                        int length = coll.size();
+                        Class itemtype = collectionField.getType().getComponentType();
+                        int j = 0;
 
-                        // assign collection to objects attribute
-                        // if attribute has an array type build an array, else assign collection directly
-                        if (collectionField.getType().isArray())
+                        value = Array.newInstance(itemtype, length);
+                        for (Iterator iter = coll.iterator(); iter.hasNext();j++)
                         {
-                            int length = result.size();
-                            Class itemtype = collectionField.getType().getComponentType();
-                            Object resultArray = Array.newInstance(itemtype, length);
-                            int j = 0;
-                            for (Iterator iter = result.iterator(); iter.hasNext();j++)
-                            {
-                                Array.set(resultArray, j, iter.next());
-                            }
-                            collectionField.set(obj, resultArray);
+                            Array.set(value, j, iter.next());
                         }
-                        else
-                        {
-                            collectionField.set(obj, result);
-                        }
-                        value = result;
                     }
                     else
                     {
-                        ManageableCollection result = getCollectionByQuery(collectionClass, fkQuery, lazyLoad);
-                        collectionField.set(obj, result);
                         value = result;
                     }
 
+                    collectionField.set(obj, value);
+
                     if (prefetchProxies && (m_retrievalTasks != null)
                             && (cds.getProxyPrefetchingLimit() > 0)
                             && (cds.getQueryCustomizer() == null)
@@ -821,18 +766,18 @@
      * Answer the foreign key query to retrieve the collection
      * defined by CollectionDescriptor
      */
-    private Query getFKQuery(Object obj, ClassDescriptor cld, CollectionDescriptor cds)
+    private Query getFKQuery(Object obj, CollectionDescriptor cds)
     {
         Query fkQuery;
         QueryByCriteria fkQueryCrit;
 
         if (cds.isMtoNRelation())
         {
-            fkQueryCrit = getFKQueryMtoN(obj, cld, cds);
+            fkQueryCrit = getFKQueryMtoN(obj, cds);
         }
         else
         {
-            fkQueryCrit = getFKQuery1toN(obj, cld, cds);
+            fkQueryCrit = getFKQuery1toN(obj, cds);
         }
 
         // check if collection must be ordered
@@ -863,10 +808,9 @@
      * supports UNIDIRECTIONAL m:n using QueryByMtoNCriteria
      * @return org.apache.ojb.broker.query.QueryByCriteria
      * @param obj the owner of the relationship
-     * @param cld the ClassDescriptor for the owner
      * @param cod the CollectionDescriptor
      */
-    private QueryByCriteria getFKQueryMtoN(Object obj, ClassDescriptor cld, CollectionDescriptor cod)
+    private QueryByCriteria getFKQueryMtoN(Object obj, CollectionDescriptor cod)
     {
         ValueContainer[] values = pb.serviceBrokerHelper().getFkTargetValuesForObject(cod, obj ,false);
         Object[] thisClassFks = cod.getFksToThisClass();
@@ -891,10 +835,9 @@
      * Get Foreign key query for 1:n
      * @return org.apache.ojb.broker.query.QueryByCriteria
      * @param obj
-     * @param cld
      * @param cod
      */
-    private QueryByCriteria getFKQuery1toN(Object obj, ClassDescriptor cld, CollectionDescriptor cod)
+    private QueryByCriteria getFKQuery1toN(Object obj, CollectionDescriptor cod)
     {
         ValueContainer[]  container = pb.serviceBrokerHelper().getFkTargetValuesForObject(cod, obj ,false);
         ClassDescriptor refCld = pb.getClassDescriptor(cod.getItemClass());

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationImpl.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationImpl.java?view=diff&rev=450014&r1=450013&r2=450014
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationImpl.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationImpl.java Tue Sep 26 05:15:47 2006
@@ -79,7 +79,12 @@
         lockManager = LockManagerFactory.getLockManager();
         setConfigurator(PersistenceBrokerFactory.getConfigurator());
         Configuration conf = getConfigurator().getConfigurationFor(null);
-        oqlCollectionClass = conf.getClass("OqlCollectionClass", ManageableArrayList.class);
+        oqlCollectionClass = conf.getClass("CollectionTypes.OQLQuery", ManageableArrayList.class);
+        // backward compatible fallback
+        if(conf.getClass("CollectionTypes.OQLQuery", null) == null)
+        {
+            oqlCollectionClass = conf.getClass("OqlCollectionClass", ManageableArrayList.class);
+        }
         impliciteWriteLocks = (conf.getString("LockAssociations", "WRITE").equalsIgnoreCase("WRITE"));
         implicitLocking = conf.getBoolean("ImplicitLocking", true);
         ordering = conf.getBoolean("Ordering", true);



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