2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.bin.Cache;
24 import jalview.datamodel.AlignedCodonFrame;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.AlignmentView;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.AlignFrame;
30 import jalview.gui.WebserviceInfo;
31 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
32 import jalview.util.MessageManager;
34 public abstract class AWSThread extends Thread
38 * view that this job was associated with
40 protected AlignmentI currentView = null;
43 * feature settings from view that job was associated with
45 protected FeatureRendererSettings featureSettings = null;
48 * metadata about this web service
50 protected WebserviceInfo wsInfo = null;
53 * original input data for this job
55 protected AlignmentView input = null;
58 * dataset sequence relationships to be propagated onto new results
60 protected AlignedCodonFrame[] codonframe = null;
63 * are there jobs still running in this thread.
65 protected boolean jobComplete = false;
68 * one or more jobs being managed by this thread.
70 protected AWsJob jobs[] = null;
73 * full name of service
75 protected String WebServiceName = null;
77 protected char defGapChar = '-';
80 * header prepended to all output from job
82 protected String OutputHeader;
85 * only used when reporting a web service out of memory error - the job ID
86 * will be concatenated to the URL
88 protected String WsUrl = null;
91 * generic web service job/subjob poll loop
95 JobStateSummary jstate = null;
102 jstate = new JobStateSummary();
103 for (int j = 0; j < jobs.length; j++)
106 if (!jobs[j].submitted && jobs[j].hasValidInput())
111 if (jobs[j].submitted && !jobs[j].subjobComplete)
116 if (!jobs[j].hasResponse())
118 throw (new Exception(
119 "Timed out when communicating with server\nTry again later.\n"));
121 jalview.bin.Cache.log.debug("Job " + j + " Result state "
122 + jobs[j].getState() + "(ServerError="
123 + jobs[j].isServerError() + ")");
124 } catch (Exception ex)
126 // Deal with Transaction exceptions
127 wsInfo.appendProgressText(jobs[j].jobnum,
128 MessageManager.formatMessage("info.server_exception", new String[]{WebServiceName,ex.getMessage()}));
129 // always output the exception's stack trace to the log
130 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
131 + ") Server exception.");
132 // todo: could limit trace to cause if this is a SOAPFaultException.
133 ex.printStackTrace();
135 if (jobs[j].allowedServerExceptions > 0)
137 jobs[j].allowedServerExceptions--;
138 Cache.log.debug("Sleeping after a server exception.");
142 } catch (InterruptedException ex1)
148 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
149 jobs[j].subjobComplete = true;
150 wsInfo.setStatus(jobs[j].jobnum,
151 WebserviceInfo.STATE_STOPPED_SERVERERROR);
153 } catch (OutOfMemoryError er)
156 jobs[j].subjobComplete = true;
157 jobs[j].clearResponse(); // may contain out of date result data
158 wsInfo.setStatus(jobs[j].jobnum,
159 WebserviceInfo.STATE_STOPPED_ERROR);
160 Cache.log.error("Out of memory when retrieving Job " + j
161 + " id:" + WsUrl + "/" + jobs[j].jobId, er);
162 new jalview.gui.OOMWarning("retrieving result for "
163 + WebServiceName, er);
167 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
169 // Decide on overall state based on collected jobs[] states
170 updateGlobalStatus(jstate);
176 } catch (InterruptedException e)
179 .debug("Interrupted sleep waiting for next job poll.", e);
181 // System.out.println("I'm alive "+alTitle);
184 if (jobComplete && jobs != null)
186 parseResult(); // tidy up and make results available to user
191 .debug("WebServiceJob poll loop finished with no jobs created.");
192 wsInfo.setStatus(wsInfo.STATE_STOPPED_ERROR);
193 wsInfo.appendProgressText(MessageManager.getString("info.no_jobs_ran"));
194 wsInfo.setFinishedNoResults();
198 protected void updateGlobalStatus(JobStateSummary jstate)
200 if (jstate.running > 0)
202 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
204 else if (jstate.queuing > 0)
206 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
211 if (jstate.finished > 0)
213 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
215 else if (jstate.error > 0)
217 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
219 else if (jstate.serror > 0)
221 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
231 public AWSThread(Runnable target)
236 public AWSThread(String name)
241 public AWSThread(ThreadGroup group, Runnable target)
243 super(group, target);
246 public AWSThread(ThreadGroup group, String name)
251 public AWSThread(Runnable target, String name)
256 public AWSThread(ThreadGroup group, Runnable target, String name)
258 super(group, target, name);
262 * query web service for status of job. on return, job.result must not be null
263 * - if it is then it will be assumed that the job status query timed out and
264 * a server exception will be logged.
268 * will be logged as a server exception for this job
270 public abstract void pollJob(AWsJob job) throws Exception;
273 * submit job to web service
277 public abstract void StartJob(AWsJob job);
280 * process the set of AWsJob objects into a set of results, and tidy up.
282 public abstract void parseResult();
285 * helper function to conserve dataset references to sequence objects returned
286 * from web services 1. Propagates AlCodonFrame data from
287 * <code>codonframe</code> to <code>al</code> TODO: refactor to datamodel
291 public void propagateDatasetMappings(Alignment al)
293 if (codonframe != null)
295 SequenceI[] alignment = al.getSequencesArray();
296 for (int sq = 0; sq < alignment.length; sq++)
298 for (int i = 0; i < codonframe.length; i++)
300 if (codonframe[i] != null
301 && codonframe[i].involvesSequence(alignment[sq]))
303 al.addCodonFrame(codonframe[i]);
304 codonframe[i] = null;
312 public AWSThread(ThreadGroup group, Runnable target, String name,
315 super(group, target, name, stackSize);
320 * @return gap character to use for any alignment generation
322 public char getGapChar()
330 * reference for copying mappings across
332 * gui attachment point
334 * input data for the calculation
335 * @param webServiceName
338 * url of the service being invoked
340 public AWSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
341 AlignmentView input, String webServiceName, String wsUrl)
343 this(alignFrame, wsinfo, input, wsUrl);
344 WebServiceName = webServiceName;
348 * Extracts additional info from alignment view's context.
351 * - reference for copying mappings and display styles across
353 * - gui attachment point - may be null
355 * - input data for the calculation
357 * - url of the service being invoked
359 public AWSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
360 AlignmentView alview, String wsurl2)
363 // this.alignFrame = alframe;
364 currentView = alframe.getCurrentView().getAlignment();
365 featureSettings = alframe.getFeatureRenderer().getSettings();
366 defGapChar = alframe.getViewport().getGapCharacter();
367 this.wsInfo = wsinfo2;
372 AlignedCodonFrame[] cf = alframe.getViewport().getAlignment()
376 codonframe = new AlignedCodonFrame[cf.length];
377 System.arraycopy(cf, 0, codonframe, 0, cf.length);