applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / FileWatcher.java
index 93df102..1fc1476 100644 (file)
-/**
- * 
- */
-package uk.ac.vamsas.client.simpleclient;
-
-import java.io.File;
-
-
-/**
- * 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 {
-
-  private File subject = null;
-
-  private long lastStat[];
-  boolean waslocked=false;
-  boolean exists = false;
-  /**
-   * transient lock on subject - can be passed back to calling class
-   * 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 = LockFactory.tryLock(subject);
-      if (subjectLock.isLocked()) {
-        return false;
-      }
-      clearLock();
-      return true;
-    }
-    return false;
-  }
-  
-  private long[] getStat(File subject) {
-    return new long[] { subject.lastModified(), subject.length() };
-  }
-  private boolean compStat(long[] stat, long[] newstat) {
-    if (stat[0]!=newstat[0] || stat[1]!=newstat[1])
-      return false;
-    return true;
-  }
-  /**
-   * Detect changes in file state and release of any
-   * lock in place during change.
-   * @return true if file state has changed. Leaves lock in subjectLock (ready to be passed to caller)
-   */
-  private boolean check() {
-    if (subject != null) {
-      if (!subject.exists()) {
-        if (exists) {
-          if (!waslocked) {
-            // !checkLock()) {
-          
-            exists = false;
-            // waslocked=false;
-            return true;
-          }
-        }
-        // locked - state change registered after lock is released
-        return false;
-      } else {
-        long[] newStat = getStat(subject); // subject.lastModified();
-        if (!checkLock()) {
-          // file is free to access, return state change
-          if (!exists || !compStat(lastStat, newStat)) {
-            waslocked=false;
-            exists=true;
-            lastStat=newStat;
-            return true;
-          }
-          // no change
-          return false;
-        } else {
-          waslocked=true;
-          return false;
-        }
-      }
-    }
-    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) {
-      if (exists = subject.exists()) {
-        lastStat = getStat(subject);
-        waslocked = false;
-      }
-    }
-  }
-  
-  /**
-   * Make a watcher for a particular file. If the file doesn't exist, the
-   * watcher will watch for its creation (and indicate a change of state) 
-   * For locked files, the removal of a lock constitutes a change of 
-   * state if the file was modified.
-   * 
-   * @param subject
-   */
-  
-  public FileWatcher(File subject) {
-    this.subject = subject;
-    setState();
-  }
-  /**
-   * Test for change in file state. Only indicates a change 
-   * after any lock on a file has been released.
-   * @return true if file has been modified.
-   */
-  public boolean hasChanged() {
-    boolean res = check();
-    clearLock();
-    return res;
-  }
-  /**
-   * passes lock back to caller if hasChanged returned true.
-   * @return
-   */
-  public Lock getChangedState() {
-    boolean res = check();
-    if (res)
-      return subjectLock;
-    else {
-      clearLock();
-      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);
-  }
-  /**
-   * @return the subject
-   */
-  public File getSubject() {
-    return subject;
-  }
-}
+/*\r
+ * This file is part of the Vamsas Client version 0.1. \r
+ * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
+ *  Andrew Waterhouse and Dominik Lindner.\r
+ * \r
+ * Earlier versions have also been incorporated into Jalview version 2.4 \r
+ * since 2008, and TOPALi version 2 since 2007.\r
+ * \r
+ * The Vamsas Client is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU Lesser General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *  \r
+ * The Vamsas Client is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public License\r
+ * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package uk.ac.vamsas.client.simpleclient;\r
+\r
+import java.io.File;\r
+\r
+/**\r
+ * Watches a particular file for its creation, deletion, or modification. The\r
+ * watcher is thread safe and different instances watching the state of a\r
+ * particular file carry their own state record for the file.\r
+ */\r
+public class FileWatcher {\r
+\r
+  private File subject = null;\r
+\r
+  private long lastStat[];\r
+\r
+  boolean waslocked = false;\r
+\r
+  boolean exists = false;\r
+\r
+  /**\r
+   * transient lock on subject - can be passed back to calling class to preserve\r
+   * new state of file for immediate reading.\r
+   */\r
+  private Lock subjectLock = null;\r
+\r
+  /**\r
+   * clear local locks on subject.\r
+   * \r
+   */\r
+  private void clearLock() {\r
+    if (subjectLock != null)\r
+      subjectLock.release();\r
+    subjectLock = null;\r
+  }\r
+\r
+  /**\r
+   * \r
+   * @return true if subject exists and is locked by another process.\r
+   */\r
+  private boolean checkLock() {\r
+    if (subject != null && subject.exists()) {\r
+      if (subjectLock != null) {\r
+        subjectLock.release();\r
+      }\r
+      subjectLock = LockFactory.tryLock(subject);\r
+      if (subjectLock.isLocked()) {\r
+        return false;\r
+      }\r
+      clearLock();\r
+      return true;\r
+    }\r
+    return false;\r
+  }\r
+\r
+  private long[] getStat(File subject) {\r
+    return new long[] { subject.lastModified(), subject.length() };\r
+  }\r
+\r
+  private boolean compStat(long[] stat, long[] newstat) {\r
+    if (stat[0] != newstat[0] || stat[1] != newstat[1])\r
+      return false;\r
+    return true;\r
+  }\r
+\r
+  /**\r
+   * Detect changes in file state and release of any lock in place during\r
+   * change.\r
+   * \r
+   * @return true if file state has changed. Leaves lock in subjectLock (ready\r
+   *         to be passed to caller)\r
+   */\r
+  private boolean check() {\r
+    if (subject != null) {\r
+      if (!subject.exists()) {\r
+        if (exists) {\r
+          if (!waslocked) {\r
+            // !checkLock()) {\r
+\r
+            exists = false;\r
+            // waslocked=false;\r
+            return true;\r
+          }\r
+        }\r
+        // locked - state change registered after lock is released\r
+        return false;\r
+      } else {\r
+        long[] newStat = getStat(subject); // subject.lastModified();\r
+        if (!checkLock()) {\r
+          // file is free to access, return state change\r
+          if (!exists || !compStat(lastStat, newStat)) {\r
+            waslocked = false;\r
+            exists = true;\r
+            lastStat = newStat;\r
+            return true;\r
+          }\r
+          // no change\r
+          return false;\r
+        } else {\r
+          waslocked = true;\r
+          return false;\r
+        }\r
+      }\r
+    }\r
+    return false;\r
+  }\r
+\r
+  /**\r
+   * updates internal record of file state when caller has intentionally\r
+   * modified subject. (ignores locked-state of subject)\r
+   */\r
+  public void setState() {\r
+    if (subject != null) {\r
+      if (exists = subject.exists()) {\r
+        lastStat = getStat(subject);\r
+        waslocked = false;\r
+      }\r
+    }\r
+  }\r
+\r
+  /**\r
+   * Make a watcher for a particular file. If the file doesn't exist, the\r
+   * watcher will watch for its creation (and indicate a change of state) For\r
+   * locked files, the removal of a lock constitutes a change of state if the\r
+   * file was modified.\r
+   * \r
+   * @param subject\r
+   */\r
+\r
+  public FileWatcher(File subject) {\r
+    this.subject = subject;\r
+    setState();\r
+  }\r
+\r
+  /**\r
+   * Test for change in file state. Only indicates a change after any lock on a\r
+   * file has been released.\r
+   * \r
+   * @return true if file has been modified.\r
+   */\r
+  public boolean hasChanged() {\r
+    boolean res = check();\r
+    clearLock();\r
+    return res;\r
+  }\r
+\r
+  /**\r
+   * passes lock back to caller if hasChanged returned true.\r
+   * \r
+   * @return\r
+   */\r
+  public Lock getChangedState() {\r
+    boolean res = check();\r
+    if (res)\r
+      return subjectLock;\r
+    else {\r
+      clearLock();\r
+      return null;\r
+    }\r
+  }\r
+\r
+  /**\r
+   * safely? getting current state of the watched file\r
+   * \r
+   * @return\r
+   */\r
+  public long[] getCurrentState() {\r
+    return getStat(subject);\r
+  }\r
+\r
+  /**\r
+   * safely compare an externally recorded state with the current state for\r
+   * significant modifications.\r
+   * \r
+   * @param a\r
+   * @param b\r
+   * @return\r
+   */\r
+  public boolean diffState(long[] a, long[] b) {\r
+    return compStat(a, b);\r
+  }\r
+\r
+  /**\r
+   * @return the subject\r
+   */\r
+  public File getSubject() {\r
+    return subject;\r
+  }\r
+}\r