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/01/08 21:03:30 UTC

svn commit: r367080 - /db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/IdentityFactoryImpl.java

Author: arminw
Date: Sun Jan  8 12:03:26 2006
New Revision: 367080

URL: http://svn.apache.org/viewcvs?rev=367080&view=rev
Log:
minor improvements, rename field, add TODO comment

Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/IdentityFactoryImpl.java

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/IdentityFactoryImpl.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/IdentityFactoryImpl.java?rev=367080&r1=367079&r2=367080&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/IdentityFactoryImpl.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/core/IdentityFactoryImpl.java Sun Jan  8 12:03:26 2006
@@ -17,13 +17,16 @@
 
 import java.util.Map;
 
+import org.apache.commons.collections.map.ReferenceIdentityMap;
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.SystemUtils;
+import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.ojb.broker.Identity;
 import org.apache.ojb.broker.IdentityFactory;
-import org.apache.ojb.broker.OJBRuntimeException;
+import org.apache.ojb.broker.PBStateEvent;
+import org.apache.ojb.broker.PBStateListener;
 import org.apache.ojb.broker.PersistenceBroker;
 import org.apache.ojb.broker.PersistenceBrokerException;
-import org.apache.ojb.broker.PBStateListener;
-import org.apache.ojb.broker.PBStateEvent;
 import org.apache.ojb.broker.core.proxy.IndirectionHandler;
 import org.apache.ojb.broker.core.proxy.ProxyHelper;
 import org.apache.ojb.broker.metadata.ClassDescriptor;
@@ -32,27 +35,32 @@
 import org.apache.ojb.broker.util.BrokerHelper;
 import org.apache.ojb.broker.util.sequence.SequenceManager;
 import org.apache.ojb.broker.util.sequence.SequenceManagerTransientImpl;
-import org.apache.commons.lang.SystemUtils;
-import org.apache.commons.lang.ArrayUtils;
-import org.apache.commons.lang.exception.ExceptionUtils;
-import org.apache.commons.collections.map.ReferenceIdentityMap;
 
 /**
- * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
+ * Implementation class for {@link org.apache.ojb.broker.IdentityFactory}.
+ *
  * @version $Id$
  * @see org.apache.ojb.broker.IdentityFactory
  */
 public class IdentityFactoryImpl implements IdentityFactory, PBStateListener
 {
     private PersistenceBroker broker;
-    //private boolean activeTx;
-    private Map objectToIdentityMap;
-    private SequenceManager transientSequenceManager;
+    //private final Map persistentIdentityMap;
+    private final Map transientIdentityMap;
+    private final SequenceManager transientSequenceManager;
 
     public IdentityFactoryImpl(PersistenceBroker broker)
     {
         this.broker = broker;
-        this.objectToIdentityMap = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.HARD, true);
+        this.transientIdentityMap = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.HARD, false);
+        /*
+        TODO: Introduce cache for persistent object Identity objects, as long as user can use the direct
+        constructor call of Identity class we can run into problems when an object is stored/deleted/stored in
+        the same PB-tx, because then the PK of the object can change (e.g. when DB identity columns are used)
+        but the cached Identity object will always be the same when the Identity object is requested by the
+        IdentityFactory
+        */
+        //this.persistentIdentityMap = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.HARD, true);
         this.transientSequenceManager = new SequenceManagerTransientImpl(broker);
         broker.addListener(this, true);
     }
@@ -63,9 +71,9 @@
      * is transient and former call for the same object returns already a transient Identity, the same transient
      * Identity object will be returned.
      */
