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.*;
26 import jalview.gui.FeatureRenderer.FeatureRendererSettings;
28 public abstract class WSThread
32 * Generic properties for Web Service Client threads.
34 AlignmentI currentView = null;
35 FeatureRendererSettings featureSettings = null;
36 WebserviceInfo wsInfo = null;
37 AlignmentView input = null;
38 AlignedCodonFrame[] codonframe = null;
39 boolean jobComplete = false;
44 * Generic properties for an individual job within a Web Service Client thread
46 int jobnum = 0; // WebServiceInfo pane for this job
47 String jobId; // ws job ticket
48 boolean cancelled = false;
49 int allowedServerExceptions = 3; // job dies if too many exceptions.
50 boolean submitted = false;
51 boolean subjobComplete = false;
54 * @return true if job has completed and valid results are available
56 abstract boolean hasResults();
60 * @return boolean true if job can be submitted.
62 abstract boolean hasValidInput();
64 vamsas.objects.simple.Result result;
76 void updateJobPanelState(WebserviceInfo wsInfo, String OutputHeader,
81 String progheader = "";
82 // Parse state of job[j]
83 if (j.result.isRunning())
86 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_RUNNING);
88 else if (j.result.isQueued())
91 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_QUEUING);
93 else if (j.result.isFinished())
96 j.subjobComplete = true;
101 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_OK);
103 else if (j.result.isFailed())
105 progheader += "Job failed.\n";
106 j.subjobComplete = true;
107 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
110 else if (j.result.isServerError())
113 j.subjobComplete = true;
114 wsInfo.setStatus(j.jobnum,
115 WebserviceInfo.STATE_STOPPED_SERVERERROR);
117 else if (j.result.isBroken() || j.result.isFailed())
120 j.subjobComplete = true;
121 wsInfo.setStatus(j.jobnum, WebserviceInfo.STATE_STOPPED_ERROR);
123 // and pass on any sub-job messages to the user
124 wsInfo.setProgressText(j.jobnum, OutputHeader);
125 wsInfo.appendProgressText(j.jobnum, progheader);
126 if (j.result.getStatus() != null)
128 wsInfo.appendProgressText(j.jobnum, j.result.getStatus());
133 if (j.submitted && j.subjobComplete)
135 if (j.allowedServerExceptions == 0)
139 else if (j.result == null)
149 String WebServiceName = null;
152 abstract void pollJob(WSJob job)
157 JobStateSummary jstate = null;
164 jstate = new JobStateSummary();
165 for (int j = 0; j < jobs.length; j++)
168 if (!jobs[j].submitted && jobs[j].hasValidInput())
173 if (jobs[j].submitted && !jobs[j].subjobComplete)
178 if (jobs[j].result == null)
180 throw (new Exception(
181 "Timed out when communicating with server\nTry again later.\n"));
183 jalview.bin.Cache.log.debug("Job " + j + " Result state " +
184 jobs[j].result.getState()
186 jobs[j].result.isServerError() + ")");
190 // Deal with Transaction exceptions
191 wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName
192 + " Server exception!\n" + ex.getMessage());
193 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
194 + ") Server exception: " + ex.getMessage());
196 if (jobs[j].allowedServerExceptions > 0)
198 jobs[j].allowedServerExceptions--;
199 Cache.log.debug("Sleeping after a server exception.");
204 catch (InterruptedException ex1)
210 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
211 jobs[j].subjobComplete = true;
212 wsInfo.setStatus(jobs[j].jobnum,
213 WebserviceInfo.STATE_STOPPED_SERVERERROR);
216 catch (OutOfMemoryError er)
219 jobs[j].subjobComplete = true;
220 jobs[j].result = null; // may contain out of date result object
221 wsInfo.setStatus(jobs[j].jobnum,
222 WebserviceInfo.STATE_STOPPED_ERROR);
224 .showInternalMessageDialog(
226 "Out of memory handling result for job !!"
228 "\nSee help files for increasing Java Virtual Machine memory.",
229 "Out of memory", JOptionPane.WARNING_MESSAGE);
230 Cache.log.error("Out of memory when retrieving Job " + j + " id:" +
231 WsUrl + "/" + jobs[j].jobId, er);
235 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
237 // Decide on overall state based on collected jobs[] states
238 if (jstate.running > 0)
240 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
242 else if (jstate.queuing > 0)
244 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
249 if (jstate.finished > 0)
251 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
253 else if (jstate.error > 0)
255 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
257 else if (jstate.serror > 0)
259 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
268 catch (InterruptedException e)
270 Cache.log.debug("Interrupted sleep waiting for next job poll.", e);
272 // System.out.println("I'm alive "+alTitle);
275 if (jobComplete && jobs != null)
277 parseResult(); // tidy up and make results available to user
281 Cache.log.debug("WebServiceJob poll loop finished with no jobs created.");
285 abstract void StartJob(WSJob job);
287 abstract void parseResult();
289 protected void propagateDatasetMappings(Alignment al)
291 if (codonframe!=null)
293 SequenceI[] alignment = al.getSequencesArray();
294 for (int sq = 0; sq<alignment.length; sq++)
296 for (int i=0; i<codonframe.length; i++)
298 if (codonframe[i]!=null &&
299 codonframe[i].involvesSequence(alignment[sq]))
301 al.addCodonFrame(codonframe[i]);
302 codonframe[i] = null;
312 * @param alignFrame reference for copying mappings across
313 * @param wsInfo gui attachment point
314 * @param input input data for the calculation
315 * @param webServiceName name of service
316 * @param wsUrl url of the service being invoked
318 public WSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
319 AlignmentView input, String webServiceName,
322 this(alignFrame, wsinfo, input, wsUrl);
323 WebServiceName = webServiceName;
325 char defGapChar = '-';
328 * @return gap character to use for any alignment generation
330 public char getGapChar()
337 * @param alframe - reference for copying mappings and display styles across
338 * @param wsinfo2 - gui attachment point
339 * @param alview - input data for the calculation
340 * @param wsurl2 - url of the service being invoked
342 public WSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
343 AlignmentView alview, String wsurl2)
346 // this.alignFrame = alframe;
347 currentView = alframe.getCurrentView().getAlignment();
348 featureSettings = alframe.getFeatureRenderer().getSettings();
349 defGapChar = alframe.getViewport().getGapCharacter();
350 this.wsInfo = wsinfo2;
355 AlignedCodonFrame[] cf = alframe.getViewport().getAlignment().getCodonFrames();
358 codonframe = new AlignedCodonFrame[cf.length];
359 System.arraycopy(cf, 0, codonframe, 0, cf.length);