From f1f7e1f9ce8cadb3372c8b423d03b5cd1000991f Mon Sep 17 00:00:00 2001 From: jprocter Date: Sat, 22 Oct 2005 21:58:11 +0000 Subject: [PATCH] debugged fileWatcher logic and Lock release function. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@72 be28352e-c001-0410-b1a7-c7978e42abec --- .../vamsas/client/simpleclient/FileWatcher.java | 50 +++++++++++++++----- src/org/vamsas/client/simpleclient/Lock.java | 23 +++++---- 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/org/vamsas/client/simpleclient/FileWatcher.java b/src/org/vamsas/client/simpleclient/FileWatcher.java index 84a5e0a..bc9f705 100644 --- a/src/org/vamsas/client/simpleclient/FileWatcher.java +++ b/src/org/vamsas/client/simpleclient/FileWatcher.java @@ -16,7 +16,7 @@ public class FileWatcher { private File subject = null; private long lastStat[]; - + boolean waslocked=false; boolean exists = false; /** @@ -33,12 +33,13 @@ public class FileWatcher { if (tl.isLocked()) { tl.release(); return false; - } else { - return true; } + tl.release(); + return true; } return false; } + private long[] getStat(File subject) { return new long[] { subject.lastModified(), subject.length() }; } @@ -47,32 +48,57 @@ public class FileWatcher { 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 + */ private boolean check() { if (subject != null) { if (!subject.exists()) { if (exists) { - exists = false; - return true; + if (!waslocked) { + // !checkLock()) { + + exists = false; + // waslocked=false; + return true; + } } + // locked - state change registered after lock is released return false; } else { - long[] newStat = getStat(subject); // subject.lastModified(); - if (exists && ((compStat(lastStat, newStat) || checkLock()))) { + if (!checkLock()) { + // file is free to access, return state change + if (!exists || !compStat(lastStat, newStat)) { + waslocked=false; + exists=true; + lastStat=newStat; + return true; + } + // no change + return false; + } else { + waslocked=true; return false; } - lastStat = newStat; - exists = true; - return true; } } return false; } - + public void setState() { + if (subject!=null) { + lastStat = getStat(subject); + exists = subject.exists(); + waslocked = checkLock(); + } + } + public FileWatcher(File subject) { // TODO Auto-generated constructor stub this.subject = subject; - check(); + setState(); } public boolean hasChanged() { diff --git a/src/org/vamsas/client/simpleclient/Lock.java b/src/org/vamsas/client/simpleclient/Lock.java index a185233..ab159ba 100644 --- a/src/org/vamsas/client/simpleclient/Lock.java +++ b/src/org/vamsas/client/simpleclient/Lock.java @@ -16,7 +16,7 @@ import java.nio.channels.FileLock; public class Lock { FileLock lock = null; - RandomAccessFile rafile; + RandomAccessFile rafile=null; /** * creates a valid Lock (test with isLocked) * if a lock could be obtained for lockfile @@ -25,7 +25,7 @@ public class Lock { public Lock(java.io.File lockfile) { // try and get a lock. lock = null; - + try { if (!lockfile.exists()) if (!lockfile.createNewFile()) { @@ -43,7 +43,7 @@ public class Lock { e.printStackTrace(); } } - + boolean isLocked() { if (lock != null && lock.isValid()) { return true; @@ -51,16 +51,19 @@ public class Lock { return false; } public void release() { - if (lock!=null) { - try { - rafile.close(); + try { + if (lock!=null && lock.isValid()) lock.release(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(System.err); - } + if (rafile!=null && rafile.getChannel().isOpen()) + rafile.getChannel().close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(System.err); } + lock=null; + rafile=null; } + /* Explicitly release lock (probably don't need to do this!) * @see java.lang.Object#finalize() */ -- 1.7.10.2