safety checks and informational Errors generated when IClientDocument is not stored...
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / VamsasSession.java
index 61cf0fb..2ae181d 100644 (file)
@@ -1,8 +1,11 @@
 package uk.ac.vamsas.client.simpleclient;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+import java.nio.channels.OverlappingFileLockException;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -13,6 +16,7 @@ import org.apache.log4j.PatternLayout;
 import uk.ac.vamsas.client.ClientHandle;
 import uk.ac.vamsas.client.Events;
 import uk.ac.vamsas.client.IClient;
+import uk.ac.vamsas.client.SessionHandle;
 import uk.ac.vamsas.client.UserHandle;
 /**
  * Does all the IO operations for a SimpleClient instance accessing 
@@ -83,14 +87,14 @@ public class VamsasSession {
   /**
    * Count of cycles before considering the current client as the last one of the session (if no other client registered as active )
    */
-  private final int watchCycleCountBeforeLastClient = 1100 ;
+  private final int watchCycleCountBeforeLastClient = 1220 ;
   
   /**
    * time between checking 
    */
   public int WATCH_SLEEP=30; 
   
-  //protected String clientFileDirectory = "clients";
+  protected String clientFileDirectory = "clients";
   
   /**
    * called to clear update flag after a successful offline storage event
@@ -361,7 +365,7 @@ public class VamsasSession {
    * add the client to the client list file
    * @param client client to add to the session
    */
-  protected void addClient(IClient client)
+  protected void addClient(SimpleClient client)
   {
     if (client == null)
       slog.error("Try to add a null client to the session ");
@@ -372,14 +376,15 @@ public class VamsasSession {
      
       log.debug("Added.");
       log.debug("Register Client as Active.");
-     /* try {
+      try {
         client.createActiveClientFile();
       } catch (IOException e) {
         log.debug("Error during  active client file creation.");
-      }*/
+      }
       //tracks modification to the client list and readds client to the list
       getClientWatcherElement().setHandler(new AddClientWatchCallBack(client));
       getClientWatcherElement().enableWatch();
+     
     }
   }
   
@@ -391,13 +396,13 @@ public class VamsasSession {
   private class AddClientWatchCallBack  implements WatcherCallBack
   {
    
-    private IClient client ;
+    private SimpleClient client ;
     
     /**
     *Inits the handler with the client to check in the list
      * @param client client to monitor in the client list
      */
-    protected  AddClientWatchCallBack (IClient client)
+    protected  AddClientWatchCallBack (SimpleClient client)
       {
         this.client = client;
       }
@@ -411,7 +416,7 @@ public class VamsasSession {
           boolean isWatchEnable = watcher.isWatchEnabled();
           if (lock== null)//no update on the list
             return isWatchEnable;
-        //  log.debug("change on the client list ");
+          log.debug("change on the client list ");
           if (client != null)
             {
         
@@ -436,7 +441,7 @@ public class VamsasSession {
                 log.debug("client is in the list");
 
             }
-          //log.debug("isWatchEnable "+isWatchEnable);
+          log.debug("isWatchEnable "+isWatchEnable);
           return isWatchEnable;
         }
       }
@@ -468,41 +473,139 @@ public class VamsasSession {
     //Wait for several watchers cycle to see if the current client was the last client active in the session.
     //if yes, close the session
     
-    getClientWatcherElement().setHandler(new RemoveClientWatchCallBack (client));
+   // getClientWatcherElement().setHandler(new RemoveClientWatchCallBack (client));
     getClientWatcherElement().setTimeoutBeforeLastCycle(this.watchCycleCountBeforeLastClient);
     log.info("remove client from list");
     clist.clearList();
-    log.info("client list cleared");
+    log.info("list cleared");
     if (cwe!=null) {
       cwe.enableWatch();
       
       
-     /* try {
-        log.debug("Releasing  active client file");
-        client.releaseActiveClientFile();
-      } catch (IOException e) {
-        log.error("error during active file client release");
-      }*/
-    }
-   
-  
-   
-    /*clist.removeClient(client.getClientHandle(),null);
-    if (this.clist.retrieveClientList() == null|| this.clist.retrieveClientList().length<1)
-      {//assume it is the last client has been removed shutting down session
-        slog.info("last client removed: removing session");
-        log.debug("last client removed: removing session");
-        this.getSessionManager().removeSession(client.getSessionHandle());
+      log.debug("Stopping EventGenerator..");
+      client.evgen.stopWatching();
+      cwe.setHandler(null);
+      
+      // ask to the client to copy application data into the document
+      client.evgen._raise(Events.DOCUMENT_FINALIZEAPPDATA, null, client,null);
+      boolean closeSession=isLastActiveClient(client);
+      if (closeSession)
+      {
+        log.debug("Raising request-to-save event");
+        client.evgen._raise(Events.DOCUMENT_REQUESTTOCLOSE, null, client, null);
+        client.evgen._raise(Events.SESSION_SHUTDOWN, null, client.getSessionHandle(), null);
       }
-    else
+      try 
+        {
+          log.debug("Attempting to release active client locks");
+          client.releaseActiveClientFile();
+        }
+      catch (IOException e)
+        {
+          log.error("error during active file client release");
+         }
+      if (closeSession)
       {
-        int active=clist.retrieveClientList().length;
-        log.debug("Still "+active+" active clients");
-        slog.info("Still "+active+" active clients");
-      }*/
-   
+        log.debug("Last active client: closing session");
+        log.info("Closing session");
+          getSessionManager().removeSession(client.getSessionHandle());
+      }
+    }
   }
   
