2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import jalview.bin.Cache;
21 import jalview.datamodel.AlignedCodonFrame;
22 import jalview.datamodel.Alignment;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.AlignmentView;
25 import jalview.datamodel.SequenceI;
26 import jalview.gui.AlignFrame;
27 import jalview.gui.WebserviceInfo;
28 import jalview.gui.FeatureRenderer.FeatureRendererSettings;
30 public abstract class AWSThread extends Thread
34 * view that this job was associated with
36 protected AlignmentI currentView = null;
39 * feature settings from view that job was associated with
41 protected FeatureRendererSettings featureSettings = null;
44 * metadata about this web service
46 protected WebserviceInfo wsInfo = null;
49 * original input data for this job
51 protected AlignmentView input = null;
54 * dataset sequence relationships to be propagated onto new results
56 protected AlignedCodonFrame[] codonframe = null;
59 * are there jobs still running in this thread.
61 protected boolean jobComplete = false;
64 * one or more jobs being managed by this thread.
66 protected AWsJob jobs[] = null;
69 * full name of service
71 protected String WebServiceName = null;
73 protected char defGapChar = '-';
76 * header prepended to all output from job
78 protected String OutputHeader;
81 * only used when reporting a web service out of memory error - the job ID
82 * will be concatenated to the URL
84 protected String WsUrl = null;
87 * generic web service job/subjob poll loop
91 JobStateSummary jstate = null;
98 jstate = new JobStateSummary();
99 for (int j = 0; j < jobs.length; j++)
102 if (!jobs[j].submitted && jobs[j].hasValidInput())
107 if (jobs[j].submitted && !jobs[j].subjobComplete)
112 if (!jobs[j].hasResponse())
114 throw (new Exception(
115 "Timed out when communicating with server\nTry again later.\n"));
117 jalview.bin.Cache.log.debug("Job " + j + " Result state "
118 + jobs[j].getState() + "(ServerError="
119 + jobs[j].isServerError() + ")");
120 } catch (Exception ex)
122 // Deal with Transaction exceptions
123 wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName
124 + " Server exception!\n" + ex.getMessage());
125 // always output the exception's stack trace to the log
126 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
127 + ") Server exception.");
128 // todo: could limit trace to cause if this is a SOAPFaultException.
129 ex.printStackTrace();
131 if (jobs[j].allowedServerExceptions > 0)
133 jobs[j].allowedServerExceptions--;
134 Cache.log.debug("Sleeping after a server exception.");
138 } catch (InterruptedException ex1)
144 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
145 jobs[j].subjobComplete = true;
146 wsInfo.setStatus(jobs[j].jobnum,
147 WebserviceInfo.STATE_STOPPED_SERVERERROR);
149 } catch (OutOfMemoryError er)
152 jobs[j].subjobComplete = true;
153 jobs[j].clearResponse(); // may contain out of date result data
154 wsInfo.setStatus(jobs[j].jobnum,
155 WebserviceInfo.STATE_STOPPED_ERROR);
156 Cache.log.error("Out of memory when retrieving Job " + j
157 + " id:" + WsUrl + "/" + jobs[j].jobId, er);
158 new jalview.gui.OOMWarning("retrieving result for "
159 + WebServiceName, er);
163 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
165 // Decide on overall state based on collected jobs[] states
166 updateGlobalStatus(jstate);
172 } catch (InterruptedException e)
175 .debug("Interrupted sleep waiting for next job poll.", e);
177 // System.out.println("I'm alive "+alTitle);
180 if (jobComplete && jobs != null)
182 parseResult(); // tidy up and make results available to user
187 .debug("WebServiceJob poll loop finished with no jobs created.");
188 wsInfo.setStatus(wsInfo.STATE_STOPPED_ERROR);
189 wsInfo.appendProgressText("No jobs ran.");
190 wsInfo.setFinishedNoResults();
194 protected void updateGlobalStatus(JobStateSummary jstate)
196 if (jstate.running > 0)
198 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
200 else if (jstate.queuing > 0)
202 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
207 if (jstate.finished > 0)
209 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
211 else if (jstate.error > 0)
213 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
215 else if (jstate.serror > 0)
217 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
227 public AWSThread(Runnable target)
232 public AWSThread(String name)
237 public AWSThread(ThreadGroup group, Runnable target)
239 super(group, target);
242 public AWSThread(ThreadGroup group, String name)
247 public AWSThread(Runnable target, String name)
252 public AWSThread(ThreadGroup group, Runnable target, String name)
254 super(group, target, name);
258 * query web service for status of job. on return, job.result must not be null
259 * - if it is then it will be assumed that the job status query timed out and
260 * a server exception will be logged.
264 * will be logged as a server exception for this job
266 public abstract void pollJob(AWsJob job) throws Exception;
269 * submit job to web service
273 public abstract void StartJob(AWsJob job);
276 * process the set of AWsJob objects into a set of results, and tidy up.
278 public abstract void parseResult();
281 * helper function to conserve dataset references to sequence objects returned
282 * from web services 1. Propagates AlCodonFrame data from
283 * <code>codonframe</code> to <code>al</code> TODO: refactor to datamodel
287 public 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;
308 public AWSThread(ThreadGroup group, Runnable target, String name,
311 super(group, target, name, stackSize);
316 * @return gap character to use for any alignment generation
318 public char getGapChar()
326 * reference for copying mappings across
328 * gui attachment point
330 * input data for the calculation
331 * @param webServiceName
334 * url of the service being invoked
336 public AWSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
337 AlignmentView input, String webServiceName, String wsUrl)
339 this(alignFrame, wsinfo, input, wsUrl);
340 WebServiceName = webServiceName;
344 * Extracts additional info from alignment view's context.
347 * - reference for copying mappings and display styles across
349 * - gui attachment point - may be null
351 * - input data for the calculation
353 * - url of the service being invoked
355 public AWSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
356 AlignmentView alview, String wsurl2)
359 // this.alignFrame = alframe;
360 currentView = alframe.getCurrentView().getAlignment();
361 featureSettings = alframe.getFeatureRenderer().getSettings();
362 defGapChar = alframe.getViewport().getGapCharacter();
363 this.wsInfo = wsinfo2;
368 AlignedCodonFrame[] cf = alframe.getViewport().getAlignment()
372 codonframe = new AlignedCodonFrame[cf.length];
373 System.arraycopy(cf, 0, codonframe, 0, cf.length);