org.apache.ojb.broker.core
Class PersistenceBrokerImpl

java.lang.Object
  extended byorg.apache.ojb.broker.core.PersistenceBrokerAbstractImpl
      extended byorg.apache.ojb.broker.core.PersistenceBrokerImpl
All Implemented Interfaces:
Configurable, ObjectContainer, PBState, PersistenceBroker

public class PersistenceBrokerImpl
extends PersistenceBrokerAbstractImpl
implements PBState

The PersistenceBrokerImpl is an implementation of the PersistenceBroker Interface that specifies a persistence mechanism for Java objects. This Concrete implementation provides an object relational mapping and allows to store and retrieve arbitrary objects in/from relational databases accessed by JDBC.

Version:
$Id: PersistenceBrokerImpl.java,v 1.68 2004/02/17 20:52:30 baserose Exp $
Author:
Thomas Mahler, Leandro Rodrigo Saad Cruz, Matthew Baird, Jakob Braeuchi
See Also:
for a sample application

Field Summary
protected  BrokerHelper brokerHelper
           
protected  MtoNBroker mtoNBroker
           
protected  PersistenceBrokerFactoryIF pbf
           
protected  QueryReferenceBroker referencesBroker
           
 
Fields inherited from class org.apache.ojb.broker.core.PersistenceBrokerAbstractImpl
AFTER_BEGIN_EVENT, AFTER_COMMIT_EVENT, AFTER_DELETE_EVENT, AFTER_LOOKUP_EVENT, AFTER_OPEN_EVENT, AFTER_ROLLBACK_EVENT, AFTER_STORE_EVENT, AFTER_UPDATE_EVENT, BEFORE_BEGIN_EVENT, BEFORE_CLOSE_EVENT, BEFORE_COMMIT_EVENT, BEFORE_DELETE_EVENT, BEFORE_ROLLBACK_EVENT, BEFORE_STORE_EVENT, BEFORE_UPDATE_EVENT
 
Constructor Summary
PersistenceBrokerImpl(PBKey key, PersistenceBrokerFactoryIF pbf)
          Constructor used by PersistenceBrokerFactoryIF implementation.
 
Method Summary
 void abortTransaction()
          Abort and close the transaction.
 void addMtoNImplementor(MtoNImplementor m2n)
           
 void beginTransaction()
          begin a transaction against the underlying RDBMS.
 void clearCache()
          clears the brokers internal cache.
 boolean close()
          Close this PersistenceBroker so that no further requests may be made on it.
 void commitTransaction()
          Commit and close the transaction.
 void delete(java.lang.Object obj)
          deletes the objects obj concrete representation in the underlying persistence system.
 void deleteByQuery(Query query)
          Deletes all objects matching the query from the underlying persistence system.
 void deleteMtoNImplementor(MtoNImplementor m2nImpl)
          Deletes and MtoN implementor (a row on a indirection table) As it is today, ojb doesn't handle collection inherence, so collections descriptors are written per class.
 void destroy()
          Release all resources used by this class - CAUTION: No further operations can be done with this instance after calling this method.
 java.lang.Object doGetObjectByIdentity(Identity id)
          Internal used method to retrieve object based on Identity.
protected  void finalize()
           
 ClassDescriptor getClassDescriptor(java.lang.Class clazz)
          returns a ClassDescriptor for the persistence capable class clazz.
 ManageableCollection getCollectionByQuery(java.lang.Class collectionClass, Query query)
          retrieve a collection of type collectionClass matching the Query query
 java.util.Collection getCollectionByQuery(Query query)
          retrieve a collection of itemClass Objects matching the Query query
 int getCount(Query query)
          Returns the count of elements a given query will return.
 DescriptorRepository getDescriptorRepository()
          Returns the DescriptorRepository associated with this broker.
 InternalCache getInternalCache()
           
 java.util.Iterator getIteratorByQuery(Query query)
          returns an Iterator that iterates Objects of class c if calling the .next() method.
