org.sourceforge.jemm.client
Class DatabaseAdapter

java.lang.Object
  extended by org.sourceforge.jemm.client.DatabaseAdapter
All Implemented Interfaces:
ObjectDatabase, ObjectSource, TypeRequestHandler

public class DatabaseAdapter
extends java.lang.Object
implements ObjectDatabase

An Adapter that maps the ObjectState fields onto actual objects, which are created and stores the objects it creates into a memory sensitive cache. When objects are garbage collected the reference is cleared. Otherwise requests for objects with an id already pulled will return the exact same object.

Author:
Paul Keeble

Field Summary
protected  ClientId clientId
           
protected  Database database
           
protected  JEMMObjectCreator objectCreator
           
protected  JEMMObjectFactory objectFactory
           
 
Constructor Summary
DatabaseAdapter(Database database)
           
DatabaseAdapter(Database database, JEMMObjectCreator creator)
           
 
Method Summary
 void acquireLock(ClientThreadId threadId, ID jemmId)
          Asynchronous request to acquire a lock.
 int cacheSize()
           
 ClassInfo getClassInfo(ClassId classId)
          Returns the class information for the given class id.
 EnumInfo getEnumInfo(EnumId enumId)
          Returns the classname for the given enum type id.
 JEMMObject getObject(ID jemmId)
          Looks up the Object with ID jemmId and converts from the underlying Delegate database into the actual JEMMObject from the response data.
 JEMMObject getRefreshedObject(ID jemmId)
          Retrieves an object's information from the database but always calls the underlying database, updating the cache.
 JEMMObject getRoot(java.lang.String rootName)
          Returns the object stored in the persistent root called 'rootName'
 ID newObject(ClassId classId, JEMMObject target)
          Notification of a new user object creation by the client.
 TypeResponse<?> processTypeRequest(JEMMObject obj, ClassId classId, TypeRequest<?> request)
           
 void refreshObject(JEMMObject obj)
          Refreshes an objects state by: - Retrieving the object again from the database and setting all primitives to the new values - setting all uninitialised Object fields to the new values - For initialised fields that are objects either: *) Matches the ID return from step 1 and refresh the object recursively *) Does not match the ID, the new object is retrieved and set as the value.
 ClassId registerClass(ClassInfo classInfo)
          Register a user class.
 EnumId registerEnum(EnumInfo enumInfo)
          Register an enumerated type.
 void releaseLock(ClientThreadId threadId, ID jemmId)
          Release the given lock held by the thread.
 void removeLockAcquiredListener(ClientId clientId)
          Remove a lock listener
 void setClientLockAcquiredListener(ClientId clientId, LockAcquiredListener listener)
          Register a lock listener
 void setObjectCreator(JEMMObjectCreator creator)
           
 void setRoot(java.lang.String rootName, JEMMObject newValue)
          Set the persistent root reference to the given object.
 JEMMObject setRootIfNull(java.lang.String rootName, JEMMObject newValue)
          Sets the persistent root reference to the given object, if the root is currently null.
 void shutdown()
           
 void synchroniseObject(JEMMObject jo)
          Takes all the data from the syncData object and creates an ObjectSyncData packet which is then passed to the delegated Database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clientId

protected final ClientId clientId

database

protected final Database database

objectFactory

protected JEMMObjectFactory objectFactory

objectCreator

protected JEMMObjectCreator objectCreator
Constructor Detail

DatabaseAdapter

public DatabaseAdapter(Database database,
                       JEMMObjectCreator creator)

DatabaseAdapter

public DatabaseAdapter(Database database)
Method Detail

setObjectCreator

public void setObjectCreator(JEMMObjectCreator creator)

getObject

public JEMMObject getObject(ID jemmId)
Looks up the Object with ID jemmId and converts from the underlying Delegate database into the actual JEMMObject from the response data. This routine: - Converts any IDs to trackedIDs - Creates the object based on the known class information - Sets up the ShadowObject - Sets up the fields on the object/ShadowData - caches the resulting objects mapped by ID

Specified by:
getObject in interface ObjectDatabase
Specified by:
getObject in interface ObjectSource
Parameters:
jemmId - The id of the object to retrieve.
Returns:
The object information (class type, field values etc.).

getRefreshedObject

public JEMMObject getRefreshedObject(ID jemmId)
Description copied from interface: ObjectDatabase
Retrieves an object's information from the database but always calls the underlying database, updating the cache.

Specified by:
getRefreshedObject in interface ObjectDatabase
Parameters:
jemmId - The id of the object to retrieve.
Returns:
The object information (class type, field values etc.).

refreshObject

public void refreshObject(JEMMObject obj)
Refreshes an objects state by: - Retrieving the object again from the database and setting all primitives to the new values - setting all uninitialised Object fields to the new values - For initialised fields that are objects either: *) Matches the ID return from step 1 and refresh the object recursively *) Does not match the ID, the new object is retrieved and set as the value.

