db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattba...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/otm/kit SimpleKit.java
Date Tue, 08 Jul 2003 23:17:44 GMT
mattbaird    2003/07/08 16:17:44

  Modified:    src/java/org/apache/ojb/otm/copy
                        MetadataObjectCopyStrategy.java
                        CloneableObjectCopyStrategy.java
                        SerializeObjectCopyStrategy.java
                        ObjectCopyStrategy.java
                        ReflectiveObjectCopyStrategy.java
                        NoOpObjectCopyStrategy.java
               src/java/org/apache/ojb/otm/core BaseConnection.java
               src/java/org/apache/ojb/otm/kit SimpleKit.java
  Log:
  fix swizzling issues. Use the reflectiveObjectCopyStrategy which is faster anyway.
  
  Revision  Changes    Path
  1.3       +8 -7      db-ojb/src/java/org/apache/ojb/otm/copy/MetadataObjectCopyStrategy.java
  
  Index: MetadataObjectCopyStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/copy/MetadataObjectCopyStrategy.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MetadataObjectCopyStrategy.java	8 Jul 2003 18:16:28 -0000	1.2
  +++ MetadataObjectCopyStrategy.java	8 Jul 2003 23:17:43 -0000	1.3
  @@ -57,6 +57,7 @@
   import org.apache.ojb.broker.metadata.*;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
   import org.apache.ojb.broker.accesslayer.CollectionProxy;
  +import org.apache.ojb.broker.PersistenceBroker;
   
   import java.util.Map;
   import java.util.Collection;
  @@ -85,15 +86,15 @@
   	 * Uses an IdentityMap to make sure we don't recurse infinitely on the same object in
