db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olegn...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/otm/copy MetadataObjectCopyStrategy.java
Date Tue, 15 Jul 2003 23:13:38 GMT
olegnitz    2003/07/15 16:13:38

  Modified:    src/java/org/apache/ojb/otm/copy
                        MetadataObjectCopyStrategy.java
  Log:
  Switched to MetadataObjectCopyStrategy
  
  Revision  Changes    Path
  1.5       +156 -156  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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MetadataObjectCopyStrategy.java	11 Jul 2003 17:21:42 -0000	1.4
  +++ MetadataObjectCopyStrategy.java	15 Jul 2003 23:13:38 -0000	1.5
  @@ -58,7 +58,9 @@
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
   import org.apache.ojb.broker.accesslayer.CollectionProxy;
   import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.util.ConstructorHelper;
   
  +import java.lang.reflect.Constructor;
   import java.util.Map;
   import java.util.Collection;
   import java.util.Iterator;
  @@ -81,160 +83,158 @@
    */
   public final class MetadataObjectCopyStrategy implements ObjectCopyStrategy
   {
  -	/**
  -	 * TODO: handle proxies appropriately
  -	 * Uses an IdentityMap to make sure we don't recurse infinitely on the same object in
a cyclic object model.
  -	 * Proxies
  -	 * @param obj
  -	 * @param broker
  -	 * @return
  -	 */
  -	public Object copy(final Object obj, final PersistenceBroker broker)
  -	{
  -		return clone(obj, new IdentityHashMap(), broker);
  -	}
  -
  -	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.
  -		 * if the toCopy is in the objMap, just return it.
  -		 */
  -		if (objMap.containsKey(toCopy)) return objMap.get(toCopy);
  -		/**
  -		 * if null, return null, duh
  -		 */
  -		if (toCopy == null)
  -			return null;
  -		/**
  -		 * if no classdescriptor exists for this object, just return this object, we
  -		 * can't copy it.
  -		 */
  -		final ClassDescriptor cld = broker.getDescriptorRepository().getDescriptorFor(toCopy.getClass());
  -		if (cld == null)
  -			return toCopy;
  -
  -		Object retval;
  -		try
  -		{
  -			retval = toCopy.getClass().newInstance();
  -			objMap.put(toCopy,retval);
  -		}
  -		catch (InstantiationException e)
  -		{
  -			throw new ObjectCopyException("InstantiationException", e);
  -		}
  -		catch (IllegalAccessException e)
  -		{
  -			throw new ObjectCopyException("IllegalAccessException", e);
  -		}
  -		/**
  -		 * first copy all the fields
  -		 * fields are not mapped objects (ie ObjectReferenceDescriptors)
  -		 */
  -		final FieldDescriptor[] fieldDescs = cld.getFieldDescriptions();
  -		for (int i = 0; i < fieldDescs.length; i++)
  -		{
  -			final FieldDescriptor fd = fieldDescs[i];
  -			final PersistentField f = fd.getPersistentField();
  -			/**
  -			 * somewhat confusing, use the same field descriptor to set the
  -			 * value on the copy as the field descriptor used to get the value from the
  -			 * object to be copied.
  -			 */
  -			f.set(retval, f.get(toCopy));
  -		}
  -
  -		/**
  -		 * then copy all the 1:1 references
  -		 */
  -		final Collection refDescsCol = cld.getObjectReferenceDescriptors();
  -		final ObjectReferenceDescriptor[] rds = (ObjectReferenceDescriptor[]) refDescsCol.toArray(new
ObjectReferenceDescriptor[refDescsCol.size()]);
  -		for (int i = 0; i < rds.length; i++)
  -		{
  -			final ObjectReferenceDescriptor rd = rds[i];
  -			final PersistentField f = rd.getPersistentField();
  -			/**
  -			 * recursively copy the referenced objects
  -			 * register in the objMap first
  -			 */
  -			Object object = f.get(toCopy);
  -			/**
  -			 * if this is a proxy, just copy the proxy, don't materialize it, and stop recursing
  -			 */
  -			if (object instanceof Proxy)
  -			{
  -				f.set(retval, object);
  -			}
  -			else
  -			{
  -				Object clone = clone(object, objMap, broker);
  -				objMap.put(object, clone);
  -				f.set(retval, clone);
  -			}
  -		}
  -		/**
  -		 * then copy all the 1:M and M:N references
  -		 */
  -		final Collection colDescsCol = cld.getCollectionDescriptors();
  -		final Iterator it = colDescsCol.iterator();
  -		while (it.hasNext())
  -		{
  -			final CollectionDescriptor cd = (CollectionDescriptor) it.next();
  -			final PersistentField f = cd.getPersistentField();
  -			final Object collection = f.get(toCopy);
  -			/**
  -			 * handle collection proxies where the entire Collection is a big proxy
  -			 * (vs all the elements in the collection are proxies
  -			 */
  -			if (collection == null)
  -			{
  -				f.set(retval, null);
  -			}
  -			else if (collection instanceof CollectionProxy)
  -			{
  -				f.set(retval, collection);
  -			}
  -			else if (collection instanceof Collection)
  -			{
  -				try
  -				{
  -					final Collection newCollection = (Collection) collection.getClass().newInstance();
  -					final Iterator tempIter = ((Collection) collection).iterator();
  -					Object obj = null;
  -					while (tempIter.hasNext())
  -					{
  -						obj = tempIter.next();
  -						/**
  -						 * if this is a proxy, just copy the proxy, don't materialize it, and stop recursing
  -						 */
  -						if (obj instanceof Proxy)
  -						{
  -							newCollection.add(obj);
  -						}
  -						else
  -						{
  -							Object clone = clone(obj, objMap, broker);
  -							objMap.put(obj, clone);
  -							newCollection.add(clone);
  -						}
  -					}
  -					f.set(retval, newCollection);
  -				}
  -				catch (InstantiationException e)
  -				{
  -					throw new ObjectCopyException("InstantiationException", e);
  -				}
  -				catch (IllegalAccessException e)
  -				{
  -					throw new ObjectCopyException("IllegalAccessException", e);
  -				}
  -			}
  -			else
  -			{
  -				throw new java.lang.UnsupportedOperationException("MetadataObjectCopyStrategy cannot
handle Collection of type: " + collection.getClass().getName());
  -			}
  -		}
  -		return retval;
  -	}
  +    /**
  +     * TODO: handle proxies appropriately
  +     * Uses an IdentityMap to make sure we don't recurse infinitely on the same object
in a cyclic object model.
  +     * Proxies
  +     * @param obj
  +     * @param broker
  +     * @return
  +     */
  +    public Object copy(final Object obj, final PersistenceBroker broker)
  +    {
  +        return clone(obj, new IdentityHashMap(), broker);
  +    }
  +
  +    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.
  +         * if the toCopy is in the objMap, just return it.
  +         */
  +        if (objMap.containsKey(toCopy)) return objMap.get(toCopy);
  +        /**
  +         * if null, return null, duh
  +         */
  +        if (toCopy == null)
  +            return null;
  +        /**
  +         * if no classdescriptor exists for this object, just return this object, we
  +         * can't copy it.
  +         */
  +        final ClassDescriptor cld = broker.getDescriptorRepository().getDescriptorFor(toCopy.getClass());
  +        if (cld == null)
  +            return toCopy;
  +
  +        Object retval;
  +        try
  +        {
  +            Constructor con = cld.getZeroArgumentConstructor();
  +            retval = ConstructorHelper.instantiate(con);
  +            objMap.put(toCopy,retval);
  +        }
  +        catch (InstantiationException e)
  +        {
  +            throw new ObjectCopyException("InstantiationException", e);
  +        }
  +        
  +        /**
  +         * first copy all the fields
  +         * fields are not mapped objects (ie ObjectReferenceDescriptors)
  +         */
  +        final FieldDescriptor[] fieldDescs = cld.getFieldDescriptions();
  +        for (int i = 0; i < fieldDescs.length; i++)
  +        {
  +            final FieldDescriptor fd = fieldDescs[i];
  +            final PersistentField f = fd.getPersistentField();
  +            /**
  +            * somewhat confusing, use the same field descriptor to set the
  +            * value on the copy as the field descriptor used to get the value from the
  +            * object to be copied.
  +            */
  +            f.set(retval, f.get(toCopy));
  +        }
  +
  +        /**
  +         * then copy all the 1:1 references
  +         */
  +        final Collection refDescsCol = cld.getObjectReferenceDescriptors();
  +        final ObjectReferenceDescriptor[] rds = (ObjectReferenceDescriptor[]) refDescsCol.toArray(new
ObjectReferenceDescriptor[refDescsCol.size()]);
  +        for (int i = 0; i < rds.length; i++)
  +        {
  +            final ObjectReferenceDescriptor rd = rds[i];
  +            final PersistentField f = rd.getPersistentField();
  +            /**
  +             * recursively copy the referenced objects
  +             * register in the objMap first
  +             */
  +            Object object = f.get(toCopy);
  +            /**
  +             * if this is a proxy, just copy the proxy, don't materialize it, and stop
recursing
  +             */
  +            if (object instanceof Proxy)
  +            {
  +                f.set(retval, object);
  +            }
  +            else
  +            {
  +                Object clone = clone(object, objMap, broker);
  +                objMap.put(object, clone);
  +                f.set(retval, clone);
  +            }
  +        }
  +        /**
  +         * then copy all the 1:M and M:N references
  +         */
  +        final Collection colDescsCol = cld.getCollectionDescriptors();
  +        final Iterator it = colDescsCol.iterator();
  +        while (it.hasNext())
  +        {
  +            final CollectionDescriptor cd = (CollectionDescriptor) it.next();
  +            final PersistentField f = cd.getPersistentField();
  +            final Object collection = f.get(toCopy);
  +            /**
  +             * handle collection proxies where the entire Collection is a big proxy
  +             * (vs all the elements in the collection are proxies
  +             */
  +            if (collection == null)
  +            {
  +                f.set(retval, null);
  +            }
  +            else if (collection instanceof CollectionProxy)
  +            {
  +                f.set(retval, collection);
  +            }
  +            else if (collection instanceof Collection)
  +            {
  +                try
  +                {
  +                    final Collection newCollection = (Collection) collection.getClass().newInstance();
  +                    final Iterator tempIter = ((Collection) collection).iterator();
  +                    Object obj = null;
  +                    while (tempIter.hasNext())
  +                    {
  +                        obj = tempIter.next();
  +                        /**
  +                        * if this is a proxy, just copy the proxy, don't materialize it,
and stop recursing
  +                        */
  +                        if (obj instanceof Proxy)
  +                        {
  +                            newCollection.add(obj);
  +                        }
  +                        else
  +                        {
  +                            Object clone = clone(obj, objMap, broker);
  +                            objMap.put(obj, clone);
  +                            newCollection.add(clone);
  +                        }
  +                    }
  +                    f.set(retval, newCollection);
  +                }
  +                catch (InstantiationException e)
  +                {
  +                    throw new ObjectCopyException("InstantiationException", e);
  +                }
  +                catch (IllegalAccessException e)
  +                {
  +                    throw new ObjectCopyException("IllegalAccessException", e);
  +                }
  +            }
  +            else
  +            {
  +                throw new java.lang.UnsupportedOperationException("MetadataObjectCopyStrategy
cannot handle Collection of type: " + collection.getClass().getName());
  +            }
  +        }
  +        return retval;
  +    }
   }
  
  
  

---------------------------------------------------------------------
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