1 package org.vamsas.client.simpleclient;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.RandomAccessFile;
11 import org.apache.commons.logging.LogFactory;
14 * transient object representing a file lock
15 * This lock should hold for all processes interacting in a session.
19 public abstract class Lock {
20 protected org.apache.commons.logging.Log log = LogFactory.getLog(Lock.class);
21 File target = null; // The file that is being locked
22 protected RandomAccessFile rafile=null;
25 * creates a valid Lock (test with <method>isLocked</method>)
26 * if a lock could be obtained for <param>lockfile</param>
29 protected Lock(java.io.File lockfile) {
34 * @return true if lock is held on the target
36 public abstract boolean isLocked();
38 * release lock and close all managed channels to file
41 public abstract void release();
43 * optionally close the open random access channel on the file when releasing lock
46 public abstract void release(boolean closeChannel);
49 * gets Locked Stream for reading from
50 * @param atStart true to start reading at beginning of file.
51 * @return null if file not locked
54 public abstract FileInputStream getFileInputStream(boolean atStart) throws IOException;
57 * gets Locked stream to write to
58 * FileInput always starts at the *end* of the file (after any truncation)
59 * @param clear true means file will be cleared to zero length
60 * @return null if file is not locked
63 public abstract FileOutputStream getFileOutputStream(boolean clear) throws IOException;
65 * return buffered output stream to locked file.
66 * @param clear - true means file is truncated to 0 length before writing
69 public abstract BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException;
71 protected void finalize() throws Throwable {
75 * return buffered input stream for locked file.
76 * @param atStart - true means read from begining of file
77 * @return null if file is not locked.
79 public BufferedInputStream getBufferedInputStream(boolean atStart) throws IOException {
80 FileInputStream fis = getFileInputStream(atStart);
82 return new BufferedInputStream(fis);