ensured that hash binding vorbaIds is passed to client document object for vorba...
[vamsas.git] / src / org / vamsas / client / simpleclient / VamsasArchive.java
index 6e8138b..c30c0bc 100644 (file)
@@ -196,6 +196,15 @@ public class VamsasArchive {
    */
   public VamsasArchive(VamsasFile archive, boolean vamsasdocument, boolean overwrite) throws IOException {
     this(archive.sessionFile, overwrite, vamsasdocument, archive);
+    // log.debug("using non-functional lock-IO stream jar access constructor");
+  }
+  /**
+   * read and write to archive - will not overwrite original contents, and will always write an up to date vamsas document structure.
+   * @param archive
+   * @throws IOException
+   */
+  public VamsasArchive(VamsasFile archive) throws IOException {
+    this(archive, true, false); 
   }
   /**
    * 
@@ -207,7 +216,7 @@ public class VamsasArchive {
    */
   public VamsasArchive(File archive, boolean overwrite, boolean vamsasdocument, SessionFile extantLock) throws IOException {
     super();
-    if (archive==null || (archive!=null && !(archive.getParentFile().canWrite() && (!archive.exists() || archive.canWrite())))) {
+    if (archive==null || (archive!=null && !(archive.getAbsoluteFile().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;
@@ -239,9 +248,9 @@ public class VamsasArchive {
       else
         rchive = new SessionFile(archive);
       rchive.lockFile();
-      if (rchive.fileLock==null || rchive.fileLock.rafile==null || !rchive.fileLock.isLocked())
+      if (rchive.fileLock==null || !rchive.fileLock.isLocked())
         throw new IOException("Lock failed for new archive"+archive);
-      rchive.fileLock.rafile.setLength(0); // empty the archive.
+      rchive.fileLock.getRaFile().setLength(0); // empty the archive.
       virginArchive = true;
     }
     this.openArchive(); // open archive
@@ -256,7 +265,8 @@ public class VamsasArchive {
         odoclock = new SessionFile(original);
       odoclock.lockFile();
       if (odoc == null) 
-        odoc = new VamsasArchiveReader(odoclock.fileLock);
+        odoc = new VamsasArchiveReader(original);
+        // this constructor is not implemented yet odoc = new VamsasArchiveReader(odoclock.fileLock);
     }
   }
   
@@ -373,7 +383,9 @@ public class VamsasArchive {
       newarchive.closeEntry();
       if (!isDocumentWritten())
         log.warn("Premature closure of archive '"+archive.getAbsolutePath()+"': No document has been written.");
-      newarchive.finish();
+      newarchive.finish();// close(); // use newarchive.finish(); for a stream IO
+      newarchive.flush();
+      //
       updateOriginal();
       closeAndReset();
     } else {
@@ -552,8 +564,10 @@ public class VamsasArchive {
       rchive = new SessionFile(archive);
     if (!rchive.lockFile()) 
       throw new IOException("Failed to get lock on file "+archive);
+    // LATER: locked IO stream based access.
     Manifest newmanifest = new Manifest();
     newarchive = new JarOutputStream(rchive.fileLock.getBufferedOutputStream(true), newmanifest);  
+    //newarchive = new JarOutputStream(new BufferedOutputStream(new java.io.FileOutputStream(archive)));  
     entries = new Hashtable();
   }
   public void putVamsasDocument(VamsasDocument doc) throws IOException, 
@@ -769,14 +783,19 @@ public class VamsasArchive {
         odoclock.updateFrom(null, rchive);
       }
       catch (IOException e) {
-        // LATER: decide if leaving nastily named backup files around is necessary. 
-        log.error("Problem updating archive from temporary file! - backup left in '"
+        // LATER: decide if leaving nastily named backup files around is necessary.
+        File backupFile=backupFile();
+        if (backupFile!=null)
+          log.error("Problem updating archive from temporary file! - backup left in '"
             +backupFile().getAbsolutePath()+"'",e);
+        else
+          log.error("Problems updating, and failed to even make a backup file. Ooops!", e);
       }
       // Tidy up if necessary.
       removeBackup();
     } else {
       
+      
     }
   }
 }