updated test cases for spring 2006 schema, and major debug of locking system
[vamsas.git] / src / org / vamsas / test / simpleclient / ArchiveClient.java
index d72b43e..da555af 100644 (file)
@@ -83,7 +83,7 @@ public class ArchiveClient extends IdFactory {
     if (vsess==null)
       throw new Error("ArchiveClient instance is invalid!.");
   }
-
+  public static int WATCH_SLEEP=300;
   /**
    * watch the document file for updates.
    * @param time - length of time to watch for.
@@ -91,15 +91,17 @@ public class ArchiveClient extends IdFactory {
    */
   public ClientDoc watch(long time) {
     valid();
+    vsess.unLock(); // doh.
     FileWatcher watcher = new FileWatcher(vsess.getVamsasFile());
     long endtime=System.currentTimeMillis()+time;
     try {
       org.vamsas.client.simpleclient.Lock doclock;
-      watcher.setState();
+      //watcher.setState();
       do {
-        Thread.sleep(50); // tuning.
         doclock=watcher.getChangedState();
-      } while (doclock==null && (time==0 || endtime>System.currentTimeMillis()));
+        if (doclock==null)
+          Thread.sleep(WATCH_SLEEP);
+      } while (doclock==null && (time==0 || endtime>System.currentTimeMillis())); // tuning.
       if (doclock==null)
         return null;
       else {
@@ -170,7 +172,13 @@ public class ArchiveClient extends IdFactory {
   public ClientDoc getUpdateable() {
     valid();
     try {
-      vsess.getLock();
+      // patiently wait for a lock on the document.
+      long tries=5000;
+      org.vamsas.client.simpleclient.Lock lock;
+      while (((lock=vsess.getLock())==null || !lock.isLocked()) && --tries>0) {
+//        Thread.sleep(1);
+        log.debug("Trying to get a document lock for the "+tries+"'th time.");
+      }
       VamsasArchive varc = new VamsasArchive(vsess, true, false); // read archive, write as vamsasDocument, don't erase original contents.
       varc.setVorba(this);
       VamsasDocument d = varc.getVamsasDocument(getProvenanceUser(), "Created new document.", VersionEntries.latestVersion()); // VAMSAS: provenance user and client combined
@@ -211,6 +219,7 @@ public class ArchiveClient extends IdFactory {
       // do any appDatas first.
       if (cdoc.iohandler.transferRemainingAppDatas())
         log.debug("Remaining appdatas were transfered.");
+      cdoc.updateDocumentRoots();
       cdoc.iohandler.putVamsasDocument(cdoc.doc);
       cdoc.iohandler.closeArchive();
       cdoc.iohandler=null;
@@ -244,14 +253,17 @@ public class ArchiveClient extends IdFactory {
       cdoc.addVamsasRoot(Core.getDemoVamsas());
       System.out.println("Doing update.");
       client.doUpdate(cdoc);
+      cdoc.closeDoc();
       cdoc = null;
       int u=5;
       while (--u>0) {
         System.out.println("Watch for more... ("+u+" left)");
-        cdoc = client.watch(0);
-        if (cdoc!=null) {
+        ClientDoc ucdoc = client.watch(50000);
+        if (ucdoc!=null) {
           System.out.println("****\nUpdate detected at "+new Date());
-          ArchiveReports.reportDocument(cdoc.doc, cdoc.getReader(), true, System.out);
+          ArchiveReports.reportDocument(ucdoc.doc, ucdoc.getReader(), true, System.out);
+          ucdoc.closeDoc();
+          ucdoc=null;
         } else {
           System.out.println("!!!! Null document update detected at "+new Date());
         }