new method to test if a particular file is or is related to the target of a lock
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / Lock.java
1 package uk.ac.vamsas.client.simpleclient;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.OutputStream;
10 import java.io.RandomAccessFile;
11 import java.nio.channels.ByteChannel;
12 import java.nio.channels.FileChannel;
13 import java.nio.channels.ReadableByteChannel;
14
15 import org.apache.commons.logging.LogFactory;
16
17 /**
18  * transient object representing a file lock
19  * This lock should hold for all processes interacting in a session.
20  * @author jimp
21  */
22
23 public abstract class Lock {
24   protected org.apache.commons.logging.Log log = LogFactory.getLog(Lock.class);
25   File target = null; // The file that is being locked
26   protected RandomAccessFile rafile=null;
27   
28   /**
29    * creates a valid Lock (test with <method>isLocked</method>)
30    * if a lock could be obtained for <param>lockfile</param>
31    * @param lockfile
32    */
33   protected Lock(java.io.File lockfile) {
34     target = lockfile;
35   }
36   /**
37    * test whether the given file is a target or related to the lock
38    * on the target file.
39    * @param afile a file
40    * @return true if target is locked and afile is related to target
41    */
42   public abstract boolean isTargetLockFile(File afile);
43   /**
44    * 
45    * @return true if lock is held on the target
46    */
47   public abstract boolean isLocked();
48   /**
49    * release lock and close all managed channels to file
50    *
51    */
52   public abstract void release();
53   /**
54    * optionally close the open random access channel on the file when releasing lock
55    * @param closeChannel
56    */
57   public abstract void release(boolean closeChannel);
58
59   /**
60    * gets Locked Stream for reading from
61    * @param atStart true to start reading at beginning of file.
62    * @return null if file not locked
63    * @throws IOException
64    */
65   public abstract FileInputStream getFileInputStream(boolean atStart) throws IOException;
66
67   /**
68    * gets Locked stream to write to
69    * FileInput always starts at the *end* of the file (after any truncation)
70    * @param clear true means file will be cleared to zero length
71    * @return null if file is not locked
72    * @throws IOException
73    */
74   public abstract FileOutputStream getFileOutputStream(boolean clear) throws IOException;
75   /**
76    * return buffered output stream to locked file.
77    * @param clear - true means file is truncated to 0 length before writing 
78    * @return
79    */
80   public abstract BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException;
81   
82   protected void finalize() throws Throwable {
83     target=null;
84   }
85   /**
86    * return buffered input stream for locked file.
87    * @param atStart - true means read from begining of file
88    * @return null if file is not locked.
89    */
90   public BufferedInputStream getBufferedInputStream(boolean atStart) throws IOException {
91     FileInputStream fis = getFileInputStream(atStart);
92     if (fis!=null)
93       return new BufferedInputStream(fis);
94     return null;
95   }
96   /**
97    * safe lock target length() function.
98    * @return -1 for non-lockable target, otherwise target's file length 
99    */
100   public abstract long length();
101   public abstract RandomAccessFile getRaFile() throws IOException;
102   public abstract FileChannel getRaChannel() throws IOException;
103 }