Formatting
[jalview.git] / src / jalview / ws / WSThread.java
index 86ee49a..6b6c483 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  */
- package jalview.ws;
+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;
+import javax.swing.*;
 
-public abstract class WSThread extends Thread
+import jalview.bin.*;
+import jalview.datamodel.*;
+import jalview.gui.*;
+
+public abstract class WSThread
+    extends Thread
 {
-    /**
-     * Generic properties for Web Service Client threads.
-     */
-    AlignFrame alignFrame=null;
+  /**
+   * Generic properties for Web Service Client threads.
+   */
+  AlignFrame alignFrame = null;
   WebserviceInfo wsInfo = null;
-  AlignmentView input=null;
+  AlignmentView input = null;
   boolean jobComplete = false;
-  abstract class WSJob {
-      /**
-       * Generic properties for an individual job within a Web Service Client thread
-       */
+  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;
@@ -49,14 +50,18 @@ public abstract class WSThread extends Thread
      * @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 {
+
+  class JobStateSummary
+  {
     int running = 0;
     int queuing = 0;
     int finished = 0;
@@ -86,7 +91,9 @@ public abstract class WSThread extends Thread
           finished++;
           j.subjobComplete = true;
           if (j.hasResults())
+          {
             results++;
+          }
           wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
         }
         else if (j.result.isFailed())
@@ -138,132 +145,140 @@ public abstract class WSThread extends Thread
   String WebServiceName = null;
   String OutputHeader;
   String WsUrl = null;
-  abstract void pollJob(WSJob job) throws Exception;
+  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++)
+    JobStateSummary jstate = null;
+    if (jobs == null)
     {
-
-      if (!jobs[j].submitted && jobs[j].hasValidInput())
+      jobComplete = true;
+    }
+    while (!jobComplete)
+    {
+      jstate = new JobStateSummary();
+      for (int j = 0; j < jobs.length; j++)
       {
-        StartJob(jobs[j]);
-      }
 
-      if (jobs[j].submitted && !jobs[j].subjobComplete)
-      {
-        try
+        if (!jobs[j].submitted && jobs[j].hasValidInput())
         {
-          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() + ")");
+          StartJob(jobs[j]);
         }
-        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)
+        if (jobs[j].submitted && !jobs[j].subjobComplete)
+        {
+          try
           {
-            jobs[j].allowedServerExceptions--;
-            Cache.log.debug("Sleeping after a server exception.");
-            try
+            pollJob(jobs[j]);
+            if (jobs[j].result == null)
             {
-              Thread.sleep(5000);
+              throw (new Exception(
+                  "Timed out when communicating with server\nTry again later.\n"));
             }
-            catch (InterruptedException ex1)
+            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);
             }
           }
-          else
+          catch (OutOfMemoryError er)
           {
-            Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
+            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_SERVERERROR);
+                             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();
           }
         }
-        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);
+        jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
       }
-      else if (jstate.error > 0)
+      // Decide on overall state based on collected jobs[] states
+      if (jstate.running > 0)
       {
-        wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
+        wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
       }
-      else if (jstate.serror > 0)
+      else if (jstate.queuing > 0)
       {
-        wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
+        wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
       }
-    }
-    if (!jobComplete)
-    {
-      try
+      else
       {
-        Thread.sleep(5000);
+        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);
+        }
       }
-      catch (InterruptedException e)
+      if (!jobComplete)
       {
-        Cache.log.debug("Interrupted sleep waiting for next job poll.", e);
+        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);
       }
-      // System.out.println("I'm alive "+alTitle);
+    }
+    if (jobComplete && jobs != null)
+    {
+      parseResult(); // tidy up and make results available to user
+    }
+    else
+    {
+      Cache.log.debug("WebServiceJob poll loop finished with no jobs created.");
     }
   }
-  if (jobComplete && jobs!=null)
-  {
-    parseResult(); // tidy up and make results available to user
-  } else {
-    Cache.log.debug("WebServiceJob poll loop finished with no jobs created.");
-  }
-}
-abstract void StartJob(WSJob job);
-abstract void parseResult();
+
+  abstract void StartJob(WSJob job);
+
+  abstract void parseResult();
 }