protected  OJBIterator getIteratorFromQuery(Query query, ClassDescriptor cld)
          Get an extent aware Iterator based on the Query
 java.lang.Object getObjectByIdentity(Identity id)
          Retrieve an Object by its Identity.
 java.lang.Object getObjectByQuery(Query query)
          retrieve an Object by query I.e perform a SELECT ...
 PBKey getPBKey()
          Get the PBKey for this broker.
 java.util.Enumeration getPKEnumerationByQuery(java.lang.Class primaryKeyClass, Query query)
          returns an Enumeration of PrimaryKey Objects for objects of class DataClass.
 QueryReferenceBroker getReferenceBroker()
           
 java.util.Iterator getReportQueryIteratorByQuery(Query query)
          Get an Iterator based on the ReportQuery
 java.lang.Class getTopLevelClass(java.lang.Class clazz)
          Returns the top level (extent) class to which the given class belongs.
 boolean hasClassDescriptor(java.lang.Class clazz)
          same as getClassDescriptor, but does not throw.
 boolean isClosed()
           
 boolean isInTransaction()
          returns true if the broker is currently running a transaction.
 Query query()
          factory method to create a new Query object.
 void refresh()
          Lookup the current DescriptorRepository for this class.
 void refreshRelationships(java.lang.Object obj, ClassDescriptor cld)
          Refresh Relationships
 void removeFromCache(java.lang.Object obj)
          Removes the object from the brokers internal cache.
 void retrieveAllReferences(java.lang.Object pInstance)
          retrieve all References (also Collection-attributes) of a given instance.
 void retrieveReference(java.lang.Object pInstance, java.lang.String pAttributeName)
          retrieve a single reference- or collection attribute of a persistent instance.
 BrokerHelper serviceBrokerHelper()
          Returns the BrokerHelper instance associated with this broker.
 ConnectionManagerIF serviceConnectionManager()
          Returns the ConnectionManagerIF instance associated with this broker.
 JdbcAccess serviceJdbcAccess()
          Returns the JdbcAccess instance associated with this broker.
 ObjectCache serviceObjectCache()
          Returns the ObjectCache instance associated with this broker.
 SequenceManager serviceSequenceManager()
          Returns the SequenceManager instance associated with this broker.
 SqlGenerator serviceSqlGenerator()
          Returns the SqlGenerator instance associated with this broker.
 StatementManagerIF serviceStatementManager()
          Returns the StatementManagerIF instance associated with this broker.
 void setClosed(boolean closed)
           
 void setPBKey(PBKey key)
           
 void store(java.lang.Object obj)
          Store an Object
 void store(java.lang.Object obj, ObjectModification mod)
          makes object obj persistent in the underlying persistence system.
 
Methods inherited from class org.apache.ojb.broker.core.PersistenceBrokerAbstractImpl
addListener, addListener, configure, contains, fireBrokerEvent, fireBrokerEvent, fireBrokerEvent, removeAllListeners, removeAllListeners, removeListener
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pbf

protected PersistenceBrokerFactoryIF pbf

brokerHelper

protected BrokerHelper brokerHelper

mtoNBroker

protected MtoNBroker mtoNBroker

referencesBroker

protected QueryReferenceBroker referencesBroker
Constructor Detail

PersistenceBrokerImpl

public PersistenceBrokerImpl(PBKey key,
                             PersistenceBrokerFactoryIF pbf)
Constructor used by PersistenceBrokerFactoryIF implementation.

Method Detail

getInternalCache

public InternalCache getInternalCache()

serviceSqlGenerator

public SqlGenerator serviceSqlGenerator()
Description copied from interface: PersistenceBroker
Returns the SqlGenerator instance associated with this broker.

Specified by:
serviceSqlGenerator in interface PersistenceBroker

serviceStatementManager

public StatementManagerIF serviceStatementManager()
Description copied from interface: PersistenceBroker
Returns the StatementManagerIF instance associated with this broker.

Specified by:
serviceStatementManager in interface PersistenceBroker

serviceJdbcAccess

public JdbcAccess serviceJdbcAccess()
Description copied from interface: PersistenceBroker
Returns the JdbcAccess instance associated with this broker.

Specified by:
serviceJdbcAccess in interface PersistenceBroker

serviceConnectionManager

public ConnectionManagerIF serviceConnectionManager()
Description copied from interface: PersistenceBroker
Returns the ConnectionManagerIF instance associated with this broker.

Specified by:
serviceConnectionManager in interface PersistenceBroker

serviceSequenceManager

public SequenceManager serviceSequenceManager()
Description copied from interface: PersistenceBroker
Returns the SequenceManager instance associated with this broker.

Specified by:
serviceSequenceManager in interface PersistenceBroker

serviceBrokerHelper

