safety checks and informational Errors generated when IClientDocument is not stored...
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / EventGeneratorThread.java
index 311c825..4a0b1fb 100644 (file)
@@ -83,7 +83,15 @@ public class EventGeneratorThread {
     */
     log.debug("Watchers inited.");
   }
-  void _raise(String handlerEvent, String property, Object oldval, Object newval) {
+  /**
+   * Call registered handlers for a vamsas session event
+   * @param handlerEvent a named event
+   * @param property property name to pass to handler
+   * @param oldval old value of property to pass
+   * @param newval new value of property to pass
+   * @return true if event generation did not raise any exceptions.
+   */
+  boolean _raise(String handlerEvent, String property, Object oldval, Object newval) {
     PropertyChangeSupport h = (PropertyChangeSupport) handlers.get(handlerEvent);
     if (h!=null) {
       log.debug("Triggering:"+handlerEvent);
@@ -91,10 +99,17 @@ public class EventGeneratorThread {
         h.firePropertyChange(property, oldval, newval);
       } catch (Exception e) {
         log.warn("Client Exception during handling of "+handlerEvent, e);
+        return false;
+      }
+      catch (Error e)
+      {
+        log.error("Serious! Client Error during handling of "+handlerEvent, e);
+        return false;
       }
       log.debug("Finished  :"+handlerEvent);
     } else
       log.debug("No handlers for raised "+handlerEvent);
+    return true;
   }
   protected boolean storeDocRequest(Lock lock) {
     if (log.isDebugEnabled())
@@ -115,13 +130,38 @@ public class EventGeneratorThread {
       session.vamArchive.fileLock=doclock;
       if (client.pickmanager!=null)
         client.pickmanager.setPassThru(false);
+      if (log.isDebugEnabled()) {
+        log.debug("Initiating a documentChanged event. Document is "+(client.cdocument==null ? "closed" : "open"));
+      }
       // TODO: decide if individual object update handlers are called as well as overall event handler
-      _raise(Events.DOCUMENT_UPDATE, client.getSessionUrn(), null, client);
+      if (!_raise(Events.DOCUMENT_UPDATE, client.getSessionUrn(), null, client))
+      {
+        log.info("Recovering from errors or exceptions generated by client application");
+        if (client.cdocument!=null)
+        {
+          try {
+            client.tidyAwaySessionDocumentState(); 
+          }
+          catch (Exception e)
+          {
+            log.warn("Exception generated by vamsas library - when tidying away session document:",e);
+          }
+          catch (Error e)
+          {
+            log.error("LIBRARY Implementation error - when tidying away session document:",e);
+          }
+        }
+          
+      }
       if (client.pickmanager!=null)
         client.pickmanager.setPassThru(true);
       if (log.isDebugEnabled()) {
         log.debug("Finished handling a documentChanged event. Document is "+(client.cdocument==null ? "closed" : "open"));
       }
+      if (client.cdocument!=null)
+      {
+        log.warn("Implementation Error ?  ClientDocument instance has not been closed or updated by handler!");
+      }
       /*try {
         client._session.getVamsasDocument().closeArchive();
       } catch (Exception e) {log.warn("Unexpected exception when closing document after update.",e);};