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/jdori/org/apache/ojb/jdori/sql OjbStorePMF.java OjbExtent.java OjbFieldManager.java OjbStoreManager.java
Date Fri, 05 Dec 2003 22:45:40 GMT
thma        2003/12/05 14:45:40

  Modified:    src/jdori/org/apache/ojb/jdori/sql OjbStorePMF.java
                        OjbExtent.java OjbFieldManager.java
                        OjbStoreManager.java
  Log:
  Fixed the Problem of missing StateManagers in Extent based queries.
  Thanks to Gus for investigation and putting some pressure on me.
  
  Revision  Changes    Path
  1.3       +6 -0      db-ojb/src/jdori/org/apache/ojb/jdori/sql/OjbStorePMF.java
  
  Index: OjbStorePMF.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/jdori/org/apache/ojb/jdori/sql/OjbStorePMF.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- OjbStorePMF.java	9 Jun 2003 16:12:25 -0000	1.2
  +++ OjbStorePMF.java	5 Dec 2003 22:45:40 -0000	1.3
  @@ -77,7 +77,13 @@
       private final HashMap storeManagers = new HashMap();
   
       private static HashMap trackedClassesMap = getTrackedClasses();
  +    
  +    private static final OjbStorePMF INSTANCE = new OjbStorePMF();
   
  +	public static OjbStorePMF getInstance()
  +	{
  +		return INSTANCE;
  +	}
   
       /**
        * Constructor for PMF.
  
  
  
  1.2       +61 -11    db-ojb/src/jdori/org/apache/ojb/jdori/sql/OjbExtent.java
  
  Index: OjbExtent.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/jdori/org/apache/ojb/jdori/sql/OjbExtent.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- OjbExtent.java	24 Dec 2002 12:06:01 -0000	1.1
  +++ OjbExtent.java	5 Dec 2003 22:45:40 -0000	1.2
  @@ -53,37 +53,52 @@
    * <http://www.apache.org/>.
    */
   
  +import java.util.ArrayList;
  +import java.util.Collection;
   import java.util.Iterator;
   
   import javax.jdo.PersistenceManager;
  +import javax.jdo.spi.PersistenceCapable;
   
  +import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.accesslayer.RsIterator;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryFactory;
   
  +import com.sun.jdori.FieldManager;
  +import com.sun.jdori.PersistenceManagerInternal;
  +import com.sun.jdori.StateManagerInternal;
  +import com.sun.jdori.model.jdo.JDOClass;
  +
   /**
    * @see javax.jdo.Extent
    * @author Thomas Mahler
    */
   public class OjbExtent implements javax.jdo.Extent
   {
  -    private RsIterator iterator;
  +    private Collection extentCollection;
       private Class clazz;
       private PersistenceBroker broker;
  +    private PersistenceManagerInternal pmi;
   
       /**
        * Constructor for OjbExtent.
        */
  -    public OjbExtent(Class pClazz, PersistenceBroker pBroker)
  +    public OjbExtent(Class pClazz, PersistenceBroker pBroker, PersistenceManagerInternal
pPmi)
       {
  -        clazz = pClazz;
  +    	clazz = pClazz;
  +    	broker = pBroker;
  +    	pmi = pPmi;
           Criteria selectExtent = null;
           Query q = QueryFactory.newQuery(clazz, selectExtent);
  -        broker = pBroker;
  -        iterator = (RsIterator) broker.getIteratorByQuery(q);
  +        
  +        // the PB loads plain java objects
  +        Collection pojoInstances = broker.getCollectionByQuery(q);
  +        // To bring these instances under JDO management, 
  +        // each instance must be provided with its own StateManager
  +        extentCollection = provideStateManagers(pojoInstances);
       }
   
       /**
  @@ -91,7 +106,7 @@
        */
       public Iterator iterator()
       {
  -        return iterator;
  +        return extentCollection.iterator();
       }
   
       /**
  @@ -116,7 +131,7 @@
        */
       public PersistenceManager getPersistenceManager()
       {
  -        return null;
  +    	return pmi.getCurrentWrapper();
       }
   
       /**
  @@ -124,15 +139,50 @@
        */
       public void closeAll()
       {
  -        iterator.releaseDbResources();
  +        // noop
       }
   
       /**
        * @see javax.jdo.Extent#close(Iterator)
        */
  -    public void close(Iterator it)
  +    public void close(Iterator pIterator)
       {
  -        iterator.releaseDbResources();
  +        // noop
       }
  +    
  +    /**
  +     * This methods enhances the objects loaded by a broker query
  +     * with a JDO StateManager an brings them under JDO control.
  +     * @param pojos the OJB pojos as obtained by the broker
  +     * @return the collection of JDO PersistenceCapable instances
  +     */
  +    protected Collection provideStateManagers(Collection pojos)
  +    {
  +    	PersistenceCapable pc;
  +    	int [] fieldNums;
  +    	Iterator iter = pojos.iterator();
  +    	Collection result = new ArrayList();
  +    	
  +    	while (iter.hasNext())
  +    	{
  +    		// obtain a StateManager
  +    		pc = (PersistenceCapable) iter.next();
  +    		Identity oid = new Identity(pc, broker);
  +    		StateManagerInternal smi = pmi.getStateManager(oid, pc.getClass()); 
  +    		
  +    		// fetch attributes into StateManager
  +			JDOClass jdoClass = Helper.getJDOClass(pc.getClass());
  +			fieldNums = jdoClass.getManagedFieldNumbers();
  +
  +			FieldManager fm = new OjbFieldManager(pc, broker);
  +			smi.replaceFields(fieldNums, fm);
  +			smi.retrieve();
  +			
  +			// get JDO PersistencecCapable instance from SM and add it to result collection
  +			Object instance = smi.getObject();
  +			result.add(instance);
  +    	}
  +    	return result;   
  +	}
   
   }
  
  
  
  1.5       +3 -27     db-ojb/src/jdori/org/apache/ojb/jdori/sql/OjbFieldManager.java
  
  Index: OjbFieldManager.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/jdori/org/apache/ojb/jdori/sql/OjbFieldManager.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- OjbFieldManager.java	1 Dec 2003 21:31:21 -0000	1.4
  +++ OjbFieldManager.java	5 Dec 2003 22:45:40 -0000	1.5
  @@ -53,7 +53,6 @@
    * <http://www.apache.org/>.
    */
   
  -import javax.jdo.JDOFatalInternalException;
   import javax.jdo.spi.PersistenceCapable;
   
   import org.apache.ojb.broker.PersistenceBroker;
  @@ -61,13 +60,8 @@
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   
   import com.sun.jdori.FieldManager;
  -import com.sun.jdori.common.model.jdo.JDOModelFactoryImpl;
  -import com.sun.jdori.common.model.runtime.RuntimeJavaModelFactory;
  -import com.sun.jdori.model.java.JavaModel;
  -import com.sun.jdori.model.java.JavaModelFactory;
   import com.sun.jdori.model.jdo.JDOClass;
   import com.sun.jdori.model.jdo.JDOField;
  -import com.sun.jdori.model.jdo.JDOModel;
   
   /**
    * @author Thomas Mahler
  @@ -282,27 +276,9 @@
           this.pc = pc;
       }
   
  -    JDOClass getJDOClass(Class c)
  -    {
  -        JDOClass rc = null;
  -        try
  -        {
  -            ClassLoader cl = c.getClassLoader();
  -			JavaModelFactory javaModelFactory = RuntimeJavaModelFactory.getInstance();
  -			JavaModel javaModel = javaModelFactory.getJavaModel(c.getClassLoader());
  -            JDOModel m = JDOModelFactoryImpl.getInstance().getJDOModel(javaModel);
  -            rc = m.getJDOClass(c.getName());
  -        }
  -        catch (RuntimeException ex)
  -        {
  -            throw new JDOFatalInternalException("Not a JDO class: " + c.getName());
  -        }
  -        return rc;
  -    }
  -
       String getAttributeName(int fieldNum)
       {
  -        JDOClass jdoClass = getJDOClass(pc.getClass());
  +        JDOClass jdoClass = Helper.getJDOClass(pc.getClass());
           JDOField jdoField = jdoClass.getField(fieldNum);
           String attributeName = jdoField.getName();
           return attributeName;
  @@ -317,9 +293,9 @@
           String attributeName = getAttributeName(fieldNum);
           ClassDescriptor cld = broker.getClassDescriptor(pc.getClass());
           
  -        // field could a primitive typed attribute...
  +        // field could be a primitive typed attribute...
           AttributeDescriptorBase fld = cld.getFieldDescriptorByName(attributeName);
  -        // field could a reference attribute...
  +        // field could be a reference attribute...
           if (fld == null) 
           {
   			fld = cld.getObjectReferenceDescriptorByName(attributeName);
  
  
  
  1.6       +5 -50     db-ojb/src/jdori/org/apache/ojb/jdori/sql/OjbStoreManager.java
  
  Index: OjbStoreManager.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/jdori/org/apache/ojb/jdori/sql/OjbStoreManager.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- OjbStoreManager.java	1 Dec 2003 21:31:21 -0000	1.5
  +++ OjbStoreManager.java	5 Dec 2003 22:45:40 -0000	1.6
  @@ -53,7 +53,6 @@
    * <http://www.apache.org/>.
    */
   
  -import java.lang.reflect.Field;
   import java.util.BitSet;
   import java.util.Iterator;
   
  @@ -73,13 +72,8 @@
   import com.sun.jdori.PersistenceManagerInternal;
   import com.sun.jdori.StateManagerInternal;
   import com.sun.jdori.StoreManager;
  -import com.sun.jdori.common.model.jdo.JDOModelFactoryImpl;
  -import com.sun.jdori.common.model.runtime.RuntimeJavaModelFactory;
   import com.sun.jdori.common.query.BasicQueryResult;
  -import com.sun.jdori.model.java.JavaModel;
  -import com.sun.jdori.model.java.JavaModelFactory;
   import com.sun.jdori.model.jdo.JDOClass;
  -import com.sun.jdori.model.jdo.JDOModel;
   import com.sun.jdori.query.QueryResult;
   import com.sun.jdori.query.QueryResultHelper;
   
  @@ -224,10 +218,10 @@
               {                
                   oid = new Identity(instance,broker);
               }
  -            broker. removeFromCache(instance);
  +            broker.removeFromCache(instance);
               PersistenceCapable pc = (PersistenceCapable) broker.getObjectByIdentity(oid);
   
  -            JDOClass jdoClass = this.getJDOClass(pc.getClass());
  +            JDOClass jdoClass = Helper.getJDOClass(pc.getClass());
               if (fieldNums == null)
               {
                   fieldNums = jdoClass.getManagedFieldNumbers();
  @@ -245,24 +239,6 @@
           }
       }
   
  -    JDOClass getJDOClass(Class c)
  -    {
  -        JDOClass rc = null;
  -        try
  -        {
  -			ClassLoader cl = c.getClassLoader();
  -			JavaModelFactory javaModelFactory = RuntimeJavaModelFactory.getInstance();
  -			JavaModel javaModel = javaModelFactory.getJavaModel(c.getClassLoader());
  -			JDOModel m = JDOModelFactoryImpl.getInstance().getJDOModel(javaModel);
  -			rc = m.getJDOClass(c.getName());
  -        }
  -        catch (RuntimeException ex)
  -        {
  -            throw new JDOFatalInternalException("Not a JDO class: " + c.getName()); 
  -        }
  -        return rc;
  -    }
  -
       /**
        * @see com.sun.jdori.StoreManager#getExtent
        */
  @@ -272,7 +248,7 @@
           PersistenceManagerInternal pm)
       {
           PersistenceBroker broker = connector.getBroker();
  -        return new OjbExtent(pcClass, broker);
  +        return new OjbExtent(pcClass, broker, pm);
       }
   
       /**
  @@ -361,7 +337,7 @@
           while (it.hasNext())
           {
               StateManagerInternal sm = (StateManagerInternal) it.next();
  -            logger.debug("OjbStoreManager.flush: " + sm.getInternalObjectId() + ", " +
getLCState(sm));
  +            logger.debug("OjbStoreManager.flush: " + sm.getInternalObjectId() + ", " +
Helper.getLCState(sm));
               sm.preStore();
               sm.replaceSCOFields();
               sm.flush(this);
  @@ -380,28 +356,7 @@
           }
       }
   
  -	/**
  -	 * @return the LifeCycleState of a StateManager instance
  -	 */
  -	protected Object getLCState(StateManagerInternal sm)
  -	{
  -		// unfortunately the LifeCycleState classes ar package private.
  -		// so we have to do some dirty reflection hack to access them
  -        try
  -        {
  -            Field myLC = sm.getClass().getDeclaredField("myLC");
  -            myLC.setAccessible(true);
  -            return myLC.get(sm);
  -        }
  -        catch (NoSuchFieldException e)
  -        {
  -        	return e;
  -        }
  -        catch (IllegalAccessException e)
  -        {
  -        	return e;
  -        }	
  -	}
  +
   
       /**
        * @see com.sun.jdori.StoreManager#newObjectIdInstance
  
  
  

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