2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
\r
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
21 import javax.swing.*;
\r
23 import jalview.bin.*;
\r
24 import jalview.datamodel.*;
\r
25 import jalview.gui.*;
\r
26 import jalview.gui.FeatureRenderer.FeatureRendererSettings;
\r
28 public abstract class WSThread extends Thread
\r
31 * Generic properties for Web Service Client threads.
\r
34 * view that this job was associated with
\r
36 AlignmentI currentView = null;
\r
39 * feature settings from view that job was associated with
\r
41 FeatureRendererSettings featureSettings = null;
\r
44 * metadata about this web service
\r
46 WebserviceInfo wsInfo = null;
\r
49 * original input data for this job
\r
51 AlignmentView input = null;
\r
54 * dataset sequence relationships to be propagated onto new results
\r
56 AlignedCodonFrame[] codonframe = null;
\r
59 * are there jobs still running in this thread.
\r
61 boolean jobComplete = false;
\r
63 abstract class WSJob
\r
66 * Generic properties for an individual job within a Web Service Client
\r
69 int jobnum = 0; // WebServiceInfo pane for this job
\r
71 String jobId; // ws job ticket
\r
74 * has job been cancelled
\r
76 boolean cancelled = false;
\r
79 * number of exceptions left before job dies
\r
81 int allowedServerExceptions = 3;
\r
84 * has job been submitted
\r
86 boolean submitted = false;
\r
89 * are all sub-jobs complete
\r
91 boolean subjobComplete = false;
\r
95 * @return true if job has completed and valid results are available
\r
97 abstract boolean hasResults();
\r
101 * @return boolean true if job can be submitted.
\r
103 abstract boolean hasValidInput();
\r
106 * The last result object returned by the service.
\r
108 vamsas.objects.simple.Result result;
\r
111 class JobStateSummary
\r
114 * number of jobs running
\r
119 * number of jobs queued
\r
124 * number of jobs finished
\r
129 * number of jobs failed
\r
134 * number of jobs stopped due to server error
\r
139 * number of jobs cancelled
\r
144 * number of jobs finished with results
\r
149 * processes WSJob and updates job status counters and WebService status
\r
153 * @param OutputHeader
\r
156 void updateJobPanelState(WebserviceInfo wsInfo, String OutputHeader,
\r
159 if (j.result != null)
\r
161 String progheader = "";
\r
162 // Parse state of job[j]
\r
163 if (j.result.isRunning())
\r
166 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
\r
168 else if (j.result.isQueued())
\r
171 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
\r
173 else if (j.result.isFinished())
\r
176 j.subjobComplete = true;
\r
177 if (j.hasResults())
\r
181 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
\r
183 else if (j.result.isFailed())
\r
185 progheader += "Job failed.\n";
\r
186 j.subjobComplete = true;
\r
187 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
\r
190 else if (j.result.isServerError())
\r
193 j.subjobComplete = true;
\r
194 wsInfo.setStatus(j.jobnum,
\r
195 WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
197 else if (j.result.isBroken() || j.result.isFailed())
\r
200 j.subjobComplete = true;
\r
201 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
\r
203 // and pass on any sub-job messages to the user
\r
204 wsInfo.setProgressText(j.jobnum, OutputHeader);
\r
205 wsInfo.appendProgressText(j.jobnum, progheader);
\r
206 if (j.result.getStatus() != null)
\r
208 wsInfo.appendProgressText(j.jobnum, j.result.getStatus());
\r
213 if (j.submitted && j.subjobComplete)
\r
215 if (j.allowedServerExceptions == 0)
\r
219 else if (j.result == null)
\r
229 * one or more jobs being managed by this thread.
\r
231 WSJob jobs[] = null;
\r
234 * full name of service
\r
236 String WebServiceName = null;
\r
238 String OutputHeader;
\r
240 String WsUrl = null;
\r
243 * query web service for status of job. on return, job.result must not be null -
\r
244 * if it is then it will be assumed that the job status query timed out and a
\r
245 * server exception will be logged.
\r
248 * @throws Exception
\r
249 * will be logged as a server exception for this job
\r
251 abstract void pollJob(WSJob job) throws Exception;
\r
255 JobStateSummary jstate = null;
\r
258 jobComplete = true;
\r
260 while (!jobComplete)
\r
262 jstate = new JobStateSummary();
\r
263 for (int j = 0; j < jobs.length; j++)
\r
266 if (!jobs[j].submitted && jobs[j].hasValidInput())
\r
271 if (jobs[j].submitted && !jobs[j].subjobComplete)
\r
276 if (jobs[j].result == null)
\r
278 throw (new Exception(
\r
279 "Timed out when communicating with server\nTry again later.\n"));
\r
281 jalview.bin.Cache.log.debug("Job " + j + " Result state "
\r
282 + jobs[j].result.getState() + "(ServerError="
\r
283 + jobs[j].result.isServerError() + ")");
\r
284 } catch (Exception ex)
\r
286 // Deal with Transaction exceptions
\r
287 wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName
\r
288 + " Server exception!\n" + ex.getMessage());
\r
289 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
\r
290 + ") Server exception: " + ex.getMessage());
\r
292 if (jobs[j].allowedServerExceptions > 0)
\r
294 jobs[j].allowedServerExceptions--;
\r
295 Cache.log.debug("Sleeping after a server exception.");
\r
298 Thread.sleep(5000);
\r
299 } catch (InterruptedException ex1)
\r
305 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
\r
306 jobs[j].subjobComplete = true;
\r
307 wsInfo.setStatus(jobs[j].jobnum,
\r
308 WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
310 } catch (OutOfMemoryError er)
\r
312 jobComplete = true;
\r
313 jobs[j].subjobComplete = true;
\r
314 jobs[j].result = null; // may contain out of date result object
\r
315 wsInfo.setStatus(jobs[j].jobnum,
\r
316 WebserviceInfo.STATE_STOPPED_ERROR);
\r
317 Cache.log.error("Out of memory when retrieving Job " + j
\r
318 + " id:" + WsUrl + "/" + jobs[j].jobId, er);
\r
319 new jalview.gui.OOMWarning("retrieving result for "
\r
320 + WebServiceName, er);
\r
324 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
\r
326 // Decide on overall state based on collected jobs[] states
\r
327 if (jstate.running > 0)
\r
329 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
\r
331 else if (jstate.queuing > 0)
\r
333 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
\r
337 jobComplete = true;
\r
338 if (jstate.finished > 0)
\r
340 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
342 else if (jstate.error > 0)
\r
344 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
346 else if (jstate.serror > 0)
\r
348 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
355 Thread.sleep(5000);
\r
356 } catch (InterruptedException e)
\r
359 .debug("Interrupted sleep waiting for next job poll.", e);
\r
361 // System.out.println("I'm alive "+alTitle);
\r
364 if (jobComplete && jobs != null)
\r
366 parseResult(); // tidy up and make results available to user
\r
371 .debug("WebServiceJob poll loop finished with no jobs created.");
\r
372 wsInfo.setFinishedNoResults();
\r
377 * submit job to web service
\r
381 abstract void StartJob(WSJob job);
\r
384 * process the set of WSJob objects into a set of results, and tidy up.
\r
386 abstract void parseResult();
\r
389 * helper function to conserve dataset references to sequence objects returned
\r
390 * from web services 1. Propagates AlCodonFrame data from
\r
391 * <code>codonframe</code> to <code>al</code>
\r
395 protected void propagateDatasetMappings(Alignment al)
\r
397 if (codonframe != null)
\r
399 SequenceI[] alignment = al.getSequencesArray();
\r
400 for (int sq = 0; sq < alignment.length; sq++)
\r
402 for (int i = 0; i < codonframe.length; i++)
\r
404 if (codonframe[i] != null
\r
405 && codonframe[i].involvesSequence(alignment[sq]))
\r
407 al.addCodonFrame(codonframe[i]);
\r
408 codonframe[i] = null;
\r
418 * @param alignFrame
\r
419 * reference for copying mappings across
\r
421 * gui attachment point
\r
423 * input data for the calculation
\r
424 * @param webServiceName
\r
427 * url of the service being invoked
\r
429 public WSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
\r
430 AlignmentView input, String webServiceName, String wsUrl)
\r
432 this(alignFrame, wsinfo, input, wsUrl);
\r
433 WebServiceName = webServiceName;
\r
436 char defGapChar = '-';
\r
440 * @return gap character to use for any alignment generation
\r
442 public char getGapChar()
\r
450 * reference for copying mappings and display styles across
\r
452 * gui attachment point
\r
454 * input data for the calculation
\r
456 * url of the service being invoked
\r
458 public WSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
\r
459 AlignmentView alview, String wsurl2)
\r
462 // this.alignFrame = alframe;
\r
463 currentView = alframe.getCurrentView().getAlignment();
\r
464 featureSettings = alframe.getFeatureRenderer().getSettings();
\r
465 defGapChar = alframe.getViewport().getGapCharacter();
\r
466 this.wsInfo = wsinfo2;
\r
467 this.input = alview;
\r
469 if (alframe != null)
\r
471 AlignedCodonFrame[] cf = alframe.getViewport().getAlignment()
\r
475 codonframe = new AlignedCodonFrame[cf.length];
\r
476 System.arraycopy(cf, 0, codonframe, 0, cf.length);
\r