X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fvamsas%2Fclient%2Fsimpleclient%2FLock.java;h=dfc978ee85f6701db952bc33d8ffad67c30418c4;hb=182b7b1359e39d1357939c4c834f52abaf2651c6;hp=a185233891cec9b7f3b42d7d5315c4f396435b5d;hpb=8323ce3c74cff67c0006c6d0cc4155a5e6fe7ca1;p=vamsas.git
diff --git a/src/org/vamsas/client/simpleclient/Lock.java b/src/org/vamsas/client/simpleclient/Lock.java
index a185233..dfc978e 100644
--- a/src/org/vamsas/client/simpleclient/Lock.java
+++ b/src/org/vamsas/client/simpleclient/Lock.java
@@ -8,15 +8,15 @@ import java.nio.channels.FileLock;
/**
* transient object representing a file lock
- *
- *
+ * This lock should hold for all processes interacting in a session.
+ * TODO: currently implemented for local filesystem style locking - need a fallback mechanism for systems without file locks.
* @author jimp
*
*/
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()) {
@@ -33,6 +33,9 @@ public class Lock {
}
lock = (rafile=new RandomAccessFile(lockfile,"rw")).getChannel().tryLock();
+ if (lock==null || !lock.isValid())
+ // failed to get lock. Close the file channel
+ rafile.getChannel().close();
} catch (FileNotFoundException e) {
System.err.println("Error! Couldn't create a lockfile at "
+ lockfile.getAbsolutePath());
@@ -43,7 +46,7 @@ public class Lock {
e.printStackTrace();
}
}
-
+
boolean isLocked() {
if (lock != null && lock.isValid()) {
return true;
@@ -51,16 +54,20 @@ public class Lock {
return false;
}
public void release() {
- if (lock!=null) {
- try {
- rafile.close();
+ try {
+ // TODO: verify that channel.close should be called after release() for rigourous locking.
+ 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()
*/