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.util.MessageManager;
32 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
34 import java.util.LinkedHashSet;
37 public abstract class AWSThread extends Thread
41 * view that this job was associated with
43 protected AlignmentI currentView = null;
46 * feature settings from view that job was associated with
48 protected FeatureRendererSettings featureSettings = null;
51 * metadata about this web service
53 protected WebserviceInfo wsInfo = null;
56 * original input data for this job
58 protected AlignmentView input = null;
61 * dataset sequence relationships to be propagated onto new results
63 protected Set<AlignedCodonFrame> codonframe = null;
66 * are there jobs still running in this thread.
68 protected boolean jobComplete = false;
71 * one or more jobs being managed by this thread.
73 protected AWsJob jobs[] = null;
76 * full name of service
78 protected String WebServiceName = null;
80 protected char defGapChar = '-';
83 * header prepended to all output from job
85 protected String OutputHeader;
88 * only used when reporting a web service out of memory error - the job ID
89 * will be concatenated to the URL
91 protected String WsUrl = null;
93 private boolean fromSplitFrame;
96 * generic web service job/subjob poll loop
100 JobStateSummary jstate = null;
107 jstate = new JobStateSummary();
108 for (int j = 0; j < jobs.length; j++)
111 if (!jobs[j].submitted && jobs[j].hasValidInput())
116 if (jobs[j].submitted && !jobs[j].subjobComplete)
121 if (!jobs[j].hasResponse())
123 throw (new Exception(
124 "Timed out when communicating with server\nTry again later.\n"));
126 jalview.bin.Cache.log.debug("Job " + j + " Result state "
127 + jobs[j].getState() + "(ServerError="
128 + jobs[j].isServerError() + ")");
129 } catch (Exception ex)
131 // Deal with Transaction exceptions
132 wsInfo.appendProgressText(jobs[j].jobnum, MessageManager
133 .formatMessage("info.server_exception", new Object[]
134 { WebServiceName, ex.getMessage() }));
135 // always output the exception's stack trace to the log
136 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
137 + ") Server exception.");
138 // todo: could limit trace to cause if this is a SOAPFaultException.
139 ex.printStackTrace();
141 if (jobs[j].allowedServerExceptions > 0)
143 jobs[j].allowedServerExceptions--;
144 Cache.log.debug("Sleeping after a server exception.");
148 } catch (InterruptedException ex1)
154 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
155 jobs[j].subjobComplete = true;
156 wsInfo.setStatus(jobs[j].jobnum,
157 WebserviceInfo.STATE_STOPPED_SERVERERROR);
159 } catch (OutOfMemoryError er)
162 jobs[j].subjobComplete = true;
163 jobs[j].clearResponse(); // may contain out of date result data
164 wsInfo.setStatus(jobs[j].jobnum,
165 WebserviceInfo.STATE_STOPPED_ERROR);
166 Cache.log.error("Out of memory when retrieving Job " + j
167 + " id:" + WsUrl + "/" + jobs[j].jobId, er);
168 new jalview.gui.OOMWarning("retrieving result for "
169 + WebServiceName, er);
173 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
175 // Decide on overall state based on collected jobs[] states
176 updateGlobalStatus(jstate);
182 } catch (InterruptedException e)
185 .debug("Interrupted sleep waiting for next job poll.", e);
187 // System.out.println("I'm alive "+alTitle);
190 if (jobComplete && jobs != null)
192 parseResult(); // tidy up and make results available to user
197 .debug("WebServiceJob poll loop finished with no jobs created.");
198 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
199 wsInfo.appendProgressText(MessageManager.getString("info.no_jobs_ran"));
200 wsInfo.setFinishedNoResults();
204 protected void updateGlobalStatus(JobStateSummary jstate)
206 if (jstate.running > 0)
208 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
210 else if (jstate.queuing > 0)
212 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
217 if (jstate.finished > 0)
219 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
221 else if (jstate.error > 0)
223 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
225 else if (jstate.serror > 0)
227 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
237 public AWSThread(Runnable target)
242 public AWSThread(String name)
247 public AWSThread(ThreadGroup group, Runnable target)
249 super(group, target);
252 public AWSThread(ThreadGroup group, String name)
257 public AWSThread(Runnable target, String name)
262 public AWSThread(ThreadGroup group, Runnable target, String name)
264 super(group, target, name);
268 * query web service for status of job. on return, job.result must not be null
269 * - if it is then it will be assumed that the job status query timed out and
270 * a server exception will be logged.
274 * will be logged as a server exception for this job
276 public abstract void pollJob(AWsJob job) throws Exception;
279 * submit job to web service
283 public abstract void StartJob(AWsJob job);
286 * process the set of AWsJob objects into a set of results, and tidy up.
288 public abstract void parseResult();
291 * helper function to conserve dataset references to sequence objects returned
292 * from web services 1. Propagates AlCodonFrame data from
293 * <code>codonframe</code> to <code>al</code> TODO: refactor to datamodel
297 public void propagateDatasetMappings(Alignment al)
299 if (codonframe != null)
301 SequenceI[] alignment = al.getSequencesArray();
302 for (int sq = 0; sq < alignment.length; sq++)
304 for (AlignedCodonFrame acf : codonframe)
306 final SequenceI seq = alignment[sq];
307 if (acf != null && acf.involvesSequence(seq))
309 al.addCodonFrame(acf);
317 public AWSThread(ThreadGroup group, Runnable target, String name,
320 super(group, target, name, stackSize);
325 * @return gap character to use for any alignment generation
327 public char getGapChar()
335 * reference for copying mappings across
337 * gui attachment point
339 * input data for the calculation
340 * @param webServiceName
343 * url of the service being invoked
345 public AWSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
346 AlignmentView input, String webServiceName, String wsUrl)
348 this(alignFrame, wsinfo, input, wsUrl);
349 WebServiceName = webServiceName;
353 * Extracts additional info from alignment view's context.
356 * - reference for copying mappings and display styles across
358 * - gui attachment point - may be null
360 * - input data for the calculation
362 * - url of the service being invoked
364 public AWSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
365 AlignmentView alview, String wsurl2)
368 // this.alignFrame = alframe;
369 this.fromSplitFrame = alframe.getSplitViewContainer() != null;
370 currentView = alframe.getCurrentView().getAlignment();
371 featureSettings = alframe.getFeatureRenderer().getSettings();
372 defGapChar = alframe.getViewport().getGapCharacter();
373 this.wsInfo = wsinfo2;
378 Set<AlignedCodonFrame> cf = alframe.getViewport().getAlignment()
382 codonframe = new LinkedHashSet<AlignedCodonFrame>();
383 codonframe.addAll(cf);
388 protected boolean isFromSplitFrame()
390 return this.fromSplitFrame;