uk.org.ogsadai.common.files
Class FileLock

java.lang.Object
  |
  +--uk.org.ogsadai.common.files.FileLock

public class FileLock
extends java.lang.Object

This class implements a Multiple Reader, Single Writer (MRSW) mutual exclusion locking facility.

Threads wishing to obtain a lock on a file should call getReadLock or getWriteLock, and should call returnReadLock or returnWriteLock to relinquish the lock.

The MRSW lock permits either one thread to hold a write lock, or any number of threads to hold read locks. For example, if two threads have read locks and another thread calls getWriteLock to obtain a write lock, this thread waits until both reading threads have relinquished their read locks via returnReadLock.

Author:
The OGSA-DAI Project Team

Field Summary
private static java.lang.String COPYRIGHT_NOTICE
           
private static DAILogger LOG
           
private  java.util.Map readers
           
private  java.util.Map writers
           
 
Constructor Summary
FileLock()
           
 
Method Summary
private  java.lang.String canonical(java.lang.String fileName)
          Return the canonical version of the given filename, if at all possible.
 int countReaders(java.lang.String fileName)
          Return the number of threads with a read lock on a file.
 int countWriters(java.lang.String fileName)
          Return the number of threads with a write lock on a file.
private  void decrementReaders(java.lang.String fileName)
          Decrement the number of threads with a read lock on a file.
private  void decrementWriters(java.lang.String fileName)
          Decrement the number of threads with a write lock on a file.
 void doLocking(java.lang.String fileName, boolean lock, boolean read)
          Perform a locking or unlocking operation on a file.
 void getReadLock(java.lang.String fileName)
          Block until a read lock is available on a file, and obtain it.
 void getWriteLock(java.lang.String fileName)
          Block until a write lock is available on a file, and obtain it.
private  void incrementReaders(java.lang.String fileName)
          Increment the number of threads with a read lock on a file.
private  void incrementWriters(java.lang.String fileName)
          Increment the number of threads with a write lock on a file.
 void returnReadLock(java.lang.String fileName)
          Relinquish a read lock on a file.
 void returnWriteLock(java.lang.String fileName)
          Relinquish a write lock on a file.
private  void setReaders(java.lang.String fileName, int number)
          Set the number of threads with a read lock on a file.
private  void setWriters(java.lang.String fileName, int number)
          Set the number of threads with a write lock on a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYRIGHT_NOTICE

private static final java.lang.String COPYRIGHT_NOTICE
See Also:
Constant Field Values

LOG

private static DAILogger LOG

readers

private java.util.Map readers

writers

private java.util.Map writers
Constructor Detail

FileLock

public FileLock()
Method Detail

doLocking

public void doLocking(java.lang.String fileName,
                      boolean lock,
                      boolean read)
               throws FileLockException
Perform a locking or unlocking operation on a file.

Parameters:
fileName - Name of file.
lock - true to lock the file, false to unlock the file
read - true if the lock is a read lock, false if the lock is a write lock
Throws:
FileLockException - if an error occurs while waiting for a lock.

getReadLock

public void getReadLock(java.lang.String fileName)
                 throws FileLockException
Block until a read lock is available on a file, and obtain it.

Parameters:
fileName - Name of file.
Throws:
FileLockException - if an error occurs while waiting for the lock.

returnReadLock

public void returnReadLock(java.lang.String fileName)
                    throws NoExistingFileLockException
Relinquish a read lock on a file.

Parameters:
fileName - Name of file.
Throws:
NoExistingFileLockException - if there is no existing read lock.

getWriteLock

public void getWriteLock(java.lang.String fileName)
                  throws FileLockException
Block until a write lock is available on a file, and obtain it.

Parameters:
fileName - Name of file.
Throws:
FileLockException - if an error occurs while waiting for the lock.

returnWriteLock

public void returnWriteLock(java.lang.String fileName)
                     throws NoExistingFileLockException
Relinquish a write lock on a file.

Parameters:
fileName - Name of file.
Throws:
NoExistingFileLockException - if there is no existing write lock.

countReaders

public int countReaders(java.lang.String fileName)
Return the number of threads with a read lock on a file.

Parameters:
fileName - Name of file.
Returns:
the number of readers

countWriters

public int countWriters(java.lang.String fileName)
Return the number of threads with a write lock on a file.

Parameters:
fileName - Name of file.
Returns:
the number of writers

setReaders

private void setReaders(java.lang.String fileName,
                        int number)
Set the number of threads with a read lock on a file.

Parameters:
fileName - Name of file.
number - Number of readers.

setWriters

private void setWriters(java.lang.String fileName,
                        int number)
Set the number of threads with a write lock on a file.

Parameters:
fileName - Name of file.
number - Number of writers.

incrementReaders

private void incrementReaders(java.lang.String fileName)
Increment the number of threads with a read lock on a file.

Parameters:
fileName - Name of file.

decrementReaders

private void decrementReaders(java.lang.String fileName)
Decrement the number of threads with a read lock on a file.

Parameters:
fileName - Name of file.
Throws:
NoExistingFileLockException - if there is no existing read lock.

incrementWriters

private void incrementWriters(java.lang.String fileName)
Increment the number of threads with a write lock on a file.

Parameters:
fileName - Name of file.

decrementWriters

private void decrementWriters(java.lang.String fileName)
Decrement the number of threads with a write lock on a file.

Parameters:
fileName - Name of file.
Throws:
NoExistingFileLockException - if there is no existing write lock.

canonical

private java.lang.String canonical(java.lang.String fileName)
Return the canonical version of the given filename, if at all possible.

Parameters:
fileName - Name of file.
Returns:
the canonical filename