applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleClientFactory.java
index 27aaf77..43cc4b6 100644 (file)
@@ -1,12 +1,24 @@
-/**
+/*
+ * This file is part of the Vamsas Client version 0.1. 
+ * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, 
+ *  Andrew Waterhouse and Dominik Lindner.
+ * 
+ * Earlier versions have also been incorporated into Jalview version 2.4 
+ * since 2008, and TOPALi version 2 since 2007.
  * 
- * VAMSAS Project
- *
+ * The Vamsas Client is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *  
+ * The Vamsas Client is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
  * 
- * Dec 13, 2006 
- *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.
  */
-
 package uk.ac.vamsas.client.simpleclient;
 
 import java.io.File;
@@ -107,8 +119,8 @@ public class SimpleClientFactory implements IClientFactory {
   /**
    * @see uk.ac.vamsas.client.IClientFactory#getIClient(uk.ac.vamsas.client.ClientHandle)
    * 
-   * Creates a IClient object, using default UserHandle with system
-   * variables:"user.name" or "USERNAME")), "host.name" or "HOSTNAME"
+   *      Creates a IClient object, using default UserHandle with system
+   *      variables:"user.name" or "USERNAME")), "host.name" or "HOSTNAME"
    */
   public IClient getIClient(ClientHandle applicationHandle)
       throws NoDefaultSessionException {
@@ -172,25 +184,28 @@ public class SimpleClientFactory implements IClientFactory {
           .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",
+       * 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); }
+       * 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, null);
+      client = this.initClient(sessionDirectory, userId, applicationHandle,
+          null);
     } catch (MalformedURLException e) {
       log.error("error while creating new IClient: incorrect session urn", e);
       client = null;
-    } catch (InvalidSessionDocumentException e)
-    {
-      log.error("error while creating new IClient: invalid session document", e);
+    } catch (InvalidSessionDocumentException e) {
+      log
+          .error("error while creating new IClient: invalid session document",
+              e);
       client = null;
     } catch (InvalidSessionUrnException e) {
       log.error("error while creating new IClient: incorrect session urn", e);
@@ -201,23 +216,32 @@ public class SimpleClientFactory implements IClientFactory {
     }
     return client;
   }
