X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fuk%2Fac%2Fvamsas%2Fclient%2Fsimpleclient%2FFileLock.java;h=8a340c319d60bc81380637707441f7e9044b0255;hb=f5e6905752f20e5f77d5ada2a216c3cb2b50907d;hp=ef29f47b52b45b8008e19002656e2e7b699932b5;hpb=c8c7209ad474cce3a5c0e998644ab0eaa0ce9912;p=vamsas.git diff --git a/src/uk/ac/vamsas/client/simpleclient/FileLock.java b/src/uk/ac/vamsas/client/simpleclient/FileLock.java index ef29f47..8a340c3 100644 --- a/src/uk/ac/vamsas/client/simpleclient/FileLock.java +++ b/src/uk/ac/vamsas/client/simpleclient/FileLock.java @@ -34,10 +34,13 @@ public class FileLock extends Lock { try { advisory=new NativeLock(_lock, block); } catch (Exception e) { - log.fatal("Failed to create advisory lock file "+_lock,e); - throw new Error("Failed to create advisory lock file "+_lock); + if (!_lock.exists()) { + // advisory cannot be created. this is serious. + log.fatal("Failed to create advisory lock file "+_lock,e); + throw new Error("Failed to create advisory lock file "+_lock); + } } - return advisory.isLocked(); + return (advisory!=null) && advisory.isLocked(); } /** * call to clear up a filelock file after its been made @@ -45,9 +48,13 @@ public class FileLock extends Lock { */ private void tidy() { if (_lock!=null) { - if ( advisory!=null) + if ( advisory!=null) { + // TODO: fix occasional exceptions raised here (usually on JVM shutdown) + if (advisory.target!=null) { + advisory.target.deleteOnExit(); // release will null the target + } advisory.release(true); - advisory.target.deleteOnExit(); + } advisory=null; _lock=null; } @@ -60,7 +67,7 @@ public class FileLock extends Lock { super(lockfile); // try and get a lock. try { - _lock = new File(lockfile.getParentFile(), lockfile.getName()+"."+_LockSuffix); + _lock = make_lockForTarget(lockfile); if (!ensureLockFile(block)) { log.debug("Couldn't get lock on "+_lock); tidy(); @@ -83,7 +90,14 @@ public class FileLock extends Lock { + lockfile.getAbsolutePath(),e); } } - + /** + * + * @param lockfile target of lock + * @return file object that will be locked for lockfile + */ + private File make_lockForTarget(File lockfile) { + return new File(lockfile.getParentFile(), lockfile.getName()+"."+_LockSuffix); + } private boolean openRaFile() throws IOException { if (target==null) return false; @@ -204,4 +218,12 @@ public class FileLock extends Lock { log.debug("Failed to getRaChannel on target "+target); return null; } + public boolean isTargetLockFile(File afile) { + if (isLocked()) + { + if (target.equals(afile) || make_lockForTarget(target).equals(afile)) + return true; + } + return false; + } }