occasional null pointer exception
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleClientFactory.java
index f3dfb52..b4a74d3 100644 (file)
@@ -12,6 +12,7 @@ package uk.ac.vamsas.client.simpleclient;
 import java.io.File;
 
 import java.io.IOException;
+import java.net.MalformedURLException;
 
 
 import org.apache.commons.logging.Log;
@@ -27,7 +28,8 @@ import uk.ac.vamsas.client.UserHandle;
 
 /**
  * 
- * 
+ * creates a session arena in the user home directory under .vamsas.
+ * Each session has its own subdirectory.
  */
 public class SimpleClientFactory implements IClientFactory {
 
@@ -37,7 +39,7 @@ public class SimpleClientFactory implements IClientFactory {
   
   private String vamsasSubdirectoryName = ".vamsas";
   
-  private SessionsFile sessionFile = null;
+ private SimpleSessionManager sessionManager = null;
   private static final String SESSION_LIST="sessions.obj";
   
   //private   String[] currentlyAvailableDessions = null; 
@@ -61,7 +63,7 @@ public class SimpleClientFactory implements IClientFactory {
     String sessionArenaPath =  userHomeDirectory.concat(File.separator.concat(this.vamsasSubdirectoryName));
     
     this.initSessionArena(sessionArenaPath);
-    this.initFactoryObjects();
+   // this.initFactoryObjects();
   }
   
   
@@ -104,42 +106,9 @@ public class SimpleClientFactory implements IClientFactory {
       }
   }
   
-  /**
-   * construct SessionFile objects and watchers for each
-   */
-  private void initFactoryObjects() throws IOException {
-    if (this.sessionFile!=null )
-      throw new IOException("initFactoryObjects called for initialised ClientFactory object.");
-    this.sessionFile = new SessionsFile(new File(this.sessionArena,SESSION_LIST));
-
-  }
-  /**
-   * @see uk.ac.vamsas.client.IClientFactory#getCurrentSessions()
-   */
-  public String[] getCurrentSessions() 
-  { 
-    String[] sessions = null;
-    if (this.sessionFile!=null )
-      {
-        SessionHandle[] sessionHandles =  this.sessionFile.retrieveSessionsList();
-        if (sessionHandles != null)
-          {
-            sessions = new String[sessionHandles.length];
-            for (int i = sessionHandles.length -1; i > 0; i--)
-              {
-                SessionHandle sessionHandle = sessionHandles[i];
-                sessions [i] = sessionHandle.getSessionUrn();
-              }
-          }
-      }
-    return sessions;
-  }
   
+
   
-  private void discoverSession()
-  {
-   
-  }
 
   /**
    * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle)
@@ -149,33 +118,140 @@ public class SimpleClientFactory implements IClientFactory {
    */
   public IClient getIClient(ClientHandle applicationHandle)
       throws NoDefaultSessionException {
-    
+    // create a new session
+    // register new ClientHandle in session
+    // create SimpleClient instance
     return this.getIClient(applicationHandle, (UserHandle) null);
   }
 
   /**
+   * the URN should be something like simpleclient:FILEPATH URL encoded
    * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle, java.lang.String)
    */
   public IClient getIClient(ClientHandle applicationHandle, String sessionUrn) {
-    // TODO Auto-generated method stub
-    return null;
+//  locate session from Urn
+    // check that clientHandle is unique (with default user) - if not update the clientHandle urn to make it unique.
+    // wait for lock and attach to session
+    // create SimpleClient instance
+    log.debug("Trying to create session with URN "+sessionUrn);
+    return this.getIClient(applicationHandle, null, sessionUrn);
+  
   }
 
+  private File convertSessionUrnToFile(String sessionUrn) throws InvalidSessionUrnException
+  {
+    if (sessionUrn == null)
+      {
+        log.debug("Incorrect URN: can not open session.");
+        throw new InvalidSessionUrnException();
+      }
+    
+    SessionUrn urn = new SessionUrn(sessionUrn);
+   return urn.asFile();
+    
+  }
+  
   /**
    * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle, uk.ac.vamsas.client.UserHandle, java.lang.String)
    */
   public IClient getIClient(ClientHandle applicationHandle, UserHandle userId,
       String sessionUrn) {
-    // TODO Auto-generated method stub
-    return null;
+//  locate session from Urn
+    // check Uniqueness of user + ClientHandle in the session. Update clientHandle urn accordingly.
+    // wait for lock, attach to session
+    // create client instance
+    IClient client = null;
+    
+    try {
+      File sessionDirectory = this.convertSessionUrnToFile(sessionUrn);
+      //create session
+      log.debug("found session directory "+sessionDirectory.getAbsolutePath());
+      VamsasSession vamsasSession = new VamsasSession(sessionDirectory);
+   /*   if (userId == null)
+        {
+        //create a default userHandle
+        //with current OS user and hostname
+          userId = new UserHandle(System.getProperty("user.name", System.getProperty("USERNAME","Joe Doe")),
+              System.getProperty("host.name",System.getProperty("HOSTNAME", "Unknown") ));// clientName, clientVersion,  sessionPath);
+        }
+ //create simple client
+       client = new SimpleClient(userId,  applicationHandle,  vamsasSession);*/
+      client = this.initClient(sessionDirectory, userId, applicationHandle);
+    } catch (MalformedURLException e) {
+      log.error("error while creating new IClient: incorrect session urn",e);
+      client = null;
+     
+    } catch (InvalidSessionUrnException e) {
+      log.error("error while creating new IClient: incorrect session urn",e);
+      client = null;
+    } catch (IOException e) {
+      log.error("error while creating new IClient: file access error",e);
+      client = null;
+    }
+    return client;
   }
 
+  
+  private IClient initClient( File sessdir, UserHandle userId, ClientHandle clientHandle) throws IOException, InvalidSessionUrnException
+  {
+    IClient client = null;
+//  create session
+    VamsasSession vamsasSession = new VamsasSession(sessdir);
+  
+    this.getSessionManager().addSession(new SessionHandle(new SessionUrn(vamsasSession).getSessionUrn()));
+    if (userId == null)
+      {
+  //create a default userHandle
+     //   userId = new UserHandle(System.getProperty("user.name", System.getProperty("USERNAME","Joe Doe")),
+       //   System.getProperty("host.name",System.getProperty("HOSTNAME", "Unknown") ));// clientName, clientVersion,  sessionPath);
+        userId = new UserHandle(null, null);
+        
+      }
+    
+    //FullName and organisation should not be null (otherwise UserHandle equals method raises an java.lang.NullPointerException )
+    //use    current OS user and hostname, if null
+    if ( userId.getFullName() == null)
+      {
+        userId.setFullName(System.getProperty("user.name", System.getProperty("USERNAME","Joe Doe")));
+      }
+        
+    if (userId.getOrganization() == null)
+      {
+        userId.setOrganization( System.getProperty("host.name",System.getProperty("HOSTNAME", "Unknown") ));
+      }
+      
+    if (clientHandle == null)
+      clientHandle = new ClientHandle("SimpleVamsasClientApp","0.1");
+     else
+       {
+         if (clientHandle.getClientName() == null)
+           {
+             clientHandle.setClientName("SimpleVamsasClientApp");
+           }
+         if (clientHandle.getVersion() == null)
+           {
+             clientHandle.setVersion("0.1");
+           }
+       }
+    
+  //create simple client
+     client = new SimpleClient(userId,  clientHandle,  vamsasSession);
+     vamsasSession.addClient(client);
+     vamsasSession.setSessionManager(this.getSessionManager());
+     return client;
+  }
   /**
    * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle, uk.ac.vamsas.client.UserHandle)
    */
   public IClient getIClient(ClientHandle applicationHandle, UserHandle userId)
       throws NoDefaultSessionException {
-    SimpleClient client = null;
+    // create new session
+    // register SimpleClient and UserHandles in session
+    // create client instance
+    IClient client = null;
     if (this.sessionArena==null)
       throw new Error("Improperly initialised SimpleClientFactory object - null sessionArena.");
     
@@ -183,7 +259,19 @@ public class SimpleClientFactory implements IClientFactory {
     //create default clientHandle with "SimpleVamsasClientApp","0.1",
     if (clientHandle == null)
      clientHandle = new ClientHandle("SimpleVamsasClientApp","0.1");
+    else
+      {
+        if (clientHandle.getClientName() == null)
+        {
+          clientHandle.setClientName("SimpleVamsasClientApp");
+        }
+      
     
+    if (clientHandle.getVersion() == null)
+      {
+        clientHandle.setVersion("0.1");
+      }
+      }
     //check if any available session(s)
     String[] availableSessions = this.getCurrentSessions();
     if (availableSessions != null) 
@@ -218,7 +306,7 @@ public class SimpleClientFactory implements IClientFactory {
        if (!(sessdir.delete() && sessdir.mkdir()))
           throw new IOException("Could not make session directory "+sessdir);
       //create session
-        VamsasSession vamsasSession = new VamsasSession(sessdir);
+     /*   VamsasSession vamsasSession = new VamsasSession(sessdir);
       
         this.getSessionFile().addSession(new SessionHandle(new SessionUrn(vamsasSession).getSessionUrn()), false);
         if (userId == null)
@@ -231,7 +319,8 @@ public class SimpleClientFactory implements IClientFactory {
      
       
       //create simple client
-         client = new SimpleClient(userId,  clientHandle,  vamsasSession);
+         client = new SimpleClient(userId,  clientHandle,  vamsasSession);*/
+       client = this.initClient(sessdir, userId, clientHandle);
       } 
     catch (IOException e) 
       {
@@ -247,15 +336,30 @@ public class SimpleClientFactory implements IClientFactory {
 
 
   /**
+   * @see uk.ac.vamsas.client.IClientFactory#getCurrentSessions()
+   */
+  public String[] getCurrentSessions() {
+    String [] sessions = null;
+    try {
+      sessions =this.getSessionManager().getCurrentSessions();
+    } catch (IOException e) {
+     log.error("Unable to get available sessions",e);
+     sessions = null;
+    }
+    return sessions;
+  }
+
+
+  /**
    * @return the sessionFile
    */
-  private SessionsFile getSessionFile()  throws IOException
+  private SimpleSessionManager getSessionManager()  throws IOException
     {
-      if (this.sessionFile == null)
+      if (this.sessionManager == null)
         {
-          this.sessionFile = new SessionsFile( new File (this.sessionArena, SESSION_LIST));
+          this.sessionManager = new SimpleSessionManager( new File (this.sessionArena, SESSION_LIST));
         }
-      return this.sessionFile;
+      return this.sessionManager;
     }