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 * updates internal record of file state when caller has intentionally
100 public void setState() {
102 if (exists = subject.exists()) {
103 lastStat = getStat(subject);
110 * Make a watcher for a particular file. If the file doesn't exist, the
111 * watcher will watch for its creation (and indicate a change of state)
112 * For locked files, the removal of a lock constitutes a change of
113 * state if the file was modified.
118 public FileWatcher(File subject) {
119 // TODO Auto-generated constructor stub
120 this.subject = subject;
124 * Test for change in file state. Only indicates a change
125 * after any lock on a file has been released.
126 * @return true if file has been modified.
128 public boolean hasChanged() {
129 boolean res = check();
134 * passes lock back to caller if hasChanged returned true.
137 public Lock getChangedState() {
138 boolean res = check();