public BrokerHelper serviceBrokerHelper()
Description copied from interface: PersistenceBroker
Returns the BrokerHelper instance associated with this broker. Makes some helper methods available.

Specified by:
serviceBrokerHelper in interface PersistenceBroker

serviceObjectCache

public ObjectCache serviceObjectCache()
Description copied from interface: PersistenceBroker
Returns the ObjectCache instance associated with this broker.

Specified by:
serviceObjectCache in interface PersistenceBroker

getReferenceBroker

public QueryReferenceBroker getReferenceBroker()

isClosed

public boolean isClosed()
Specified by:
isClosed in interface PersistenceBroker

setClosed

public void setClosed(boolean closed)
Specified by:
setClosed in interface PBState

refresh

public void refresh()
Lookup the current DescriptorRepository for this class. This method is responsible to keep this PB instance in sync with MetadataManager.


destroy

public void destroy()
Release all resources used by this class - CAUTION: No further operations can be done with this instance after calling this method.


getPBKey

public PBKey getPBKey()
Description copied from interface: PersistenceBroker
Get the PBKey for this broker.

Specified by:
getPBKey in interface PersistenceBroker

setPBKey

public void setPBKey(PBKey key)

close

public boolean close()
Description copied from interface: PersistenceBroker
Close this PersistenceBroker so that no further requests may be made on it. A PersistenceBroker instance can be used only until it is closed. Closing a PersistenceBroker might release it to the pool of available PersistenceBrokers, or might be garbage collected, at the option of the implementation.

Specified by:
close in interface PersistenceBroker
Returns:
true if successful
See Also:
PersistenceBroker.close()

abortTransaction

public void abortTransaction()
                      throws TransactionNotInProgressException
Abort and close the transaction. Calling abort abandons all persistent object modifications and releases the associated locks. If transaction is not in progress a TransactionNotInProgressException is thrown

Specified by:
abortTransaction in interface PersistenceBroker
Throws:
TransactionNotInProgressException

beginTransaction

public void beginTransaction()
                      throws TransactionInProgressException,
                             TransactionAbortedException
begin a transaction against the underlying RDBMS. Calling beginTransaction multiple times, without an intervening call to commitTransaction or abortTransaction, causes the exception TransactionInProgressException to be thrown on the second and subsequent calls.

Specified by:
beginTransaction in interface PersistenceBroker
Throws:
TransactionInProgressException
TransactionAbortedException

commitTransaction

public void commitTransaction()
                       throws TransactionNotInProgressException,
                              TransactionAbortedException
Commit and close the transaction. Calling commit commits to the database all UPDATE, INSERT and DELETE statements called within the transaction and releases any locks held by the transaction. If beginTransaction() has not been called before a TransactionNotInProgressException exception is thrown. If the transaction cannot be commited a TransactionAbortedException exception is thrown.

Specified by:
commitTransaction in interface PersistenceBroker
Throws:
TransactionNotInProgressException
TransactionAbortedException

delete

public void delete(java.lang.Object obj)
            throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
deletes the objects obj concrete representation in the underlying persistence system. E.G. by DELETE FROM ... WHERE ... in an RDBMS

Specified by:
delete in interface PersistenceBroker
Throws:
PersistenceBrokerException
See Also:
PersistenceBroker.delete(java.lang.Object)

deleteByQuery

public void deleteByQuery(Query query)
                   throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
Deletes all objects matching the query from the underlying persistence system. E.G. by DELETE FROM ... WHERE ... in an RDBMS

Specified by:
deleteByQuery in interface PersistenceBroker
Throws:
PersistenceBrokerException
See Also:
PersistenceBroker.deleteByQuery(Query)

store

public void store(java.lang.Object obj)
           throws PersistenceBrokerException
Store an Object

Specified by:
store in interface PersistenceBroker
Throws:
PersistenceBrokerException
See Also:
PersistenceBroker.store(Object)

retrieveAllReferences

public void retrieveAllReferences(java.lang.Object pInstance)
                           throws PersistenceBrokerException
retrieve all References (also Collection-attributes) of a given instance. Loading is forced, even if the collection- and reference-descriptors differ.

Specified by:
retrieveAllReferences in interface PersistenceBroker
Parameters:
pInstance - the persistent instance to work with
Throws:
PersistenceBrokerException

retrieveReference

public void retrieveReference(java.lang.Object pInstance,
                              java.lang.String pAttributeName)
                       throws PersistenceBrokerException
