remove old ebi packages
[vamsas.git] / src / org / vamsas / client / simpleclient / FileWatcher.java
index 34897a8..fe72381 100644 (file)
@@ -5,11 +5,12 @@ package org.vamsas.client.simpleclient;
 
 import java.io.File;
 
-import org.vamsas.client.SimpleClient;
 
 /**
- * @author jim Watches a particular file for its creation, deletion, or
- *         modification.
+ * Watches a particular file for its creation, deletion, or
+ * modification. The watcher is thread safe and different 
+ * instances watching the state of a particular file carry
+ * their own state record for the file.
  */
 public class FileWatcher {
 
@@ -23,21 +24,26 @@ public class FileWatcher {
    * to preserve new state of file for immediate reading.
    */
   private Lock subjectLock = null;
-  
+  /**
+   * clear local locks on subject.
+   *
+   */
   private void clearLock() {
     if (subjectLock!=null)
       subjectLock.release();
     subjectLock=null;
   }
-  
+  /**
+   *  
+   * @return true if subject exists and is locked by another process.
+   */
   private boolean checkLock() {
     if (subject!=null && subject.exists()) {
       if (subjectLock!=null) {
         subjectLock.release();
       }
-      subjectLock = new Lock(subject);
+      subjectLock = LockFactory.tryLock(subject);
       if (subjectLock.isLocked()) {
-        // subjectLock.release();
         return false;
       }
       clearLock();
@@ -93,12 +99,16 @@ public class FileWatcher {
     }
     return false;
   }
-  
+  /**
+   * updates internal record of file state when caller has intentionally
+   * modified subject. (ignores locked-state of subject)
+   */
   public void setState() {
     if (subject!=null) {
-      lastStat = getStat(subject);
-      exists = subject.exists();
-      waslocked = checkLock();
+      if (exists = subject.exists()) {
+        lastStat = getStat(subject);
+        waslocked = false;
+      }
     }
   }
   
@@ -112,7 +122,6 @@ public class FileWatcher {
    */
   
   public FileWatcher(File subject) {
-    // TODO Auto-generated constructor stub
     this.subject = subject;
     setState();
   }
@@ -139,4 +148,21 @@ public class FileWatcher {
       return null;
     }
   }
+  /**
+   * safely? getting current state of the watched file
+   * @return
+   */
+  public long[] getCurrentState() {
+    return getStat(subject);
+  }
+  /**
+   * safely compare an externally recorded state with the current state
+   * for significant modifications.
+   * @param a
+   * @param b
+   * @return
+   */
+  public boolean diffState(long[] a, long[] b) {
+    return compStat(a,b);
+  }
 }