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 {
* 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();
}
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;
+ }
}
}
*/
public FileWatcher(File subject) {
- // TODO Auto-generated constructor stub
this.subject = subject;
setState();
}
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);
+ }
}