-    protected Identity createTransientOrRealIdentity(ClassDescriptor cld, Object objOrProxy)
+    protected Identity createTransientOrRealIdentity(ClassDescriptor cld, final Object objOrProxy)
     {
-        if(objOrProxy == null) throw new OJBRuntimeException("Can't create Identity for 'null'-object");
+        if(objOrProxy == null) throw new NullPointerException("Can't create Identity for 'null'-object");
         Identity result = null;
         Class topLevelClass = null;
         Class realClass = null;
@@ -73,14 +81,14 @@
         try
         {
             final IndirectionHandler handler = ProxyHelper.getIndirectionHandler(objOrProxy);
-
-            synchronized(objOrProxy)
+            if(handler != null)
             {
-                if(handler != null)
-                {
-                    result = handler.getIdentity();
-                }
-                else
+                result = handler.getIdentity();
+            }
+            else
+            {
+                //result = (Identity) persistentIdentityMap.get(objOrProxy);
+                if(result == null)
                 {
                     // now we are sure that the specified object is not a proxy
                     realClass = objOrProxy.getClass();
@@ -103,13 +111,13 @@
                         Object value = fld.getPersistentField().get(objOrProxy);
                         if(helper.representsNull(fld, value))
                         {
-                            result = (Identity) objectToIdentityMap.get(objOrProxy);
+                            result = (Identity) transientIdentityMap.get(objOrProxy);
                             if(result == null)
                             {
                                 pks[i] = transientSequenceManager.getUniqueValue(fld);
                                 result = new Identity(realClass, topLevelClass, pks, true);
                                 //if(activeTx) objectToIdentityMap.put(objOrProxy, result);
-                                objectToIdentityMap.put(objOrProxy, result);
+                                transientIdentityMap.put(objOrProxy, result);
                             }
                             break;
                         }
@@ -121,6 +129,7 @@
                     if(result == null)
                     {
                         result = new Identity(realClass, topLevelClass, pks, false);
+                        //persistentIdentityMap.put(objOrProxy, result);
                     }
                 }
             }
@@ -137,19 +146,19 @@
     }
 
     /** @see org.apache.ojb.broker.IdentityFactory#buildIdentity(Object) */
-    public Identity buildIdentity(Object obj)
+    public Identity buildIdentity(final Object obj)
     {
         return createTransientOrRealIdentity(broker.getClassDescriptor(ProxyHelper.getRealClass(obj)), obj);
     }
 
     /** @see org.apache.ojb.broker.IdentityFactory#buildIdentity(Object) */
-    public Identity buildIdentity(ClassDescriptor cld, Object obj)
+    public Identity buildIdentity(final ClassDescriptor cld, final Object obj)
     {
         return createTransientOrRealIdentity(cld, obj);
     }
 
     /** @see org.apache.ojb.broker.IdentityFactory#buildIdentity(Class, Class, String[], Object[]) */
-    public Identity buildIdentity(Class realClass, Class topLevelClass, String[] pkFieldNames, Object[] pkValues)
+    public Identity buildIdentity(final Class realClass, final Class topLevelClass, final String[] pkFieldNames, final Object[] pkValues)
     {
         Object[] orderedPKValues = pkValues;
         if(pkValues == null)
@@ -184,7 +193,7 @@
      * @param fieldValues The field values.
      * @return The ordered field values.
      */
-    private Object[] reorderFieldValues(FieldDescriptor[] flds, String[] fieldNames, Object[] fieldValues)
+    private Object[] reorderFieldValues(final FieldDescriptor[] flds, final String[] fieldNames, final Object[] fieldValues)
     {
         String fieldName;
         Object[] orderedValues = new Object[flds.length];
@@ -232,19 +241,19 @@
     }
 
     /** @see org.apache.ojb.broker.IdentityFactory#buildIdentity(Class, String[], Object[]) */
-    public Identity buildIdentity(Class realClass, String[] pkFieldNames, Object[] pkValues)
+    public Identity buildIdentity(final Class realClass, final String[] pkFieldNames, final Object[] pkValues)
     {
         return buildIdentity(realClass, broker.getTopLevelClass(realClass), pkFieldNames, pkValues);
     }
 
     /** @see org.apache.ojb.broker.IdentityFactory#buildIdentity(Class, String[], Object[]) */
-    public Identity buildIdentity(Class realClass, Class topLevelClass, Object[] pkValues)
+    public Identity buildIdentity(final Class realClass, final Class topLevelClass, final Object[] pkValues)
     {
         return new Identity(realClass, topLevelClass, pkValues);
     }
 
     /** @see org.apache.ojb.broker.IdentityFactory#buildIdentity(Class, Object) */
-    public Identity buildIdentity(Class realClass, Object pkValue)
+    public Identity buildIdentity(final Class realClass, final Object pkValue)
     {
         return buildIdentity(realClass, (String[]) null, new Object[]{pkValue});
     }
@@ -304,17 +313,17 @@
 
     public void afterCommit(PBStateEvent event)
     {
-        if(objectToIdentityMap.size() > 0) objectToIdentityMap.clear();
+        if(transientIdentityMap.size() > 0) transientIdentityMap.clear();
     }
 
     public void afterRollback(PBStateEvent event)
     {
-        if(objectToIdentityMap.size() > 0) objectToIdentityMap.clear();
+        if(transientIdentityMap.size() > 0) transientIdentityMap.clear();
     }
 
     public void beforeClose(PBStateEvent event)
     {
-        if(objectToIdentityMap.size() > 0) objectToIdentityMap.clear();
+        if(transientIdentityMap.size() > 0) transientIdentityMap.clear();
     }
 
     public void beforeRollback(PBStateEvent event)



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