- * 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)
- * @param subject
+ * transient lock on subject - can be passed back to calling class
+ * to preserve new state of file for immediate reading.
+ */
+ private Lock subjectLock = null;
+
+ private void clearLock() {
+ if (subjectLock!=null)
+ subjectLock.release();
+ subjectLock=null;
+ }
+
+ private boolean checkLock() {
+ if (subject!=null && subject.exists()) {
+ if (subjectLock!=null) {
+ subjectLock.release();
+ }
+ subjectLock = new Lock(subject);
+ if (subjectLock.isLocked()) {
+ // subjectLock.release();
+ return false;
+ }
+ clearLock();
+ 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;
+ }
+ /**
+ * Detect changes in file state and release of any
+ * lock in place during change.
+ * @return true if file state has changed. Leaves lock in subjectLock (ready to be passed to caller)