FileDocCategorySizeDatePackage
Lock.javaAPI DocExample7116Sat Feb 01 06:56:50 GMT 1997imaginary.persist

Lock

public class Lock extends UnicastRemoteObject implements RemoteLock
This class represents a lock held by a lock holder over one or more modified persistent objects. Each time a new object is locked by the Lock, it spawns a thread for monitoring that lock.

Fields Summary
private static Hashtable
locks
Class attributes
private RemoteLockHolder
lock_holder
Instance attributes
private Hashtable
threads
private Transaction
transaction
Constructors Summary
public Lock(RemoteLockHolder client)
Constructs a new lock for the remote lock holder.

param
client the remote lock holder holding this lock
exception
imaginary.persist.LockException An error occurred finding a Transaction for this lock.
exception
java.rmi.RemoteException An error occurred setting the lock in the remote lock holder.


                                                   
      
       
        super();
        lock_holder = client;
        lock_holder.setLock(this);
        try {
            transaction = Transaction.getTransaction();
        }
        catch( PersistenceException e ) {
            throw new LockException(e);
        }
    
Methods Summary
public static imaginary.persist.LockcreateLock(RemoteLockHolder holder, Persistent p)
Checks to see if the lock holder already has a lock associated with it. If so, return it. If not, create a new one. Either way, spawn a for this persistent.

param
holder the remote object holding the lock
param
p the persistent object being locked
exception
imaginary.persist.LockException


       
                                                           
          
      
        Lock l;
        
        if( locks.containsKey(holder) ) {
            l = (Lock)locks.get(holder);
            l.createThread(p);
        }
        else {
            try {
                l = new Lock(holder);
            }
            catch( RemoteException e ) {
                throw new LockException(e.getMessage());
            }
            l.createThread(p);
            locks.put(holder, l);
        }
        return l;
    
public voidcreateThread(Persistent p)
Creates a new LockThread for monitoring a new persistent object locked by this lock.

param
p the Persistent instance being locked
exception
imaginary.persist.LockException An error occurred creating the LockThread.

        if( threads.containsKey(p) ) {
            return;
        }
        else {
            LockThread thread = new LockThread(this, p);

            threads.put(p, thread);
            transaction.addPersistent(p);
        }
    
public RemoteLockHoldergetHolder()

return
the remote object holding this lock

        return lock_holder;
    
public TransactiongetTransaction()

return
the Transaction associated with this lock

        return transaction;
    
public voidloseLock(Persistent p)
If a problem occurs while a thread is monitoring a lock, it calls this method. This method then triggers loseLock() in both the thread and persistent so they can clean up after the loss of the lock.

param
p the persistent whose lock was lost

        if( !threads.containsKey(p) ) {
            return;
        }
        else {
            LockThread thread = (LockThread)threads.get(p);

            thread.loseLock();
            p.loseLock();
            threads.remove(p);
            transaction.removePersistent(p);
        }
    
public voidmonitorLock(Persistent p)
This method is triggered periodically by the LockThread to monitor the lock and make sure the client/server connection is still there.

param
p the perisistent object whose lock is being checked

        p.monitorLock();
        if( !threads.containsKey(p) ) {
            return;
        }
        else {
            lock_holder.monitorLock(p);
        }
    
public voidreleaseLock(Persistent p)
This method signals a normal end to a lock, normally because the client saved its objects.

param
p the persistent being released

        if( !threads.containsKey(p) ) {
            return;
        }
        else {
            LockThread thread = (LockThread)threads.get(p);
            
            thread.releaseLock();
            threads.remove(p);
        }
    
public synchronized voidsave()
Triggers the save process for all locked objects.

exception
imaginary.persist.PersistenceException An error occurred during the save.

        transaction.save();