2.5.1 release branding
[jalview.git] / src / jalview / ws / WSThread.java
index 4c29537..121533b 100644 (file)
-package jalview.ws;
-
-import jalview.gui.AlignFrame;
-import jalview.gui.WebserviceInfo;
-import jalview.datamodel.AlignmentView;
-import jalview.gui.Desktop;
-import javax.swing.JOptionPane;
-import jalview.bin.Cache;
-
-public abstract class WSThread extends Thread
-{
-    /**
-     * Generic properties for Web Service Client threads.
-     */
-    AlignFrame alignFrame;
-  WebserviceInfo wsInfo = null;
-  AlignmentView input;
-  boolean jobComplete = false;
-  abstract class WSJob {
-      /**
-       * Generic properties for an individual job within a Web Service Client thread
-       */
-    int jobnum = 0; // WebServiceInfo pane for this job
-    String jobId; // ws job ticket
-    boolean cancelled = false;
-    int allowedServerExceptions = 3; // job dies if too many exceptions.
-    boolean submitted = false;
-    boolean subjobComplete = false;
-    /**
-     *
-     * @return true if job has completed and valid results are available
-     */
-    abstract boolean hasResults();
-    /**
-     *
-     * @return boolean true if job can be submitted.
-     */
-    abstract boolean hasValidInput();
-    vamsas.objects.simple.Result result;
-  }
-  class JobStateSummary {
-    int running = 0;
-    int queuing = 0;
-    int finished = 0;
-    int error = 0;
-    int serror = 0;
-    int cancelled = 0;
-    int results = 0;
-    void updateJobPanelState(WebserviceInfo wsInfo, String OutputHeader,
-                             WSJob j)
-    {
-      if (j.result != null)
-      {
-        String progheader = "";
-        // Parse state of job[j]
-        if (j.result.isRunning())
-        {
-          running++;
-          wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
-        }
-        else if (j.result.isQueued())
-        {
-          queuing++;
-          wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
-        }
-        else if (j.result.isFinished())
-        {
-          finished++;
-          j.subjobComplete = true;
-          if (j.hasResults())
-            results++;
-          wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
-        }
-        else if (j.result.isFailed())
-        {
-          progheader += "Job failed.\n";
-          j.subjobComplete = true;
-          wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
-          error++;
-        }
-        else if (j.result.isServerError())
-        {
-          serror++;
-          j.subjobComplete = true;
-          wsInfo.setStatus(j.jobnum,
-                           WebserviceInfo.STATE_STOPPED_SERVERERROR);
-        }
-        else if (j.result.isBroken() || j.result.isFailed())
-        {
-          error++;
-          j.subjobComplete = true;
-          wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
-        }
-        // and pass on any sub-job messages to the user
-        wsInfo.setProgressText(j.jobnum, OutputHeader);
-        wsInfo.appendProgressText(j.jobnum, progheader);
-        if (j.result.getStatus() != null)
-        {
-          wsInfo.appendProgressText(j.jobnum, j.result.getStatus());
-        }
-      }
-      else
-      {
-        if (j.submitted && j.subjobComplete)
-        {
-          if (j.allowedServerExceptions == 0)
-          {
-            serror++;
-          }
-          else if (j.result == null)
-          {
-            error++;
-          }
-        }
-      }
-    }
-  }
-
-  WSJob jobs[] = null;
-  String WebServiceName = null;
-  String OutputHeader;
-  String WsUrl = null;
-  abstract void pollJob(WSJob job) throws Exception;
-  public void run()
-  {
-  JobStateSummary jstate=null;
-  if (jobs==null)
-    jobComplete=true;
-  while (!jobComplete)
-  {
-    jstate = new JobStateSummary();
-    for (int j = 0; j < jobs.length; j++)
-    {
-
-      if (!jobs[j].submitted && jobs[j].hasValidInput())
-      {
-        StartJob(jobs[j]);
-      }
-
-      if (jobs[j].submitted && !jobs[j].subjobComplete)
-      {
-        try
-        {
-          pollJob(jobs[j]);
-          if (jobs[j].result == null)
-          {
-            throw (new Exception(
-                "Timed out when communicating with server\nTry again later.\n"));
-          }
-          jalview.bin.Cache.log.debug("Job " + j + " Result state " +
-                                      jobs[j].result.getState()
-                                      + "(ServerError=" +
-                                      jobs[j].result.isServerError() + ")");
-        }
-        catch (Exception ex)
-        {
-          // Deal with Transaction exceptions
-          wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName
-                                    + " Server exception!\n" + ex.getMessage());
-          Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
-                         + ") Server exception: " + ex.getMessage());
-
-          if (jobs[j].allowedServerExceptions > 0)
-          {
-            jobs[j].allowedServerExceptions--;
-            Cache.log.debug("Sleeping after a server exception.");
-            try
-            {
-              Thread.sleep(5000);
-            }
-            catch (InterruptedException ex1)
-            {
-            }
-          }
-          else
-          {
-            Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
-            jobs[j].subjobComplete = true;
-            wsInfo.setStatus(jobs[j].jobnum,
-                             WebserviceInfo.STATE_STOPPED_SERVERERROR);
-          }
-        }
-        catch (OutOfMemoryError er)
-        {
-          jobComplete = true;
-          jobs[j].subjobComplete = true;
-          jobs[j].result = null; // may contain out of date result object
-          wsInfo.setStatus(jobs[j].jobnum,
-                           WebserviceInfo.STATE_STOPPED_ERROR);
-          JOptionPane
-              .showInternalMessageDialog(
-                  Desktop.desktop,
-                  "Out of memory handling result for job !!"
-                  +
-              "\nSee help files for increasing Java Virtual Machine memory.",
-                  "Out of memory", JOptionPane.WARNING_MESSAGE);
-          Cache.log.error("Out of memory when retrieving Job " + j + " id:" +
-                          WsUrl + "/" + jobs[j].jobId, er);
-          System.gc();
-        }
-      }
-      jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
-    }
-    // Decide on overall state based on collected jobs[] states
-    if (jstate.running > 0)
-    {
-      wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
-    }
-    else if (jstate.queuing > 0)
-    {
-      wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
-    }
-    else
-    {
-      jobComplete = true;
-      if (jstate.finished > 0)
-      {
-        wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
-      }
-      else if (jstate.error > 0)
-      {
-        wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
-      }
-      else if (jstate.serror > 0)
-      {
-        wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
-      }
-    }
-    if (!jobComplete)
-    {
-      try
-      {
-        Thread.sleep(5000);
-      }
-      catch (InterruptedException e)
-      {
-        Cache.log.debug("Interrupted sleep waiting for next job poll.", e);
-      }
-      // System.out.println("I'm alive "+alTitle);
-    }
-  }
-  if (jobComplete)
-  {
-    parseResult(); // tidy up and make results available to user
-  }
-}
-abstract void StartJob(WSJob job);
-abstract void parseResult();
-}
+/*\r
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5.1)\r
+ * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle\r
+ * \r
+ * This file is part of Jalview.\r
+ * \r
+ * Jalview is free software: you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License \r
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
+ * \r
+ * Jalview is distributed in the hope that it will be useful, but \r
+ * WITHOUT ANY WARRANTY; without even the implied warranty \r
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
+ * PURPOSE.  See the GNU General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package jalview.ws;\r
+\r
+import javax.swing.*;\r
+\r
+import jalview.bin.*;\r
+import jalview.datamodel.*;\r
+import jalview.gui.*;\r
+import jalview.gui.FeatureRenderer.FeatureRendererSettings;\r
+\r
+public abstract class WSThread extends Thread\r
+{\r
+  /**\r
+   * Generic properties for Web Service Client threads.\r
+   */\r
+  /**\r
+   * view that this job was associated with\r
+   */\r
+  AlignmentI currentView = null;\r
+\r
+  /**\r
+   * feature settings from view that job was associated with\r
+   */\r
+  FeatureRendererSettings featureSettings = null;\r
+\r
+  /**\r
+   * metadata about this web service\r
+   */\r
+  WebserviceInfo wsInfo = null;\r
+\r
+  /**\r
+   * original input data for this job\r
+   */\r
+  AlignmentView input = null;\r
+\r
+  /**\r
+   * dataset sequence relationships to be propagated onto new results\r
+   */\r
+  AlignedCodonFrame[] codonframe = null;\r
+\r
+  /**\r
+   * are there jobs still running in this thread.\r
+   */\r
+  boolean jobComplete = false;\r
+\r
+  abstract class WSJob\r
+  {\r
+    /**\r
+     * Generic properties for an individual job within a Web Service Client\r
+     * thread\r
+     */\r
+    int jobnum = 0; // WebServiceInfo pane for this job\r
+\r
+    String jobId; // ws job ticket\r
+\r
+    /**\r
+     * has job been cancelled\r
+     */\r
+    boolean cancelled = false;\r
+\r
+    /**\r
+     * number of exceptions left before job dies\r
+     */\r
+    int allowedServerExceptions = 3;\r
+\r
+    /**\r
+     * has job been submitted\r
+     */\r
+    boolean submitted = false;\r
+\r
+    /**\r
+     * are all sub-jobs complete\r
+     */\r
+    boolean subjobComplete = false;\r
+\r
+    /**\r
+     * \r
+     * @return true if job has completed and valid results are available\r
+     */\r
+    abstract boolean hasResults();\r
+\r
+    /**\r
+     * \r
+     * @return boolean true if job can be submitted.\r
+     */\r
+    abstract boolean hasValidInput();\r
+\r
+    /**\r
+     * The last result object returned by the service.\r
+     */\r
+    vamsas.objects.simple.Result result;\r
+  }\r
+\r
+  class JobStateSummary\r
+  {\r
+    /**\r
+     * number of jobs running\r
+     */\r
+    int running = 0;\r
+\r
+    /**\r
+     * number of jobs queued\r
+     */\r
+    int queuing = 0;\r
+\r
+    /**\r
+     * number of jobs finished\r
+     */\r
+    int finished = 0;\r
+\r
+    /**\r
+     * number of jobs failed\r
+     */\r
+    int error = 0;\r
+\r
+    /**\r
+     * number of jobs stopped due to server error\r
+     */\r
+    int serror = 0;\r
+\r
+    /**\r
+     * number of jobs cancelled\r
+     */\r
+    int cancelled = 0;\r
+\r
+    /**\r
+     * number of jobs finished with results\r
+     */\r
+    int results = 0;\r
+\r
+    /**\r
+     * processes WSJob and updates job status counters and WebService status\r
+     * displays\r
+     * \r
+     * @param wsInfo\r
+     * @param OutputHeader\r
+     * @param j\r
+     */\r
+    void updateJobPanelState(WebserviceInfo wsInfo, String OutputHeader,\r
+            WSJob j)\r
+    {\r
+      if (j.result != null)\r
+      {\r
+        String progheader = "";\r
+        // Parse state of job[j]\r
+        if (j.result.isRunning())\r
+        {\r
+          running++;\r
+          wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);\r
+        }\r
+        else if (j.result.isQueued())\r
+        {\r
+          queuing++;\r
+          wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);\r
+        }\r
+        else if (j.result.isFinished())\r
+        {\r
+          finished++;\r
+          j.subjobComplete = true;\r
+          if (j.hasResults())\r
+          {\r
+            results++;\r
+          }\r
+          wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);\r
+        }\r
+        else if (j.result.isFailed())\r
+        {\r
+          progheader += "Job failed.\n";\r
+          j.subjobComplete = true;\r
+          wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);\r
+          error++;\r
+        }\r
+        else if (j.result.isServerError())\r
+        {\r
+          serror++;\r
+          j.subjobComplete = true;\r
+          wsInfo.setStatus(j.jobnum,\r
+                  WebserviceInfo.STATE_STOPPED_SERVERERROR);\r
+        }\r
+        else if (j.result.isBroken() || j.result.isFailed())\r
+        {\r
+          error++;\r
+          j.subjobComplete = true;\r
+          wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);\r
+        }\r
+        // and pass on any sub-job messages to the user\r
+        wsInfo.setProgressText(j.jobnum, OutputHeader);\r
+        wsInfo.appendProgressText(j.jobnum, progheader);\r
+        if (j.result.getStatus() != null)\r
+        {\r
+          wsInfo.appendProgressText(j.jobnum, j.result.getStatus());\r
+        }\r
+      }\r
+      else\r
+      {\r
+        if (j.submitted && j.subjobComplete)\r
+        {\r
+          if (j.allowedServerExceptions == 0)\r
+          {\r
+            serror++;\r
+          }\r
+          else if (j.result == null)\r
+          {\r
+            error++;\r
+          }\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  /**\r
+   * one or more jobs being managed by this thread.\r
+   */\r
+  WSJob jobs[] = null;\r
+\r
+  /**\r
+   * full name of service\r
+   */\r
+  String WebServiceName = null;\r
+\r
+  String OutputHeader;\r
+\r
+  String WsUrl = null;\r
+\r
+  /**\r
+   * query web service for status of job. on return, job.result must not be null\r
+   * - if it is then it will be assumed that the job status query timed out and\r
+   * a server exception will be logged.\r
+   * \r
+   * @param job\r
+   * @throws Exception\r
+   *           will be logged as a server exception for this job\r
+   */\r
+  abstract void pollJob(WSJob job) throws Exception;\r
+\r
+  public void run()\r
+  {\r
+    JobStateSummary jstate = null;\r
+    if (jobs == null)\r
+    {\r
+      jobComplete = true;\r
+    }\r
+    while (!jobComplete)\r
+    {\r
+      jstate = new JobStateSummary();\r
+      for (int j = 0; j < jobs.length; j++)\r
+      {\r
+\r
+        if (!jobs[j].submitted && jobs[j].hasValidInput())\r
+        {\r
+          StartJob(jobs[j]);\r
+        }\r
+\r
+        if (jobs[j].submitted && !jobs[j].subjobComplete)\r
+        {\r
+          try\r
+          {\r
+            pollJob(jobs[j]);\r
+            if (jobs[j].result == null)\r
+            {\r
+              throw (new Exception(\r
+                      "Timed out when communicating with server\nTry again later.\n"));\r
+            }\r
+            jalview.bin.Cache.log.debug("Job " + j + " Result state "\r
+                    + jobs[j].result.getState() + "(ServerError="\r
+                    + jobs[j].result.isServerError() + ")");\r
+          } catch (Exception ex)\r
+          {\r
+            // Deal with Transaction exceptions\r
+            wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName\r
+                    + " Server exception!\n" + ex.getMessage());\r
+            Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum\r
+                    + ") Server exception: " + ex.getMessage());\r
+\r
+            if (jobs[j].allowedServerExceptions > 0)\r
+            {\r
+              jobs[j].allowedServerExceptions--;\r
+              Cache.log.debug("Sleeping after a server exception.");\r
+              try\r
+              {\r
+                Thread.sleep(5000);\r
+              } catch (InterruptedException ex1)\r
+              {\r
+              }\r
+            }\r
+            else\r
+            {\r
+              Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);\r
+              jobs[j].subjobComplete = true;\r
+              wsInfo.setStatus(jobs[j].jobnum,\r
+                      WebserviceInfo.STATE_STOPPED_SERVERERROR);\r
+            }\r
+          } catch (OutOfMemoryError er)\r
+          {\r
+            jobComplete = true;\r
+            jobs[j].subjobComplete = true;\r
+            jobs[j].result = null; // may contain out of date result object\r
+            wsInfo.setStatus(jobs[j].jobnum,\r
+                    WebserviceInfo.STATE_STOPPED_ERROR);\r
+            Cache.log.error("Out of memory when retrieving Job " + j\r
+                    + " id:" + WsUrl + "/" + jobs[j].jobId, er);\r
+            new jalview.gui.OOMWarning("retrieving result for "\r
+                    + WebServiceName, er);\r
+            System.gc();\r
+          }\r
+        }\r
+        jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);\r
+      }\r
+      // Decide on overall state based on collected jobs[] states\r
+      if (jstate.running > 0)\r
+      {\r
+        wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);\r
+      }\r
+      else if (jstate.queuing > 0)\r
+      {\r
+        wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);\r
+      }\r
+      else\r
+      {\r
+        jobComplete = true;\r
+        if (jstate.finished > 0)\r
+        {\r
+          wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);\r
+        }\r
+        else if (jstate.error > 0)\r
+        {\r
+          wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);\r
+        }\r
+        else if (jstate.serror > 0)\r
+        {\r
+          wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);\r
+        }\r
+      }\r
+      if (!jobComplete)\r
+      {\r
+        try\r
+        {\r
+          Thread.sleep(5000);\r
+        } catch (InterruptedException e)\r
+        {\r
+          Cache.log\r
+                  .debug("Interrupted sleep waiting for next job poll.", e);\r
+        }\r
+        // System.out.println("I'm alive "+alTitle);\r
+      }\r
+    }\r
+    if (jobComplete && jobs != null)\r
+    {\r
+      parseResult(); // tidy up and make results available to user\r
+    }\r
+    else\r
+    {\r
+      Cache.log\r
+              .debug("WebServiceJob poll loop finished with no jobs created.");\r
+      wsInfo.setFinishedNoResults();\r
+    }\r
+  }\r
+\r
+  /**\r
+   * submit job to web service\r
+   * \r
+   * @param job\r
+   */\r
+  abstract void StartJob(WSJob job);\r
+\r
+  /**\r
+   * process the set of WSJob objects into a set of results, and tidy up.\r
+   */\r
+  abstract void parseResult();\r
+\r
+  /**\r
+   * helper function to conserve dataset references to sequence objects returned\r
+   * from web services 1. Propagates AlCodonFrame data from\r
+   * <code>codonframe</code> to <code>al</code>\r
+   * \r
+   * @param al\r
+   */\r
+  protected void propagateDatasetMappings(Alignment al)\r
+  {\r
+    if (codonframe != null)\r
+    {\r
+      SequenceI[] alignment = al.getSequencesArray();\r
+      for (int sq = 0; sq < alignment.length; sq++)\r
+      {\r
+        for (int i = 0; i < codonframe.length; i++)\r
+        {\r
+          if (codonframe[i] != null\r
+                  && codonframe[i].involvesSequence(alignment[sq]))\r
+          {\r
+            al.addCodonFrame(codonframe[i]);\r
+            codonframe[i] = null;\r
+            break;\r
+          }\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  /**\r
+   * \r
+   * @param alignFrame\r
+   *          reference for copying mappings across\r
+   * @param wsInfo\r
+   *          gui attachment point\r
+   * @param input\r
+   *          input data for the calculation\r
+   * @param webServiceName\r
+   *          name of service\r
+   * @param wsUrl\r
+   *          url of the service being invoked\r
+   */\r
+  public WSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,\r
+          AlignmentView input, String webServiceName, String wsUrl)\r
+  {\r
+    this(alignFrame, wsinfo, input, wsUrl);\r
+    WebServiceName = webServiceName;\r
+  }\r
+\r
+  char defGapChar = '-';\r
+\r
+  /**\r
+   * \r
+   * @return gap character to use for any alignment generation\r
+   */\r
+  public char getGapChar()\r
+  {\r
+    return defGapChar;\r
+  }\r
+\r
+  /**\r
+   * \r
+   * @param alframe\r
+   *          - reference for copying mappings and display styles across\r
+   * @param wsinfo2\r
+   *          - gui attachment point\r
+   * @param alview\r
+   *          - input data for the calculation\r
+   * @param wsurl2\r
+   *          - url of the service being invoked\r
+   */\r
+  public WSThread(AlignFrame alframe, WebserviceInfo wsinfo2,\r
+          AlignmentView alview, String wsurl2)\r
+  {\r
+    super();\r
+    // this.alignFrame = alframe;\r
+    currentView = alframe.getCurrentView().getAlignment();\r
+    featureSettings = alframe.getFeatureRenderer().getSettings();\r
+    defGapChar = alframe.getViewport().getGapCharacter();\r
+    this.wsInfo = wsinfo2;\r
+    this.input = alview;\r
+    WsUrl = wsurl2;\r
+    if (alframe != null)\r
+    {\r
+      AlignedCodonFrame[] cf = alframe.getViewport().getAlignment()\r
+              .getCodonFrames();\r
+      if (cf != null)\r
+      {\r
+        codonframe = new AlignedCodonFrame[cf.length];\r
+        System.arraycopy(cf, 0, codonframe, 0, cf.length);\r
+      }\r
+    }\r
+  }\r
+}\r