retrieve a single reference- or collection attribute of a persistent instance.

Specified by:
retrieveReference in interface PersistenceBroker
Parameters:
pInstance - the persistent instance
pAttributeName - the name of the Attribute to load
Throws:
PersistenceBrokerException

refreshRelationships

public void refreshRelationships(java.lang.Object obj,
                                 ClassDescriptor cld)
Refresh Relationships

Parameters:
obj -
cld -
Throws:
PersistenceBrokerException - if there is a error refreshing collections or references

getCollectionByQuery

public ManageableCollection getCollectionByQuery(java.lang.Class collectionClass,
                                                 Query query)
                                          throws PersistenceBrokerException
retrieve a collection of type collectionClass matching the Query query

Specified by:
getCollectionByQuery in interface PersistenceBroker
Throws:
PersistenceBrokerException
See Also:
PersistenceBroker.getCollectionByQuery(Class, Query)

getCollectionByQuery

public java.util.Collection getCollectionByQuery(Query query)
                                          throws PersistenceBrokerException
retrieve a collection of itemClass Objects matching the Query query

Specified by:
getCollectionByQuery in interface PersistenceBroker
Throws:
PersistenceBrokerException

getIteratorByQuery

public java.util.Iterator getIteratorByQuery(Query query)
                                      throws PersistenceBrokerException
returns an Iterator that iterates Objects of class c if calling the .next() method. The Elements returned come from a SELECT ... WHERE Statement that is defined by the Query query. If itemProxy is null, no proxies are used.

Specified by:
getIteratorByQuery in interface PersistenceBroker
Throws:
PersistenceBrokerException

getIteratorFromQuery

protected OJBIterator getIteratorFromQuery(Query query,
                                           ClassDescriptor cld)
                                    throws PersistenceBrokerException
Get an extent aware Iterator based on the Query

Parameters:
query -
cld - the ClassDescriptor
Returns:
OJBIterator
Throws:
PersistenceBrokerException

getObjectByIdentity

public java.lang.Object getObjectByIdentity(Identity id)
                                     throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
Retrieve an Object by its Identity. Application Developers are encouraged to use getObjectByQuery(). This method is mainly used for internal performant handling of materialization by OID (e.g. in Proxies)

Specified by:
getObjectByIdentity in interface PersistenceBroker
Throws:
PersistenceBrokerException

doGetObjectByIdentity

public java.lang.Object doGetObjectByIdentity(Identity id)
                                       throws PersistenceBrokerException
Internal used method to retrieve object based on Identity.

Parameters:
id -
Returns:
Throws:
PersistenceBrokerException

getObjectByQuery

public java.lang.Object getObjectByQuery(Query query)
                                  throws PersistenceBrokerException
retrieve an Object by query I.e perform a SELECT ... FROM ... WHERE ... in an RDBMS

Specified by:
getObjectByQuery in interface PersistenceBroker
Throws:
PersistenceBrokerException

getPKEnumerationByQuery

public java.util.Enumeration getPKEnumerationByQuery(java.lang.Class primaryKeyClass,
                                                     Query query)
                                              throws PersistenceBrokerException
returns an Enumeration of PrimaryKey Objects for objects of class DataClass. The Elements returned come from a SELECT ... WHERE Statement that is defined by the fields and their coresponding values of listFields and listValues. Useful for EJB Finder Methods...

Specified by:
getPKEnumerationByQuery in interface PersistenceBroker
Parameters:
primaryKeyClass - the pk class for the searched objects
query - the query
Throws:
PersistenceBrokerException

store

public void store(java.lang.Object obj,
                  ObjectModification mod)
           throws PersistenceBrokerException
makes object obj persistent in the underlying persistence system. E.G. by INSERT INTO ... or UPDATE ... in an RDBMS. The ObjectModification parameter can be used to determine whether INSERT or update is to be used. This functionality is typically called from transaction managers, that track which objects have to be stored.

Specified by:
store in interface PersistenceBroker
Throws:
PersistenceBrokerException

isInTransaction

public boolean isInTransaction()
returns true if the broker is currently running a transaction.

Specified by:
isInTransaction in interface PersistenceBroker
Returns:
boolean

removeFromCache

public void removeFromCache(java.lang.Object obj)
                     throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
Removes the object from the brokers internal cache. If object is instance of Identity, the associated object was removed from cache.
Removing is not recursive.

