introduced locked IO testing for Archive IO
authorjprocter <jprocter@compbio.dundee.ac.uk>
Thu, 19 Jan 2006 18:17:31 +0000 (18:17 +0000)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Thu, 19 Jan 2006 18:17:31 +0000 (18:17 +0000)
git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@150 be28352e-c001-0410-b1a7-c7978e42abec

src/org/vamsas/test/simpleclient/ArchiveWatcher.java [new file with mode: 0644]
src/org/vamsas/test/simpleclient/VamsasArchive.java

diff --git a/src/org/vamsas/test/simpleclient/ArchiveWatcher.java b/src/org/vamsas/test/simpleclient/ArchiveWatcher.java
new file mode 100644 (file)
index 0000000..23396a6
--- /dev/null
@@ -0,0 +1,56 @@
+package org.vamsas.test.simpleclient;
+
+import java.io.File;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.vamsas.client.simpleclient.FileWatcher;
+import org.vamsas.client.simpleclient.Lock;
+import org.vamsas.client.simpleclient.SimpleDocument;
+import org.vamsas.client.simpleclient.VamsasArchiveReader;
+import org.vamsas.objects.core.VamsasDocument;
+/**
+ * demo of archive watching process - should mimic the clientsfileTest watcher/monitor process.
+ * @author jimp
+ *
+ */
+public class ArchiveWatcher {
+  private static Log log = LogFactory.getLog(ArchiveWatcher.class); 
+  public static void main(String[] args) {
+    try {
+      if (args!=null && args.length>0) {
+        
+        File archive = new File(args[0]);
+        log.info("Watching file "+args[0]);
+/*        if (!archive.exists())
+          archive.createNewFile();
+ */       // watch the new file... - taken straight from ClientsFileTest
+        FileWatcher w = new FileWatcher(archive);
+        while (archive.exists()) {
+          
+          // get watcher's lock to ensure state change is fixed for retrieval
+          Lock chlock = w.getChangedState();
+          if (chlock != null)
+            if (archive.length()>0) {
+              log.info("Noticed Testing update: ");
+              VamsasArchiveReader vreader = new VamsasArchiveReader(archive);
+              SimpleDocument sdoc = new SimpleDocument("testing vamsas watcher");
+              try {
+                VamsasDocument d = sdoc.getVamsasDocument(vreader);
+                if (d!=null) {
+                  ArchiveReports.reportDocument(d, vreader, false, System.out);
+                }
+                System.out.println("Update at "+System.currentTimeMillis()+"\n\n********************************************************\n");
+              } catch (Exception e) {
+                log.error("Unmarshalling failed.",e);
+              }
+              vreader.close();
+            }
+        }
+      }
+    } catch (Exception e) {
+      log.error(e);
+    }
+    
+  }
+}
index 94d702b..ccef739 100644 (file)
@@ -5,8 +5,11 @@ import java.io.ObjectOutputStream;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.vamsas.client.simpleclient.Lock;
+import org.vamsas.client.simpleclient.SessionFile;
 import org.vamsas.client.simpleclient.SimpleDocument;
 import org.vamsas.client.simpleclient.VamsasArchiveReader;
+import org.vamsas.client.simpleclient.VamsasFile;
 import org.vamsas.objects.core.ApplicationData;
 import org.vamsas.objects.core.User;
 import org.vamsas.objects.core.VAMSAS;
@@ -114,14 +117,15 @@ public class VamsasArchive {
       else 
         log.info(av+" has been backed up as "+backup);
       File newf=new File(av.getAbsolutePath()+"_new.zip");
-      if (newf.exists()) {
+      VamsasFile sfile = new VamsasFile(newf);
+      /* if (newf.exists()) {
         int q=1;
         do {
           newf=new File(av.getAbsolutePath()+"_"+q+++"_new.zip");
         }
         while (newf.exists()); 
-      } 
-      log.info("Now writing new Archive "+newf.getAbsolutePath());
+      } */
+      log.info("Now writing new Archive into "+newf.getAbsolutePath());
       org.vamsas.client.simpleclient.VamsasArchive va = new org.vamsas.client.simpleclient.VamsasArchive(newf, true);
       // open another and...
       ApplicationData appdata = makeDemoAppdata(va, 
@@ -129,17 +133,21 @@ public class VamsasArchive {
       log.info("Preparing to write new document.");
       doc.addApplicationData(appdata);
       doc.addVAMSAS(Core.getDemoVamsas());
-      // TODO: ensure all jar entries are transferred over (at least those that are referenced - make a flag for 'tidy zip manifest'.
       va.putVamsasDocument(doc); // gets stream and puts it.
-      // TODO: verify that the vxml stream close method overridden ?
       va.closeArchive();
-      
       log.info("Dump of new vamsas document :");
       log.info("Testing update: ");
-      VamsasArchiveReader vreader = new VamsasArchiveReader(newf);
-      SimpleDocument sdoc = new SimpleDocument("testing new vamsas write");
-      ArchiveReports.reportDocument(sdoc.getVamsasDocument(vreader), vreader, true, System.out);
-      
+      {
+        Lock lock=sfile.getLock();
+        if (lock==null)
+          while ((lock=sfile.getLock())!=null)
+            log.info("Waiting for lock.");
+        VamsasArchiveReader vreader = new VamsasArchiveReader(newf);
+        SimpleDocument sdoc = new SimpleDocument("testing new vamsas write");
+        ArchiveReports.reportDocument(sdoc.getVamsasDocument(vreader), vreader, true, System.out);
+        lock.release();
+        lock = null;
+      }
       // backup.delete(); // tidy up
       
       log.info("Now Cancelling write to original archive "+av);
@@ -149,7 +157,7 @@ public class VamsasArchive {
         log.info("Didn't cancel.");
       
       log.info("Now testing archive update.");
-      va = new org.vamsas.client.simpleclient.VamsasArchive(newf, true); // TODO - refactor Reader class to have deserializing helpers
+      va = new org.vamsas.client.simpleclient.VamsasArchive(newf, true);
       doc = va.getVamsasDocument();
       doc.addVAMSAS(Core.getDemoVamsas());
       doc.addApplicationData(makeDemoAppdata(va, 
@@ -161,9 +169,17 @@ public class VamsasArchive {
       va.putVamsasDocument(doc);
       va.closeArchive();
       log.info("Testing update: ");
-      vreader = new VamsasArchiveReader(newf);
-      sdoc = new SimpleDocument("testing vamsas update");
-      ArchiveReports.reportDocument(sdoc.getVamsasDocument(vreader), vreader, true, System.out);
+      {
+        Lock lock=sfile.getLock();
+        if (lock==null)
+          while ((lock=sfile.getLock())!=null)
+            log.info("Waiting for lock.");
+        VamsasArchiveReader vreader = new VamsasArchiveReader(newf);
+        SimpleDocument sdoc = new SimpleDocument("testing vamsas update");
+        ArchiveReports.reportDocument(sdoc.getVamsasDocument(vreader), vreader, true, System.out);
+        lock.release();
+        lock = null;
+      }
     } catch (Exception e) {
       e.printStackTrace(System.err);
     }