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.gui.FeatureRenderer.FeatureRendererSettings;
33 public abstract class AWSThread extends Thread
37 * view that this job was associated with
39 protected AlignmentI currentView = null;
42 * feature settings from view that job was associated with
44 protected FeatureRendererSettings featureSettings = null;
47 * metadata about this web service
49 protected WebserviceInfo wsInfo = null;
52 * original input data for this job
54 protected AlignmentView input = null;
57 * dataset sequence relationships to be propagated onto new results
59 protected AlignedCodonFrame[] codonframe = null;
62 * are there jobs still running in this thread.
64 protected boolean jobComplete = false;
67 * one or more jobs being managed by this thread.
69 protected AWsJob jobs[] = null;
72 * full name of service
74 protected String WebServiceName = null;
76 protected char defGapChar = '-';
79 * header prepended to all output from job
81 protected String OutputHeader;
84 * only used when reporting a web service out of memory error - the job ID
85 * will be concatenated to the URL
87 protected String WsUrl = null;
90 * generic web service job/subjob poll loop
94 JobStateSummary jstate = null;
101 jstate = new JobStateSummary();
102 for (int j = 0; j < jobs.length; j++)
105 if (!jobs[j].submitted && jobs[j].hasValidInput())
110 if (jobs[j].submitted && !jobs[j].subjobComplete)
115 if (!jobs[j].hasResponse())
117 throw (new Exception(
118 "Timed out when communicating with server\nTry again later.\n"));
120 jalview.bin.Cache.log.debug("Job " + j + " Result state "
121 + jobs[j].getState() + "(ServerError="
122 + jobs[j].isServerError() + ")");
123 } catch (Exception ex)
125 // Deal with Transaction exceptions
126 wsInfo.appendProgressText(jobs[j].jobnum, "\n" + WebServiceName
127 + " Server exception!\n" + ex.getMessage());
128 // always output the exception's stack trace to the log
129 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
130 + ") Server exception.");
131 // todo: could limit trace to cause if this is a SOAPFaultException.
132 ex.printStackTrace();
134 if (jobs[j].allowedServerExceptions > 0)
136 jobs[j].allowedServerExceptions--;
137 Cache.log.debug("Sleeping after a server exception.");
141 } catch (InterruptedException ex1)
147 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
148 jobs[j].subjobComplete = true;
149 wsInfo.setStatus(jobs[j].jobnum,
150 WebserviceInfo.STATE_STOPPED_SERVERERROR);
152 } catch (OutOfMemoryError er)
155 jobs[j].subjobComplete = true;
156 jobs[j].clearResponse(); // may contain out of date result data
157 wsInfo.setStatus(jobs[j].jobnum,
158 WebserviceInfo.STATE_STOPPED_ERROR);
159 Cache.log.error("Out of memory when retrieving Job " + j
160 + " id:" + WsUrl + "/" + jobs[j].jobId, er);
161 new jalview.gui.OOMWarning("retrieving result for "
162 + WebServiceName, er);
166 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
168 // Decide on overall state based on collected jobs[] states
169 updateGlobalStatus(jstate);
175 } catch (InterruptedException e)
178 .debug("Interrupted sleep waiting for next job poll.", e);
180 // System.out.println("I'm alive "+alTitle);
183 if (jobComplete && jobs != null)
185 parseResult(); // tidy up and make results available to user
190 .debug("WebServiceJob poll loop finished with no jobs created.");
191 wsInfo.setStatus(wsInfo.STATE_STOPPED_ERROR);
192 wsInfo.appendProgressText("No jobs ran.");
193 wsInfo.setFinishedNoResults();
197 protected void updateGlobalStatus(JobStateSummary jstate)
199 if (jstate.running > 0)
201 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
203 else if (jstate.queuing > 0)
205 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
210 if (jstate.finished > 0)
212 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
214 else if (jstate.error > 0)
216 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
218 else if (jstate.serror > 0)
220 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
230 public AWSThread(Runnable target)
235 public AWSThread(String name)
240 public AWSThread(ThreadGroup group, Runnable target)
242 super(group, target);
245 public AWSThread(ThreadGroup group, String name)
250 public AWSThread(Runnable target, String name)
255 public AWSThread(ThreadGroup group, Runnable target, String name)
257 super(group, target, name);
261 * query web service for status of job. on return, job.result must not be null
262 * - if it is then it will be assumed that the job status query timed out and
263 * a server exception will be logged.
267 * will be logged as a server exception for this job
269 public abstract void pollJob(AWsJob job) throws Exception;
272 * submit job to web service
276 public abstract void StartJob(AWsJob job);
279 * process the set of AWsJob objects into a set of results, and tidy up.
281 public abstract void parseResult();
284 * helper function to conserve dataset references to sequence objects returned
285 * from web services 1. Propagates AlCodonFrame data from
286 * <code>codonframe</code> to <code>al</code> TODO: refactor to datamodel
290 public void propagateDatasetMappings(Alignment al)
292 if (codonframe != null)
294 SequenceI[] alignment = al.getSequencesArray();
295 for (int sq = 0; sq < alignment.length; sq++)
297 for (int i = 0; i < codonframe.length; i++)
299 if (codonframe[i] != null
300 && codonframe[i].involvesSequence(alignment[sq]))
302 al.addCodonFrame(codonframe[i]);
303 codonframe[i] = null;
311 public AWSThread(ThreadGroup group, Runnable target, String name,
314 super(group, target, name, stackSize);
319 * @return gap character to use for any alignment generation
321 public char getGapChar()
329 * reference for copying mappings across
331 * gui attachment point
333 * input data for the calculation
334 * @param webServiceName
337 * url of the service being invoked
339 public AWSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
340 AlignmentView input, String webServiceName, String wsUrl)
342 this(alignFrame, wsinfo, input, wsUrl);
343 WebServiceName = webServiceName;
347 * Extracts additional info from alignment view's context.
350 * - reference for copying mappings and display styles across
352 * - gui attachment point - may be null
354 * - input data for the calculation
356 * - url of the service being invoked
358 public AWSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
359 AlignmentView alview, String wsurl2)
362 // this.alignFrame = alframe;
363 currentView = alframe.getCurrentView().getAlignment();
364 featureSettings = alframe.getFeatureRenderer().getSettings();
365 defGapChar = alframe.getViewport().getGapCharacter();
366 this.wsInfo = wsinfo2;
371 AlignedCodonFrame[] cf = alframe.getViewport().getAlignment()
375 codonframe = new AlignedCodonFrame[cf.length];
376 System.arraycopy(cf, 0, codonframe, 0, cf.length);