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
297 .showInternalMessageDialog(
\r
299 "Out of memory handling result for job !!"
\r
301 "\nSee help files for increasing Java Virtual Machine memory.",
\r
302 "Out of memory", JOptionPane.WARNING_MESSAGE);
\r
303 Cache.log.error("Out of memory when retrieving Job " + j + " id:" +
\r
304 WsUrl + "/" + jobs[j].jobId, er);
\r
308 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
\r
310 // Decide on overall state based on collected jobs[] states
\r
311 if (jstate.running > 0)
\r
313 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
\r
315 else if (jstate.queuing > 0)
\r
317 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
\r
321 jobComplete = true;
\r
322 if (jstate.finished > 0)
\r
324 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
\r
326 else if (jstate.error > 0)
\r
328 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
\r
330 else if (jstate.serror > 0)
\r
332 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
\r
339 Thread.sleep(5000);
\r
341 catch (InterruptedException e)
\r
343 Cache.log.debug("Interrupted sleep waiting for next job poll.", e);
\r
345 // System.out.println("I'm alive "+alTitle);
\r
348 if (jobComplete && jobs != null)
\r
350 parseResult(); // tidy up and make results available to user
\r
354 Cache.log.debug("WebServiceJob poll loop finished with no jobs created.");
\r
355 wsInfo.setFinishedNoResults();
\r
360 * submit job to web service
\r
363 abstract void StartJob(WSJob job);
\r
366 * process the set of WSJob objects into a set of results, and tidy up.
\r
368 abstract void parseResult();
\r
371 * helper function to conserve dataset references to sequence objects returned from web services
\r
372 * 1. Propagates AlCodonFrame data from <code>codonframe</code> to <code>al</code>
\r
375 protected void propagateDatasetMappings(Alignment al)
\r
377 if (codonframe!=null)
\r
379 SequenceI[] alignment = al.getSequencesArray();
\r
380 for (int sq = 0; sq<alignment.length; sq++)
\r
382 for (int i=0; i<codonframe.length; i++)
\r
384 if (codonframe[i]!=null &&
\r
385 codonframe[i].involvesSequence(alignment[sq]))
\r
387 al.addCodonFrame(codonframe[i]);
\r
388 codonframe[i] = null;
\r
398 * @param alignFrame reference for copying mappings across
\r
399 * @param wsInfo gui attachment point
\r
400 * @param input input data for the calculation
\r
401 * @param webServiceName name of service
\r
402 * @param wsUrl url of the service being invoked
\r
404 public WSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
\r
405 AlignmentView input, String webServiceName,
\r
408 this(alignFrame, wsinfo, input, wsUrl);
\r
409 WebServiceName = webServiceName;
\r
411 char defGapChar = '-';
\r
414 * @return gap character to use for any alignment generation
\r
416 public char getGapChar()
\r
423 * @param alframe - reference for copying mappings and display styles across
\r
424 * @param wsinfo2 - gui attachment point
\r
425 * @param alview - input data for the calculation
\r
426 * @param wsurl2 - url of the service being invoked
\r
428 public WSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
\r
429 AlignmentView alview, String wsurl2)
\r
432 // this.alignFrame = alframe;
\r
433 currentView = alframe.getCurrentView().getAlignment();
\r
434 featureSettings = alframe.getFeatureRenderer().getSettings();
\r
435 defGapChar = alframe.getViewport().getGapCharacter();
\r
436 this.wsInfo = wsinfo2;
\r
437 this.input = alview;
\r
441 AlignedCodonFrame[] cf = alframe.getViewport().getAlignment().getCodonFrames();
\r
444 codonframe = new AlignedCodonFrame[cf.length];
\r
445 System.arraycopy(cf, 0, codonframe, 0, cf.length);
\r