4 package org.vamsas.client.simpleclient;
8 import org.vamsas.client.SimpleClient;
11 * @author jim Watches a particular file for its creation, deletion, or
14 public class FileWatcher {
16 private File subject = null;
18 private long lastStat[];
19 boolean waslocked=false;
20 boolean exists = false;
23 * Make a watcher for a particular file. If the file doesn't exist, the
24 * watcher will watch for its creation (and indicate a change of state)
25 * For locked files, the removal of a lock constitutes a change of
26 * state if the file was modified.
30 private boolean checkLock() {
31 if (subject!=null && subject.exists()) {
32 Lock tl = new Lock(subject);
43 private long[] getStat(File subject) {
44 return new long[] { subject.lastModified(), subject.length() };
46 private boolean compStat(long[] stat, long[] newstat) {
47 if (stat[0]!=newstat[0] || stat[1]!=newstat[1])
52 * Detect changes in file state and release of any
53 * lock in place during change.
54 * @return true if file state has changed
56 private boolean check() {
57 if (subject != null) {
58 if (!subject.exists()) {
68 // locked - state change registered after lock is released
71 long[] newStat = getStat(subject); // subject.lastModified();
73 // file is free to access, return state change
74 if (!exists || !compStat(lastStat, newStat)) {
90 public void setState() {
92 lastStat = getStat(subject);
93 exists = subject.exists();
94 waslocked = checkLock();
98 public FileWatcher(File subject) {
99 // TODO Auto-generated constructor stub
100 this.subject = subject;
104 public boolean hasChanged() {