modified FileWatcher to only register file change after a lock release (incurs a...
[vamsas.git] / src / org / vamsas / client / simpleclient / FileWatcher.java
1 /**
2  * 
3  */
4 package org.vamsas.client.simpleclient;
5
6 import java.io.File;
7
8 import org.vamsas.client.SimpleClient;
9
10 /**
11  * @author jim Watches a particular file for its creation, deletion, or
12  *         modification.
13  */
14 public class FileWatcher {
15
16   private File subject = null;
17
18   private long lastStat;
19
20   boolean exists = false;
21
22   /**
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.
27    * 
28    * @param subject
29    */
30   private boolean checkLock() {
31     if (subject!=null) {
32       Lock tl = new Lock(subject);
33       if (tl.isLocked()) {
34         tl.release();
35         return false;
36       } else {
37         return true;
38       }
39     }
40     return false;
41   }
42   private boolean check() {
43     if (subject != null) {
44       if (!subject.exists()) {
45         if (exists) {
46           exists = false;
47           return true;
48         }
49         return false;
50       } else {
51         
52         long newStat = subject.lastModified();
53         if (exists && ((lastStat == newStat) || checkLock())) {
54           return false;
55         }
56         lastStat = newStat;
57         exists = true;
58         return true;
59       }
60     }
61     return false;
62   }
63
64   public FileWatcher(File subject) {
65     // TODO Auto-generated constructor stub
66     this.subject = subject;
67     check();
68   }
69
70   public boolean hasChanged() {
71     return check();
72   }
73 }