+
   /**
-   * initialise the vamsas session state and create a SimpleClient object to connect to it
-   * @param sessdir newly created or existing session directory
-   * @param userId 
+   * initialise the vamsas session state and create a SimpleClient object to
+   * connect to it
+   * 
+   * @param sessdir
+   *          newly created or existing session directory
+   * @param userId
    * @param clientHandle
-   * @param vamsasDocument null or a document to pass to SimpleCLient to write into the sessdir
+   * @param vamsasDocument
+   *          null or a document to pass to SimpleCLient to write into the
+   *          sessdir
    * @return the client
-   * @throws IOException if there are problems in session or client creation or if the session already has a vamsasDocument
-   * @throws InvalidSessionUrnException for a malformed sessdir
+   * @throws IOException
+   *           if there are problems in session or client creation or if the
+   *           session already has a vamsasDocument
+   * @throws InvalidSessionUrnException
+   *           for a malformed sessdir
    */
   private IClient initClient(File sessdir, UserHandle userId,
-      ClientHandle clientHandle, File vamsasDocument) throws IOException, InvalidSessionUrnException, InvalidSessionDocumentException {
+      ClientHandle clientHandle, File vamsasDocument) throws IOException,
+      InvalidSessionUrnException, InvalidSessionDocumentException {
     IClient client = null;
     // create session
     VamsasSession vamsasSession = null;
-    if (vamsasDocument==null)
-    {
+    if (vamsasDocument == null) {
       vamsasSession = new VamsasSession(sessdir);
     } else {
       vamsasSession = new VamsasSession(sessdir, vamsasDocument);
@@ -295,7 +319,8 @@ public class SimpleClientFactory implements IClientFactory {
     // check if any available session(s)
     String[] availableSessions = this.getCurrentSessions();
     if (availableSessions != null) {// there are available sessions
-      if (availableSessions.length > 1) {// more than one session if available... can not choose
+      if (availableSessions.length > 1) {// more than one session if
+                                         // available... can not choose
 
         // represents list of session as String
         StringBuffer sessionURNs = new StringBuffer("");
@@ -317,43 +342,47 @@ public class SimpleClientFactory implements IClientFactory {
     // no session available - create a new one
     try {
       client = clientInNewSession(userId, clientHandle, null);
-    } catch (Exception e)
-    {
-      throw new Error("IMPLEMENTATION ERROR: unexpected exception when creating a new session to connect to.",e);
+    } catch (Exception e) {
+      throw new Error(
+          "IMPLEMENTATION ERROR: unexpected exception when creating a new session to connect to.",
+          e);
     }
     return client;
   }
 
   /**
-   * create a new session directory and possibly import an existing document into it
+   * create a new session directory and possibly import an existing document
+   * into it
+   * 
    * @param userId
    * @param clientHandle
-   * @param vamsasDocument null or a document file to copy into the new session
+   * @param vamsasDocument
+   *          null or a document file to copy into the new session
    * @return null or a valid IClient instance
    */
   private IClient clientInNewSession(UserHandle userId,
-      ClientHandle clientHandle, File vamsasDocument) throws InvalidSessionDocumentException, InvalidSessionUrnException{
+      ClientHandle clientHandle, File vamsasDocument)
+      throws InvalidSessionDocumentException, InvalidSessionUrnException {
 
     IClient client = null;
     try {
       // try and make a friendly session name
       String sesspref = "";
-      if (vamsasDocument!=null)
-      {
-        sesspref = vamsasDocument.getName().replaceAll("([^-A-Za-z0-9]|\\.vdj)", "");
+      if (vamsasDocument != null) {
+        sesspref = vamsasDocument.getName().replaceAll(
+            "([^-A-Za-z0-9]|\\.vdj)", "");
       }
-      sesspref += (new java.util.Date()).toString().replaceAll("[^-A-Za-z0-9]","_");
+      sesspref += (new java.util.Date()).toString().replaceAll("[^-A-Za-z0-9]",
+          "_");
       // create sessionDirectory
-      File sessdir = new File(sessionArena, sesspref+".simpleclient");
-      if (sessdir.exists())
-      {  
+      File sessdir = new File(sessionArena, sesspref + ".simpleclient");
+      if (sessdir.exists()) {
         // make a unique session name
-        sessdir = File.createTempFile(sesspref, ".simpleclient",
-          sessionArena);
+        sessdir = File.createTempFile(sesspref, ".simpleclient", sessionArena);
       } else {
-        if (!sessdir.createNewFile())
-        {
-          throw new Error("VAMSAS Implementation error : sesspref friendly session name is invalid on this platform - please tell the authors!");
+        if (!sessdir.createNewFile()) {
+          throw new Error(
+              "VAMSAS Implementation error : sesspref friendly session name is invalid on this platform - please tell the authors!");
         }
       }
       log.debug("Creating new session  directory");
@@ -363,8 +392,8 @@ public class SimpleClientFactory implements IClientFactory {
     } catch (IOException e) {
       log.error("error while creating new IClient", e);
     } catch (InvalidSessionUrnException e) {
-      log.error("Unable to create new IClient. The new session urn is malformed.",
-          e);
+      log.error(
+          "Unable to create new IClient. The new session urn is malformed.", e);
     }
 
     return client;
@@ -398,9 +427,8 @@ public class SimpleClientFactory implements IClientFactory {
   public IClient getNewSessionIClient(ClientHandle applicationHandle) {
     try {
       return clientInNewSession(null, applicationHandle, null);
-    }
-    catch (Exception e) {
-      log.error("Failed to create new session for app with default user.",e);
+    } catch (Exception e) {
+      log.error("Failed to create new session for app with default user.", e);
     }
     return null;
   }
@@ -410,40 +438,41 @@ public class SimpleClientFactory implements IClientFactory {
     try {
       return clientInNewSession(userId, applicationHandle, null);
     } catch (Exception e) {
-      log.error("Failed to create new session for app and user.",e);
+      log.error("Failed to create new session for app and user.", e);
     }
     return null;
   }
-  private void checkImportedDocument(File vamsasDocument) throws InvalidSessionDocumentException 
-  {
-    if (!vamsasDocument.exists())
-    {
-      throw new InvalidSessionDocumentException("File "+vamsasDocument+" does not exist");
+
+  private void checkImportedDocument(File vamsasDocument)
+      throws InvalidSessionDocumentException {
+    if (!vamsasDocument.exists()) {
+      throw new InvalidSessionDocumentException("File " + vamsasDocument
+          + " does not exist");
     }
-    if (!vamsasDocument.canRead())
-    {
-      throw new InvalidSessionDocumentException("File "+vamsasDocument+" does not exist");
+    if (!vamsasDocument.canRead()) {
+      throw new InvalidSessionDocumentException("File " + vamsasDocument
+          + " does not exist");
     }
   }
+
   public IClient openAsNewSessionIClient(ClientHandle applicationHandle,
       File vamsasDocument) throws InvalidSessionDocumentException {
-    checkImportedDocument(vamsasDocument);  
+    checkImportedDocument(vamsasDocument);
     try {
       return clientInNewSession(null, applicationHandle, vamsasDocument);
-    } catch (InvalidSessionUrnException e)
-    {
+    } catch (InvalidSessionUrnException e) {
       throw new InvalidSessionDocumentException("Unexpected exception", e);
     }
   }
 
   public IClient openAsNewSessionIClient(ClientHandle applicationHandle,
-      UserHandle userId, File vamsasDocument) throws InvalidSessionDocumentException {
-    checkImportedDocument(vamsasDocument);  
+      UserHandle userId, File vamsasDocument)
+      throws InvalidSessionDocumentException {
+    checkImportedDocument(vamsasDocument);
     try {
-    return clientInNewSession(userId, applicationHandle, vamsasDocument);
-    } catch (InvalidSessionUrnException e)
-    {
-    throw new InvalidSessionDocumentException("Unexpected exception", e);
+      return clientInNewSession(userId, applicationHandle, vamsasDocument);
+    } catch (InvalidSessionUrnException e) {
+      throw new InvalidSessionDocumentException("Unexpected exception", e);
     }
   }