--- /dev/null
+package jalview.ws.jws2.jabaws2;
+
+import jalview.bin.Cache;
+import jalview.gui.WebserviceInfo;
+import jalview.util.MessageManager;
+import jalview.ws.gui.WsJob;
+import jalview.ws.gui.WsJob.JobState;
+import jalview.ws.jws2.JabaParamStore;
+import jalview.ws.jws2.JabaPreset;
+import jalview.ws.jws2.dm.JabaWsParamSet;
+import jalview.ws.params.WsParamSetI;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import compbio.metadata.Argument;
+import compbio.metadata.ChunkHolder;
+import compbio.metadata.JobStatus;
+import compbio.metadata.Preset;
+
+/**
+ * Base class for JABAWS service instances. Provides helper methods for
+ * interfacing with Jalview.
+ *
+ * @author jprocter
+ *
+ */
+public class JabawsServiceInstance<T extends compbio.data.msa.JManagement>
+ implements
+ jalview.ws.api.JalviewWebServiceI, jalview.ws.api.CancellableI
+{
+ /**
+ * our service instance handler generated by the discoverer
+ */
+ Jws2Instance our;
+ protected T service;
+ protected Map<JobStatus, JobState> jwsState = new HashMap<>();
+
+ @Override
+ public boolean cancel(WsJob job)
+ {
+ service.cancelJob(job.getJobId());
+ // if the Jaba server indicates the job can't be cancelled, its
+ // because its running on the server's local execution engine
+ // so we just close the window anyway.
+
+ return true;
+ }
+
+
+ public JabawsServiceInstance(Jws2Instance handle)
+ {
+ our = handle;
+ service = (T) handle.service;
+ }
+
+ @Override
+ public void updateStatus(WsJob job)
+ {
+ JobStatus jwsstatus = service.getJobStatus(job.getJobId());
+ job.setState(jwsState.get(jwsstatus));
+ }
+
+ @Override
+ public boolean updateJobProgress(WsJob job) throws Exception
+ {
+ StringBuilder response = new StringBuilder(job.getStatus());
+ long lastchunk = job.getNextChunk();
+ if (lastchunk == -1)
+ {
+ Cache.log.debug("No more status messages for job " + job.getJobId());
+ return false;
+ }
+ boolean changed = false;
+ do
+ {
+ ChunkHolder chunk = service.pullExecStatistics(job.getJobId(),
+ lastchunk);
+ if (chunk != null)
+ {
+ changed |= chunk.getChunk().length() > 0;
+ response.append(chunk.getChunk());
+ lastchunk = chunk.getNextPosition();
+ try
+ {
+ Thread.sleep(50);
+ } catch (InterruptedException x)
+ {
+ }
+ ;
+ }
+ ;
+ job.setnextChunk(lastchunk);
+ } while (lastchunk >= 0 && job.getNextChunk() != lastchunk);
+ if (job instanceof WsJob)
+ {
+ // TODO decide if WsJob will be the bean for all ng-webservices
+ job.setStatus(response.toString());
+ }
+ return changed;
+ }
+
+ {
+ jwsState.put(JobStatus.CANCELLED, JobState.CANCELLED);
+ jwsState.put(JobStatus.COLLECTED, JobState.FINISHED);
+ jwsState.put(JobStatus.FAILED, JobState.FAILED);
+ jwsState.put(JobStatus.FINISHED, JobState.FINISHED);
+ jwsState.put(JobStatus.PENDING, JobState.QUEUED);
+ jwsState.put(JobStatus.RUNNING, JobState.RUNNING);
+ jwsState.put(JobStatus.STARTED, JobState.RUNNING);
+ jwsState.put(JobStatus.SUBMITTED, JobState.SUBMITTED);
+ jwsState.put(JobStatus.UNDEFINED, JobState.UNKNOWN);
+ }
+
+ public boolean isPresetJob(WsJob job)
+ {
+ return job.getPreset() != null && job.getPreset() instanceof JabaPreset;
+ }
+
+ public Preset getServerPreset(WsJob job)
+ {
+ return (isPresetJob(job))
+ ? ((JabaPreset) job.getPreset()).getJabaPreset()
+ : null;
+ }
+
+ public List<Argument> getJabaArguments(WsParamSetI preset)
+ {
+ List<Argument> newargs = new ArrayList<>();
+ if (preset != null)
+ {
+ if (preset instanceof JabaWsParamSet)
+ {
+ newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
+ }
+ else
+ {
+ newargs.addAll(
+ JabaParamStore.getJabafromJwsArgs(preset.getArguments()));
+ }
+ }
+ return newargs;
+ }
+
+ @Override
+ public boolean handleSubmitError(Throwable _lex, WsJob j,
+ WebserviceInfo wsInfo) throws Exception, Error
+ {
+ if (_lex instanceof compbio.metadata.UnsupportedRuntimeException)
+ {
+ wsInfo.appendProgressText(MessageManager.formatMessage(
+ "info.job_couldnt_be_run_server_doesnt_support_program",
+ new String[]
+ { _lex.getMessage() }));
+ wsInfo.warnUser(_lex.getMessage(),
+ MessageManager.getString("warn.service_not_supported"));
+ wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
+ wsInfo.setStatus(j.getJobnum(),
+ WebserviceInfo.STATE_STOPPED_SERVERERROR);
+ return true;
+ }
+ if (_lex instanceof compbio.metadata.LimitExceededException)
+ {
+ wsInfo.appendProgressText(MessageManager.formatMessage(
+ "info.job_couldnt_be_run_exceeded_hard_limit", new String[]
+ { _lex.getMessage() }));
+ wsInfo.warnUser(_lex.getMessage(),
+ MessageManager.getString("warn.input_is_too_big"));
+ wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
+ wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
+ return true;
+ }
+ if (_lex instanceof compbio.metadata.WrongParameterException)
+ {
+ wsInfo.warnUser(_lex.getMessage(),
+ MessageManager.getString("warn.invalid_job_param_set"));
+ wsInfo.appendProgressText(MessageManager.formatMessage(
+ "info.job_couldnt_be_run_incorrect_param_setting",
+ new String[]
+ { _lex.getMessage() }));
+ wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
+ wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
+ return true;
+ }
+ // pass on to generic error handler
+ return false;
+ }
+
+ @Override
+ public boolean handleCollectionException(Exception ex, WsJob msjob,
+ WebserviceInfo wsInfo)
+ {
+ if (ex instanceof compbio.metadata.ResultNotAvailableException)
+ {
+ // job has failed for some reason - probably due to invalid
+ // parameters
+ Cache.log.debug(
+ "Results not available for finished job - marking as broken job.",
+ ex);
+ String status = msjob.getStatus();
+
+ msjob.setStatus(status
+ + "\nResult not available. Probably due to invalid input or parameter settings. Server error message below:\n\n"
+ + ex.getLocalizedMessage());
+ msjob.setState(WsJob.JobState.BROKEN);
+ return true;
+ }
+ return false;
+ }
+}