Added to mechanism to determine if a client closing is the last one of the session.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleClient.java
index 455a0f2..ca68e54 100644 (file)
@@ -10,6 +10,10 @@ import java.beans.PropertyChangeListener;
 import java.beans.PropertyChangeSupport;
 import java.io.File;
 import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+
+import java.nio.channels.OverlappingFileLockException;
 import java.util.Hashtable;
 import java.util.Vector;
 
@@ -43,6 +47,15 @@ public class SimpleClient implements IClient {
   protected ClientHandle client = null;
   protected EventGeneratorThread evgen = null;
   protected ClientDocument cdocument = null;
+  
+  
+  
+  
+  private java.nio.channels.FileLock activeClientFilelock = null;
+  
+  private FileChannel activeClientFileChannel = null;
+  private  File clientlockFile = null;
+  
   /**
    * object hash table that persists in each client holding vorbaIds and hash values after a document write
    */
@@ -445,7 +458,7 @@ public class SimpleClient implements IClient {
     // more complex if data is already present in document. Could have a 'clearSession' method, too, or dump and overwrite instead.
     log.error("importDocument is not yet implemented for a SimpleClient Session.");
     
-   /* try {
+    /*try {
       this._session.setVamsasDocument(location);
     } catch (IOException e) {
       log.error("importDocument failed.");
@@ -495,4 +508,102 @@ public class SimpleClient implements IClient {
       pickmanager = new SimplePickManager(new uk.ac.vamsas.client.picking.SocketManager());
     }
   }
+  
+  
+  protected void releaseActiveClientFile() throws IOException
+  {
+   
+    log.debug("Releasing active client file");
+    if( activeClientFilelock != null)
+    // Release the lock
+      activeClientFilelock.release();
+          
+    if (activeClientFileChannel != null)
+        // Close the file
+        activeClientFileChannel.close();
+    if (this.clientlockFile != null)
+    {
+      this.clientlockFile.delete();
+      log.debug("deleted active client lock file");
+    }
+
+  }
+  
+  protected void  createActiveClientFile() throws IOException
+  {
+    if(this.clientlockFile != null )return; 
+   log.debug("createActiveClientFile");
+    //create, if need,  subdirectory to contain client files
+   File clientlockFileDir = new File ( this.get_session().sessionDir, this.get_session().clientFileDirectory);
+    if( !clientlockFileDir.exists())
+      {//the directory does not exist, create it
+        if (! clientlockFileDir.mkdirs())
+        {
+          throw new IOException("Failed to create sub directory to session directory  for client lock files'"+clientlockFileDir.getAbsolutePath()+"'");
+        }
+      }
+    else
+    {
+      if (!(clientlockFileDir.isDirectory() && clientlockFileDir.canWrite()))
+      {
+        throw new IOException("Directory  for client lock files is not a directory or is not accessibl: '"+clientlockFileDir.getAbsolutePath()+"'");
+       }
+    }
+    this.clientlockFile = new File (clientlockFileDir, this.getClientHandle().getClientUrn().replaceAll(File.separator, "").replaceAll(":", "").replaceAll(";", ""));
+   
+    log.debug("Creating active client lock file "+ this.clientlockFile.getAbsolutePath());
+    if (clientlockFile.exists())
+     {//should not happen, file should be deleted when the application closes or when a crashed application has been detected
+       log.error("client lock file already exits");
+     }
+   try {
+     //create the empty file
+     if(! clientlockFile.createNewFile())
+       {
+       log.error("Unable to create active client lock file");
+       
+         return;
+       }
+     else
+       log.debug("file created");
+       // Get a file channel for the file
+       FileChannel channel = new RandomAccessFile(clientlockFile, "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
+          {
+           activeClientFilelock = channel.tryLock();
+           log.debug("Got lock");
+          }
+       catch (OverlappingFileLockException e) 
+       {
+         // File is already locked in this thread or virtual machine
+         log.error("Oups the file is already locked",e);
+         
+       }
+     // Release the lock
+ /*    lock.release();
+       
+     // Close the file
+     channel.close();*/
+ } catch (Exception e) {
+   log.error("Error  during lock file creation",e);
+ }
+
+
+    
+  }
+
+  /**
+   * @return the clientlockFile
+   */
+  protected File getClientlockFile() {
+    return clientlockFile;
+  }
 }