todo for importing stored session as a new session
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / FileLock.java
index 4ab0a65..40ee9cf 100644 (file)
@@ -34,10 +34,13 @@ public class FileLock extends Lock {
     try { 
       advisory=new NativeLock(_lock, block);
     } catch (Exception e) {
-      log.fatal("Failed to create advisory lock file "+_lock,e);
-      throw new Error("Failed to create advisory lock file "+_lock);          
+      if (!_lock.exists()) {
+        // advisory cannot be created. this is serious.
+        log.fatal("Failed to create advisory lock file "+_lock,e);
+        throw new Error("Failed to create advisory lock file "+_lock);
+      }
     }
-    return advisory.isLocked();
+    return (advisory!=null) && advisory.isLocked();
   }
   /**
    * call to clear up a filelock file after its been made
@@ -45,9 +48,10 @@ public class FileLock extends Lock {
    */
   private void tidy() {
     if (_lock!=null) { 
-      if ( advisory!=null) 
+      if ( advisory!=null) {
+        advisory.target.deleteOnExit(); // release will null the target
         advisory.release(true);
-      advisory.target.deleteOnExit();
+      }
       advisory=null;
       _lock=null;
     }
@@ -60,7 +64,7 @@ public class FileLock extends Lock {
     super(lockfile);
     // try and get a lock.
     try {
-      _lock = new File(lockfile.getParentFile(), lockfile.getName()+"."+_LockSuffix);
+      _lock = make_lockForTarget(lockfile);
       if (!ensureLockFile(block)) {
         log.debug("Couldn't get lock on "+_lock);
         tidy();
@@ -83,7 +87,14 @@ public class FileLock extends Lock {
           + lockfile.getAbsolutePath(),e);
     }
   }
-  
+  /**
+   * 
+   * @param lockfile target of lock
+   * @return file object that will be locked for lockfile
+   */
+  private File make_lockForTarget(File lockfile) {
+    return new File(lockfile.getParentFile(), lockfile.getName()+"."+_LockSuffix);
+  }
   private boolean openRaFile() throws IOException {
     if (target==null)
       return false;
@@ -116,16 +127,18 @@ public class FileLock extends Lock {
   public void release(boolean closeChannel) {
     if (!isLocked())
       return;
-    if (log.isDebugEnabled())
-      log.debug("Releasing advisory lock on "+target);
-    if (closeChannel) {
-      if (rafile!=null) 
+    if (rafile!=null) {
+      if (closeChannel) {
         try {
           rafile.close();
         } catch (Exception e) {
           log.debug("Unexpected exception whilst closing RandomAccessFile on "+target, e);
         }
-        rafile=null; 
+        rafile=null; // do not hold reference to rafile anymore either
+      }
+      if (log.isDebugEnabled())
+        log.debug("Releasing advisory lock on "+target);
+      // TODO: LATER: verify this change in closeChannel semantics really is correct - ArchiveClient works correctly
     }
     tidy();
   }
@@ -202,4 +215,12 @@ public class FileLock extends Lock {
     log.debug("Failed to getRaChannel on target "+target);
     return null;
   }
+  public boolean isTargetLockFile(File afile) {
+    if (isLocked())
+    {
+      if (target.equals(afile) || make_lockForTarget(target).equals(afile))
+        return true;
+    }
+    return false;
+  }
 }