import java.io.File;
+import org.vamsas.client.SimpleClient;
+
/**
- * @author jim
- * Watches a particular file for its creation, deletion, or modification.
+ * @author jim Watches a particular file for its creation, deletion, or
+ * modification.
*/
public class FileWatcher {
- private File subject=null;
+
+ private File subject = null;
+
private long lastStat;
- boolean exists=false;
+
+ boolean exists = 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)
+ * 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
*/
- private boolean check() {
+ private boolean checkLock() {
if (subject!=null) {
+ Lock tl = new Lock(subject);
+ if (tl.isLocked()) {
+ tl.release();
+ return false;
+ } else {
+ return true;
+ }
+ }
+ return false;
+ }
+ private boolean check() {
+ if (subject != null) {
if (!subject.exists()) {
if (exists) {
- exists=false;
- return true;
- }
- return false;
- } else {
- long newStat=subject.lastModified();
- if (exists && lastStat==newStat) {
+ exists = false;
+ return true;
+ }
return false;
+ } else {
+
+ long newStat = subject.lastModified();
+ if (exists && ((lastStat == newStat) || checkLock())) {
+ return false;
+ }
+ lastStat = newStat;
+ exists = true;
+ return true;
}
- lastStat=newStat;
- exists=true;
- return true;
}
- }
return false;
}
-
-
+
public FileWatcher(File subject) {
// TODO Auto-generated constructor stub
this.subject = subject;
check();
}
+
public boolean hasChanged() {
return check();
}