a1247452bebf4033e00be5c50896c1e67ba2d307
[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    * 
38    * @return true if lock is held on the target
39    */
40   public abstract boolean isLocked();
41   /**
42    * release lock and close all managed channels to file
43    *
44    */
45   public abstract void release();
46   /**
47    * optionally close the open random access channel on the file when releasing lock
48    * @param closeChannel
49    */
50   public abstract void release(boolean closeChannel);
51
52   /**
53    * gets Locked Stream for reading from
54    * @param atStart true to start reading at beginning of file.
55    * @return null if file not locked
56    * @throws IOException
57    */
58   public abstract FileInputStream getFileInputStream(boolean atStart) throws IOException;
59
60   /**
61    * gets Locked stream to write to
62    * FileInput always starts at the *end* of the file (after any truncation)
63    * @param clear true means file will be cleared to zero length
64    * @return null if file is not locked
65    * @throws IOException
66    */
67   public abstract FileOutputStream getFileOutputStream(boolean clear) throws IOException;
68   /**
69    * return buffered output stream to locked file.
70    * @param clear - true means file is truncated to 0 length before writing 
71    * @return
72    */
73   public abstract BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException;
74   
75   protected void finalize() throws Throwable {
76     target=null;
77   }
78   /**
79    * return buffered input stream for locked file.
80    * @param atStart - true means read from begining of file
81    * @return null if file is not locked.
82    */
83   public BufferedInputStream getBufferedInputStream(boolean atStart) throws IOException {
84     FileInputStream fis = getFileInputStream(atStart);
85     if (fis!=null)
86       return new BufferedInputStream(fis);
87     return null;
88   }
89   /**
90    * safe lock target length() function.
91    * @return -1 for non-lockable target, otherwise target's file length 
92    */
93   public abstract long length();
94   public abstract RandomAccessFile getRaFile() throws IOException;
95   public abstract FileChannel getRaChannel() throws IOException;
96 }