Specified by:
removeFromCache in interface PersistenceBroker
Throws:
PersistenceBrokerException
See Also:
PersistenceBroker.removeFromCache(java.lang.Object)

getClassDescriptor

public ClassDescriptor getClassDescriptor(java.lang.Class clazz)
                                   throws PersistenceBrokerException
returns a ClassDescriptor for the persistence capable class clazz. throws a PersistenceBrokerException if clazz is not persistence capable, i.e. if clazz is not defined in the DescriptorRepository.

Specified by:
getClassDescriptor in interface PersistenceBroker
Throws:
PersistenceBrokerException

hasClassDescriptor

public boolean hasClassDescriptor(java.lang.Class clazz)
Description copied from interface: PersistenceBroker
same as getClassDescriptor, but does not throw. Useful for checking if an object is persistence Capable.

Specified by:
hasClassDescriptor in interface PersistenceBroker
Parameters:
clazz -
Returns:
true if descriptor was found

clearCache

public void clearCache()
                throws PersistenceBrokerException
clears the brokers internal cache. removing is recursive. That is referenced Objects are also removed from the cache, if the auto-retrieve flag is set for obj.getClass() in the metadata repository.

Specified by:
clearCache in interface PersistenceBroker
Throws:
PersistenceBrokerException

getTopLevelClass

public java.lang.Class getTopLevelClass(java.lang.Class clazz)
                                 throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
Returns the top level (extent) class to which the given class belongs. This may be a (abstract) base-class, an interface or the given class itself, if no extent is defined.

Specified by:
getTopLevelClass in interface PersistenceBroker
Throws:
PersistenceBrokerException - if clazz is not persistence capable, i.e. if clazz is not defined in the DescriptorRepository.
See Also:
PersistenceBroker.getTopLevelClass(java.lang.Class)

getCount

public int getCount(Query query)
             throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
Returns the count of elements a given query will return.

Specified by:
getCount in interface PersistenceBroker
Throws:
PersistenceBrokerException
See Also:
PersistenceBroker.getCount(Query)

getReportQueryIteratorByQuery

public java.util.Iterator getReportQueryIteratorByQuery(Query query)
                                                 throws PersistenceBrokerException
Get an Iterator based on the ReportQuery

Specified by:
getReportQueryIteratorByQuery in interface PersistenceBroker
Parameters:
query -
Returns:
Iterator
Throws:
PersistenceBrokerException

query

public Query query()
Description copied from interface: ObjectContainer
factory method to create a new Query object.

Specified by:
query in interface ObjectContainer
Returns:
a new Query object
See Also:
ObjectContainer.query()

getDescriptorRepository

public DescriptorRepository getDescriptorRepository()
Description copied from interface: PersistenceBroker
Returns the DescriptorRepository associated with this broker.

Specified by:
getDescriptorRepository in interface PersistenceBroker
Returns:
DescriptorRepository

finalize

protected void finalize()

deleteMtoNImplementor

public void deleteMtoNImplementor(MtoNImplementor m2nImpl)
                           throws PersistenceBrokerException
Description copied from interface: PersistenceBroker
Deletes and MtoN implementor (a row on a indirection table) As it is today, ojb doesn't handle collection inherence, so collections descriptors are written per class. We try to match one of these collection descriptors, iterating from the left side and looking for possible for classes on the right side using : collection descriptor element . isAssinableFrom(rightClass) TODO: handle cache problems TODO: delete more than one row if possible Pre-coditions :
  • Pos-coditions :
  • Assertions :
  • Specified by:
    deleteMtoNImplementor in interface PersistenceBroker
    Parameters:
    m2nImpl -
    Throws:
    PersistenceBrokerException - if an error occours
    See Also:
    PersistenceBroker.deleteMtoNImplementor(org.apache.ojb.broker.MtoNImplementor)

    addMtoNImplementor

    public void addMtoNImplementor(MtoNImplementor m2n)
                            throws PersistenceBrokerException
    Specified by:
    addMtoNImplementor in interface PersistenceBroker
    Throws:
    PersistenceBrokerException
    See Also:
    PersistenceBroker.addMtoNImplementor(org.apache.ojb.broker.MtoNImplementor)


    Authors: Thomas Mahler and others. (C) 2000 - 2003 Apache Software Foundation
    All rights reserved. Published under the Apache License.
    http://db.apache.org/ojb
    Version: 1.0.rc5, 2003-12-14