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;
22 * transient lock on subject - can be passed back to calling class
23 * to preserve new state of file for immediate reading.
25 private Lock subjectLock = null;
27 private void clearLock() {
28 if (subjectLock!=null)
29 subjectLock.release();
33 private boolean checkLock() {
34 if (subject!=null && subject.exists()) {
35 if (subjectLock!=null) {
36 subjectLock.release();
38 subjectLock = new Lock(subject);
39 if (subjectLock.isLocked()) {
40 // subjectLock.release();
49 private long[] getStat(File subject) {
50 return new long[] { subject.lastModified(), subject.length() };
52 private boolean compStat(long[] stat, long[] newstat) {
53 if (stat[0]!=newstat[0] || stat[1]!=newstat[1])
58 * Detect changes in file state and release of any
59 * lock in place during change.
60 * @return true if file state has changed. Leaves lock in subjectLock (ready to be passed to caller)
62 private boolean check() {
63 if (subject != null) {
64 if (!subject.exists()) {
74 // locked - state change registered after lock is released
77 long[] newStat = getStat(subject); // subject.lastModified();
79 // file is free to access, return state change
80 if (!exists || !compStat(lastStat, newStat)) {
97 public void setState() {
99 lastStat = getStat(subject);
100 exists = subject.exists();
101 waslocked = checkLock();
106 * Make a watcher for a particular file. If the file doesn't exist, the
107 * watcher will watch for its creation (and indicate a change of state)
108 * For locked files, the removal of a lock constitutes a change of
109 * state if the file was modified.
114 public FileWatcher(File subject) {
115 // TODO Auto-generated constructor stub
116 this.subject = subject;
120 * Test for change in file state. Only indicates a change
121 * after any lock on a file has been released.
122 * @return true if file has been modified.
124 public boolean hasChanged() {
125 boolean res = check();
130 * passes lock back to caller if hasChanged returned true.
133 public Lock getChangedState() {
134 boolean res = check();