net.sf.spif.util
Class Locker

java.lang.Object
  |
  +--net.sf.spif.util.Locker

public class Locker
extends java.lang.Object

A class for coordinating exclusive access to objects accross simultaneous threads. Keeps identified items locked for a given time interval (leasing).

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

Constructor Summary
Locker(long lockTimeoutMillis)
          Creates a locked with a lock timeout in milliseconds
 
Method Summary
 void clear()
          Clears all locks
 long getLockTimeoutMillis()
          Returns the lock timeout of this locked in milliseconds
 boolean isLockedBy(Id id, java.lang.String key)
          Returns whether this id is currently locked by this key.
 boolean isLockedByOther(Id id, java.lang.String key)
          Returns true if this id is currently locked, but by another key
 void lock(Id id, java.lang.String key)
          Locks (or relocks) an item.
 void setLockTimeoutMillis(long lockTimeoutMillis)
          Sets the lock timeout of this locked in missiseconds
 void unlock(Id id, java.lang.String key)
          Unlocks the given id if it is still locked by the given key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Locker

public Locker(long lockTimeoutMillis)
Creates a locked with a lock timeout in milliseconds

Method Detail

lock

public void lock(Id id,
                 java.lang.String key)
          throws LockedException
Locks (or relocks) an item. If this key allready posesses the lock, the lock time will be renewed to the current time

Parameters:
id - the id of the item to lock
key - the key to use as lock
Throws:
LockedException - if the item was allready locked by another key Note that the exception will contain the key of the item, change this if keys are secrets

unlock

public void unlock(Id id,
                   java.lang.String key)
Unlocks the given id if it is still locked by the given key. Does nothing if the id was not locked by this key


isLockedBy

public boolean isLockedBy(Id id,
                          java.lang.String key)
Returns whether this id is currently locked by this key. Note that isLockedBy(a,b)==isLockedBy(a,b) is false even on single-thread use at some point in time because locks times out


isLockedByOther

public boolean isLockedByOther(Id id,
                               java.lang.String key)
Returns true if this id is currently locked, but by another key


getLockTimeoutMillis

public long getLockTimeoutMillis()
Returns the lock timeout of this locked in milliseconds


setLockTimeoutMillis

public void setLockTimeoutMillis(long lockTimeoutMillis)
Sets the lock timeout of this locked in missiseconds


clear

public void clear()
Clears all locks



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