tidied the exceptions and ws server error behaviour
[jalview.git] / src / jalview / ws / MsaWSClient.java
index 13a3a9c..faf2faf 100755 (executable)
@@ -16,20 +16,13 @@ import vamsas.objects.*;
 
 
 public class MsaWSClient
+    extends WSClient
 {
-  int jobsRunning = 0;
-  ext.vamsas.MuscleWS server;
-  WebserviceInfo wsInfo;
-  /**
-   * MsaWSClient
-   *
-   * @param msa SequenceI[]
-   */
-
-  String WebServiceName;
-  String WebServiceJobTitle;
-  String WebServiceReference;
-  String WsURL;
+    /**
+     * server is a WSDL2Java generated stub for an archetypal MsaWSI service.
+     */
+    ext.vamsas.MuscleWS server;
+
   private boolean setWebService(String MsaWSName) {
     if (MsaWServices.info.containsKey(MsaWSName)) {
       WebServiceName = MsaWSName;
@@ -43,9 +36,6 @@ public class MsaWSClient
     }
   }
 
-//  public MsaWSClient(String MsaWSName, SequenceI[] msa) {
-//    MsaWSClient(MsaWSName, msa, true);
-//  }
 
   public MsaWSClient(String MsaWSName, String altitle, SequenceI[] msa, boolean submitGaps, boolean preserveOrder)
   {
@@ -57,13 +47,13 @@ public class MsaWSClient
 
     wsInfo = new jalview.gui.WebserviceInfo(WebServiceJobTitle, WebServiceReference);
 
-    wsInfo.setProgressText("Alignment of "+altitle+"\nJob details\n");
+    wsInfo.setProgressText(((submitGaps) ? "Re-alignment" : "Alignment")+" of "+altitle+"\nJob details\n");
 
     // TODO: MuscleWS transmuted to generic MsaWS client
     MuscleWSServiceLocator loc = new MuscleWSServiceLocator(); // Default
     try {
-      this.server = (MuscleWS) loc.getMuscleWS(// JBPNote will be set from properties
-      new java.net.URL(WsURL));
+      this.server = (MuscleWS) loc.getMuscleWS(new java.net.URL(WsURL));
+      ((MuscleWSSoapBindingStub) this.server).setTimeout(60000); // One minute timeout
     }
     catch (Exception ex) {
       wsInfo.setProgressText("Serious! "+WebServiceName+" Service location failed\nfor URL :"
@@ -71,7 +61,7 @@ public class MsaWSClient
       wsInfo.setStatus(wsInfo.ERROR);
       ex.printStackTrace();
     }
-
+    loc.getEngine().setOption("axis","1");
     MsaWSThread musclethread = new MsaWSThread(WebServiceName+" alignment of "+altitle, msa, submitGaps, preserveOrder);
     wsInfo.setthisService(musclethread);
     musclethread.start();
@@ -113,13 +103,14 @@ public class MsaWSClient
 
       for (int i = 0; i < msa.length; i++)
       {
-        String newname = new String("Sequence"+i);
-        // uniquify as we go - JBPNote: TODO: this is a ubiquitous transformation
-        SeqNames.put(newname, msa[i].getName());
+        String newname = jalview.analysis.SeqsetUtils.unique_name(i);
+        // uniquify as we go
+        // TODO: JBPNote: this is a ubiquitous transformation - set of jalview seq objects to vamsas sequences with name preservation
+        SeqNames.put(newname, jalview.analysis.SeqsetUtils.SeqCharacterHash(msa[i]));
         seqarray[i] = new vamsas.objects.simple.Sequence();
-        seqarray[i].setId(new String("Sequence"+i));
+        seqarray[i].setId(newname);
         seqarray[i].setSeq((submitGaps) ? msa[i].getSequence()
-                           : AlignSeq.extractGaps("-. ", msa[i].getSequence()));
+                           : AlignSeq.extractGaps(jalview.util.Comparison.GapChars, msa[i].getSequence()));
       }
 
       this.seqs = new vamsas.objects.simple.SequenceSet();
@@ -129,7 +120,7 @@ public class MsaWSClient
     boolean jobComplete = false;
 
     public void cancelJob() {
-      if (!jobComplete) {
+      if (jobId!=null && !jobId.equals("") && !jobComplete) {
         String cancelledMessage="";
         try {
           vamsas.objects.simple.WsJobId cancelledJob = server.cancel(jobId);
@@ -157,6 +148,11 @@ public class MsaWSClient
           exc.printStackTrace();
         }
         wsInfo.setProgressText(OutputHeader + cancelledMessage+"\n");
+      } else {
+        if (!jobComplete)
+        {
+          wsInfo.setProgressText(OutputHeader + "Server cannot cancel this job because it has not been submitted properly. just close the window.\n");
+        }
       }
     }
 
@@ -169,12 +165,13 @@ public class MsaWSClient
       {
         try
         {
-          result = server.getResult(jobId);
+          if ((result = server.getResult(jobId))==null)
+            throw(new Exception("Timed out when communicating with server\nTry again later.\n"));
 
-         if( result.isRunning() )
-           wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
-         else if( result.isQueued() )
-           wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
+          if (result.isRunning())
+            wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
+          else if (result.isQueued())
+            wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
 
           if (result.isFinished())
           {
@@ -184,44 +181,80 @@ public class MsaWSClient
           }
           else
           {
-            wsInfo.setProgressText(OutputHeader + "\n" + result.getStatus());
+            if (result.getStatus() != null)
+              wsInfo.setProgressText(OutputHeader + "\n" + result.getStatus());
             if (! (result.isJobFailed() || result.isServerError()))
             {
               Thread.sleep(5000);
               //  System.out.println("I'm alive "+seqid+" "+jobid);
             }
+            else
+            {
+              break;
+            }
           }
         }
         catch (Exception ex)
         {
           allowedServerExceptions--;
-          wsInfo.appendProgressText("\n"+ServiceName+" Server exception!\n" + ex.getMessage());
-          ex.printStackTrace();
+          wsInfo.appendProgressText("\n" + ServiceName + " Server exception!\n" +
+                                    ex.getMessage());
+          System.err.println(ServiceName + " Server exception: " +
+                                    ex.getMessage());
+          //          ex.printStackTrace(); JBPNote Debug
+          try
+          {
+            if (allowedServerExceptions>0)
+              Thread.sleep(5000);
+          }
+          catch (InterruptedException ex1)
+          {
+          }
+
         }
       }
-
-      if (! (result!=null && (result.isJobFailed() || result.isServerError())))
-        wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
+      if (allowedServerExceptions == 0)
+      {
+        wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
+      }
       else
-        wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
+      {
+        if (! (result != null && (result.isJobFailed() || result.isServerError())))
+          wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
+        else
+        {
+          if (result.isFailed())
+            wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
+          if (result.isServerError())
+            wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
+        }
+      }
     }
-
     void StartJob()
     {
       try
       {
         vamsas.objects.simple.WsJobId jobsubmit = server.align(seqs);
-        if (jobsubmit.getStatus()==1) {
-          System.out.println(jobId=jobsubmit.getJobId());
+        if (jobsubmit!=null && jobsubmit.getStatus()==1) {
+          jobId=jobsubmit.getJobId();
+          System.out.println(WsURL+" Job Id '"+jobId+"'");
         } else {
+          if (jobsubmit == null) {
+            throw new Exception("Server at "+WsURL+" returned null object, it probably cannot be contacted. Try again later ?");
+          }
           throw new Exception(jobsubmit.getJobId());
         }
       }
       catch (Exception e)
       {
-        System.out.println(ServiceName + " Client: Failed to submit the prediction\n" +
+        // TODO: JBPNote catch timeout or other fault types explicitly
+        // For unexpected errors
+        System.err.println(WebServiceName + " Client: Failed to submit the sequences for alignment.\n"+WsURL+" : " +
                            e.toString() + "\n");
-        e.printStackTrace();
+        this.allowedServerExceptions=0;
+        wsInfo.setStatus(wsInfo.STATE_STOPPED_SERVERERROR);
+        wsInfo.appendProgressText("Server problems! "+e.toString()+"\nFailed to submit sequences for alignment. Just close the window\n");
+        // e.printStackTrace(); // TODO: JBPNote DEBUG
       }
     }