debugged VamsasArchive and rationalised debugging messages.
[vamsas.git] / src / org / vamsas / client / simpleclient / VamsasArchive.java
index 39681ad..1924f02 100644 (file)
@@ -86,9 +86,10 @@ public class VamsasArchive {
    */
   public VamsasArchive(File archive, boolean vamsasdocument) throws IOException {
     super();
-    if (archive==null || (archive!=null && !archive.canWrite())) {
-      log.fatal("Invalid parameters for VamsasArchive constructor:"+((archive!=null) 
+    if (archive==null || (archive!=null && !(archive.getParentFile().canWrite() && (!archive.exists() || archive.canWrite())))) {
+      log.fatal("Expect Badness! -- Invalid parameters for VamsasArchive constructor:"+((archive!=null) 
           ? "File cannot be overwritten." : "Null Object not valid constructor parameter"));
+      return;
     }
     this.vamsasdocument = vamsasdocument;
     if (archive.exists()) {
@@ -140,28 +141,33 @@ public class VamsasArchive {
         odoc = null;
       }
       // Make a backup if it isn't done already
-      if (originalBackup==null)
-        makeBackup();
+      makeBackup();
       try {
         // copy new Archive data that was writen to a temporary file
         odoclock.updateFrom(null, rchive);
       }
       catch (IOException e) {
-        log.error("Problem updating archive from temporary file! - backup in '"
+        // TODO: LATER: decide if leaving nastily named backup files around is necessary. 
+        log.error("Problem updating archive from temporary file! - backup left in '"
             +backupFile().getAbsolutePath()+"'",e);
       }
+      // Tidy up if necessary.
+      removeBackup();
     } else {
-      // don't need to do anything.
+      
     }
   }
+  boolean donotdeletebackup=false;
   /**
    * called by app to get name of backup if it was made.
+   * If this is called, the caller app *must* delete the backup themselves.
    * @return null or a valid file object
    */
   public File backupFile() {
     
     if (!virginArchive) {
       makeBackup();
+      donotdeletebackup=false; // external reference has been made.
       return ((original!=null) ? originalBackup : null);
     }
     return null;
@@ -290,38 +296,59 @@ public class VamsasArchive {
       try { 
         newarchive.close();
         
-      } catch (Exception e) {};
+      } catch (Exception e) {
+        log.debug("Whilst closing newarchive",e);
+      };
       if (!virginArchive) {
         // then there is something to recover.
-        if (originalBackup!=null) {
-          // backup has been made.
-          // revert from backup and delete it (changing backup filename)
-          if (rchive==null) {
-            rchive = new SessionFile(original);
-          }
-          SessionFile bckup = new SessionFile(originalBackup);
-          
-          try {
-            rchive.updateFrom(null, bckup); // recover from backup file.
-            bckup.unlockFile();
-            bckup=null;
-            originalBackup.delete();
-            originalBackup=null;
-          }
-          catch (Exception e) {
-            log.warn("Problems when trying to cancel Archive "+archive.getAbsolutePath(), e);
-            return false;
-          }
+        try {
+          recoverBackup();
+        }
+        catch (Exception e) {
+          log.warn("Problems when trying to cancel Archive "+archive.getAbsolutePath(), e);
+          return false;
         }
       }
+      
     } else {
-      log.info("cancelArchive called before archive("+original.getAbsolutePath()+") has been opened!");
+      log.warn("Client Error: cancelArchive called before archive("+original.getAbsolutePath()+") has been opened!");
     }
     closeAndReset(); // tidy up and release locks.
     return true;
   }
   
   /**
+   * recovers the original file's contents from the (temporary) backup. 
+   * @throws Exception if any SessionFile or file removal operations fail.
+   */
+  private void recoverBackup() throws Exception {
+    if (originalBackup!=null) {
+      // backup has been made.
+      // revert from backup and delete it (changing backup filename)
+      if (rchive==null) {
+        rchive = new SessionFile(original);
+      }
+      SessionFile bckup = new SessionFile(originalBackup);
+      
+      rchive.updateFrom(null, bckup); // recover from backup file.
+      bckup.unlockFile();
+      bckup=null;
+      removeBackup();
+    }
+  }
+  /**
+   * forget about any backup that was made - removing it first if it was only temporary.
+   */
+  private void removeBackup() {
+    if (originalBackup!=null) {
+      log.debug("Removing backup in "+originalBackup.getAbsolutePath());
+      if (!donotdeletebackup)
+        if (!originalBackup.delete())
+          log.info("VamsasArchive couldn't remove temporary backup "+originalBackup.getAbsolutePath());
+      originalBackup=null;
+    }
+  }
+  /**
    * only do this if you want to destroy the current file output stream
    *
    */
@@ -342,6 +369,7 @@ public class VamsasArchive {
         odoclock = null;
       }
     }
+    removeBackup();
     newarchive=null;
     original=null;
     entries=null;
@@ -500,14 +528,14 @@ public class VamsasArchive {
   public VorbaIdFactory getVorba() {
     return vorba;
   }
-
+  
   /**
    * @param vorba the VorbaIdFactory to use for accessing vamsas objects.
    */
   public void setVorba(VorbaIdFactory vorba) {
     this.vorba = vorba;
   }
-
+  
   /**
    * Access and return current vamsas Document, if it exists, or create a new one 
    * (without affecting VamsasArchive object state - so is NOT THREAD SAFE)
@@ -595,7 +623,7 @@ public class VamsasArchive {
           
           if (unmarsh==null)
             log.fatal("Couldn't unmarshall document!");
-
+          
           VAMSAS root[]= new VAMSAS[] { null};
           root[0] = (VAMSAS) unmarsh[0];