2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.OOMWarning;
31 import jalview.gui.WebserviceInfo;
32 import jalview.util.MessageManager;
33 import jalview.viewmodel.seqfeatures.FeatureRendererSettings;
35 import java.util.LinkedHashSet;
38 public abstract class AWSThread extends Thread
42 * view that this job was associated with
44 protected AlignmentI currentView = null;
47 * feature settings from view that job was associated with
49 protected FeatureRendererSettings featureSettings = null;
52 * metadata about this web service
54 protected WebserviceInfo wsInfo = null;
57 * original input data for this job
59 protected AlignmentView input = null;
62 * dataset sequence relationships to be propagated onto new results
64 protected Set<AlignedCodonFrame> codonframe = null;
67 * are there jobs still running in this thread.
69 protected boolean jobComplete = false;
72 * one or more jobs being managed by this thread.
74 protected AWsJob jobs[] = null;
77 * full name of service
79 protected String WebServiceName = null;
81 protected char defGapChar = '-';
84 * header prepended to all output from job
86 protected String OutputHeader;
89 * only used when reporting a web service out of memory error - the job ID
90 * will be concatenated to the URL
92 protected String WsUrl = null;
95 * The AlignFrame from which the service was requested.
97 private AlignFrame alignFrame;
100 * generic web service job/subjob poll loop
104 JobStateSummary jstate = null;
111 jstate = new JobStateSummary();
112 for (int j = 0; j < jobs.length; j++)
115 if (!jobs[j].submitted && jobs[j].hasValidInput())
120 if (jobs[j].submitted && !jobs[j].subjobComplete)
125 if (!jobs[j].hasResponse())
127 throw (new Exception(
128 "Timed out when communicating with server\nTry again later.\n"));
130 Cache.log.debug("Job " + j + " Result state "
131 + jobs[j].getState() + "(ServerError="
132 + jobs[j].isServerError() + ")");
133 } catch (Exception ex)
135 // Deal with Transaction exceptions
136 wsInfo.appendProgressText(jobs[j].jobnum, MessageManager
137 .formatMessage("info.server_exception", new Object[]
138 { WebServiceName, ex.getMessage() }));
139 // always output the exception's stack trace to the log
140 Cache.log.warn(WebServiceName + " job(" + jobs[j].jobnum
141 + ") Server exception.");
142 // todo: could limit trace to cause if this is a SOAPFaultException.
143 ex.printStackTrace();
145 if (jobs[j].allowedServerExceptions > 0)
147 jobs[j].allowedServerExceptions--;
148 Cache.log.debug("Sleeping after a server exception.");
152 } catch (InterruptedException ex1)
158 Cache.log.warn("Dropping job " + j + " " + jobs[j].jobId);
159 jobs[j].subjobComplete = true;
160 wsInfo.setStatus(jobs[j].jobnum,
161 WebserviceInfo.STATE_STOPPED_SERVERERROR);
163 } catch (OutOfMemoryError er)
166 jobs[j].subjobComplete = true;
167 jobs[j].clearResponse(); // may contain out of date result data
168 wsInfo.setStatus(jobs[j].jobnum,
169 WebserviceInfo.STATE_STOPPED_ERROR);
170 Cache.log.error("Out of memory when retrieving Job " + j
171 + " id:" + WsUrl + "/" + jobs[j].jobId, er);
172 new OOMWarning("retrieving result for " + WebServiceName, er);
176 jstate.updateJobPanelState(wsInfo, OutputHeader, jobs[j]);
178 // Decide on overall state based on collected jobs[] states
179 updateGlobalStatus(jstate);
185 } catch (InterruptedException e)
188 .debug("Interrupted sleep waiting for next job poll.", e);
190 // System.out.println("I'm alive "+alTitle);
193 if (jobComplete && jobs != null)
195 parseResult(); // tidy up and make results available to user
200 .debug("WebServiceJob poll loop finished with no jobs created.");
201 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
202 wsInfo.appendProgressText(MessageManager
203 .getString("info.no_jobs_ran"));
204 wsInfo.setFinishedNoResults();
208 protected void updateGlobalStatus(JobStateSummary jstate)
210 if (jstate.running > 0)
212 wsInfo.setStatus(WebserviceInfo.STATE_RUNNING);
214 else if (jstate.queuing > 0)
216 wsInfo.setStatus(WebserviceInfo.STATE_QUEUING);
221 if (jstate.finished > 0)
223 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_OK);
225 else if (jstate.error > 0)
227 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
229 else if (jstate.serror > 0)
231 wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
241 public AWSThread(Runnable target)
246 public AWSThread(String name)
251 public AWSThread(ThreadGroup group, Runnable target)
253 super(group, target);
256 public AWSThread(ThreadGroup group, String name)
261 public AWSThread(Runnable target, String name)
266 public AWSThread(ThreadGroup group, Runnable target, String name)
268 super(group, target, name);
272 * query web service for status of job. on return, job.result must not be null
273 * - if it is then it will be assumed that the job status query timed out and
274 * a server exception will be logged.
278 * will be logged as a server exception for this job
280 public abstract void pollJob(AWsJob job) throws Exception;
283 * submit job to web service
287 public abstract void StartJob(AWsJob job);
290 * process the set of AWsJob objects into a set of results, and tidy up.
292 public abstract void parseResult();
295 * helper function to conserve dataset references to sequence objects returned
296 * from web services 1. Propagates AlCodonFrame data from
297 * <code>codonframe</code> to <code>al</code> TODO: refactor to datamodel
301 public void propagateDatasetMappings(Alignment al)
303 if (codonframe != null)
305 SequenceI[] alignment = al.getSequencesArray();
306 for (int sq = 0; sq < alignment.length; sq++)
308 for (AlignedCodonFrame acf : codonframe)
310 final SequenceI seq = alignment[sq];
311 if (acf != null && acf.involvesSequence(seq))
313 al.addCodonFrame(acf);
321 public AWSThread(ThreadGroup group, Runnable target, String name,
324 super(group, target, name, stackSize);
329 * @return gap character to use for any alignment generation
331 public char getGapChar()
339 * reference for copying mappings across
341 * gui attachment point
343 * input data for the calculation
344 * @param webServiceName
347 * url of the service being invoked
349 public AWSThread(AlignFrame alignFrame, WebserviceInfo wsinfo,
350 AlignmentView input, String webServiceName, String wsUrl)
352 this(alignFrame, wsinfo, input, wsUrl);
353 WebServiceName = webServiceName;
357 * Extracts additional info from alignment view's context.
360 * - reference for copying mappings and display styles across
362 * - gui attachment point - may be null
364 * - input data for the calculation
366 * - url of the service being invoked
368 public AWSThread(AlignFrame alframe, WebserviceInfo wsinfo2,
369 AlignmentView alview, String wsurl2)
372 this.alignFrame = alframe;
373 currentView = alframe.getCurrentView().getAlignment();
374 featureSettings = alframe.getFeatureRenderer().getSettings();
375 defGapChar = alframe.getViewport().getGapCharacter();
376 this.wsInfo = wsinfo2;
381 Set<AlignedCodonFrame> cf = alframe.getViewport().getAlignment()
385 codonframe = new LinkedHashSet<AlignedCodonFrame>();
386 codonframe.addAll(cf);
391 protected AlignFrame getRequestingAlignFrame()
393 return this.alignFrame;