introducing Lock File based locking (for portability) - part implemented.
[vamsas.git] / src / org / vamsas / client / simpleclient / SessionFile.java
index 9da73c0..bd2dc44 100644 (file)
@@ -33,7 +33,7 @@ public class SessionFile {
       fileLock=null;
     }
     if (extantlock!=null)
-      fileLock=extantlock;
+      fileLock=extantlock; // THIS IS BROKEN - lockFile then nulls the lock.
     return lockFile();
   }
 
@@ -49,7 +49,6 @@ public class SessionFile {
       else 
         // lock failed for some reason.
         fileLock.release();
-    fileLock = null;
     if (sessionFile != null) {
       if (!sessionFile.exists()) {
         // create new file
@@ -68,9 +67,11 @@ public class SessionFile {
       long tries=500;
       do {
         tries--;
-        if (fileLock!=null && !fileLock.isLocked())
+        if (fileLock!=null && !fileLock.isLocked()) {
           fileLock.release();
-          fileLock = new Lock(sessionFile); // TODO: wait around if we can't get the lock.
+          try { Thread.sleep(5); } catch (Exception e) {};
+        }
+        fileLock = LockFactory.getLock(sessionFile); // TODO: wait around if we can't get the lock.
       } while (tries>0 && !fileLock.isLocked());
       if (!fileLock.isLocked())
         log.error("Failed to get lock for "+sessionFile);
@@ -134,8 +135,10 @@ public class SessionFile {
     if (newData.sessionFile==null)
       throw new IOException("Null SessionFile in newData.");
       
-    lockFile(extantLock);  
-    newData.lockFile();
+    if (!lockFile(extantLock))
+      log.error("Failed to get write lock for "+sessionFile);
+    if (!newData.lockFile())
+      log.warn("Failed to get lock for updateFrom "+newData.sessionFile);
     fileLock.rafile.getChannel().transferFrom(newData.fileLock.rafile.getChannel(), 0, 
         newData.fileLock.rafile.length());
   }