introducing Lock File based locking (for portability) - part implemented.
[vamsas.git] / src / org / vamsas / client / simpleclient / Lock.java
index 300ea0e..9dd472f 100644 (file)
@@ -1,78 +1,85 @@
 package org.vamsas.client.simpleclient;
 
-import java.io.FileNotFoundException;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.RandomAccessFile;
-import java.nio.channels.FileLock;
+
+import org.apache.commons.logging.LogFactory;
 
 /**
  * transient object representing a file lock
- * 
- * 
+ * This lock should hold for all processes interacting in a session.
  * @author jimp
- * 
  */
 
-public class Lock {
-  FileLock lock = null;
-  RandomAccessFile rafile=null;
+public abstract class Lock {
+  protected org.apache.commons.logging.Log log = LogFactory.getLog(Lock.class);
+  File target = null; // The file that is being locked
+  protected RandomAccessFile rafile=null;
+  
   /**
    * creates a valid Lock (test with <method>isLocked</method>)
    * if a lock could be obtained for <param>lockfile</param>
    * @param lockfile
    */
-  public Lock(java.io.File lockfile) {
-    // try and get a lock.
-    lock = null;
-    
-    try {
-      if (!lockfile.exists())
-        if (!lockfile.createNewFile()) {
-          return;
-        }
-      
-      lock = (rafile=new RandomAccessFile(lockfile,"rw")).getChannel().tryLock();
-      if (lock==null || !lock.isValid())
-        // failed to get lock. Close the file channel
-        rafile.getChannel().close();
-    } catch (FileNotFoundException e) {
-      System.err.println("Error! Couldn't create a lockfile at "
-          + lockfile.getAbsolutePath());
-      e.printStackTrace();
-    } catch (IOException e) {
-      System.err.println("Error! Problems with IO when creating a lock on "
-          + lockfile.getAbsolutePath());
-      e.printStackTrace();
-    }
+  protected Lock(java.io.File lockfile) {
+    target = lockfile;
   }
+  /**
+   * 
+   * @return true if lock is held on the target
+   */
+  public abstract boolean isLocked();
+  /**
+   * release lock and close all managed channels to file
+   *
+   */
+  public abstract void release();
+  /**
+   * optionally close the open random access channel on the file when releasing lock
+   * @param closeChannel
+   */
+  public abstract void release(boolean closeChannel);
+
+  /**
+   * gets Locked Stream for reading from
+   * @param atStart true to start reading at beginning of file.
+   * @return null if file not locked
+   * @throws IOException
+   */
+  public abstract FileInputStream getFileInputStream(boolean atStart) throws IOException;
+
+  /**
+   * gets Locked stream to write to
+   * FileInput always starts at the *end* of the file (after any truncation)
+   * @param clear true means file will be cleared to zero length
+   * @return null if file is not locked
+   * @throws IOException
+   */
+  public abstract FileOutputStream getFileOutputStream(boolean clear) throws IOException;
+  /**
+   * return buffered output stream to locked file.
+   * @param clear - true means file is truncated to 0 length before writing 
+   * @return
+   */
+  public abstract BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException;
   
-  boolean isLocked() {
-    if (lock != null && lock.isValid()) {
-      return true;
-    }
-    return false;
-  }
-  public void release() {
-    try {
-      if (lock!=null && lock.isValid())
-        lock.release();
-      if (rafile!=null && rafile.getChannel().isOpen())
-        rafile.getChannel().close();
-    } catch (IOException e) {
-      // TODO Auto-generated catch block
-      e.printStackTrace(System.err);
-    }
-    lock=null;
-    rafile=null;
+  protected void finalize() throws Throwable {
+    target=null;
   }
-  
-  /* Explicitly release lock (probably don't need to do this!)
-   * @see java.lang.Object#finalize()
+  /**
+   * return buffered input stream for locked file.
+   * @param atStart - true means read from begining of file
+   * @return null if file is not locked.
    */
-  protected void finalize() throws Throwable {
-    release();
-    super.finalize();
+  public BufferedInputStream getBufferedInputStream(boolean atStart) throws IOException {
+    FileInputStream fis = getFileInputStream(atStart);
+    if (fis!=null)
+      return new BufferedInputStream(fis);
+    return null;
   }
-  
 }