db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/otm/core BaseConnection.java
Date Mon, 23 Feb 2004 19:09:15 GMT
brj         2004/02/23 11:09:15

  Modified:    src/java/org/apache/ojb/otm/core BaseConnection.java
  Log:
  iterator innerclass implements OJBIterator
  
  Revision  Changes    Path
  1.29      +174 -138  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.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- BaseConnection.java	8 Feb 2004 13:19:47 -0000	1.28
  +++ BaseConnection.java	23 Feb 2004 19:09:15 -0000	1.29
  @@ -79,44 +79,45 @@
   import java.util.ArrayList;
   
   /**
  - *
  + * 
    * <javadoc>
  - *
  - * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
  - * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah</a>
  - *
  + * 
  + * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird </a>
  + * @author <a href="mailto:rraghuram@hotmail.com">Raghu Rajah </a>
  + * @version $Id$
  + *  
    */
  -public abstract class BaseConnection
  -    implements OTMConnection
  +public abstract class BaseConnection implements OTMConnection
   {
   
       private PersistenceBroker _pb;
       private Transaction _tx;
       private ConcreteEditingContext _editingContext;
  -	private Configurator m_configurator;
  +    private Configurator m_configurator;
   
       /**
        * Constructor for BaseConnection.
  -     *
  +     *  
        */
       public BaseConnection(PBKey pbKey)
       {
           _pb = PersistenceBrokerFactory.createPersistenceBroker(pbKey);
  -		m_configurator = PersistenceBrokerFactory.getConfigurator();
  -	}
  -    
  +        m_configurator = PersistenceBrokerFactory.getConfigurator();
  +    }
  +
       public void close()
       {
           _pb.close();
           _pb = null;
       }
   
  -	public boolean isClosed()
  -	{
  -		if (_pb == null)
  -			return true;
  -		else return _pb.isClosed();
  -	}
  +    public boolean isClosed()
  +    {
  +        if (_pb == null)
  +            return true;
  +        else
  +            return _pb.isClosed();
  +    }
   
       public PersistenceBroker getKernelBroker()
       {
  @@ -131,8 +132,7 @@
           }
           else if (_tx != null)
           {
  -            throw new IllegalStateException(
  -                    "OTMConnection is already bound to the transacaction "
  +            throw new IllegalStateException("OTMConnection is already bound to the transacaction
"
                       + _tx);
           }
           else
  @@ -154,11 +154,10 @@
       /**
        * @see org.apache.ojb.otm.OTMConnection#getObjectByIdentity(Identity, int)
        */
  -    public Object getObjectByIdentity(Identity oid, int lock)
  -        throws LockingException
  +    public Object getObjectByIdentity(Identity oid, int lock) throws LockingException
       {
  -		checkTransaction("getObjectByIdentity");
  -		Object object;
  +        checkTransaction("getObjectByIdentity");
  +        Object object;
           ObjectCopyStrategy copyStrategy;
   
           object = _editingContext.lookup(oid);
  @@ -168,8 +167,8 @@
               object = _pb.getObjectByIdentity(oid);
               if (object != null)
               {
  -                if ((lock != LockType.WRITE_LOCK)
  -                        || !_tx.getKit().isImplicitLockingUsed());
  +                if ((lock != LockType.WRITE_LOCK) || !_tx.getKit().isImplicitLockingUsed())
  +                    ;
                   {
                       copyStrategy = _tx.getKit().getCopyStrategy(oid);
                       object = copyStrategy.copy(object);
  @@ -184,31 +183,32 @@
           return object;
       }
   
  -	private void checkTransaction(String methodBeingCalled)
  -	{
  -		if (null==_tx)
  -		{
  -			throw new TransactionNotInProgressException(methodBeingCalled + " requires a valid transaction.
Please make sure you have created a new transaction, and called begin() on it.");
  -		}
  -		if (!_tx.isInProgress())
  -		{
  -			throw new TransactionNotInProgressException(methodBeingCalled + " cannot be called before
transaction begin() is called");
  -		}
  -	}
  +    private void checkTransaction(String methodBeingCalled)
  +    {
  +        if (null == _tx)
  +        {
  +            throw new TransactionNotInProgressException(
  +                    methodBeingCalled
  +                            + " requires a valid transaction. Please make sure you have
created a new transaction, and called begin() on it.");
  +        }
  +        if (!_tx.isInProgress())
  +        {
  +            throw new TransactionNotInProgressException(methodBeingCalled
  +                    + " cannot be called before transaction begin() is called");
  +        }
  +    }
   
  -	/**
  +    /**
        * @see org.apache.ojb.otm.OTMConnection#getObjectByIdentity(Identity)
        */
  -    public Object getObjectByIdentity(Identity oid)
  -        throws LockingException
  +    public Object getObjectByIdentity(Identity oid) throws LockingException
       {
           return getObjectByIdentity(oid, LockType.READ_LOCK);
       }
   
       /**
        * @param query The query to execute
  -     * @return an Iterator that iterates Objects.
  -     * The returned objects are locked for read.
  +     * @return an Iterator that iterates Objects. The returned objects are locked for read.
        */
       public Iterator getIteratorByQuery(Query query)
       {
  @@ -217,26 +217,25 @@
   
       /**
        * @param query The query to execute
  -     * @param lock the lock that need to be acquired on the object
  -     * Possible values are:
  -     * LockType.NO_LOCK (aka read only) - changes to the object will not be written to
database;
  -     * LockType.READ_LOCK (aka optimistic lock) - changes to the object will be written
to the database,
  -     * in this case the lock will be automatically upgraded to the write lock on transaction
commit;
  -     * LockType.WRITE_LOCK (aka pessimistic lock) - changes to the object will be written
to the database.
  -     * @return an Iterator that iterates Objects of class c if calling the .next()
  -     * method. The returned objects are locked with the given lock value.
  +     * @param lock the lock that need to be acquired on the object Possible values are:
  +     *            LockType.NO_LOCK (aka read only) - changes to the object will not be
written to
  +     *            database; LockType.READ_LOCK (aka optimistic lock) - changes to the object
will
  +     *            be written to the database, in this case the lock will be automatically
upgraded
  +     *            to the write lock on transaction commit; LockType.WRITE_LOCK (aka pessimistic
  +     *            lock) - changes to the object will be written to the database.
  +     * @return an Iterator that iterates Objects of class c if calling the .next() method.
The
  +     *         returned objects are locked with the given lock value.
        */
       public Iterator getIteratorByQuery(Query query, int lock)
       {
  -		checkTransaction("getIteratorByQuery");
  -        return new OTMIterator(_pb.getIteratorByQuery(query), lock, null);
  +        checkTransaction("getIteratorByQuery");
  +        return new OTMIterator((OJBIterator) _pb.getIteratorByQuery(query), lock, null);
       }
   
       /**
  -     * @param query The OQL query to execute. Use this method if you
  -     * don't want to load all the collection at once as OQLQuery.execute() does.
  -     * @return an Iterator that iterates Objects.
  -     * The returned objects are locked for read.
  +     * @param query The OQL query to execute. Use this method if you don't want to load
all the
  +     *            collection at once as OQLQuery.execute() does.
  +     * @return an Iterator that iterates Objects. The returned objects are locked for read.
        */
       public Iterator getIteratorByOQLQuery(OQLQuery query)
       {
  @@ -244,18 +243,17 @@
       }
   
       /**
  -     * @param query The OQL query to execute. Use this method if you
  -     * don't want to load all the collection at once as OQLQuery.execute() does.
  -     * @return an Iterator that iterates Objects.
  -     * The returned objects are locked for read.
  +     * @param query The OQL query to execute. Use this method if you don't want to load
all the
  +     *            collection at once as OQLQuery.execute() does.
  +     * @return an Iterator that iterates Objects. The returned objects are locked for read.
        */
       public Iterator getIteratorByOQLQuery(OQLQuery query, int lock)
       {
  -		checkTransaction("getIteratorByOQLQuery");
  +        checkTransaction("getIteratorByOQLQuery");
           if (query instanceof OTMOQLQueryImpl)
           {
               OTMOQLQueryImpl q = (OTMOQLQueryImpl) query;
  -            return new OTMIterator(_pb.getIteratorByQuery(q.getQuery()), lock, q);
  +            return new OTMIterator((OJBIterator) _pb.getIteratorByQuery(q.getQuery()),
lock, q);
           }
           else
           {
  @@ -265,20 +263,20 @@
   
       /**
        * @param query The query to execute
  -     * @param lock the lock that need to be acquired on the object
  -     * Possible values are:
  -     * LockType.NO_LOCK (aka read only) - changes to the object will not be written to
database;
  -     * LockType.READ_LOCK (aka optimistic lock) - changes to the object will be written
to the database,
  -     * in this case the lock will be automatically upgraded to the write lock on transaction
commit;
  -     * LockType.WRITE_LOCK (aka pessimistic lock) - changes to the object will be written
to the database.
  -     * @return an Iterator that iterates Objects of class c if calling the .next()
  -     * method. The returned objects are locked with the given lock value.
  +     * @param lock the lock that need to be acquired on the object Possible values are:
  +     *            LockType.NO_LOCK (aka read only) - changes to the object will not be
written to
  +     *            database; LockType.READ_LOCK (aka optimistic lock) - changes to the object
will
  +     *            be written to the database, in this case the lock will be automatically
upgraded
  +     *            to the write lock on transaction commit; LockType.WRITE_LOCK (aka pessimistic
  +     *            lock) - changes to the object will be written to the database.
  +     * @return an Iterator that iterates Objects of class c if calling the .next() method.
The
  +     *         returned objects are locked with the given lock value.
        */
       public Collection getCollectionByQuery(Query query, int lock)
       {
  -		checkTransaction("getCollectionByQuery");
  +        checkTransaction("getCollectionByQuery");
           Collection col = _pb.getCollectionByQuery(query);
  -        for (Iterator it = col.iterator(); it.hasNext(); )
  +        for (Iterator it = col.iterator(); it.hasNext();)
           {
               insertObject(it.next(), lock);
           }
  @@ -287,8 +285,8 @@
   
       /**
        * @param query The query to execute
  -     * @return an Iterator that iterates Objects of class c if calling the .next()
  -     * method. The returned objects are locked for read.
  +     * @return an Iterator that iterates Objects of class c if calling the .next() method.
The
  +     *         returned objects are locked for read.
        */
       public Collection getCollectionByQuery(Query query)
       {
  @@ -297,6 +295,7 @@
   
       /**
        * Get the identity of the object
  +     * 
        * @param object The object
        * @return the identity of the object
        */
  @@ -307,6 +306,7 @@
   
       /**
        * Get the class descriptor
  +     * 
        * @param clazz The class
        * @return the descriptor of the class
        */
  @@ -318,13 +318,13 @@
       /**
        * @see org.apache.ojb.otm.OTMConnection#invalidate(Identity)
        */
  -    public void invalidate(Identity oid)
  -            throws LockingException
  +    public void invalidate(Identity oid) throws LockingException
       {
  -		if (null==_tx)
  -		{
  -			throw new TransactionNotInProgressException( "invalidate requires a valid transaction.
Please make sure you have created a new transaction, and called begin() on it.");
  -		}
  +        if (null == _tx)
  +        {
  +            throw new TransactionNotInProgressException(
  +                    "invalidate requires a valid transaction. Please make sure you have
created a new transaction, and called begin() on it.");
  +        }
           // lock for write and mark as invalidated in the editing context
           _editingContext.insert(oid, null, LockType.WRITE_LOCK);
   
  @@ -343,10 +343,10 @@
   
       /**
        * TODO remove all from editing context.
  +     * 
        * @throws LockingException
        */
  -    public void invalidateAll()
  -        throws LockingException
  +    public void invalidateAll() throws LockingException
       {
           _pb.serviceObjectCache().clear();
       }
  @@ -354,10 +354,9 @@
       /**
        * @see org.apache.ojb.otm.OTMConnection#lockForWrite(Object)
        */
  -    public void lockForWrite(Object object)
  -        throws LockingException
  +    public void lockForWrite(Object object) throws LockingException
       {
  -		checkTransaction("lockForWrite");
  +        checkTransaction("lockForWrite");
           Identity oid = new Identity(object, _pb);
           _editingContext.insert(oid, object, LockType.WRITE_LOCK);
       }
  @@ -365,10 +364,9 @@
       /**
        * @see org.apache.ojb.otm.OTMConnection#makePersistent(Object)
        */
  -    public void makePersistent(Object object)
  -        throws LockingException
  +    public void makePersistent(Object object) throws LockingException
       {
  -		checkTransaction("makePersistent");
  +        checkTransaction("makePersistent");
           Identity oid = new Identity(object, _pb);
           _editingContext.insert(oid, object, LockType.READ_LOCK);
       }
  @@ -376,10 +374,9 @@
       /**
        * @see org.apache.ojb.otm.OTMConnection#deletePersistent(Object)
        */
  -    public void deletePersistent(Object object)
  -        throws LockingException
  +    public void deletePersistent(Object object) throws LockingException
       {
  -		checkTransaction("deletePersistent");
  +        checkTransaction("deletePersistent");
           Identity oid = new Identity(object, _pb);
           _editingContext.deletePersistent(oid, object);
       }
  @@ -396,7 +393,7 @@
   
       public EnhancedOQLQuery newOQLQuery(int lock)
       {
  -		checkTransaction("newOQLQuery");
  +        checkTransaction("newOQLQuery");
           OQLQueryImpl query = new OTMOQLQueryImpl(_pb.getPBKey(), lock);
           try
           {
  @@ -404,14 +401,15 @@
           }
           catch (ConfigurationException e)
           {
  -            throw new ODMGRuntimeException("Error in configuration of OQLQueryImpl instance:
" + e.getMessage());
  +            throw new ODMGRuntimeException("Error in configuration of OQLQueryImpl instance:
"
  +                    + e.getMessage());
           }
           return query;
       }
   
       public int getCount(Query query)
       {
  -		checkTransaction("getCount");
  +        checkTransaction("getCount");
           return _pb.getCount(query);
       }
   
  @@ -420,8 +418,7 @@
           Object ctxObject;
           Identity oid;
           ObjectCopyStrategy copyStrategy;
  -        boolean needToCopy = (lock != LockType.WRITE_LOCK)
  -                || !_tx.getKit().isImplicitLockingUsed();
  +        boolean needToCopy = (lock != LockType.WRITE_LOCK) || !_tx.getKit().isImplicitLockingUsed();
   
           oid = getIdentity(object);
           ctxObject = _editingContext.lookup(oid);
  @@ -452,51 +449,45 @@
       ///////////////////////////////////////
   
       /**
  -     *
  -     *  Notification issued by the driving transaction to begin this transaction
  -     *
  +     * 
  +     * Notification issued by the driving transaction to begin this transaction
  +     *  
        */
  -    public abstract void transactionBegin()
  -        throws TransactionException;
  +    public abstract void transactionBegin() throws TransactionException;
   
       /**
  -     *
  -     *  Prepare for a commit. As part of a two phase commit protocol of the transaction.
  -     *
  +     * 
  +     * Prepare for a commit. As part of a two phase commit protocol of the transaction.
  +     *  
        */
  -    public abstract void transactionPrepare()
  -            throws TransactionException;
  +    public abstract void transactionPrepare() throws TransactionException;
   
       /**
  -     *
  -     *  Notification issued by the driving transaction to commit resources held by this
  -     *  connection.
  -     *
  +     * 
  +     * Notification issued by the driving transaction to commit resources held by this
connection.
  +     *  
        */
  -    public abstract void transactionCommit()
  -                throws TransactionException;
  +    public abstract void transactionCommit() throws TransactionException;
   
       /**
  -     *
  -     *  Notification issued by the driving transaction to rollback resources held by this
  -     *  connection.
  -     *
  +     * 
  +     * Notification issued by the driving transaction to rollback resources held by this
  +     * connection.
  +     *  
        */
  -    public abstract void transactionRollback()
  -                throws TransactionException;
  -
  +    public abstract void transactionRollback() throws TransactionException;
   
       ///////////////////////////////////////
       // Inner classes
       ///////////////////////////////////////
   
  -    private class OTMIterator implements Iterator
  +    private class OTMIterator implements OJBIterator
       {
  -        private final Iterator _it;
  +        private final OJBIterator _it;
           private final int _lock;
           private final OTMOQLQueryImpl _oqlQuery;
   
  -        OTMIterator(Iterator it, int lock, OTMOQLQueryImpl oqlQuery)
  +        OTMIterator(OJBIterator it, int lock, OTMOQLQueryImpl oqlQuery)
           {
               _it = it;
               _lock = lock;
  @@ -530,11 +521,7 @@
   
           public void done()
           {
  -            if (_it instanceof OJBIterator)
  -            {
  -                ((OJBIterator) _it).releaseDbResources();
  -            }
  -
  +            releaseDbResources();
               if (_oqlQuery != null)
               {
                   _oqlQuery.resetBindIterator();
  @@ -545,6 +532,56 @@
           {
               done();
           }
  +
  +        /*
  +         * (non-Javadoc)
  +         * 
  +         * @see org.apache.ojb.broker.accesslayer.OJBIterator#absolute(int)
  +         */
  +        public boolean absolute(int row) throws PersistenceBrokerException
  +        {
  +            return _it.absolute(row);
  +        }
  +
  +        /*
  +         * (non-Javadoc)
  +         * 
  +         * @see org.apache.ojb.broker.accesslayer.OJBIterator#fullSize()
  +         */
  +        public int fullSize() throws PersistenceBrokerException
  +        {
  +            return _it.fullSize();
  +        }
  +
  +        /*
  +         * (non-Javadoc)
  +         * 
  +         * @see org.apache.ojb.broker.accesslayer.OJBIterator#relative(int)
  +         */
  +        public boolean relative(int row) throws PersistenceBrokerException
  +        {
  +            return _it.relative(row);
  +        }
  +
  +        /*
  +         * (non-Javadoc)
  +         * 
  +         * @see org.apache.ojb.broker.accesslayer.OJBIterator#releaseDbResources()
  +         */
  +        public void releaseDbResources()
  +        {
  +            _it.releaseDbResources();
  +        }
  +
  +        /*
  +         * (non-Javadoc)
  +         * 
  +         * @see org.apache.ojb.broker.accesslayer.OJBIterator#size()
  +         */
  +        public int size() throws PersistenceBrokerException
  +        {
  +            return _it.size();
  +        }
       }
   
       private class OTMOQLQueryImpl extends OQLQueryImpl
  @@ -558,16 +595,15 @@
           }
   
           /**
  -        * Execute the query.
  -        * After executing a query, the parameter list is reset.
  -        * @return	The object that represents the result of the query.
  -        * The returned data, whatever its OQL type, is encapsulated into an object.
  -        * For instance, when OQL returns an integer, the result is put into an
  -        * <code>Integer</code> object. When OQL returns a collection (literal
or object),
  -        * the result is always a Java collection object of the same kind
  -        * (for instance, a <code>DList</code>).
  -        * @exception	org.odmg.QueryException	An exception has occurred while executing
the query.
  -        */
  +         * Execute the query. After executing a query, the parameter list is reset.
  +         * 
  +         * @return The object that represents the result of the query. The returned data,
whatever
  +         *         its OQL type, is encapsulated into an object. For instance, when OQL
returns an
  +         *         integer, the result is put into an <code>Integer</code>
object. When OQL
  +         *         returns a collection (literal or object), the result is always a Java
collection
  +         *         object of the same kind (for instance, a <code>DList</code>).
  +         * @exception org.odmg.QueryException An exception has occurred while executing
the query.
  +         */
           public Object execute() throws org.odmg.QueryException
           {
               Collection result = new ArrayList();
  @@ -579,7 +615,7 @@
                   if (!(query instanceof ReportQuery))
                   {
                       Collection res0 = _pb.getCollectionByQuery(query);
  -                    for (iter = res0.iterator(); iter.hasNext(); )
  +                    for (iter = res0.iterator(); iter.hasNext();)
                       {
                           result.add(insertObject(iter.next(), _lock));
                       }
  
  
  

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