partially implemented simpleclient.EventGenerator
[vamsas.git] / src / org / vamsas / client / simpleclient / VamsasSession.java
index ef76d71..242cf36 100644 (file)
@@ -1,6 +1,13 @@
 package org.vamsas.client.simpleclient;
 
 import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.log4j.Appender;
+import org.apache.log4j.Logger;
+import org.apache.log4j.FileAppender;
 /**
  * 
  * Vamsas client is intialised with a path to create live session directories. 
@@ -41,11 +48,133 @@ import java.io.File;
 
 public class VamsasSession {
   /**
-   * Holds the file handlers for a session.
+   * Holds the file handlers and watchers for a session.
+   */
+  /**
+   * TODO: make sessionDir signature to allow VamsasSession instances to recognise the form of the directory
+   */
+  /**
+   * indicator file for informing other processes that 
+   * they should finalise their vamsas datasets for 
+   * storing in a vamsas archive.
+   */
+  public static final String CLOSEANDSAVE_FILE="exiting";
+  /**
+   * log file location
+   */
+  public static final String SESSION_LOG="Log.txt";
+  private static Log log = LogFactory.getLog(VamsasSession.class);
+  protected Logger slog = Logger.getLogger("org.vamsas.client.SessionLog");
+  /**
+   * setup the sessionLog using Log4j.
+   * @throws IOException
+   */
+  private void initLog() throws IOException {
+    Appender app = slog.getAppender("SESSION_LOG");
+    slog.addAppender(new FileAppender(app.getLayout(), new File(sessionDir, SESSION_LOG).getAbsolutePath()));
+  }
+  
+  /**
+   * the sessionDir is given as the session location for new clients.
    */
   File sessionDir;
+  /**
+   * holds the list of attached clients
+   */
   ClientsFile clist;
-  File vamArchive;
+  public static final String CLIENT_LIST="Clients.obj";
+  /**
+   * holds the data
+   */
+  VamsasFile vamArchive; 
+  public static final String VAMSAS_OBJ="VamDoc.jar";
   
+  /**
+   * sets up the vamsas session files and watchers in sessionDir
+   * @param sessionDir
+   */
+  protected VamsasSession(File sessionDir) throws IOException {
+    if (sessionDir==null)
+      throw new Error("Null directory for VamsasSession.");
+    if (sessionDir.exists()) {
+      if (!sessionDir.isDirectory() || !sessionDir.canWrite() || sessionDir.canRead())
+        throw new IOException("Cannot access '"+sessionDir+"' as a read/writable Directory.");
+      if (checkSessionFiles(sessionDir)) {
+        // session files exist in the directory
+        this.sessionDir = sessionDir;
+        initSessionObjects();
+        slog.debug("Initialising additional VamsasSession instance");
+        log.debug("Attached to VamsasSession in "+sessionDir);
+      } 
+    } else {
+      // start from scratch
+      if (!sessionDir.mkdir())
+        throw new IOException("Failed to make VamsasSession directory in "+sessionDir);
+      this.sessionDir = sessionDir; 
+      createSessionFiles();
+      initSessionObjects();
+      slog.debug("Session directory created.");
+      log.debug("Initialised VamsasSession in "+sessionDir);
+    }
+  }
+  /**
+   * tests presence of existing sessionfiles files in dir
+   * @param dir
+   * @return
+   */
+  private boolean checkSessionFiles(File dir) throws IOException {
+    File c_file = new File(dir,CLIENT_LIST);
+    File v_doc = new File(dir,VAMSAS_OBJ);
+    if (c_file.exists() && v_doc.exists())
+      return true;
+    return false;
+  }
+  /**
+   * create new empty files in dir
+   *
+   */
+  private void createSessionFiles() throws IOException {
+    if (sessionDir==null)
+      throw new IOException("Invalid call to createSessionFiles() with null sessionDir");
+    File c_file = new File(sessionDir,CLIENT_LIST);
+    File v_doc = new File(sessionDir,VAMSAS_OBJ);
+    if (c_file.createNewFile())
+      log.debug("Created new ClientFile "+c_file); // don't care if this works or not
+    if (v_doc.createNewFile())
+      log.debug("Created new Vamsas Session Document File "+v_doc); 
+  }
+  /**
+   * construct SessionFile objects and watchers for each
+   */
+  private void initSessionObjects() throws IOException {
+    if (clist!=null || vamArchive!=null)
+      throw new IOException("initSessionObjects called for initialised VamsasSession object.");
+    clist = new ClientsFile(new File(sessionDir,CLIENT_LIST));
+    vamArchive = new VamsasFile(new File(sessionDir,VAMSAS_OBJ));
+  }
+  /**
+   * make a new watcher object for the clientFile
+   * @return new ClientFile watcher instance
+   */
+  public FileWatcher getClientWatcher() {
+    return new FileWatcher(clist.sessionFile);
+  }
+  /**
+   * make a new watcher object for the vamsas Document
+   * @return new ClientFile watcher instance
+   */
+  public FileWatcher getDocWatcher() {
+    return new FileWatcher(vamArchive.sessionFile);
+  }
+
+  /**
+   * make a new watcher object for the messages file
+   * @return new watcher instance
+   */
+  public FileWatcher getStoreWatcher() {
+    return new FileWatcher(new File(CLOSEANDSAVE_FILE));
+  }
   
 }
+
+