updated test cases for spring 2006 schema, and major debug of locking system
[vamsas.git] / src / org / vamsas / client / simpleclient / ClientsFile.java
index cab03cd..96fa871 100644 (file)
@@ -21,6 +21,7 @@ import java.util.Vector;
  * @author jim 
  */
 public class ClientsFile extends ListFile {
+  private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(ClientsFile.class);
   /**
    * number of my client in list - passed back when a client 
    * is added to list, and used (if valid) for quickly 
@@ -42,7 +43,7 @@ public class ClientsFile extends ListFile {
     if (lockFile()) {
       try {
         ClientHandle[] clients=null;
-        if (this.fileLock.rafile.length()>0) {
+        if (fileLock.length()>0) {
           
           ObjectInputStream is = new ObjectInputStream(fileLock.getBufferedInputStream(true));
           Object o;
@@ -222,7 +223,10 @@ public class ClientsFile extends ListFile {
     }
     return newclient;
   }
-  
+  /**
+   * when set true - get FileNotFoundExceptions on WinXP when writing to locked stream after the backup has been made (via the backupFile method)
+   */
+  boolean backup=false;
   /**
    * safely writes clients array to the file referred to by sessionFile.
    * 
@@ -231,28 +235,37 @@ public class ClientsFile extends ListFile {
    */
   protected boolean putClientList(ClientHandle[] clients) {
     if (lockFile()) {
-      File templist = backupSessionFile();
-      if (templist != null) {
-        try {
-          // fileLock.rafile.setLength(0);
-          ObjectOutputStream os = 
-            new ObjectOutputStream(fileLock.getBufferedOutputStream(true));
-          
-//              new BufferedOutputStream(new FileOutputStream(this.sessionFile)));
-          //    new BufferedOutputStream(new FileOutputStream(this.fileLock.rafile.getFD())));
-          os.writeObject(clients);
-          os.close(); // close destroys lock!
+      File templist=null;
+      if (!backup || (templist = backupSessionFile()) != null) {
+        int retries=3;
+        while (retries-->0) {
+          try {
+            ObjectOutputStream os = 
+              new ObjectOutputStream(fileLock.getBufferedOutputStream(true));
+            log.debug("About to write "+clients.length+" clientHandles to output stream.");
+            os.writeObject(clients);
+            os.close();
           // All done - remove the backup.
-          templist.delete();
+          if (backup)
+            templist.delete();
           templist = null;
-        } catch (Exception e) {
-          if (templist != null) {
+          retries=-1;
+          } catch (Exception e) {
             System.err
-                .println("Serious - problems writing to sessionFile. Backup in "
-                    + templist.getAbsolutePath());
+            .println("Serious - problems writing to sessionFile.");
+            if (retries>0 && templist != null) {
+              System.err.println("Recovering from Backup in "
+                      + templist.getAbsolutePath());
+              templist.renameTo(fileLock.target);
+            }
             e.printStackTrace(System.err);
           }
         }
+        if (retries>-2) {
+          System.err
+          .println("Serious - problems writing to sessionFile. Giving Up.");
+          return false;
+        }
       } else {
         throw new Error(
             "Couldn't create backup of the clientList before writing to it!");
@@ -265,4 +278,18 @@ public class ClientsFile extends ListFile {
     // successful!
     return true;
   }
+
+  public void clearList() {
+    if (lockFile()) {
+      try {
+        FileOutputStream fout = fileLock.getFileOutputStream(true);
+        fout.flush();
+        fout.close();
+      } catch (Exception e) {
+        throw new Error("Problems trying to clear clientlist!",e);
+        
+      }
+    }
+    
+  }
 }