2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import jalview.datamodel.*;
26 import jalview.gui.FeatureRenderer.FeatureRendererSettings;
28 public abstract class WSThread
32 * Generic properties for Web Service Client threads.
35 * view that this job was associated with
37 AlignmentI currentView = null;
39 * feature settings from view that job was associated with
41 FeatureRendererSettings featureSettings = null;
43 * metadata about this web service
45 WebserviceInfo wsInfo = null;
47 * original input data for this job
49 AlignmentView input = null;
51 * dataset sequence relationships to be propagated onto new results
53 AlignedCodonFrame[] codonframe = null;
55 * are there jobs still running in this thread.
57 boolean jobComplete = false;
62 * Generic properties for an individual job within a Web Service Client thread
64 int jobnum = 0; // WebServiceInfo pane for this job
65 String jobId; // ws job ticket
67 * has job been cancelled
69 boolean cancelled = false;
71 * number of exceptions left before job dies
73 int allowedServerExceptions = 3;
75 * has job been submitted
77 boolean submitted = false;
79 * are all sub-jobs complete
81 boolean subjobComplete = false;
84 * @return true if job has completed and valid results are available
86 abstract boolean hasResults();
90 * @return boolean true if job can be submitted.
92 abstract boolean hasValidInput();
95 * The last result object returned by the service.
97 vamsas.objects.simple.Result result;
100 class JobStateSummary
103 * number of jobs running
107 * number of jobs queued
111 * number of jobs finished
115 * number of jobs failed
119 * number of jobs stopped due to server error
123 * number of jobs cancelled
127 * number of jobs finished with results
131 * processes WSJob and updates job status counters and WebService status displays
133 * @param OutputHeader
136 void updateJobPanelState(WebserviceInfo wsInfo, String OutputHeader,
139 if (j.result != null)
141 String progheader = "";
142 // Parse state of job[j]
143 if (j.result.isRunning())
146 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
148 else if (j.result.isQueued())
151 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
153 else if (j.result.isFinished())
156 j.subjobComplete = true;
161 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
163 else if (j.result.isFailed())
165 progheader += "Job failed.\n";
166 j.subjobComplete = true;
167 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
170 else if (j.result.isServerError())
173 j.subjobComplete = true;
174 wsInfo.setStatus(j.jobnum,
175 WebserviceInfo.STATE_STOPPED_SERVERERROR);
177 else if (j.result.isBroken() || j.result.isFailed())
180 j.subjobComplete = true;
181 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
183 // and pass on any sub-job messages to the user
184 wsInfo.setProgressText(j.jobnum, OutputHeader);
185 wsInfo.appendProgressText(j.jobnum, progheader);
186 if (j.result.getStatus() != null)
188 wsInfo.appendProgressText(j.jobnum, j.result.getStatus());
193 if (j.submitted && j.subjobComplete)
195 if (j.allowedServerExceptions == 0)
199 else if (j.result == null)
208 * one or more jobs being managed by this thread.
212 * full name of service
214 String WebServiceName = null;
218 * query web service for status of job.
219 * on return, job.result must not be null - if it is then it will be
220 * assumed that the job status query timed out and a server exception
223 * @throws Exception will be logged as a server exception for this job
225 abstract void pollJob(WSJob job)
230 JobStateSummary jstate = null;
237 jstate = new JobStateSummary();
238 for (int j = 0; j < jobs.length; j++)
241 if (!jobs[j].submitted && jobs[j].hasValidInput())
246 if (jobs[j].submitted && !jobs[j].subjobComplete)
251 if (jobs[j].result == null)
253 throw (new Exception(
254 "Timed out when communicating with server\nTry again later.\n"));
256 jalview.bin.Cache.log.debug("Job " + j + " Result state " +
257 jobs[j].result.getState()
259 jobs[j].result.isServerError() + ")");
263 // Deal with Transaction exceptions
264 wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName
265 + " Server exception!\n" + ex.getMessage());
266 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
267 + ") Server exception: " + ex.getMessage());
269 if (jobs[j].allowedServerExceptions > 0)
271 jobs[j].allowedServerExceptions--;
272 Cache.log.debug("Sleeping after a server exception.");
277 catch (InterruptedException ex1)
283 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
284 jobs[j].subjobComplete = true;
285 wsInfo.setStatus(jobs[j].jobnum,
286 WebserviceInfo.STATE_STOPPED_SERVERERROR);
289 catch (OutOfMemoryError er)
292 jobs[j].subjobComplete = true;
293 jobs[j].result = null; // may contain out of date result object
294 wsInfo.setStatus(jobs[j].jobnum,
295 WebserviceInfo.STATE_STOPPED_ERROR);
297 .showInternalMessageDialog(
299 "Out of memory handling result for job !!"
301 "\nSee help files for increasing Java Virtual Machine memory.",
302 "Out of memory", JOptionPane.WARNING_MESSAGE);
303 Cache.log.error("Out of memory when retrieving Job " + j + " id:" +
304 WsUrl + "/" + jobs[j].jobId, er);
308 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
310 // Decide on overall state based on collected jobs[] states
311 if (jstate.running > 0)
313 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
315 else if (jstate.queuing > 0)
317 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
322 if (jstate.finished > 0)
324 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
326 else if (jstate.error > 0)
328 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
330 else if (jstate.serror > 0)
332 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
341 catch (InterruptedException e)
343 Cache.log.debug("Interrupted sleep waiting for next job poll.", e);
345 // System.out.println("I'm alive "+alTitle);
348 if (jobComplete && jobs != null)
350 parseResult(); // tidy up and make results available to user
354 Cache.log.debug("WebServiceJob poll loop finished with no jobs created.");
355 wsInfo.setFinishedNoResults();
360 * submit job to web service
363 abstract void StartJob(WSJob job);
366 * process the set of WSJob objects into a set of results, and tidy up.
368 abstract void parseResult();
371 * helper function to conserve dataset references to sequence objects returned from web services
372 * 1. Propagates AlCodonFrame data from <code>codonframe</code> to <code>al</code>
375 protected void propagateDatasetMappings(Alignment al)
377 if (codonframe!=null)
379 SequenceI[] alignment = al.getSequencesArray();
380 for (int sq = 0; sq<alignment.length; sq++)
382 for (int i=0; i<codonframe.length; i++)
384 if (codonframe[i]!=null &&
385 codonframe[i].involvesSequence(alignment[sq]))
387 al.addCodonFrame(codonframe[i]);
388 codonframe[i] = null;
398 * @param alignFrame reference for copying mappings across
399 * @param wsInfo gui attachment point
400 * @param input input data for the calculation
401 * @param webServiceName name of service
402 * @param wsUrl url of the service being invoked
404 public WSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
405 AlignmentView input, String webServiceName,
408 this(alignFrame, wsinfo, input, wsUrl);
409 WebServiceName = webServiceName;
411 char defGapChar = '-';
414 * @return gap character to use for any alignment generation
416 public char getGapChar()
423 * @param alframe - reference for copying mappings and display styles across
424 * @param wsinfo2 - gui attachment point
425 * @param alview - input data for the calculation
426 * @param wsurl2 - url of the service being invoked
428 public WSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
429 AlignmentView alview, String wsurl2)
432 // this.alignFrame = alframe;
433 currentView = alframe.getCurrentView().getAlignment();
434 featureSettings = alframe.getFeatureRenderer().getSettings();
435 defGapChar = alframe.getViewport().getGapCharacter();
436 this.wsInfo = wsinfo2;
441 AlignedCodonFrame[] cf = alframe.getViewport().getAlignment().getCodonFrames();
444 codonframe = new AlignedCodonFrame[cf.length];
445 System.arraycopy(cf, 0, codonframe, 0, cf.length);