Specified by:
refreshObject in interface ObjectDatabase
Parameters:
obj - The object to retrieve and update

synchroniseObject

public void synchroniseObject(JEMMObject jo)
Takes all the data from the syncData object and creates an ObjectSyncData packet which is then passed to the delegated Database.

Specified by:
synchroniseObject in interface ObjectDatabase
Parameters:
syncData - the JEMMObject data to use.

setRoot

public void setRoot(java.lang.String rootName,
                    JEMMObject newValue)
Description copied from interface: ObjectDatabase
Set the persistent root reference to the given object.

Specified by:
setRoot in interface ObjectDatabase
Parameters:
rootName - The name of the persistent root.
newValue - The new value of the root.

setRootIfNull

public JEMMObject setRootIfNull(java.lang.String rootName,
                                JEMMObject newValue)
Description copied from interface: ObjectDatabase
Sets the persistent root reference to the given object, if the root is currently null. This is an atomic operation.

Specified by:
setRootIfNull in interface ObjectDatabase
Parameters:
rootName - The name of the persistent root.
newValue - The new value of the root.
Returns:
The old value if it was not null, or the recently set value.

getRoot

public JEMMObject getRoot(java.lang.String rootName)
Description copied from interface: ObjectDatabase
Returns the object stored in the persistent root called 'rootName'

Specified by:
getRoot in interface ObjectDatabase
Parameters:
rootName - The name of the persistent root.
Returns:
The ID of the object stored in the persistent root, or null if none.

cacheSize

public int cacheSize()

acquireLock

public void acquireLock(ClientThreadId threadId,
                        ID jemmId)
Description copied from interface: ObjectDatabase
Asynchronous request to acquire a lock. This method will return immediately, but the caller should wait until notified by the LockAcquiredListener callback.

Specified by:
acquireLock in interface ObjectDatabase
Parameters:
threadId - The requesting thread.
jemmId - The id of the object to lock.

setClientLockAcquiredListener

public void setClientLockAcquiredListener(ClientId clientId,
                                          LockAcquiredListener listener)
Description copied from interface: ObjectDatabase
Register a lock listener

Specified by:
setClientLockAcquiredListener in interface ObjectDatabase
Parameters:
clientId - The client who is interested in their lock event notifications.
listener - The listener to register.

getClassInfo

public ClassInfo getClassInfo(ClassId classId)
Description copied from interface: ObjectDatabase
Returns the class information for the given class id.

Specified by:
getClassInfo in interface ObjectDatabase
Parameters:
classId - The id of the held class.
Returns:
The class information for the held class.

getEnumInfo

public EnumInfo getEnumInfo(EnumId enumId)
Description copied from interface: ObjectDatabase
Returns the classname for the given enum type id.

Specified by:
getEnumInfo in interface ObjectDatabase
Parameters:
enumId - The enum type id.
Returns:
The classname of the registered enum type.

newObject

public ID newObject(ClassId classId,
                    JEMMObject target)
Description copied from interface: ObjectDatabase
Notification of a new user object creation by the client.

Specified by:
newObject in interface ObjectDatabase
Parameters:
classId - The class id of the new object (must be previously registered).
Returns:
The ID assigned to the new object.

registerClass

public ClassId registerClass(ClassInfo classInfo)
                      throws StructureModifiedException
Description copied from interface: ObjectDatabase
Register a user class.

Specified by:
registerClass in interface ObjectDatabase
Parameters:
classInfo - The information about the loaded user class (name/fields).
Returns:
The class id of the registered class.
Throws:
StructureModifiedException - if the class has been modified and the Database does not accept the change

registerEnum

public EnumId registerEnum(EnumInfo enumInfo)
                    throws StructureModifiedException
Description copied from interface: ObjectDatabase
Register an enumerated type.

Specified by:
registerEnum in interface ObjectDatabase
Parameters:
enumInfo - The enumeration type information
Returns:
Returns the id of the registered enum type.
Throws:
StructureModifiedException - If a modification to the enum is detected.

releaseLock

public void releaseLock(ClientThreadId threadId,
                        ID jemmId)
Description copied from interface: ObjectDatabase
Release the given lock held by the thread.

Specified by:
releaseLock in interface ObjectDatabase
Parameters:
threadId - The thread currently holding the lock.
jemmId - The id of the object to release.

removeLockAcquiredListener

public void removeLockAcquiredListener(ClientId clientId)
Description copied from interface: ObjectDatabase
Remove a lock listener

Specified by:
removeLockAcquiredListener in interface ObjectDatabase
Parameters:
clientId - The id the the client who is no longer interested in lock events.

processTypeRequest

public TypeResponse<?> processTypeRequest(JEMMObject obj,
                                          ClassId classId,
                                          TypeRequest<?> request)
Specified by:
processTypeRequest in interface TypeRequestHandler

shutdown

public void shutdown()


Copyright © 2009. All Rights Reserved.