a cyclic object model.
   	 * Proxies
   	 * @param obj
  -	 * @param repo
  +	 * @param broker
   	 * @return
   	 */
  -	public Object copy(final Object obj, final DescriptorRepository repo)
  +	public Object copy(final Object obj, final PersistenceBroker broker)
   	{
  -		return clone(obj, new IdentityHashMap(), repo);
  +		return clone(obj, new IdentityHashMap(), broker);
   	}
   
  -	private static Object clone(final Object toCopy, final Map objMap, final DescriptorRepository
repo)
  +	private static Object clone(final Object toCopy, final Map objMap, final PersistenceBroker
broker)
   	{
   		/**
   		 * first, check to make sure we aren't recursing to some object that we've already copied.
  @@ -109,7 +110,7 @@
   		 * if no classdescriptor exists for this object, just return this object, we
   		 * can't copy it.
   		 */
  -		final ClassDescriptor cld = repo.getDescriptorFor(toCopy.getClass());
  +		final ClassDescriptor cld = broker.getDescriptorRepository().getDescriptorFor(toCopy.getClass());
   		if (cld == null)
   			return toCopy;
   
  @@ -166,7 +167,7 @@
   			}
   			else
   			{
  -				Object clone = clone(object, objMap, repo);
  +				Object clone = clone(object, objMap, broker);
   				objMap.put(object, clone);
   				f.set(retval, clone);
   			}
  @@ -212,7 +213,7 @@
   						}
   						else
   						{
  -							Object clone = clone(obj, objMap, repo);
  +							Object clone = clone(obj, objMap, broker);
   							objMap.put(obj, clone);
   							newCollection.add(clone);
   						}
  
  
  
  1.4       +3 -2      db-ojb/src/java/org/apache/ojb/otm/copy/CloneableObjectCopyStrategy.java
  
  Index: CloneableObjectCopyStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/copy/CloneableObjectCopyStrategy.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CloneableObjectCopyStrategy.java	8 Jul 2003 16:01:58 -0000	1.3
  +++ CloneableObjectCopyStrategy.java	8 Jul 2003 23:17:43 -0000	1.4
  @@ -55,6 +55,7 @@
    */
   
   import org.apache.ojb.broker.metadata.DescriptorRepository;
  +import org.apache.ojb.broker.PersistenceBroker;
   
   /**
    * @author matthew.baird
  @@ -69,10 +70,10 @@
   	 * Usually the OjbCloneable interface should just be delegating to the clone()
   	 * operation that the user has implemented.
   	 *
  -	 * @see org.apache.ojb.otm.copy.ObjectCopyStrategy#copy(Object, DescriptorRepository)
  +	 * @see org.apache.ojb.otm.copy.ObjectCopyStrategy#copy(Object, PersistenceBroker)
   	 *
   	 */
  -	public Object copy(Object obj, DescriptorRepository repo)
  +	public Object copy(Object obj, PersistenceBroker broker)
   			throws ObjectCopyException
   	{
   		if (obj instanceof OjbCloneable)
  
  
  
  1.4       +3 -2      db-ojb/src/java/org/apache/ojb/otm/copy/SerializeObjectCopyStrategy.java
  
  Index: SerializeObjectCopyStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/copy/SerializeObjectCopyStrategy.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SerializeObjectCopyStrategy.java	7 Jul 2003 22:54:14 -0000	1.3
  +++ SerializeObjectCopyStrategy.java	8 Jul 2003 23:17:43 -0000	1.4
  @@ -55,6 +55,7 @@
    */
   
   import org.apache.ojb.broker.metadata.DescriptorRepository;
  +import org.apache.ojb.broker.PersistenceBroker;
   
   import java.io.*;
   
  @@ -70,9 +71,9 @@
   	/**
   	 * This implementation will probably be slower than the metadata
   	 * object copy, but this was easier to implement.
  -	 * @see org.apache.ojb.otm.copy.ObjectCopyStrategy#copy(Object, DescriptorRepository)
  +	 * @see org.apache.ojb.otm.copy.ObjectCopyStrategy#copy(Object, PersistenceBroker)
   	 */
  -	public Object copy(final Object obj, final DescriptorRepository repo)
  +	public Object copy(final Object obj, final PersistenceBroker broker)
   			throws ObjectCopyException
   	{
   		ObjectOutputStream oos = null;
  
  
  
  1.3       +3 -2      db-ojb/src/java/org/apache/ojb/otm/copy/ObjectCopyStrategy.java
  
  Index: ObjectCopyStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/copy/ObjectCopyStrategy.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ObjectCopyStrategy.java	7 Jul 2003 22:54:14 -0000	1.2
  +++ ObjectCopyStrategy.java	8 Jul 2003 23:17:43 -0000	1.3
  @@ -1,6 +1,7 @@
   package org.apache.ojb.otm.copy;
   
   import org.apache.ojb.broker.metadata.DescriptorRepository;
  +import org.apache.ojb.broker.PersistenceBroker;
   
   /**
    *
  @@ -18,9 +19,9 @@
   	 * Make a copy of the given object
   	 *
   	 * @param obj object to be copied
  -	 * @param repo if needed for a meta-data type copy
  +	 * @param broker if needed for a meta-data type copy
   	 * @return Object the copy of the object
   	 */
  -	public Object copy(Object obj, DescriptorRepository repo);
  +	public Object copy(Object obj, PersistenceBroker broker);
   
   }
  
  
  
  1.3       +46 -17    db-ojb/src/java/org/apache/ojb/otm/copy/ReflectiveObjectCopyStrategy.java
  
  Index: ReflectiveObjectCopyStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/copy/ReflectiveObjectCopyStrategy.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ReflectiveObjectCopyStrategy.java	8 Jul 2003 16:01:58 -0000	1.2
  +++ ReflectiveObjectCopyStrategy.java	8 Jul 2003 23:17:43 -0000	1.3
  @@ -54,7 +54,8 @@
    * <http://www.apache.org/>.
    */
   
  -import org.apache.ojb.broker.metadata.DescriptorRepository;
  +import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.Identity;
   
   import java.lang.reflect.Array;
   import java.lang.reflect.Constructor;
  @@ -66,6 +67,7 @@
   import java.util.Set;
   //#ifdef JDK14
   import java.util.IdentityHashMap;
  +import java.sql.Timestamp;
   //#else
   /*
   import org.apache.ojb.otm.util.IdentityHashMap;
  @@ -95,17 +97,38 @@
   		FINAL_IMMUTABLE_CLASSES.add(Double.class);
   		FINAL_IMMUTABLE_CLASSES.add(Character.class);
   		FINAL_IMMUTABLE_CLASSES.add(Boolean.class);
  +		FINAL_IMMUTABLE_CLASSES.add(Timestamp.class);
   	}
   
   	/**
   	 * makes a deep clone of the object, using reflection.
  +	 *
   	 * @param toCopy the object you want to copy
  -	 * @param repo the descriptor repository, if needed.
  +	 * @param broker the broker, if needed.
   	 * @return
   	 */
  -	public final Object copy(final Object toCopy, final DescriptorRepository repo)
  +	public final Object copy(final Object toCopy, final PersistenceBroker broker)
  +	{
  +		return internalClone(toCopy, new IdentityHashMap(), new HashMap(), broker);
  +	}
  +
  +	private static final Object clone(Object toCopy, final Map objMap, final Map metadataMap,
PersistenceBroker broker)
   	{
  -		return clone(toCopy, new IdentityHashMap(), new HashMap());
  +		/**
  +		 * eliminate some up front
  +		 */
  +		if (!FINAL_IMMUTABLE_CLASSES.contains(toCopy.getClass()))
  +		{
  +			/**
  +			 * only reload if this is a mapped class.
  +			 */
  +			if (broker.getDescriptorRepository().hasDescriptorFor(toCopy.getClass()))
  +			{
  +				Identity oid = new Identity(toCopy, broker);
  +				toCopy = broker.getObjectByIdentity(oid);
  +			}
  +		}
  +		return internalClone(toCopy, objMap, metadataMap, broker);
   	}
   	/*
   	 * class used to cache class metadata info
  @@ -118,7 +141,7 @@
   		boolean m_fieldsAccessible;
   	}
   
  -	private static Object clone(final Object toCopy, final Map objMap, final Map metadataMap)
  +	private static Object internalClone(final Object toCopy, final Map objMap, final Map metadataMap,
PersistenceBroker broker)
   	{
   		/**
   		 * first, check to make sure we aren't recursing to some object that we've already copied.
  @@ -129,7 +152,7 @@
   		final Object retval;
   		if (objClass.isArray())
   		{
  -			retval = handleArray(toCopy, objMap, objClass, metadataMap);
  +			retval = handleArray(toCopy, objMap, objClass, metadataMap, broker);
   		}
   		else if (FINAL_IMMUTABLE_CLASSES.contains(objClass))
   		{
  @@ -138,12 +161,14 @@
   		}
   		else
   		{
  -			retval = handleObjectWithNoArgsConstructor(metadataMap, objClass, objMap, toCopy);
  +			retval = handleObjectWithNoArgsConstructor(metadataMap, objClass, objMap, toCopy, broker);
   		}
   		return retval;
   	}
   
  -	private static Object handleObjectWithNoArgsConstructor(final Map metadataMap, final Class
objClass, final Map objMap, final Object toCopy)
  +	private static Object handleObjectWithNoArgsConstructor(final Map metadataMap, final Class
objClass,
  +	                                                        final Map objMap, final Object
toCopy,
  +	                                                        PersistenceBroker broker)
   	{
   		final Object retval;
   		ClassMetadata metadata = (ClassMetadata) metadataMap.get(objClass);
  @@ -191,12 +216,13 @@
   		}
   		for (Class c = objClass; c != Object.class; c = c.getSuperclass())
   		{
  -			copyClass(metadataMap, c, toCopy, retval, objMap);
  +			copyClass(metadataMap, c, toCopy, retval, objMap, broker);
   		}
   		return retval;
   	}
   
  -	private static void copyClass(final Map metadataMap, final Class c, final Object obj,
final Object retval, final Map objMap)
  +	private static void copyClass(final Map metadataMap, final Class c, final Object obj,
final Object retval,
  +	                              final Map objMap, PersistenceBroker broker)
   	{
   		ClassMetadata metadata;
   		metadata = (ClassMetadata) metadataMap.get(c);
  @@ -211,11 +237,11 @@
   			declaredFields = c.getDeclaredFields();
   			metadata.m_declaredFields = declaredFields;
   		}
  -		setFields(obj, retval, declaredFields, metadata.m_fieldsAccessible, objMap, metadataMap);
  +		setFields(obj, retval, declaredFields, metadata.m_fieldsAccessible, objMap, metadataMap,
broker);
   		metadata.m_fieldsAccessible = true;
   	}
   
  -	private static Object handleArray(final Object obj, final Map objMap, final Class objClass,
final Map metadataMap)
  +	private static Object handleArray(final Object obj, final Map objMap, final Class objClass,
final Map metadataMap, PersistenceBroker broker)
   	{
   		final Object retval;
   		final int arrayLength = Array.getLength(obj);
  @@ -250,7 +276,7 @@
   					final Object slot = Array.get(obj, i);
   					if (slot != null)
   					{
  -						final Object slotClone = clone(slot, objMap, metadataMap);
  +						final Object slotClone = clone(slot, objMap, metadataMap, broker);
   						Array.set(retval, i, slotClone);
   					}
   				}
  @@ -270,7 +296,8 @@
   	 */
   	private static void setFields(final Object from, final Object to,
   	                              final Field[] fields, final boolean accessible,
  -	                              final Map objMap, final Map metadataMap)
  +	                              final Map objMap, final Map metadataMap,
  +	                              PersistenceBroker broker)
   	{
   		for (int f = 0, fieldsLength = fields.length; f < fieldsLength; ++f)
   		{
  @@ -292,7 +319,7 @@
   			}
   			try
   			{
  -				cloneAndSetFieldValue(field, from, to, objMap, metadataMap);
  +				cloneAndSetFieldValue(field, from, to, objMap, metadataMap, broker);
   			}
   			catch (Exception e)
   			{
  @@ -301,7 +328,9 @@
   		}
   	}
   
  -	private static void cloneAndSetFieldValue(final Field field, final Object src, final Object
dest, final Map objMap, final Map metadataMap) throws IllegalAccessException
  +	private static void cloneAndSetFieldValue(final Field field, final Object src, final Object
dest,
  +	                                          final Map objMap, final Map metadataMap, PersistenceBroker
broker)
  +	        throws IllegalAccessException
   	{
   		Object value = field.get(src);
   		if (value == null)
  @@ -321,7 +350,7 @@
   				 * recursively call clone on value as it could be an object reference, an array,
   				 * or some mutable type
   				 */
  -				value = clone(value, objMap, metadataMap);
  +				value = clone(value, objMap, metadataMap, broker);
   			}
   			field.set(dest, value);
   		}
  
  
  
  1.3       +3 -2      db-ojb/src/java/org/apache/ojb/otm/copy/NoOpObjectCopyStrategy.java
  
  Index: NoOpObjectCopyStrategy.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/copy/NoOpObjectCopyStrategy.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- NoOpObjectCopyStrategy.java	7 Jul 2003 22:54:14 -0000	1.2
  +++ NoOpObjectCopyStrategy.java	8 Jul 2003 23:17:43 -0000	1.3
  @@ -1,6 +1,7 @@
   package org.apache.ojb.otm.copy;
   
   import org.apache.ojb.broker.metadata.DescriptorRepository;
  +import org.apache.ojb.broker.PersistenceBroker;
   
   /* ====================================================================
    * The Apache Software License, Version 1.1
  @@ -69,10 +70,10 @@
   public class NoOpObjectCopyStrategy implements ObjectCopyStrategy
   {
   	/**
  -	 * @see org.apache.ojb.otm.copy.ObjectCopyStrategy#copy(Object, DescriptorRepository)
  +	 * @see org.apache.ojb.otm.copy.ObjectCopyStrategy#copy(Object, PersistenceBroker)
   	 *
   	 */
  -	public Object copy(Object obj, DescriptorRepository repo)
  +	public Object copy(Object obj, PersistenceBroker broker)
   			throws ObjectCopyException
   	{
   		return obj;
  
  
  
  1.16      +2 -2      db-ojb/src/java/org/apache/ojb/otm/core/BaseConnection.java
  
  Index: BaseConnection.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/core/BaseConnection.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- BaseConnection.java	8 Jul 2003 22:41:48 -0000	1.15
  +++ BaseConnection.java	8 Jul 2003 23:17:44 -0000	1.16
  @@ -187,7 +187,7 @@
               if (lock != LockType.WRITE_LOCK)
               {
                   copyStrategy = _tx.getKit().getCopyStrategy(oid);
  -                object = copyStrategy.copy(object, _pb.getDescriptorRepository());
  +                object = copyStrategy.copy(object, _pb);
               }
           }
   
  @@ -419,7 +419,7 @@
               else if (_lock != LockType.WRITE_LOCK && isMaterialized(object))
               {
                   copyStrategy = _tx.getKit().getCopyStrategy(oid);
  -                object = copyStrategy.copy(object, _pb.getDescriptorRepository());
  +                object = copyStrategy.copy(object, _pb);
               }
   
               try
  
  
  
  1.10      +6 -9      db-ojb/src/java/org/apache/ojb/otm/kit/SimpleKit.java
  
  Index: SimpleKit.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/otm/kit/SimpleKit.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- SimpleKit.java	28 Jun 2003 12:46:55 -0000	1.9
  +++ SimpleKit.java	8 Jul 2003 23:17:44 -0000	1.10
  @@ -54,14 +54,9 @@
    * <http://www.apache.org/>.
    */
   
  -import java.io.Serializable; 
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.otm.OTMKit;
  -import org.apache.ojb.otm.copy.CloneableObjectCopyStrategy;
  -import org.apache.ojb.otm.copy.NoOpObjectCopyStrategy;
  -import org.apache.ojb.otm.copy.ObjectCopyStrategy;
  -import org.apache.ojb.otm.copy.OjbCloneable;
  -import org.apache.ojb.otm.copy.SerializeObjectCopyStrategy;
  +import org.apache.ojb.otm.copy.*;
   import org.apache.ojb.otm.lock.map.InMemoryLockMap;
   import org.apache.ojb.otm.lock.map.LockMap;
   import org.apache.ojb.otm.lock.wait.LockWaitStrategy;
  @@ -71,6 +66,8 @@
   import org.apache.ojb.otm.transaction.LocalTransactionFactory;
   import org.apache.ojb.otm.transaction.TransactionFactory;
   
  +import java.io.Serializable;
  +
   /**
    *
    * <javadoc>
  @@ -88,7 +85,7 @@
       protected LockWaitStrategy _lockWaitStrategy;
       protected LockMap _lockMap;
       protected ObjectCopyStrategy _noOpCopyStrategy;
  -    protected ObjectCopyStrategy _serializableCopyStrategy;
  +    protected ObjectCopyStrategy _defaultCopyStrategy;
       protected ObjectCopyStrategy _cloneableCopyStrategy;
   
       /**
  @@ -102,7 +99,7 @@
           _lockWaitStrategy = new TimeoutStrategy();
           _lockMap = new InMemoryLockMap();
           _noOpCopyStrategy = new NoOpObjectCopyStrategy();
  -        _serializableCopyStrategy = new SerializeObjectCopyStrategy();
  +        _defaultCopyStrategy = new ReflectiveObjectCopyStrategy();
           _cloneableCopyStrategy = new CloneableObjectCopyStrategy();
       }
   
  @@ -165,7 +162,7 @@
           }
           else if (Serializable.class.isAssignableFrom(clazz))
           {
  -            return _serializableCopyStrategy;
  +            return _defaultCopyStrategy;
           }
           else
           {
  
  
  

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


Mime
View raw message