db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/odmg LockingTest.java
Date Wed, 26 Mar 2003 10:37:58 GMT
thma        2003/03/26 02:37:57

  Modified:    src/java/org/apache/ojb/broker/metadata ClassDescriptor.java
                        FieldDescriptor.java AttributeDescriptorBase.java
               src/test/org/apache/ojb/odmg LockingTest.java
  Added:       src/java/org/apache/ojb/broker/metadata/fieldaccess
                        AnonymousPersistentFieldHelper.java
                        AnonymousPersistentField.java
                        AnonymousPersistentFieldForInheritance.java
               src/java/org/apache/ojb/broker/metadata
                        AnonymousFieldDescriptor.java
                        AnonymousObjectReferenceDescriptor.java
  Log:
  add Tine Houaris support for anonymous fields (RepositoryXmlHandler not yet changed!)
  
  Revision  Changes    Path
  1.1                  db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/AnonymousPersistentFieldHelper.java
  
  Index: AnonymousPersistentFieldHelper.java
  ===================================================================
  package org.apache.ojb.broker.metadata.fieldaccess;
  
  import java.lang.reflect.Field;
  import java.util.Iterator;
  import java.util.Map;
  import java.util.Vector;
  
  import org.apache.ojb.broker.metadata.AnonymousFieldDescriptor;
  import org.apache.ojb.broker.metadata.ClassDescriptor;
  import org.apache.ojb.broker.metadata.CollectionDescriptor;
  import org.apache.ojb.broker.metadata.DescriptorRepository;
  import org.apache.ojb.broker.metadata.FieldDescriptor;
  import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
  
  public class AnonymousPersistentFieldHelper {
  
  	/**
  	 * An iterative version of org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldxxx
  	 * This implementastion is more efficient than recursive version
  	 * If fieldname not exist null is returned
  	 *
  	 * @author Houar TINE
  	 */
  	public static Field getFieldIterative(
  		final Class c,
  		final String fieldname) {
  			
  		Field f = null;
  		Class clazz = c;
  		do {
  			try {
  				f = clazz.getDeclaredField(fieldname);
  				break;
  			}
  			catch (NoSuchFieldException e) {
  				// ignore
  			}
  			clazz = clazz.getSuperclass();
  		}
  		while (clazz != Object.class);
  		return f;
  	}
  
  	public static Vector getPkFieldNames(final ClassDescriptor cld) {
  		FieldDescriptor[] pks = cld.getPkFields();
  		Vector v = new Vector(pks.length);
  		for (int i = pks.length - 1; i >= 0; i--) {
  			v.add(pks[i].getPersistentField().getName());
  		}
  		return v;
  	}
  
  	public static void computeInheritedPersistentFields(final DescriptorRepository repository)
{
  		Map descriptorTable = repository.getDescriptorTable();
  		final String OBJECT = Object.class.getName();
  		for (Iterator iter = descriptorTable.keySet().iterator();
  			iter.hasNext();
  			) {
  			String key = (String) iter.next();
  			ClassDescriptor cld = (ClassDescriptor) descriptorTable.get(key);
  
  			String baseClass = cld.getBaseClass();
  			if (null != baseClass) {
  				Vector v = new Vector();
  				do {
  					ClassDescriptor tempCld =
  						repository.getDescriptorFor(baseClass);
  					FieldDescriptor[] fields = tempCld.getFieldDescriptions();
  					Vector pkFields = getPkFieldNames(tempCld);
  					for (int i = fields.length - 1; i >= 0; i--) {
  						FieldDescriptor f = fields[i];
  						if (!(f instanceof AnonymousFieldDescriptor)) {
  							PersistentField pf = f.getPersistentField();
  							if (!pkFields.contains(pf.getName())) {
  								v.add(f.getPersistentField());
  							}
  						}
  					}
  					Vector ords = tempCld.getObjectReferenceDescriptors();
  					for (int i = ords.size() - 1; i >= 0; i--) {
  						ObjectReferenceDescriptor ord =
  							(ObjectReferenceDescriptor) ords.get(i);
  						PersistentField pf = ord.getPersistentField();
  						if (!(pf
  							instanceof AnonymousPersistentFieldForInheritance)) {
  							v.add(pf);
  						}
  					}
  					Vector cds = tempCld.getCollectionDescriptors();
  					for (int i = cds.size() - 1; i >= 0; i--) {
  						CollectionDescriptor cd =
  							(CollectionDescriptor) cds.get(i);
  						v.add(cd.getPersistentField());
  					}
  					baseClass = tempCld.getBaseClass();
  				}
  				while ((null != baseClass) && (baseClass != OBJECT));
  				cld.setSuperPersistentFieldDescriptors(v);
  			}
  		}
  	}
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/AnonymousPersistentField.java
  
  Index: AnonymousPersistentField.java
  ===================================================================
  package org.apache.ojb.broker.metadata.fieldaccess;
  
  import java.util.Map;
  import java.util.WeakHashMap;
  import org.apache.ojb.broker.metadata.MetadataException;
  
  /**
   * This class handle an anonymous persistent fiels for 1-1 association, 
   * and ojbConcreteClass
   * @author Houar TINE
   */
  
  public class AnonymousPersistentField implements PersistentField {
  	protected Map fkCache = new WeakHashMap();
  	protected String fieldname;
  
  	public AnonymousPersistentField(String fieldname) {
  		this.fieldname = fieldname;
  	}
  
  	public synchronized void set(Object obj, Object value)
  		throws MetadataException {
  		fkCache.put(obj, value);
  	}
  
  	public synchronized Object get(Object anObject) throws MetadataException {
  		return fkCache.get(anObject);
  	}
  	/**
  	 * @see PersistentField#getDeclaringClass()
  	 */
  	public Class getDeclaringClass() {
  		return null;
  	}
  
  	/**
  	 * @see PersistentField#getName()
  	 */
  	public String getName() {
  		return fieldname;
  	}
  
  	/**
  	 * @see PersistentField#getType()
  	 */
  	public Class getType() {
  		return null;
  	}
  
  	/**
  	 * @see PersistentField#usesAccessorsAndMutators()
  	 */
  	public boolean usesAccessorsAndMutators() {
  		return false;
  	}
  }
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/metadata/fieldaccess/AnonymousPersistentFieldForInheritance.java
  
  Index: AnonymousPersistentFieldForInheritance.java
  ===================================================================
  package org.apache.ojb.broker.metadata.fieldaccess;
  
  import java.util.Vector;
  
  import org.apache.ojb.broker.metadata.ClassDescriptor;
  import org.apache.ojb.broker.metadata.MetadataException;
  
  public class AnonymousPersistentFieldForInheritance
  	extends AnonymousPersistentField {
  	ClassDescriptor cld = null;
  
  	public AnonymousPersistentFieldForInheritance(ClassDescriptor cld) {
  		super(null);
  		this.cld = cld;
  	}
  
  	private void copyObjectToObject(Object fromObject, Object toObject) {
  		Vector persistentFields = cld.getSuperPersistentFieldDescriptors();
  		for (int i = persistentFields.size() - 1; i >= 0; i--) {
  			PersistentField pf = (PersistentField) persistentFields.get(i);
  			pf.set(toObject, pf.get(fromObject));
  		}
  	}
  
  	/** 
  	 * Field values of 'value' (base object) are copied to 'obj' (derived object)
  	 * then obj is saved in a map
  	 * 
  	 * @param obj - the base object instance
  	 * @param value - the derived object instance
  	 * @throws MetadataException
  	 * @author Houar TINE
  	 */
  	public synchronized void set(Object obj, Object value)
  		throws MetadataException {
  		copyObjectToObject(value, obj);
  		fkCache.put(obj, value);
  	}
  
  	/** 
  	 * Field values of 'obj' (derived object) are copied to 'value' (base object)
  	 * then value is returned as a referenced object.
  	 * 
  	 * @param obj - the base object instance
  	 * @param value - the derived object instance
  	 * @throws MetadataException
  	 * @author Houar TINE
  	 */
  	public synchronized Object get(Object obj) throws MetadataException {
  		Object value = fkCache.get(obj);
  		if (null == value) {
  			try {
  				value = Class.forName(cld.getBaseClass()).newInstance();	
  			}
  			catch (Exception e) {
  				throw new MetadataException(e);
  			}
  			fkCache.put(obj, value);
  		} else {
  			copyObjectToObject(obj, value);
  		}		
  		return value;
  	}
  }
  
  
  1.51      +20 -4     db-ojb/src/java/org/apache/ojb/broker/metadata/ClassDescriptor.java
  
  Index: ClassDescriptor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/ClassDescriptor.java,v
  retrieving revision 1.50
  retrieving revision 1.51
  diff -u -r1.50 -r1.51
  --- ClassDescriptor.java	16 Mar 2003 12:03:45 -0000	1.50
  +++ ClassDescriptor.java	26 Mar 2003 10:37:55 -0000	1.51
  @@ -101,6 +101,24 @@
    */
   public final class ClassDescriptor extends DescriptorBase implements Serializable, XmlCapable,
IsolationLevels
   {
  +	// Modifications by Houar TINE
  +	private String baseClass = null;
  +	public String getBaseClass() {
  +		return baseClass;
  +	}	
  +	public void setBaseClass(String baseClass) {
  +		this.baseClass = baseClass;
  +	}
  +	
  +	private Vector superPersistentFieldDescriptors = null;
  +	public void setSuperPersistentFieldDescriptors(Vector superPersistentFieldDescriptors)
{
  +		this.superPersistentFieldDescriptors = superPersistentFieldDescriptors;
  +	}
  +	public Vector getSuperPersistentFieldDescriptors() {
  +		return superPersistentFieldDescriptors;
  +	}
  +	// Modifications by Houar TINE
  +	
       public static final String OJB_CONCRETE_CLASS = "ojbConcreteClass";
   
       private DescriptorRepository m_repository;
  @@ -631,14 +649,13 @@
        */
       public FieldDescriptor[] getFieldDescriptorsInHeirarchy()
       {
  -        if (superClass == null)
  +		if (superClass == null)
           {
               return getFieldDescriptions();
           }
           ClassDescriptor cldSuper = getRepository().getDescriptorFor(superClass);
           return appendFieldDescriptorArrays(getFieldDescriptions(), cldSuper.getFieldDescriptorsInHeirarchy());
       }
  -
       private FieldDescriptor[] appendFieldDescriptorArrays(FieldDescriptor[] fieldDescriptions,
FieldDescriptor[] fieldDescriptorsInHeirarchy)
       {
           // take the 2 arrays and add them into one
  @@ -1165,7 +1182,6 @@
           {
               result += "    " + tags.getAttribute(INITIALIZATION_METHOD, this.getInitializationMethod().getName())
+ eol;
           }
  -
   
           result += "  >" + eol;
   
  
  
  
  1.17      +1 -1      db-ojb/src/java/org/apache/ojb/broker/metadata/FieldDescriptor.java
  
  Index: FieldDescriptor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/FieldDescriptor.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- FieldDescriptor.java	2 Mar 2003 17:32:04 -0000	1.16
  +++ FieldDescriptor.java	26 Mar 2003 10:37:56 -0000	1.17
  @@ -74,7 +74,7 @@
    *
    * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
    */
  -public final class FieldDescriptor extends AttributeDescriptorBase implements XmlCapable
  +public class FieldDescriptor extends AttributeDescriptorBase implements XmlCapable
   {
       private int m_ColNo;
       private String m_ColumnName;
  
  
  
  1.10      +3 -3      db-ojb/src/java/org/apache/ojb/broker/metadata/AttributeDescriptorBase.java
  
  Index: AttributeDescriptorBase.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/AttributeDescriptorBase.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- AttributeDescriptorBase.java	24 Dec 2002 13:14:34 -0000	1.9
  +++ AttributeDescriptorBase.java	26 Mar 2003 10:37:56 -0000	1.10
  @@ -68,8 +68,8 @@
    */
   public class AttributeDescriptorBase extends DescriptorBase implements Serializable
   {
  -    private PersistentField m_PersistentField = null;
  -    private ClassDescriptor m_ClassDescriptor = null;
  +    protected PersistentField m_PersistentField = null;
  +    protected ClassDescriptor m_ClassDescriptor = null;
   
       /**
        * Constructor declaration
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/metadata/AnonymousFieldDescriptor.java
  
  Index: AnonymousFieldDescriptor.java
  ===================================================================
  package org.apache.ojb.broker.metadata;
  
  import org.apache.ojb.broker.metadata.fieldaccess.AnonymousPersistentField;
  
  /**
   * This class model anonymous fiel descriptor
   * @author Houar TINE
   */
  
  public final class AnonymousFieldDescriptor extends FieldDescriptor {
      public AnonymousFieldDescriptor(ClassDescriptor cld, int id)
      {
          super(cld, id);
      }
      
      public void setPersistentField(Class c, String fieldName)
      {
          m_PersistentField = new AnonymousPersistentField(fieldName);
      }
  }
  
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/metadata/AnonymousObjectReferenceDescriptor.java
  
  Index: AnonymousObjectReferenceDescriptor.java
  ===================================================================
  package org.apache.ojb.broker.metadata;
  
  import org.apache.ojb.broker.metadata.fieldaccess.AnonymousPersistentFieldForInheritance;
  
  /**
   * This class handle an anonymous persistent fiels for inheritance as 1-1 association
   * @author Houar TINE
   */
  
  public class AnonymousObjectReferenceDescriptor extends ObjectReferenceDescriptor {
  	
  	public AnonymousObjectReferenceDescriptor(ClassDescriptor descriptor) 
  	{
  		super(descriptor);
  	}
  
  	public void setPersistentField(Class c, String fieldname)
      {
          m_PersistentField = new AnonymousPersistentFieldForInheritance(m_ClassDescriptor);
      }
  }
  
  
  
  
  1.8       +6 -4      db-ojb/src/test/org/apache/ojb/odmg/LockingTest.java
  
  Index: LockingTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/LockingTest.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- LockingTest.java	19 Mar 2003 06:52:21 -0000	1.7
  +++ LockingTest.java	26 Mar 2003 10:37:57 -0000	1.8
  @@ -13,14 +13,14 @@
   import org.apache.ojb.odmg.locking.LockManagerFactory;
   import org.odmg.Database;
   import org.odmg.Implementation;
  +import org.odmg.LockNotGrantedException;
   import org.odmg.OQLQuery;
   import org.odmg.Transaction;
  -import org.odmg.TransactionAbortedException;
   
   import java.util.List;
   
  -/** Demo Application that shows basic concepts for Applications using the OJB ODMG
  - * implementation as an transactional object server.
  +/** 
  + * Test optimistic and pessimistic locking mechanisms
    */
   public class LockingTest extends TestCase
   {
  @@ -137,7 +137,9 @@
   		{
   			tx2.commit();
   		}
  -		catch (TransactionAbortedException ex)
  +		// OL exceptions should be signalled as ODMG LockNotGrantedExceptions
  +		// so that users can react accordingly
  +		catch (LockNotGrantedException ex)
   		{
   			signalOLException = true;	
   		}
  
  
  

Mime
View raw message