added legacy import of version 0.1 SessionLists
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SessionsFile.java
index 5730317..9871bbf 100644 (file)
@@ -1,6 +1,6 @@
 /*\r
- * This file is part of the Vamsas Client version 0.1. \r
- * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
+ * This file is part of the Vamsas Client version 0.2. \r
+ * Copyright 2010 by Jim Procter, Iain Milne, Pierre Marguerite, \r
  *  Andrew Waterhouse and Dominik Lindner.\r
  * \r
  * Earlier versions have also been incorporated into Jalview version 2.4 \r
@@ -26,11 +26,13 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;\r
 import java.io.ObjectInputStream;\r
 import java.io.ObjectOutputStream;\r
+import java.net.URL;\r
 \r
 import org.apache.commons.logging.Log;\r
 import org.apache.commons.logging.LogFactory;\r
 \r
 import uk.ac.vamsas.client.SessionHandle;\r
+import uk.ac.vamsas.client.simpleclient.SimpleSessionHandle;\r
 \r
 /**\r
  * @author <a href="mailto:pierre@ebi.ac.uk">Pierre MARGUERITE</a>\r
@@ -67,10 +69,10 @@ public class SessionsFile extends ListFile {
    * \r
    * @return list of clients\r
    */\r
-  private SessionHandle[] retrieveSessionHandles() {\r
+  private SimpleSessionHandle[] retrieveSessionHandles() {\r
     if (lockFile()) {\r
       try {\r
-        SessionHandle[] sessions = null;\r
+        SimpleSessionHandle[] sessions = null;\r
         if (this.fileLock.length() > 0) {\r
 \r
           ObjectInputStream is = new ObjectInputStream(this.fileLock\r
@@ -79,7 +81,30 @@ public class SessionsFile extends ListFile {
           o = is.readObject();\r
           if (o != null) {\r
             try {\r
-              sessions = (SessionHandle[]) o;\r
+              if (o instanceof SimpleSessionHandle[]) {\r
+                sessions = (SimpleSessionHandle[]) o;\r
+              } else {\r
+                // legacy support or incorrect path.\r
+                if (o instanceof SessionHandle[]) {\r
+                  log.info("Importing VAMSAS 0.1 clientHandle list.");\r
+                  SessionHandle[] oldsess = (SessionHandle[]) o;\r
+                  File sessdir;\r
+                  sessions = new SimpleSessionHandle[oldsess.length];\r
+                  for (int s=0;s<oldsess.length;s++)\r
+                  {\r
+                    try {\r
+                      sessdir = new SessionUrn(oldsess[s].getSessionUrn()).asFile();\r
+                      // try to locate it.\r
+                      sessions[s] = new SimpleSessionHandle(oldsess[s].getSessionUrn(), sessdir);\r
+                    } catch (Exception e)\r
+                    {\r
+                      // give up and report an error.\r
+                      sessions = null;\r
+                      throw new Exception("Legacy SessionFile problem. Couldn't locate "+oldsess[s]+" as a file.",e);\r
+                    }\r
+                  }\r
+                }\r
+              }\r
             } catch (Exception e) {\r
               log.error("Garbage in the clientHandle list " + this.sessionFile,\r
                   e);\r
@@ -104,9 +129,9 @@ public class SessionsFile extends ListFile {
    * \r
    * @return sessionsList\r
    */\r
-  public SessionHandle[] retrieveSessionsList() {\r
+  public SimpleSessionHandle[] retrieveSessionsList() {\r
     if (lockFile()) {\r
-      SessionHandle[] clients = retrieveSessionHandles();\r
+      SimpleSessionHandle[] clients = retrieveSessionHandles();\r
       unlockFile();\r
       return clients;\r
     }\r
@@ -119,9 +144,9 @@ public class SessionsFile extends ListFile {
    * @param extantlock\r
    * @return sessionList or null if lock failed (or file was empty)\r
    */\r
-  public SessionHandle[] retrieveSessionsList(Lock extantlock) {\r
+  public SimpleSessionHandle[] retrieveSessionsList(Lock extantlock) {\r
     if (lockFile(extantlock)) {\r
-      SessionHandle[] sessions = retrieveSessionHandles();\r
+      SimpleSessionHandle[] sessions = retrieveSessionHandles();\r
       unlockFile();\r
       return sessions;\r
     }\r
@@ -136,7 +161,7 @@ public class SessionsFile extends ListFile {
    * @return session index in list or 0 if lock was invalid or addSession\r
    *         operation failed.\r
    */\r
-  public int addSession(SessionHandle newSession, Lock extantLock) {\r
+  public int addSession(SimpleSessionHandle newSession, Lock extantLock) {\r
     return addSession(newSession, true, extantLock);\r
   }\r
 \r
@@ -153,7 +178,7 @@ public class SessionsFile extends ListFile {
    * @return client index in list or 0 if addSession (or the lock) failed.\r
    */\r
 \r
-  public int addSession(SessionHandle newSession, boolean disambig,\r
+  public int addSession(SimpleSessionHandle newSession, boolean disambig,\r
       Lock extantLock) {\r
     if (lockFile(extantLock)) {\r
       this.syncnum = addSession(newSession, disambig);\r
@@ -176,10 +201,10 @@ public class SessionsFile extends ListFile {
     int mynum = -1;\r
     if (lockFile(sessionlock)) {\r
 \r
-      SessionHandle[] sessions = retrieveSessionHandles();\r
+      SimpleSessionHandle[] sessions = retrieveSessionHandles();\r
       if (sessions != null) {\r
         if ((this.syncnum <= 0 || this.syncnum > sessions.length)\r
-            || !sessions[this.syncnum - 1].equals(session)) {\r
+            || !session.equals(sessions[this.syncnum - 1])) {\r
           for (int i = 0, j = sessions.length; i < j; i++) {\r
             if (sessions[i].equals(session)) {\r
               mynum = i;\r
@@ -191,7 +216,7 @@ public class SessionsFile extends ListFile {
         }\r
 \r
         if (mynum > -1) {\r
-          SessionHandle[] newlist = new SessionHandle[sessions.length - 1];\r
+          SimpleSessionHandle[] newlist = new SimpleSessionHandle[sessions.length - 1];\r
           for (int k = 0, i = 0, j = sessions.length; i < j; i++)\r
             if (i != mynum)\r
               newlist[k++] = sessions[i];\r
@@ -223,7 +248,7 @@ public class SessionsFile extends ListFile {
    * @return index of sessionHandle in new list, or -1-position of existing\r
    *         sessionHandle (if disambiguate is true)\r
    */\r
-  protected int addSession(SessionHandle session, boolean disambiguate) {\r
+  protected int addSession(SimpleSessionHandle session, boolean disambiguate) {\r
     int newsession = 0;\r
     int tries = 5;\r
     while (tries-- > 0 && !lockFile())\r
@@ -233,10 +258,10 @@ public class SessionsFile extends ListFile {
       }\r
     ;\r
     if (lockFile()) {\r
-      SessionHandle[] sessions = retrieveSessionHandles();\r
+      SimpleSessionHandle[] sessions = retrieveSessionHandles();\r
 \r
       if (sessions == null) {\r
-        sessions = new SessionHandle[1];\r
+        sessions = new SimpleSessionHandle[1];\r
         sessions[0] = session;\r
         newsession = 1;\r
       } else {\r
@@ -258,7 +283,7 @@ public class SessionsFile extends ListFile {
           }\r
         }\r
         int i, j;\r
-        SessionHandle[] newlist = new SessionHandle[sessions.length + 1];\r
+        SimpleSessionHandle[] newlist = new SimpleSessionHandle[sessions.length + 1];\r
         for (i = 0, j = sessions.length; i < j; i++)\r
           newlist[i] = sessions[i];\r
         newlist[j] = session;\r
@@ -277,7 +302,7 @@ public class SessionsFile extends ListFile {
    * @param clients\r
    * @return true if successful write. Throws Errors otherwise.\r
    */\r
-  protected boolean putSessionsList(SessionHandle[] clients) {\r
+  protected boolean putSessionsList(SimpleSessionHandle[] clients) {\r
     if (lockFile()) {\r
       File templist = null;\r
       if (!this.backup || (templist = backupSessionFile()) != null) {\r