2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
\r
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
\r
11 * Jalview is distributed in the hope that it will be useful, but
\r
12 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
14 * PURPOSE. See the GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
20 import javax.swing.*;
\r
22 import jalview.bin.*;
\r
23 import jalview.datamodel.*;
\r
24 import jalview.gui.*;
\r
25 import jalview.gui.FeatureRenderer.FeatureRendererSettings;
\r
27 public abstract class WSThread extends Thread
\r
30 * Generic properties for Web Service Client threads.
\r
33 * view that this job was associated with
\r
35 AlignmentI currentView = null;
\r
38 * feature settings from view that job was associated with
\r
40 FeatureRendererSettings featureSettings = null;
\r
43 * metadata about this web service
\r
45 WebserviceInfo wsInfo = null;
\r
48 * original input data for this job
\r
50 AlignmentView input = null;
\r
53 * dataset sequence relationships to be propagated onto new results
\r
55 AlignedCodonFrame[] codonframe = null;
\r
58 * are there jobs still running in this thread.
\r
60 boolean jobComplete = false;
\r
62 abstract class WSJob
\r
65 * Generic properties for an individual job within a Web Service Client
\r
68 int jobnum = 0; // WebServiceInfo pane for this job
\r
70 String jobId; // ws job ticket
\r
73 * has job been cancelled
\r
75 boolean cancelled = false;
\r
78 * number of exceptions left before job dies
\r
80 int allowedServerExceptions = 3;
\r
83 * has job been submitted
\r
85 boolean submitted = false;
\r
88 * are all sub-jobs complete
\r
90 boolean subjobComplete = false;
\r
94 * @return true if job has completed and valid results are available
\r
96 abstract boolean hasResults();
\r
100 * @return boolean true if job can be submitted.
\r
102 abstract boolean hasValidInput();
\r
105 * The last result object returned by the service.
\r
107 vamsas.objects.simple.Result result;
\r
110 class JobStateSummary
\r
113 * number of jobs running
\r
118 * number of jobs queued
\r
123 * number of jobs finished
\r
128 * number of jobs failed
\r
133 * number of jobs stopped due to server error
\r
138 * number of jobs cancelled
\r
143 * number of jobs finished with results
\r
148 * processes WSJob and updates job status counters and WebService status
\r
152 * @param OutputHeader
\r
155 void updateJobPanelState(WebserviceInfo wsInfo, String OutputHeader,
\r
158 if (j.result != null)
\r
160 String progheader = "";
\r
161 // Parse state of job[j]
\r
162 if (j.result.isRunning())
\r
165 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
\r
167 else if (j.result.isQueued())
\r
170 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
\r
172 else if (j.result.isFinished())
\r
175 j.subjobComplete = true;
\r
176 if (j.hasResults())
\r
180 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
\r
182 else if (j.result.isFailed())
\r
184 progheader += "Job failed.\n";
\r
185 j.subjobComplete = true;
\r
186 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
\r
189 else if (j.result.isServerError())
\r
192 j.subjobComplete = true;
\r
193 wsInfo.setStatus(j.jobnum,
\r
194 WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
196 else if (j.result.isBroken() || j.result.isFailed())
\r
199 j.subjobComplete = true;
\r
200 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
\r
202 // and pass on any sub-job messages to the user
\r
203 wsInfo.setProgressText(j.jobnum, OutputHeader);
\r
204 wsInfo.appendProgressText(j.jobnum, progheader);
\r
205 if (j.result.getStatus() != null)
\r
207 wsInfo.appendProgressText(j.jobnum, j.result.getStatus());
\r
212 if (j.submitted && j.subjobComplete)
\r
214 if (j.allowedServerExceptions == 0)
\r
218 else if (j.result == null)
\r
228 * one or more jobs being managed by this thread.
\r
230 WSJob jobs[] = null;
\r
233 * full name of service
\r
235 String WebServiceName = null;
\r
237 String OutputHeader;
\r
239 String WsUrl = null;
\r
242 * query web service for status of job. on return, job.result must not be null
\r
243 * - if it is then it will be assumed that the job status query timed out and
\r
244 * a server exception will be logged.
\r
247 * @throws Exception
\r
248 * will be logged as a server exception for this job
\r
250 abstract void pollJob(WSJob job) throws Exception;
\r
254 JobStateSummary jstate = null;
\r
257 jobComplete = true;
\r
259 while (!jobComplete)
\r
261 jstate = new JobStateSummary();
\r
262 for (int j = 0; j < jobs.length; j++)
\r
265 if (!jobs[j].submitted && jobs[j].hasValidInput())
\r
270 if (jobs[j].submitted && !jobs[j].subjobComplete)
\r
275 if (jobs[j].result == null)
\r
277 throw (new Exception(
\r
278 "Timed out when communicating with server\nTry again later.\n"));
\r
280 jalview.bin.Cache.log.debug("Job " + j + " Result state "
\r
281 + jobs[j].result.getState() + "(ServerError="
\r
282 + jobs[j].result.isServerError() + ")");
\r
283 } catch (Exception ex)
\r
285 // Deal with Transaction exceptions
\r
286 wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName
\r
287 + " Server exception!\n" + ex.getMessage());
\r
288 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
\r
289 + ") Server exception: " + ex.getMessage());
\r
291 if (jobs[j].allowedServerExceptions > 0)
\r
293 jobs[j].allowedServerExceptions--;
\r
294 Cache.log.debug("Sleeping after a server exception.");
\r
297 Thread.sleep(5000);
\r
298 } catch (InterruptedException ex1)
\r
304 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
\r
305 jobs[j].subjobComplete = true;
\r
306 wsInfo.setStatus(jobs[j].jobnum,
\r
307 WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
309 } catch (OutOfMemoryError er)
\r
311 jobComplete = true;
\r
312 jobs[j].subjobComplete = true;
\r
313 jobs[j].result = null; // may contain out of date result object
\r
314 wsInfo.setStatus(jobs[j].jobnum,
\r
315 WebserviceInfo.STATE_STOPPED_ERROR);
\r
316 Cache.log.error("Out of memory when retrieving Job " + j
\r
317 + " id:" + WsUrl + "/" + jobs[j].jobId, er);
\r
318 new jalview.gui.OOMWarning("retrieving result for "
\r
319 + WebServiceName, er);
\r
323 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
\r
325 // Decide on overall state based on collected jobs[] states
\r
326 if (jstate.running > 0)
\r
328 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
\r
330 else if (jstate.queuing > 0)
\r
332 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
\r
336 jobComplete = true;
\r
337 if (jstate.finished > 0)
\r
339 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
341 else if (jstate.error > 0)
\r
343 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
345 else if (jstate.serror > 0)
\r
347 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
354 Thread.sleep(5000);
\r
355 } catch (InterruptedException e)
\r
358 .debug("Interrupted sleep waiting for next job poll.", e);
\r
360 // System.out.println("I'm alive "+alTitle);
\r
363 if (jobComplete && jobs != null)
\r
365 parseResult(); // tidy up and make results available to user
\r
370 .debug("WebServiceJob poll loop finished with no jobs created.");
\r
371 wsInfo.setFinishedNoResults();
\r
376 * submit job to web service
\r
380 abstract void StartJob(WSJob job);
\r
383 * process the set of WSJob objects into a set of results, and tidy up.
\r
385 abstract void parseResult();
\r
388 * helper function to conserve dataset references to sequence objects returned
\r
389 * from web services 1. Propagates AlCodonFrame data from
\r
390 * <code>codonframe</code> to <code>al</code>
\r
394 protected void propagateDatasetMappings(Alignment al)
\r
396 if (codonframe != null)
\r
398 SequenceI[] alignment = al.getSequencesArray();
\r
399 for (int sq = 0; sq < alignment.length; sq++)
\r
401 for (int i = 0; i < codonframe.length; i++)
\r
403 if (codonframe[i] != null
\r
404 && codonframe[i].involvesSequence(alignment[sq]))
\r
406 al.addCodonFrame(codonframe[i]);
\r
407 codonframe[i] = null;
\r
417 * @param alignFrame
\r
418 * reference for copying mappings across
\r
420 * gui attachment point
\r
422 * input data for the calculation
\r
423 * @param webServiceName
\r
426 * url of the service being invoked
\r
428 public WSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
\r
429 AlignmentView input, String webServiceName, String wsUrl)
\r
431 this(alignFrame, wsinfo, input, wsUrl);
\r
432 WebServiceName = webServiceName;
\r
435 char defGapChar = '-';
\r
439 * @return gap character to use for any alignment generation
\r
441 public char getGapChar()
\r
449 * - reference for copying mappings and display styles across
\r
451 * - gui attachment point
\r
453 * - input data for the calculation
\r
455 * - url of the service being invoked
\r
457 public WSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
\r
458 AlignmentView alview, String wsurl2)
\r
461 // this.alignFrame = alframe;
\r
462 currentView = alframe.getCurrentView().getAlignment();
\r
463 featureSettings = alframe.getFeatureRenderer().getSettings();
\r
464 defGapChar = alframe.getViewport().getGapCharacter();
\r
465 this.wsInfo = wsinfo2;
\r
466 this.input = alview;
\r
468 if (alframe != null)
\r
470 AlignedCodonFrame[] cf = alframe.getViewport().getAlignment()
\r
474 codonframe = new AlignedCodonFrame[cf.length];
\r
475 System.arraycopy(cf, 0, codonframe, 0, cf.length);
\r