started on ClientDocument implementation and Locked IO tests for VamsasArchive
[vamsas.git] / src / org / vamsas / client / simpleclient / VamsasArchive.java
index fb7ddd8..f55d3d8 100644 (file)
@@ -88,7 +88,11 @@ public class VamsasArchive {
    * @throws IOException if call to accessOriginal failed for updates, or openArchive failed.
    */
   public VamsasArchive(File archive, boolean vamsasdocument) throws IOException {
+    this(archive, vamsasdocument, null);
+  }
+  public VamsasArchive(File archive, boolean vamsasdocument, Lock extantLock) throws IOException {
     super();
+    
     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"));
@@ -97,6 +101,8 @@ public class VamsasArchive {
     this.vamsasdocument = vamsasdocument;
     if (archive.exists()) {
       this.original = archive;
+      this.odoclock = new SessionFile(archive); // lock the file *immediatly*
+      odoclock.lockFile(extantLock);
       this.archive = null;       // archive will be a temp file when the open method is called
       virginArchive=false;
       try {
@@ -107,6 +113,11 @@ public class VamsasArchive {
     } else {
       this.original = null;
       this.archive = archive; // archive is written in place.
+      if (archive!=null) {
+        archive.createNewFile();
+        rchive=new SessionFile(archive); // lock the file *immediatly*
+        rchive.lockFile(extantLock);
+      }
       virginArchive = true;
     }
     this.openArchive(); // open archive
@@ -227,6 +238,7 @@ public class VamsasArchive {
    * opens the new archive ready for writing. If the new archive is replacing an existing one, 
    * then the existing archive will be locked, and the new archive written to a temporary file. 
    * The new archive will be put in place once close() is called.
+   * @param doclock TODO
    * @throws IOException
    */
   private void openArchive() throws IOException {
@@ -249,8 +261,12 @@ public class VamsasArchive {
         log.warn("New archive file name already in use! Possible lock failure imminent?");
     }
     
-    rchive = new SessionFile(archive);
-    rchive.lockFile();
+    if (rchive==null)
+      rchive = new SessionFile(archive);
+    archive.createNewFile();
+    if (!rchive.lockFile()) 
+      throw new IOException("Failed to get lock on file "+archive);
+  
     newarchive = new JarOutputStream(new BufferedOutputStream(new java.io.FileOutputStream(archive)));  
     entries = new Hashtable();
   }
@@ -282,7 +298,7 @@ public class VamsasArchive {
    */
   public AppDataOutputStream getAppDataStream(String appdataReference) throws IOException {
     if (newarchive==null)
-      openArchive();
+      throw new IOException("Attempt to write to closed VamsasArchive object.");
     if (addValidEntry(appdataReference)) {
       return new AppDataOutputStream(newarchive);
     }
@@ -357,7 +373,7 @@ public class VamsasArchive {
   private void closeAndReset() {
     if (rchive!=null) {
       rchive.unlockFile();
-      rchive = null;
+      rchive=null;
     }
     if (original!=null) {
       if (odoc!=null) {