made SimpleClientConfig externally configurable and added mechanism to prevent invali...
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleClient.java
index fccfba7..d13fe24 100644 (file)
@@ -25,6 +25,7 @@ import uk.ac.vamsas.client.Events;
 import uk.ac.vamsas.client.IClient;
 import uk.ac.vamsas.client.IClientDocument;
 import uk.ac.vamsas.client.IObjectUpdate;
+import uk.ac.vamsas.client.InvalidSessionDocumentException;
 import uk.ac.vamsas.client.InvalidSessionUrnException;
 import uk.ac.vamsas.client.SessionHandle;
 import uk.ac.vamsas.client.UserHandle;
@@ -94,31 +95,22 @@ public class SimpleClient implements IClient {
       
   }
   /**
-   * construct new session by importing objects from an existing vamsas document
+   * construct new SimpleClientsession by importing objects from an existing vamsas document
    * @param user
    * @param client
    * @param sess
    * @param importingArchive
    * @throws Exception IOExceptions for Session IO problems, and general Exception if importing document is invalid.
-   */
   protected SimpleClient(UserHandle user, ClientHandle client, VamsasSession sess, File importingArchive) throws Exception {
     this(user, client, sess);
-    VamsasArchive sessdoc = _session.getVamsasDocument();
-    try {
-      VamsasArchiveReader odoc = new VamsasArchiveReader(importingArchive);
-      SimpleDocument sdoc = new SimpleDocument(makeVorbaIdFactory());
-      VamsasDocument doc = sdoc.getVamsasDocument(odoc);
-      sessdoc.putVamsasDocument(doc, sdoc.vorba);
-      sessdoc.closeArchive();
-      log.debug("Imported new vamsas data from "+importingArchive);
-    } catch (Exception e) {
-      sessdoc.cancelArchive();
-      // write a dummy iohandler
-      _session.slog.info("Exception when importing document data from "+importingArchive);
-      log.warn("While importing session data from existing archive in "+importingArchive, e);      
-      throw new Exception("Failed to import data from "+importingArchive, e);
+    if (log.isDebugEnabled())
+    {
+      log.debug("Attempting to overwrite session document with file: "+importingArchive);
     }
+  // TODO: write provenance entry for new session indicating the import.
+    
   }
+   */
   
   /*
    * (non-Javadoc)
@@ -248,8 +240,8 @@ public class SimpleClient implements IClient {
     _session.removeClient(this);
     //log.debug("Stopping EventGenerator..");
     //evgen.stopWatching();
-    SimpleClient.log.debug("EventGenerator halted.");
     this.cdocument = null;
+    SimpleClient.log.debug("EventGenerator halted.");
     log.debug("finalization Complete.");
   }
   
@@ -313,32 +305,30 @@ public class SimpleClient implements IClient {
       throw new Error("Client Error - updateDocument() called before getClientDocument() on this SimpleClient instance.");
     if (newdoc!=cdocument)
       throw new Error("Client Error - SimpleClient.updateDocument() can only take the IClientDocument instance returned from SimpleClient.getClientDocument()");
-
-    if (evgen.isDocumentWatchEnabled())
-      throw new Error("Client Error - or Library Bug : Document watcher still enabled whilst ClientDocument instance exists.");
     
-    if (!cdocument.isModified()) {
-      // client document is silently got rid of, with no session update events.
-      if (log.isDebugEnabled())
-        log.debug("updateDocument for "+session.getSessionUrn()+" with unmodified IClientDocument (skipping the write)");
+    if (evgen.isDocumentWatchEnabled())
+      throw new Error("Probable Client Error (did you remember to call SimpleClient.updateDocument(clientdoc) at the end of the document update handler?) - or Library Bug : Document watcher still enabled whilst ClientDocument instance exists.");
+    if (cdocument.isInvalidModification())
+    {
+      log.info("Client has corrupted the vamsas document. It will not be written back to the session - sorry.");
+      // TODO: modify updateDocument signature: We should really raise an exception here to tell the client that it broke the datamodel.
     } else {
-      try {
-        boolean updated=cdocument.updateSessionDocument();
-        if (!updated) {
-          log.warn("Session document did not update properly for session directory "+_session.sessionDir);
-          // cdocument.archive.cancelArchive(); // LATER: could be done - would need to prevent updateSessionDocument closing the iohandler.
-          _session.slog.warn("Session Document updated but may not be valid (false return from org.vamsas.simpleclient.ClientDocument.updateSessionDocument()");
-        } else {
-               log.debug("Document update successful.");
-        }
-        _session.setUnsavedFlag();
-      }
-      catch (IOException e) {
-        log.warn("IO Problems when updating document!",e);
-        _session.slog.error("IO problems when attempting to update document.");
+      // actually try to write - if necessary.
+      if (!cdocument.isModified()) {
+        // client document is silently got rid of, with no session update events.
+        if (log.isDebugEnabled())
+        log.debug("updateDocument for "+session.getSessionUrn()+" with unmodified IClientDocument (skipping the write)");
+      } else {
+        writeSessionDocument();
       }
     }
-    // garbage collect the ClientDocument instance.
+    // release locks, reset and start to receive events again
+    tidyAwaySessionDocumentState(); 
+  }
+  /**
+   * garbage collect the ClientDocument instance and re-enable watchers.
+   */
+  protected void tidyAwaySessionDocumentState() {
     try {
       log.debug("Finalizing ClientDocument instance.");
       cdocument.finalize();
@@ -355,7 +345,28 @@ public class SimpleClient implements IClient {
       _session.slog.error("IO problems when attempting to release lock on session document.");
     }
   }
-  
+
+  /**
+   * write the cdocument instance to the session for real.
+   */
+  private void writeSessionDocument() {
+    try {
+      boolean updated=cdocument.updateSessionDocument();
+      if (!updated) {
+        log.warn("Session document did not update properly for session directory "+_session.sessionDir);
+        // cdocument.archive.cancelArchive(); // LATER: could be done - would need to prevent updateSessionDocument closing the iohandler.
+        _session.slog.warn("Session Document updated but may not be valid (false return from org.vamsas.simpleclient.ClientDocument.updateSessionDocument()");
+      } else {
+        log.debug("Document update successful.");
+      }
+      _session.setUnsavedFlag();
+    }
+    catch (IOException e) {
+      log.warn("IO Problems when updating document!",e);
+      _session.slog.error("IO problems when attempting to update document.");
+    }
+  }
+
   /*
    * (non-Javadoc)
    * 
@@ -511,23 +522,27 @@ public class SimpleClient implements IClient {
   protected void releaseActiveClientFile() throws IOException
   {
    
-    log.debug("Releasing active client file");
+    log.debug("Releasing active client locks");
     if( activeClientFilelock != null)
     {// Release the lock
+      log.debug("Releasing lock on active client lock file");
       activeClientFilelock.release();
+      log.debug("ReleaseActiveClientFile called when client has no lock on its clientLockFile");
       activeClientFilelock = null;
     } else {
       log.debug("ReleaseActiveClientFile called when client has no lock on its clientLockFile");
     }
     if (this.clientlockFile != null)
     {
+      log.debug("trying to delete active client lock file");
       if (this.clientlockFile.exists())
-      this.clientlockFile.delete();
-      log.debug("deleted active client lock file");
+      {
+        this.clientlockFile.delete();
+        log.debug("deleted active client lock file");
+      }
     } else {
       log.debug("ReleaseActiveClientFile called when client has no clientLockFile");
     }
-
   }
   
   protected void  createActiveClientFile() throws IOException
@@ -566,4 +581,20 @@ public class SimpleClient implements IClient {
   protected File getClientlockFile() {
     return clientlockFile;
   }
+  /**
+   * 
+   * @return the lock for the client in the session
+   */
+  protected Lock getClientLock() {
+    return activeClientFilelock;
+  }
+  SimpleClientConfig _config = null;
+  public SimpleClientConfig getSimpleClientConfig() {
+    if (_config==null)
+    {
+      _config = new SimpleClientConfig();
+    }
+    return _config;
+  }
+
 }