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.*;
27 public abstract class WSThread
31 * Generic properties for Web Service Client threads.
33 AlignFrame alignFrame = null;
34 WebserviceInfo wsInfo = null;
35 AlignmentView input = null;
36 AlignedCodonFrame[] codonframe = null;
37 boolean jobComplete = false;
42 * Generic properties for an individual job within a Web Service Client thread
44 int jobnum = 0; // WebServiceInfo pane for this job
45 String jobId; // ws job ticket
46 boolean cancelled = false;
47 int allowedServerExceptions = 3; // job dies if too many exceptions.
48 boolean submitted = false;
49 boolean subjobComplete = false;
52 * @return true if job has completed and valid results are available
54 abstract boolean hasResults();
58 * @return boolean true if job can be submitted.
60 abstract boolean hasValidInput();
62 vamsas.objects.simple.Result result;
74 void updateJobPanelState(WebserviceInfo wsInfo, String OutputHeader,
79 String progheader = "";
80 // Parse state of job[j]
81 if (j.result.isRunning())
84 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
86 else if (j.result.isQueued())
89 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
91 else if (j.result.isFinished())
94 j.subjobComplete = true;
99 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
101 else if (j.result.isFailed())
103 progheader += "Job failed.\n";
104 j.subjobComplete = true;
105 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
108 else if (j.result.isServerError())
111 j.subjobComplete = true;
112 wsInfo.setStatus(j.jobnum,
113 WebserviceInfo.STATE_STOPPED_SERVERERROR);
115 else if (j.result.isBroken() || j.result.isFailed())
118 j.subjobComplete = true;
119 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
121 // and pass on any sub-job messages to the user
122 wsInfo.setProgressText(j.jobnum, OutputHeader);
123 wsInfo.appendProgressText(j.jobnum, progheader);
124 if (j.result.getStatus() != null)
126 wsInfo.appendProgressText(j.jobnum, j.result.getStatus());
131 if (j.submitted && j.subjobComplete)
133 if (j.allowedServerExceptions == 0)
137 else if (j.result == null)
147 String WebServiceName = null;
150 abstract void pollJob(WSJob job)
155 JobStateSummary jstate = null;
162 jstate = new JobStateSummary();
163 for (int j = 0; j < jobs.length; j++)
166 if (!jobs[j].submitted && jobs[j].hasValidInput())
171 if (jobs[j].submitted && !jobs[j].subjobComplete)
176 if (jobs[j].result == null)
178 throw (new Exception(
179 "Timed out when communicating with server\nTry again later.\n"));
181 jalview.bin.Cache.log.debug("Job " + j + " Result state " +
182 jobs[j].result.getState()
184 jobs[j].result.isServerError() + ")");
188 // Deal with Transaction exceptions
189 wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName
190 + " Server exception!\n" + ex.getMessage());
191 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
192 + ") Server exception: " + ex.getMessage());
194 if (jobs[j].allowedServerExceptions > 0)
196 jobs[j].allowedServerExceptions--;
197 Cache.log.debug("Sleeping after a server exception.");
202 catch (InterruptedException ex1)
208 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
209 jobs[j].subjobComplete = true;
210 wsInfo.setStatus(jobs[j].jobnum,
211 WebserviceInfo.STATE_STOPPED_SERVERERROR);
214 catch (OutOfMemoryError er)
217 jobs[j].subjobComplete = true;
218 jobs[j].result = null; // may contain out of date result object
219 wsInfo.setStatus(jobs[j].jobnum,
220 WebserviceInfo.STATE_STOPPED_ERROR);
222 .showInternalMessageDialog(
224 "Out of memory handling result for job !!"
226 "\nSee help files for increasing Java Virtual Machine memory.",
227 "Out of memory", JOptionPane.WARNING_MESSAGE);
228 Cache.log.error("Out of memory when retrieving Job " + j + " id:" +
229 WsUrl + "/" + jobs[j].jobId, er);
233 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
235 // Decide on overall state based on collected jobs[] states
236 if (jstate.running > 0)
238 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
240 else if (jstate.queuing > 0)
242 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
247 if (jstate.finished > 0)
249 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
251 else if (jstate.error > 0)
253 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
255 else if (jstate.serror > 0)
257 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
266 catch (InterruptedException e)
268 Cache.log.debug("Interrupted sleep waiting for next job poll.", e);
270 // System.out.println("I'm alive "+alTitle);
273 if (jobComplete && jobs != null)
275 parseResult(); // tidy up and make results available to user
279 Cache.log.debug("WebServiceJob poll loop finished with no jobs created.");
283 abstract void StartJob(WSJob job);
285 abstract void parseResult();
287 protected void propagateDatasetMappings(Alignment al)
289 if (codonframe!=null)
291 SequenceI[] alignment = al.getSequencesArray();
292 for (int sq = 0; sq<alignment.length; sq++)
294 for (int i=0; i<codonframe.length; i++)
296 if (codonframe[i]!=null &&
297 codonframe[i].involvesSequence(alignment[sq]))
299 al.addCodonFrame(codonframe[i]);
300 codonframe[i] = null;
310 * @param alignFrame reference for copying mappings across
311 * @param wsInfo gui attachment point
312 * @param input input data for the calculation
313 * @param webServiceName name of service
314 * @param wsUrl url of the service being invoked
316 public WSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
317 AlignmentView input, String webServiceName,
320 this(alignFrame, wsinfo, input, wsUrl);
321 WebServiceName = webServiceName;
326 * @param alframe - reference for copying mappings across
327 * @param wsinfo2 - gui attachment point
328 * @param alview - input data for the calculation
329 * @param wsurl2 - url of the service being invoked
331 public WSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
332 AlignmentView alview, String wsurl2)
335 this.alignFrame = alframe;
336 this.wsInfo = wsinfo2;
339 if (alignFrame!=null)
341 AlignedCodonFrame[] cf = alignFrame.getViewport().getAlignment().getCodonFrames();
342 codonframe = new AlignedCodonFrame[cf.length];
343 System.arraycopy(cf, 0, codonframe, 0, cf.length);