net.sf.farrago.util
Class FarragoFileLockAllocation

java.lang.Object
  extended by net.sf.farrago.util.FarragoFileLockAllocation
All Implemented Interfaces:
FarragoAllocation, ClosableAllocation

public class FarragoFileLockAllocation
extends Object
implements FarragoAllocation

FarragoFileLockAllocation takes care of unlocking a file when it is closed.

Version:
$Id: //open/dev/farrago/src/net/sf/farrago/util/FarragoFileLockAllocation.java#16 $
Author:
John V. Sichi

Field Summary
private  FileChannel channel
           
private  File file
           
private  FileLock lock
           
private static int LOCK_OFFSET
          Location to lock.
private  RandomAccessFile randomAccessFile
           
private static Logger tracer
           
 
Constructor Summary
FarragoFileLockAllocation(FarragoAllocationOwner owner, File file, boolean tryLock)
          Creates a new FarragoFileLockAllocation by locking a file.
 
Method Summary
 void closeAllocation()
          Closes this object.
static void main(String[] args)
          Command-line entry point for bench testing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCK_OFFSET

private static final int LOCK_OFFSET
Location to lock. We lock a bogus byte way beyond any real data to make sure the lock doesn't interfere with I/O on operating systems with non-advisory lock semantics such as Windows. Don't use Long.MAX_VALUE because that breaks on any OS without large file support.

See Also:
Constant Field Values

tracer

private static final Logger tracer

file

private File file

randomAccessFile

private RandomAccessFile randomAccessFile

channel

private FileChannel channel

lock

private FileLock lock
Constructor Detail

FarragoFileLockAllocation

public FarragoFileLockAllocation(FarragoAllocationOwner owner,
                                 File file,
                                 boolean tryLock)
                          throws IOException
Creates a new FarragoFileLockAllocation by locking a file.

Parameters:
owner - the FarragoAllocationOwner which will be made responsible for the lock as a result of this call
file - the file to be locked for the lifetime of this allocation; if it does not exist, it will be created (but not deleted) automatically
tryLock - if true and lock cannot be obtained throw an exception; if false, wait for the lock instead
Throws:
IOException - if lock attempt failed
Method Detail

closeAllocation

public void closeAllocation()
Description copied from interface: ClosableAllocation
Closes this object.

Specified by:
closeAllocation in interface ClosableAllocation

main

public static void main(String[] args)
                 throws Exception
Command-line entry point for bench testing. Attempts to lock the file named by the one and only argument and hold the lock for 20 seconds.

TODO jvs 24-Aug-2005: Figure out a way to test this automatically. Requires starting two concurrent processes.

Parameters:
args - args[0] = name of file to lock
Throws:
Exception