+  
+  private boolean isLastActiveClient(SimpleClient client)
+    {
+    log.debug("Testing if current client is the last one.");
+    log.debug("current client lockfile is '"+client.getClientlockFile()+"'");
+      boolean noOtherActiveClient = true;
+      //create, if need,  subdirectory to contain client files
+      File clientlockFileDir = new File (this.sessionDir, clientFileDirectory);
+      if( !clientlockFileDir.exists())
+        {
+          log.error("Something wrong the active client file does not exits... should not happen");
+          return false;
+        }
+      
+      try {
+    
+        //no check every file in the directory and try to get lock on it.
+        File [] clientFiles = clientlockFileDir.listFiles();
+        if(clientFiles == null || clientFiles.length==0)
+          {//there is not file on the directory. the current client should be the last one.
+            return true;
+          }
+       
+        for (int i = clientFiles.length - 1; i>-1&& noOtherActiveClient  ;i--)
+          {
+             File clientFile = clientFiles[i];
+             log.debug("testing file for lock: "+clientFile.getAbsolutePath());
+             if(client.getClientLock().isTargetLockFile(clientFile)) 
+               {
+               log.debug("current client file found");
+                 continue;
+               }
+             if (clientFile != null &&  clientFile.exists() )
+             {
+               try
+                 {
+                 log.debug("Try to acquire a lock on the file");
+                 // Get a file channel for the file
+                   FileChannel channel = new RandomAccessFile(clientFile, "rw").getChannel();
+         
+                 // Use the file channel to create a lock on the file.
+                 // This method blocks until it can retrieve the lock.
+                 //  java.nio.channels.FileLock activeClientFilelock = channel.lock();
+         
+                 // Try acquiring the lock without blocking. This method returns
+                 // null or throws an exception if the file is already locked.
+                   try
+                     {
+                     java.nio.channels.FileLock  activeClientFilelock = channel.tryLock();
+                     
+                       //the lock has been acquired. 
+                       //the file was not lock and so the corresponding application seems to have die
+                     if(activeClientFilelock != null)
+                       {
+                         log.debug("lock obtained : file must be from a crashed application");
+                         
+                     
+                         activeClientFilelock.release();
+                         log.debug("lock released");
+                         
+                         channel.close();
+                         log.debug("channel closed");
+                         
+                       //delete file 
+                         clientFile.delete();
+                         log.debug("crashed application file deleted");
+                         
+                       }
+                     else
+                       {
+                       noOtherActiveClient  = false;
+                         log.debug("lock not obtained : another application is active");
+                       }
+                     }
+                   catch (OverlappingFileLockException e) 
+                     {
+                     // File is already locked in this thread or virtual machine
+                     //that the expected behaviour
+                     log.debug("lock not accessible ",e);
+                     }
+                 } 
+               catch (Exception e) 
+                 {
+                 log.debug("error during lock testing ",e);
+                 }
+             }
+          }
+      
+      } catch (Exception e) {
+       log.error("error during counting active clients");
+      }
+      return noOtherActiveClient;
+    }
   /**
    * Handler for the client watcher. after a client have been removed
    * 
@@ -540,8 +643,7 @@ public class VamsasSession {
             
             //checks if the client is not already in the lists
            //   ClientHandle[] cl = clist.retrieveClientList();//lock);//clist.retrieveClientList();
-//            ask to the client to cpoy application data into the document
-              client.evgen._raise(Events.DOCUMENT_FINALIZEAPPDATA, null, client,null);
+
               boolean islastClient =  true;
               if (manualCheckOfClientCount)
                 {
@@ -564,13 +666,7 @@ public class VamsasSession {
               {
                 //the client is the last one, so close current session
                   log.info("last client removed: closing session");
-
-//                close document 
-                  client.evgen._raise(Events.DOCUMENT_REQUESTTOCLOSE, null, client,null);
-                  log.debug("close document request done");
-                  
-                  getSessionManager().removeSession(client.getSessionHandle());
-                  log.debug("Session removed");
+                  closeSession(client);
               }
             }
               else
@@ -583,6 +679,7 @@ public class VamsasSession {
          
             }
               log.debug("Stopping EventGenerator..");
+              // TODO: ensure ClientsFile lock is really released!! clist.unlockFile();
           client.evgen.stopWatching();
         }
           watcher.setHandler(null);//Do not check if the client is the last client. watcher will shutdown anyway
@@ -592,6 +689,28 @@ public class VamsasSession {
         }
       }
 
+  /**
+   * closes the current session, 
+   * and send an event to the last client to close the document
+   * @param client the last client of the client
+   */
+  private void closeSession(SimpleClient client)
+    {
+//   close document 
+      client.evgen._raise(Events.DOCUMENT_REQUESTTOCLOSE, null, client,null);
+      log.debug("close document request done");
+      closeSession(client.getSessionHandle());
+    }
+  
+  /**
+   * CLoses the current session
+   * @param sessionHandle sessionHandle of the session to remove
+   */
+  private void closeSession(SessionHandle sessionHandle)
+    {
+      getSessionManager().removeSession(sessionHandle);
+      log.debug("Session removed");
+    }
 /**
  * @return the sessionManager
  */