X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fvamsas%2Fclient%2Fsimpleclient%2FFileWatcher.java;h=fe72381831db41936f19412eaf14752944d408c9;hb=5c0c34895c290164caad28e0aeac86b5a52ee655;hp=34897a85e0292c22d537e3f083b60d8d7d139ea3;hpb=a0f33613dd310add2b9ece2d170e47cfadef145d;p=vamsas.git diff --git a/src/org/vamsas/client/simpleclient/FileWatcher.java b/src/org/vamsas/client/simpleclient/FileWatcher.java index 34897a8..fe72381 100644 --- a/src/org/vamsas/client/simpleclient/FileWatcher.java +++ b/src/org/vamsas/client/simpleclient/FileWatcher.java @@ -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); + } }