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
\r
32 * Generic properties for Web Service Client threads.
\r
35 * view that this job was associated with
\r
37 AlignmentI currentView = null;
\r
39 * feature settings from view that job was associated with
\r
41 FeatureRendererSettings featureSettings = null;
\r
43 * metadata about this web service
\r
45 WebserviceInfo wsInfo = null;
\r
47 * original input data for this job
\r
49 AlignmentView input = null;
\r
51 * dataset sequence relationships to be propagated onto new results
\r
53 AlignedCodonFrame[] codonframe = null;
\r
55 * are there jobs still running in this thread.
\r
57 boolean jobComplete = false;
\r
59 abstract class WSJob
\r
62 * Generic properties for an individual job within a Web Service Client thread
\r
64 int jobnum = 0; // WebServiceInfo pane for this job
\r
65 String jobId; // ws job ticket
\r
67 * has job been cancelled
\r
69 boolean cancelled = false;
\r
71 * number of exceptions left before job dies
\r
73 int allowedServerExceptions = 3;
\r
75 * has job been submitted
\r
77 boolean submitted = false;
\r
79 * are all sub-jobs complete
\r
81 boolean subjobComplete = false;
\r
84 * @return true if job has completed and valid results are available
\r
86 abstract boolean hasResults();
\r
90 * @return boolean true if job can be submitted.
\r
92 abstract boolean hasValidInput();
\r
95 * The last result object returned by the service.
\r
97 vamsas.objects.simple.Result result;
\r
100 class JobStateSummary
\r
103 * number of jobs running
\r
107 * number of jobs queued
\r
111 * number of jobs finished
\r
115 * number of jobs failed
\r
119 * number of jobs stopped due to server error
\r
123 * number of jobs cancelled
\r
127 * number of jobs finished with results
\r
131 * processes WSJob and updates job status counters and WebService status displays
\r
133 * @param OutputHeader
\r
136 void updateJobPanelState(WebserviceInfo wsInfo, String OutputHeader,
\r
139 if (j.result != null)
\r
141 String progheader = "";
\r
142 // Parse state of job[j]
\r
143 if (j.result.isRunning())
\r
146 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
\r
148 else if (j.result.isQueued())
\r
151 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
\r
153 else if (j.result.isFinished())
\r
156 j.subjobComplete = true;
\r
157 if (j.hasResults())
\r
161 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
\r
163 else if (j.result.isFailed())
\r
165 progheader += "Job failed.\n";
\r
166 j.subjobComplete = true;
\r
167 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
\r
170 else if (j.result.isServerError())
\r
173 j.subjobComplete = true;
\r
174 wsInfo.setStatus(j.jobnum,
\r
175 WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
177 else if (j.result.isBroken() || j.result.isFailed())
\r
180 j.subjobComplete = true;
\r
181 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
\r
183 // and pass on any sub-job messages to the user
\r
184 wsInfo.setProgressText(j.jobnum, OutputHeader);
\r
185 wsInfo.appendProgressText(j.jobnum, progheader);
\r
186 if (j.result.getStatus() != null)
\r
188 wsInfo.appendProgressText(j.jobnum, j.result.getStatus());
\r
193 if (j.submitted && j.subjobComplete)
\r
195 if (j.allowedServerExceptions == 0)
\r
199 else if (j.result == null)
\r
208 * one or more jobs being managed by this thread.
\r
210 WSJob jobs[] = null;
\r
212 * full name of service
\r
214 String WebServiceName = null;
\r
215 String OutputHeader;
\r
216 String WsUrl = null;
\r
218 * query web service for status of job.
\r
219 * on return, job.result must not be null - if it is then it will be
\r
220 * assumed that the job status query timed out and a server exception
\r
223 * @throws Exception will be logged as a server exception for this job
\r
225 abstract void pollJob(WSJob job)
\r
230 JobStateSummary jstate = null;
\r
233 jobComplete = true;
\r
235 while (!jobComplete)
\r
237 jstate = new JobStateSummary();
\r
238 for (int j = 0; j < jobs.length; j++)
\r
241 if (!jobs[j].submitted && jobs[j].hasValidInput())
\r
246 if (jobs[j].submitted && !jobs[j].subjobComplete)
\r
251 if (jobs[j].result == null)
\r
253 throw (new Exception(
\r
254 "Timed out when communicating with server\nTry again later.\n"));
\r
256 jalview.bin.Cache.log.debug("Job " + j + " Result state " +
\r
257 jobs[j].result.getState()
\r
258 + "(ServerError=" +
\r
259 jobs[j].result.isServerError() + ")");
\r
261 catch (Exception ex)
\r
263 // Deal with Transaction exceptions
\r
264 wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName
\r
265 + " Server exception!\n" + ex.getMessage());
\r
266 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
\r
267 + ") Server exception: " + ex.getMessage());
\r
269 if (jobs[j].allowedServerExceptions > 0)
\r
271 jobs[j].allowedServerExceptions--;
\r
272 Cache.log.debug("Sleeping after a server exception.");
\r
275 Thread.sleep(5000);
\r
277 catch (InterruptedException ex1)
\r
283 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
\r
284 jobs[j].subjobComplete = true;
\r
285 wsInfo.setStatus(jobs[j].jobnum,
\r
286 WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
289 catch (OutOfMemoryError er)
\r
291 jobComplete = true;
\r
292 jobs[j].subjobComplete = true;
\r
293 jobs[j].result = null; // may contain out of date result object
\r
294 wsInfo.setStatus(jobs[j].jobnum,
\r
295 WebserviceInfo.STATE_STOPPED_ERROR);
\r
296 Cache.log.error("Out of memory when retrieving Job " + j + " id:" +
\r
297 WsUrl + "/" + jobs[j].jobId, er);
\r
298 new jalview.gui.OOMWarning("retrieving result for "+WebServiceName,er);
\r
302 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
\r
304 // Decide on overall state based on collected jobs[] states
\r
305 if (jstate.running > 0)
\r
307 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
\r
309 else if (jstate.queuing > 0)
\r
311 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
\r
315 jobComplete = true;
\r
316 if (jstate.finished > 0)
\r
318 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
320 else if (jstate.error > 0)
\r
322 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
324 else if (jstate.serror > 0)
\r
326 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
333 Thread.sleep(5000);
\r
335 catch (InterruptedException e)
\r
337 Cache.log.debug("Interrupted sleep waiting for next job poll.", e);
\r
339 // System.out.println("I'm alive "+alTitle);
\r
342 if (jobComplete && jobs != null)
\r
344 parseResult(); // tidy up and make results available to user
\r
348 Cache.log.debug("WebServiceJob poll loop finished with no jobs created.");
\r
349 wsInfo.setFinishedNoResults();
\r
354 * submit job to web service
\r
357 abstract void StartJob(WSJob job);
\r
360 * process the set of WSJob objects into a set of results, and tidy up.
\r
362 abstract void parseResult();
\r
365 * helper function to conserve dataset references to sequence objects returned from web services
\r
366 * 1. Propagates AlCodonFrame data from <code>codonframe</code> to <code>al</code>
\r
369 protected void propagateDatasetMappings(Alignment al)
\r
371 if (codonframe!=null)
\r
373 SequenceI[] alignment = al.getSequencesArray();
\r
374 for (int sq = 0; sq<alignment.length; sq++)
\r
376 for (int i=0; i<codonframe.length; i++)
\r
378 if (codonframe[i]!=null &&
\r
379 codonframe[i].involvesSequence(alignment[sq]))
\r
381 al.addCodonFrame(codonframe[i]);
\r
382 codonframe[i] = null;
\r
392 * @param alignFrame reference for copying mappings across
\r
393 * @param wsInfo gui attachment point
\r
394 * @param input input data for the calculation
\r
395 * @param webServiceName name of service
\r
396 * @param wsUrl url of the service being invoked
\r
398 public WSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
\r
399 AlignmentView input, String webServiceName,
\r
402 this(alignFrame, wsinfo, input, wsUrl);
\r
403 WebServiceName = webServiceName;
\r
405 char defGapChar = '-';
\r
408 * @return gap character to use for any alignment generation
\r
410 public char getGapChar()
\r
417 * @param alframe - reference for copying mappings and display styles across
\r
418 * @param wsinfo2 - gui attachment point
\r
419 * @param alview - input data for the calculation
\r
420 * @param wsurl2 - url of the service being invoked
\r
422 public WSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
\r
423 AlignmentView alview, String wsurl2)
\r
426 // this.alignFrame = alframe;
\r
427 currentView = alframe.getCurrentView().getAlignment();
\r
428 featureSettings = alframe.getFeatureRenderer().getSettings();
\r
429 defGapChar = alframe.getViewport().getGapCharacter();
\r
430 this.wsInfo = wsinfo2;
\r
431 this.input = alview;
\r
435 AlignedCodonFrame[] cf = alframe.getViewport().getAlignment().getCodonFrames();
\r
438 codonframe = new AlignedCodonFrame[cf.length];
\r
439 System.arraycopy(cf, 0, codonframe, 0, cf.length);
\r