net.sf.spif
Class StoreLocker

java.lang.Object
  |
  +--net.sf.spif.StoreChain
        |
        +--net.sf.spif.StoreLocker

public class StoreLocker
extends StoreChain

Include in a store chain to get pessimistic locking of all objects fecthed for writing.

Objects will be locked for a configurable time. This class is backed by a Locker.

Usually, locking should be ties to a certain identity, so Bob may lock one object and Alice another. If the call ending up here is entering the server though the store sesison bean, this is taken care of. Otherwise you'll have to explicitly make identities available to Spif and the locker by calling ThreadContext.setPrincipalName(java.lang.String), usually with the value of sessionContext.getCallerPrincipal().getName().

Version:
$Id: StoreLocker.java,v 1.2 2003/02/05 22:15:44 bratseth Exp $
Author:
Jon S Bratseth

Field Summary
 
Fields inherited from class net.sf.spif.StoreChain
chained
 
Constructor Summary
StoreLocker()
          Creates a store locked with a default lock timeout (30 minutes)
StoreLocker(long timeout)
          Creates a store locked with a lock timeout
 
Method Summary
 void clear()
          Removes all locks
 java.lang.Object get(Id id, boolean writeable)
          Returns an objects, but locks it first if it writeable is true.
 Id put(Id id, java.lang.Object object)
          Puts a changed or new object into the store.
 boolean remove(Id id)
          Removes any held lock before forwarding,
 void unlock(Id id)
          Unlocks a previously locked object
 
Methods inherited from class net.sf.spif.StoreChain
equals, getChained, getChained, getChained, hashCode, setChained
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StoreLocker

public StoreLocker()
Creates a store locked with a default lock timeout (30 minutes)


StoreLocker

public StoreLocker(long timeout)
Creates a store locked with a lock timeout

Method Detail

get

public java.lang.Object get(Id id,
                            boolean writeable)

Returns an objects, but locks it first if it writeable is true.

The locker identity is the principal name of the requester, obtained from ThreadContext. Thus the thread context must have a principal set for this to work. The StoreSessionBean does this, if you get to the server by another bean, make sure it sets the principal to threadcontext.

Overrides:
get in class StoreChain
Parameters:
id - the id of the object to return
Returns:
the object, or null if the object could not be retrieved or if the input id was null
Throws:
LockedException - if the objects was allready locked by somebody else

put

public Id put(Id id,
              java.lang.Object object)
Puts a changed or new object into the store.

Overrides:
put in class StoreChain
Parameters:
id - the id of the object to put. May be null
object - the object to put. May be null
Returns:
the id of this object. This will usually be (and is in this default implementation) the input id, unless the given id is null and there is an id generating service on the store chain
Throws:
LockedException - if the object is locked by somebody else

remove

public boolean remove(Id id)
Removes any held lock before forwarding,

Overrides:
remove in class StoreChain
Parameters:
id - the id of the object to remove
Returns:
true if the object did not exists
Throws:
LockedException - if locked by somebody else

unlock

public void unlock(Id id)
Unlocks a previously locked object


clear

public void clear()
Removes all locks

Overrides:
clear in class StoreChain


Spif 1.01 (2003-04-03). http://spif.sf.net