br.shob
Class DefaultSharedReference

java.lang.Object
  |
  +--br.shob.DefaultSharedReference
All Implemented Interfaces:
SharedReference
Direct Known Subclasses:
PhantomReference

public class DefaultSharedReference
extends Object
implements SharedReference

This class represents a reference to shared object.

Usually, this class is used as superclass to other SharedReference classes.

Shared References should always come from the SharedSpace instance and never shared locally between threads.

Version:
$Revision: 1.7 $
Author:
crolmos, dukejeffrie
Created:
August 30, 2002

Field Summary
 
Fields inherited from interface br.shob.SharedReference
defaultLeaseTime
 
Constructor Summary
DefaultSharedReference(SharedSpace shob, String globalName)
          Creates new SharedReference.
 
Method Summary
 boolean equals(Object other)
          This method delegates to #srEquals, so references can be equal to themselves as well as the shared objects they represent.
protected  Object getData()
          Gets the data attribute of the SharedReference object
 int hashCode()
          DefaultSharedReferences use the hash code of ther global names.
 void srAddStorageListener(StorageListener listener)
          Adds a StorageListener to this reference's listener list.
 void srBeginTrans()
          Initiates a transaction for this object.
 Object srCallMethod(String name, Class[] argTypes, Object[] args)
          This method calls a method from the shared object.
 void srCommit()
          Commits a transaction.
 boolean srEquals(Object other)
          Tests for object equality.
 boolean srEquals(SharedReference other)
          Tests for reference equality.
 String srGetGlobalName()
          Acessor for the global name.
 boolean srIsUpdated()
          Checks if the corresponding object is updated (i.e., if his lease is expired)
 void srRemoveStorageListener(StorageListener listener)
          Remove a previously added StorageListener from this reference's listener list.
 void srRollback()
          Rolls back a transaction.
 String srToString()
          Returns a String description of this reference.
 void srUnshare()
          Unshares this object from the global space.
 void srValidate()
          Validates this object.
 String toString()
          Returns a String description of this reference.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DefaultSharedReference

public DefaultSharedReference(SharedSpace shob,
                              String globalName)
Creates new SharedReference.
This constructor should never be called directly. Instead use br.shob.SharedSpace.get().

Parameters:
shob - the SharedSpace in wich this object is shared.
globalName - the global name of the object that will be accessed throw this reference.
Method Detail

equals

public boolean equals(Object other)
This method delegates to #srEquals, so references can be equal to themselves as well as the shared objects they represent.

Overrides:
equals in class Object
Parameters:
other - The object to be tested with
Returns:
true if the other object is equivalent to this.

hashCode

public int hashCode()
DefaultSharedReferences use the hash code of ther global names. This is useful if you consider that global names are unique, no matter where they are.

Overrides:
hashCode in class Object
Returns:
a hash code value for this object.

srAddStorageListener

public void srAddStorageListener(StorageListener listener)
Adds a StorageListener to this reference's listener list. The listener will listen to StorageEvents fired in the SharedSpace's local cache.

Parameters:
listener - A storage listener who wants to listen to events in the cache
See Also:
srRemoveStorageListener(br.shob.event.StorageListener)

srBeginTrans

public void srBeginTrans()
                  throws ShobException,
                         AlreadyLockedException
Initiates a transaction for this object.

Specified by:
srBeginTrans in interface SharedReference
Throws:
ShobException - If a low-level error occurs
AlreadyLockedException - If the object is already locked by this reference.

srCallMethod

public Object srCallMethod(String name,
                           Class[] argTypes,
                           Object[] args)
                    throws InvocationException,
                           InvocationTargetException,
                           ShobException
This method calls a method from the shared object.
If the lease has expired it validates the object before the call is made.

Specified by:
srCallMethod in interface SharedReference
Parameters:
name - the name of the method.
argTypes - the types of the arguments.
args - the arguments.
Returns:
the value returned by the method
Throws:
InvocationException - if the method does not exist, is not public or the argument is invalid (thrown with real cause chained)
InvocationTargetException - if the underlying method throws an exception.
ShobException - If a low-level error occurs

srCommit

public void srCommit()
              throws ShobException,
                     NotLockedException
Commits a transaction.

Specified by:
srCommit in interface SharedReference
Throws:
ShobException - If a low-level error occurs
NotLockedException - if the object is not inside a transaction

srEquals

public boolean srEquals(SharedReference other)
Tests for reference equality. This implementation returns true if the two references have the same global name.

Parameters:
other - another shared reference
Returns:
true if the references point to the same shared object, false otherwise

srEquals

public boolean srEquals(Object other)
Tests for object equality. If the argument is a reference and they are equal, first it tests for reference equality then for data equality. If not, the argument is tested against the data.

Parameters:
other - a reference or a real object.
Returns:
true if the objects are equal as per this method's description, false otherwise.

srGetGlobalName

public String srGetGlobalName()
Acessor for the global name.

Specified by:
srGetGlobalName in interface SharedReference
Returns:
the global name of the shared object.

srIsUpdated

public boolean srIsUpdated()
Checks if the corresponding object is updated (i.e., if his lease is expired)

Specified by:
srIsUpdated in interface SharedReference
Returns:
true if the local cache is up to date

srRemoveStorageListener

public void srRemoveStorageListener(StorageListener listener)
Remove a previously added StorageListener from this reference's listener list.

Parameters:
listener - A storage listener who wants to listen to events in the cache
See Also:
srAddStorageListener(br.shob.event.StorageListener)

srRollback

public void srRollback()
                throws ShobException,
                       NotLockedException
Rolls back a transaction.

Specified by:
srRollback in interface SharedReference
Throws:
ShobException - If a low-level error occurs
NotLockedException - if the object is not inside a transaction

srToString

public String srToString()
Returns a String description of this reference. This implementation also prints the object data

Returns:
A description of this shared reference

srUnshare

public void srUnshare()
               throws ShobException
Unshares this object from the global space.

Specified by:
srUnshare in interface SharedReference
Throws:
ShobException - If a low-level error occurs

srValidate

public void srValidate()
                throws ShobException
Validates this object.

Specified by:
srValidate in interface SharedReference
Throws:
ShobException - If a low-level error occurs

toString

public String toString()
Returns a String description of this reference. This implementation calls the srToString() method

Overrides:
toString in class Object
Returns:
A description of this shared reference

getData

protected Object getData()
Gets the data attribute of the SharedReference object

Returns:
The data value