/** * */ package org.vamsas.client.simpleclient; import java.io.File; import org.vamsas.client.SimpleClient; /** * @author jim Watches a particular file for its creation, deletion, or * modification. */ public class FileWatcher { private File subject = null; private long lastStat[]; 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) * For locked files, the removal of a lock constitutes a change of * state if the file was modified. * * @param subject */ private boolean checkLock() { if (subject!=null && subject.exists()) { Lock tl = new Lock(subject); if (tl.isLocked()) { tl.release(); return false; } else { return true; } } return false; } private long[] getStat(File subject) { return new long[] { subject.lastModified(), subject.length() }; } private boolean compStat(long[] stat, long[] newstat) { if (stat[0]!=newstat[0] || stat[1]!=newstat[1]) return false; return true; } private boolean check() { if (subject != null) { if (!subject.exists()) { if (exists) { exists = false; return true; } return false; } else { long[] newStat = getStat(subject); // subject.lastModified(); if (exists && ((compStat(lastStat, newStat) || checkLock()))) { return false; } 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(); } }