X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2Fslivkaws%2FSlivkaWSInstance.java;h=fd89a2e9d44c8d74a1be4b3d9ad3c2e407d273ba;hb=9f503489b06e09a6a7aec83048301938e3710863;hp=8bb554a3ef0fb83f062c84a0f241784897b9da25;hpb=d3f8117988649e07fcd27a5e5623a1af6391b508;p=jalview.git diff --git a/src/jalview/ws/slivkaws/SlivkaWSInstance.java b/src/jalview/ws/slivkaws/SlivkaWSInstance.java index 8bb554a..fd89a2e 100644 --- a/src/jalview/ws/slivkaws/SlivkaWSInstance.java +++ b/src/jalview/ws/slivkaws/SlivkaWSInstance.java @@ -1,18 +1,15 @@ package jalview.ws.slivkaws; -import jalview.datamodel.AlignmentI; import jalview.datamodel.SequenceI; import jalview.gui.WebserviceInfo; -import jalview.io.DataSourceType; import jalview.io.FileFormat; import jalview.io.FormatAdapter; import jalview.ws.api.JalviewServiceEndpointProviderI; +import jalview.ws.api.JalviewWebServiceI; import jalview.ws.api.JobId; -import jalview.ws.api.MultipleSequenceAlignmentI; import jalview.ws.api.ServiceWithParameters; import jalview.ws.gui.WsJob; import jalview.ws.params.ArgumentI; -import jalview.ws.params.InvalidArgumentException; import jalview.ws.params.ParamDatastoreI; import jalview.ws.params.ParamManager; import jalview.ws.params.WsParamSetI; @@ -21,11 +18,16 @@ import java.io.ByteArrayInputStream; import java.io.IOError; import java.io.IOException; import java.io.InputStream; -import java.rmi.ServerError; +import java.io.InputStreamReader; +import java.util.Arrays; import java.util.EnumMap; -import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Optional; +import java.util.Set; +import uk.ac.dundee.compbio.slivkaclient.FieldType; +import uk.ac.dundee.compbio.slivkaclient.FileField; import uk.ac.dundee.compbio.slivkaclient.FormField; import uk.ac.dundee.compbio.slivkaclient.FormValidationException; import uk.ac.dundee.compbio.slivkaclient.JobState; @@ -35,55 +37,155 @@ import uk.ac.dundee.compbio.slivkaclient.SlivkaForm; import uk.ac.dundee.compbio.slivkaclient.SlivkaService; import uk.ac.dundee.compbio.slivkaclient.ValidationException; -public class SlivkaWSInstance extends ServiceWithParameters - implements MultipleSequenceAlignmentI, JalviewServiceEndpointProviderI +public abstract class SlivkaWSInstance extends ServiceWithParameters + implements JalviewServiceEndpointProviderI, JalviewWebServiceI { - private SlivkaClient client; - private SlivkaService service; - private SlivkaDatastore store = null; + protected final SlivkaClient client; - private EnumMap stateMap = new EnumMap<>(JobState.class); + protected final SlivkaService service; + + protected SlivkaDatastore store = null; + + protected static final EnumMap stateMap = new EnumMap<>(JobState.class); { stateMap.put(JobState.PENDING, WsJob.JobState.QUEUED); + stateMap.put(JobState.REJECTED, WsJob.JobState.INVALID); + stateMap.put(JobState.ACCEPTED, WsJob.JobState.QUEUED); stateMap.put(JobState.QUEUED, WsJob.JobState.QUEUED); stateMap.put(JobState.RUNNING, WsJob.JobState.RUNNING); stateMap.put(JobState.COMPLETED, WsJob.JobState.FINISHED); + stateMap.put(JobState.INTERRUPED, WsJob.JobState.CANCELLED); + stateMap.put(JobState.DELETED, WsJob.JobState.CANCELLED); stateMap.put(JobState.FAILED, WsJob.JobState.FAILED); stateMap.put(JobState.ERROR, WsJob.JobState.SERVERERROR); stateMap.put(JobState.UNKNOWN, WsJob.JobState.UNKNOWN); } + protected final Set failedStates = new HashSet<>(Arrays.asList( + WsJob.JobState.INVALID, WsJob.JobState.BROKEN, WsJob.JobState.FAILED, + WsJob.JobState.SERVERERROR, WsJob.JobState.CANCELLED + )); - SlivkaWSInstance(SlivkaClient client, SlivkaService service) { - super(service.getName(), "Alignment", service.getName(), "Slivka", client.getUrl().toString()); + public SlivkaWSInstance(SlivkaClient client, SlivkaService service, String action) + { + super(service.getName(), action, service.getLabel(), "Slivka", client.getUrl().toString()); this.client = client; this.service = service; } + protected final JobId submit(List sequences, + WsParamSetI preset, List args) throws Throwable + { + SlivkaForm form = service.getForm(); + for (FormField field : form.getFields()) + { + if (field.getType() == FieldType.FILE) + { + FormatAdapter fa = new FormatAdapter(); + fa.setNewlineString("\r\n"); + FileField fileField = (FileField) field; + FileFormat format; + switch (fileField.getMediaType()) + { + case "application/pfam": + format = FileFormat.Pfam; + break; + case "application/stockholm": + format = FileFormat.Stockholm; + break; + default: + case "application/fasta": + format = FileFormat.Fasta; + break; + } + InputStream stream = new ByteArrayInputStream( + fa.formatSequences(format, sequences.toArray(new SequenceI[0])) + .getBytes()); + RemoteFile rf = client.uploadFile(stream, "input", + fileField.getMediaType()); + form.insert(field.getName(), rf); + } + } + if (args != null) + { + for (ArgumentI arg : args) + { + // multiple choice field names are name$number to avoid duplications + // the number is stripped here + String fieldName = arg.getName().split("\\$", 2)[0]; + FormField field = form.getField(fieldName); + if (field.getType() == FieldType.BOOLEAN) + { + form.insert(fieldName, + (arg.getValue() != null && !arg.getValue().isBlank()) + ? true + : false); + } + else + { + form.insert(fieldName, field.valueOf(arg.getValue())); + } + } + } + return new JobId(service.getName(), service.getName(), form.submit()); + } + @Override - public void updateStatus(WsJob job) + public final void updateStatus(WsJob job) { try { job.setState(stateMap.get(client.getJobState(job.getJobId()))); } catch (IOException e) { - throw new RuntimeException(e); + throw new IOError(e); } } - SlivkaService getService() + @Override + public final boolean updateJobProgress(WsJob job) throws IOException { - return service; + List files = client.getJobResults(job.getJobId()); + Optional logFile = files.stream() + .filter(f -> f.getLabel().equals("log")).findFirst(); + boolean newContent = false; + if (logFile.isPresent()) + { + InputStream stream = logFile.get().getContent(); + long nextChunk = stream.skip(job.getNextChunk()); + int len = appendJobStatus(job, stream); + job.setnextChunk(nextChunk + len); + newContent |= len > 0; + } + if (failedStates.contains(job.getJobState())) + { + Optional errLogFile = files.stream() + .filter(f -> f.getLabel().equals("error-log")).findFirst(); + if (errLogFile.isPresent()) + { + newContent |= appendJobStatus(job, errLogFile.get().getContent()) > 0; + } + } + return newContent; } - @Override - public boolean updateJobProgress(WsJob job) + private int appendJobStatus(WsJob job, InputStream stream) throws IOException { - return false; + StringBuilder builder = new StringBuilder(job.getStatus()); + InputStreamReader reader = new InputStreamReader(stream); + char[] buffer = new char[4096]; + int chunkLen = 0; + int len = 0; + while ((len = reader.read(buffer)) != -1) + { + chunkLen += len; + builder.append(buffer, 0, len); + } + job.setStatus(builder.toString()); + return chunkLen; } @Override - public boolean handleSubmitError(Throwable _lex, WsJob j, WebserviceInfo wsInfo) + public final boolean handleSubmitError(Throwable _lex, WsJob j, WebserviceInfo wsInfo) { if (_lex instanceof FormValidationException) { @@ -102,81 +204,25 @@ public class SlivkaWSInstance extends ServiceWithParameters } @Override - public boolean handleCollectionException(Exception e, WsJob msjob, WebserviceInfo wsInfo) + public final boolean handleCollectionException(Exception e, WsJob msjob, WebserviceInfo wsInfo) { + // TODO return false; } - @Override - public JobId align(List toalign, WsParamSetI parameters, List list) throws Throwable - { - StringBuilder builder = new StringBuilder(); - for (SequenceI seq : toalign) - { - builder.append(">").append(seq.getName()).append("\n"); - builder.append(seq.getSequence()).append("\n"); - } - InputStream stream = new ByteArrayInputStream(builder.toString().getBytes()); - RemoteFile file = client.uploadFile(stream, "input.fasta", "application/fasta"); - SlivkaForm form = service.getForm(); - HashMap values = new HashMap<>(list != null ? list.size() : 0); - if (list != null) - { - for (ArgumentI arg : list) - { - values.put(arg.getName(), arg.getValue()); - } - } - for (FormField field : form.getFields()) - { - switch (field.getType()) { - case FILE: - form.insert(field.getName(), file); - break; - case BOOLEAN: - String value = values.get(field.getName()); - form.insert(field.getName(), (value != null && !value.isBlank()) ? true : false); - break; - default: - form.insert(field.getName(), field.valueOf(values.get(field.getName()))); - } - } - return new JobId(service.getName(), service.getName(), form.submit()); - } - - @Override - public AlignmentI getAlignmentFor(JobId jobId) throws InvalidArgumentException, ServerError, IOError + final SlivkaService getService() { - List files; - try - { - files = client.getJobResults(jobId.getJobId()); - for (RemoteFile f : files) - { - if (f.getMimeType().equals("application/clustal")) - { - return new FormatAdapter().readFile(f.getURL().toString(), DataSourceType.URL, FileFormat.Clustal); - } - else if (f.getMimeType().equals("application/fasta")) - { - return new FormatAdapter().readFile(f.getURL().toString(), DataSourceType.URL, FileFormat.Fasta); - } - } - } catch (IOException e) - { - throw new IOError(e); - } - return null; + return service; } @Override - public Object getEndpoint() + public final Object getEndpoint() { return this; } @Override - public void initParamStore(ParamManager userParameterStore) + public final void initParamStore(ParamManager userParameterStore) { if (store == null) { @@ -197,8 +243,13 @@ public class SlivkaWSInstance extends ServiceWithParameters } @Override - public ParamDatastoreI getParamStore() + public final ParamDatastoreI getParamStore() { + if (store == null) + { + initParamStore(null); + } return store; } + }