uk.ac.vamsas.client.simpleclient
Class Lock

java.lang.Object
  extended byuk.ac.vamsas.client.simpleclient.Lock
Direct Known Subclasses:
FileLock, NativeLock

public abstract class Lock
extends java.lang.Object

transient object representing a file lock This lock should hold for all processes interacting in a session.

Author:
jimp

Field Summary
protected  org.apache.commons.logging.Log log
           
protected  java.io.RandomAccessFile rafile
           
 
Constructor Summary
protected Lock(java.io.File lockfile)
          creates a valid Lock (test with isLocked) if a lock could be obtained for lockfile
 
Method Summary
protected  void finalize()
           
 java.io.BufferedInputStream getBufferedInputStream(boolean atStart)
          return buffered input stream for locked file.
abstract  java.io.BufferedOutputStream getBufferedOutputStream(boolean clear)
          return buffered output stream to locked file.
abstract  java.io.FileInputStream getFileInputStream(boolean atStart)
          gets Locked Stream for reading from
abstract  java.io.FileOutputStream getFileOutputStream(boolean clear)
          gets Locked stream to write to FileInput always starts at the *end* of the file (after any truncation)
abstract  java.nio.channels.FileChannel getRaChannel()
           
abstract  java.io.RandomAccessFile getRaFile()
           
abstract  boolean isLocked()
           
abstract  boolean isTargetLockFile(java.io.File afile)
          test whether the given file is a target or related to the lock on the target file.
abstract  long length()
          safe lock target length() function.
abstract  void release()
          release lock and close all managed channels to file
abstract  void release(boolean closeChannel)
          optionally close the open random access channel on the file when releasing lock
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected org.apache.commons.logging.Log log

rafile

protected java.io.RandomAccessFile rafile
Constructor Detail

Lock

protected Lock(java.io.File lockfile)
creates a valid Lock (test with isLocked) if a lock could be obtained for lockfile

Parameters:
lockfile -
Method Detail

isTargetLockFile

public abstract boolean isTargetLockFile(java.io.File afile)
test whether the given file is a target or related to the lock on the target file.

Parameters:
afile - a file
Returns:
true if target is locked and afile is related to target

isLocked

public abstract boolean isLocked()
Returns:
true if lock is held on the target

release

public abstract void release()
release lock and close all managed channels to file


release

public abstract void release(boolean closeChannel)
optionally close the open random access channel on the file when releasing lock

Parameters:
closeChannel -

getFileInputStream

public abstract java.io.FileInputStream getFileInputStream(boolean atStart)
                                                    throws java.io.IOException
gets Locked Stream for reading from

Parameters:
atStart - true to start reading at beginning of file.
Returns:
null if file not locked
Throws:
java.io.IOException

getFileOutputStream

public abstract java.io.FileOutputStream getFileOutputStream(boolean clear)
                                                      throws java.io.IOException
gets Locked stream to write to FileInput always starts at the *end* of the file (after any truncation)

Parameters:
clear - true means file will be cleared to zero length
Returns:
null if file is not locked
Throws:
java.io.IOException

getBufferedOutputStream

public abstract java.io.BufferedOutputStream getBufferedOutputStream(boolean clear)
                                                              throws java.io.IOException
return buffered output stream to locked file.

Parameters:
clear - - true means file is truncated to 0 length before writing
Returns:
Throws:
java.io.IOException

finalize

protected void finalize()
                 throws java.lang.Throwable
Throws:
java.lang.Throwable

getBufferedInputStream

public java.io.BufferedInputStream getBufferedInputStream(boolean atStart)
                                                   throws java.io.IOException
return buffered input stream for locked file.

Parameters:
atStart - - true means read from begining of file
Returns:
null if file is not locked.
Throws:
java.io.IOException

length

public abstract long length()
safe lock target length() function.

Returns:
-1 for non-lockable target, otherwise target's file length

getRaFile

public abstract java.io.RandomAccessFile getRaFile()
                                            throws java.io.IOException
Throws:
java.io.IOException

getRaChannel

public abstract java.nio.channels.FileChannel getRaChannel()
                                                    throws java.io.IOException
Throws:
java.io.IOException