From 05aa2f3418a9425fba206d2f445d9d58ce93aabf Mon Sep 17 00:00:00 2001 From: Mateusz Warowny Date: Fri, 24 Sep 2021 16:04:04 +0200 Subject: [PATCH] JAL-3878 Code reformatting. --- src/jalview/ws2/WSJobStatus.java | 5 +- src/jalview/ws2/WebServiceDiscoverer.java | 20 +++- src/jalview/ws2/WebServiceExecutor.java | 80 +++++++++------- src/jalview/ws2/WebServiceI.java | 29 +++--- src/jalview/ws2/WebServiceInfoUpdater.java | 32 ++++--- src/jalview/ws2/WebServiceWorkerI.java | 6 +- src/jalview/ws2/operations/Operation.java | 7 +- src/jalview/ws2/slivka/SlivkaWSDiscoverer.java | 75 +++++++++------ src/jalview/ws2/slivka/SlivkaWebService.java | 117 ++++++++++++++++-------- 9 files changed, 235 insertions(+), 136 deletions(-) diff --git a/src/jalview/ws2/WSJobStatus.java b/src/jalview/ws2/WSJobStatus.java index 73e5fd8..0a3872d 100755 --- a/src/jalview/ws2/WSJobStatus.java +++ b/src/jalview/ws2/WSJobStatus.java @@ -1,6 +1,5 @@ package jalview.ws2; - public enum WSJobStatus { /** Job has invalid parameters and cannot be started. */ @@ -14,8 +13,7 @@ public enum WSJobStatus /** Job is running. */ RUNNING, /** Job has finished with no errors. */ - FINISHED, - BROKEN, + FINISHED, BROKEN, /** Job has finished with errors. */ FAILED, /** Job cannot be processed or completed due to server error. */ @@ -25,7 +23,6 @@ public enum WSJobStatus /** Status cannot be determined. */ UNKNOWN; - public boolean isSubmitted() { switch (this) diff --git a/src/jalview/ws2/WebServiceDiscoverer.java b/src/jalview/ws2/WebServiceDiscoverer.java index 0a1565e..635bec9 100644 --- a/src/jalview/ws2/WebServiceDiscoverer.java +++ b/src/jalview/ws2/WebServiceDiscoverer.java @@ -10,8 +10,11 @@ import java.util.concurrent.CopyOnWriteArrayList; public interface WebServiceDiscoverer { public static final int STATUS_OK = 1; + public static final int STATUS_NO_SERVICES = 0; + public static final int STATUS_INVALID = -1; + public static final int STATUS_UNKNOWN = -2; public List getUrls(); @@ -35,23 +38,30 @@ public interface WebServiceDiscoverer public String getErrorMessages(); @FunctionalInterface - static interface ServiceChangeListener { + static interface ServiceChangeListener + { public void servicesChanged(WebServiceDiscoverer discoverer, Collection services); } List serviceListeners = new CopyOnWriteArrayList<>(); - public default void addServiceChangeListener(ServiceChangeListener listener) { + public default void addServiceChangeListener( + ServiceChangeListener listener) + { serviceListeners.add(listener); } - public default void removeServiceChangeListener(ServiceChangeListener listener) { + public default void removeServiceChangeListener( + ServiceChangeListener listener) + { serviceListeners.remove(listener); } - default void fireServicesChanged(List services) { - for (var listener: serviceListeners) { + default void fireServicesChanged(List services) + { + for (var listener : serviceListeners) + { listener.servicesChanged(this, services); } } diff --git a/src/jalview/ws2/WebServiceExecutor.java b/src/jalview/ws2/WebServiceExecutor.java index 453e6ce..9306020 100644 --- a/src/jalview/ws2/WebServiceExecutor.java +++ b/src/jalview/ws2/WebServiceExecutor.java @@ -13,44 +13,48 @@ import jalview.bin.Cache; public class WebServiceExecutor { - private ScheduledExecutorService executor = - Executors.newSingleThreadScheduledExecutor(); + private ScheduledExecutorService executor = Executors + .newSingleThreadScheduledExecutor(); public void submit(final WebServiceWorkerI worker) { executor.submit(() -> { - try { - worker.startJobs(); - wsThreadSupport.submitted(worker); - } - catch (Exception e) { - Cache.log.error("Failed to submit web service jobs.", e); - wsThreadSupport.submissionFailed(worker, e); - return; - } - executor.schedule(() -> pollJobs(worker), 1, TimeUnit.SECONDS); + try + { + worker.startJobs(); + wsThreadSupport.submitted(worker); + } catch (Exception e) + { + Cache.log.error("Failed to submit web service jobs.", e); + wsThreadSupport.submissionFailed(worker, e); + return; } - ); + executor.schedule(() -> pollJobs(worker), 1, TimeUnit.SECONDS); + }); executor.schedule(() -> pollJobs(worker), 1, TimeUnit.SECONDS); -} - + } - private void pollJobs(WebServiceWorkerI worker) { - try { + private void pollJobs(WebServiceWorkerI worker) + { + try + { worker.pollJobs(); - } - catch (Exception e) { + } catch (Exception e) + { Cache.log.error("Failed to poll web service jobs.", e); - for (WSJob job : worker.getJobs()) { + for (WSJob job : worker.getJobs()) + { job.setStatus(WSJobStatus.SERVER_ERROR); } wsThreadSupport.pollFailed(worker, e); return; } - if (!worker.isDone()) { + if (!worker.isDone()) + { executor.schedule(() -> pollJobs(worker), 1, TimeUnit.SECONDS); } - else { + else + { worker.done(); wsThreadSupport.done(worker); } @@ -68,7 +72,6 @@ public class WebServiceExecutor wsThreadSupport.removeListener(listener); } - public void shutdown() { executor.shutdownNow(); @@ -76,46 +79,55 @@ public class WebServiceExecutor } - -class WebServiceThreadSupport implements WebServiceThreadListenerI { +class WebServiceThreadSupport implements WebServiceThreadListenerI +{ List listeners = new CopyOnWriteArrayList<>(); @Override public void submitted(WebServiceWorkerI thread) { - for (var listener : listeners) listener.submitted(thread); + for (var listener : listeners) + listener.submitted(thread); } @Override public void submissionFailed(WebServiceWorkerI thread, Exception e) { - for (var listener : listeners) listener.submissionFailed(thread, e); + for (var listener : listeners) + listener.submissionFailed(thread, e); } @Override - public void pollFailed(WebServiceWorkerI thread, Exception e) { - for (var listener : listeners) listener.pollFailed(thread, e); + public void pollFailed(WebServiceWorkerI thread, Exception e) + { + for (var listener : listeners) + listener.pollFailed(thread, e); } @Override public void cancelled(WebServiceWorkerI thread) { - for (var listener : listeners) listener.cancelled(thread); + for (var listener : listeners) + listener.cancelled(thread); } @Override public void done(WebServiceWorkerI thread) { - for (var listener : listeners) listener.done(thread); + for (var listener : listeners) + listener.done(thread); } - public void addListener(WebServiceThreadListenerI listener) { - if (!listeners.contains(listener)) { + public void addListener(WebServiceThreadListenerI listener) + { + if (!listeners.contains(listener)) + { listeners.add(listener); } } - public void removeListener(WebServiceThreadListenerI listener) { + public void removeListener(WebServiceThreadListenerI listener) + { listeners.remove(listener); } } diff --git a/src/jalview/ws2/WebServiceI.java b/src/jalview/ws2/WebServiceI.java index 954bab7..73e58ee 100755 --- a/src/jalview/ws2/WebServiceI.java +++ b/src/jalview/ws2/WebServiceI.java @@ -10,42 +10,45 @@ import jalview.ws.params.WsParamSetI; import jalview.ws2.operations.Operation; /** - * Provides information about the web service and sub-routines - * to submit, track and cancel the jobs running on the server as well as - * retrieve the results. + * Provides information about the web service and sub-routines to submit, track + * and cancel the jobs running on the server as well as retrieve the results. * The instances should not depend on any other jalview components, especially - * must be oblivious to the existence of any UI. - * They are used by other classes such as WebServiceWorkers rather than - * manipulate data themselves. + * must be oblivious to the existence of any UI. They are used by other classes + * such as WebServiceWorkers rather than manipulate data themselves. * * @author mmwarowny */ public interface WebServiceI { public String getHostName(); + public String getProviderName(); + public String getName(); + public String getDescription(); + public String getOperationType(); + public List getOperations(); + public boolean hasParameters(); + public ParamDatastoreI getParamStore(); public String submit(List sequences, List args) throws IOException; - public void updateProgress(WSJob job) - throws IOException; + public void updateProgress(WSJob job) throws IOException; -// public ResultSupplier getResultSupplier(Class type); + // public ResultSupplier getResultSupplier(Class type); public void cancel(WSJob job) throws IOException; /** - * Handle an exception that happened during job submission. - * If the exception was handled property by this method, it - * returns true. Otherwise, returns false indicating the exception - * should be handled by the caller. + * Handle an exception that happened during job submission. If the exception + * was handled property by this method, it returns true. Otherwise, returns + * false indicating the exception should be handled by the caller. */ public boolean handleSubmissionError(WSJob job, Exception ex); diff --git a/src/jalview/ws2/WebServiceInfoUpdater.java b/src/jalview/ws2/WebServiceInfoUpdater.java index eb3e350..8152abf 100644 --- a/src/jalview/ws2/WebServiceInfoUpdater.java +++ b/src/jalview/ws2/WebServiceInfoUpdater.java @@ -9,24 +9,29 @@ import jalview.gui.WebserviceInfo; public class WebServiceInfoUpdater implements PropertyChangeListener { private final WebserviceInfo wsInfo; + private String outputHeader = ""; - public WebServiceInfoUpdater(WebserviceInfo wsInfo) { + public WebServiceInfoUpdater(WebserviceInfo wsInfo) + { this.wsInfo = wsInfo; } - public String getOutputHeader() { + public String getOutputHeader() + { return outputHeader; } - public void setOutputHeader(String header) { + public void setOutputHeader(String header) + { this.outputHeader = header; } @Override public void propertyChange(PropertyChangeEvent evt) { - switch (evt.getPropertyName()) { + switch (evt.getPropertyName()) + { case "status": statusChanged(evt); break; @@ -39,11 +44,13 @@ public class WebServiceInfoUpdater implements PropertyChangeListener } } - private void statusChanged(PropertyChangeEvent evt) { + private void statusChanged(PropertyChangeEvent evt) + { WSJob job = (WSJob) evt.getSource(); WSJobStatus status = (WSJobStatus) evt.getNewValue(); int wsInfoStatus = 0; - switch (status) { + switch (status) + { case READY: case SUBMITTED: case QUEUED: @@ -71,19 +78,22 @@ public class WebServiceInfoUpdater implements PropertyChangeListener wsInfo.setStatus(job.getJobNum(), wsInfoStatus); } - private void logChanged(PropertyChangeEvent evt) { + private void logChanged(PropertyChangeEvent evt) + { WSJob job = (WSJob) evt.getSource(); String oldLog = (String) evt.getOldValue(); String newLog = (String) evt.getNewValue(); - wsInfo.appendProgressText(job.getJobNum(), newLog.substring(oldLog.length())); + wsInfo.appendProgressText(job.getJobNum(), + newLog.substring(oldLog.length())); } - private void errorLogChanged(PropertyChangeEvent evt) { + private void errorLogChanged(PropertyChangeEvent evt) + { WSJob job = (WSJob) evt.getSource(); String oldLog = (String) evt.getOldValue(); String newLog = (String) evt.getNewValue(); - wsInfo.appendProgressText(job.getJobNum(), newLog.substring(oldLog.length())); + wsInfo.appendProgressText(job.getJobNum(), + newLog.substring(oldLog.length())); } - } diff --git a/src/jalview/ws2/WebServiceWorkerI.java b/src/jalview/ws2/WebServiceWorkerI.java index da6e540..be719c1 100644 --- a/src/jalview/ws2/WebServiceWorkerI.java +++ b/src/jalview/ws2/WebServiceWorkerI.java @@ -19,10 +19,12 @@ public interface WebServiceWorkerI WebServiceI getWebService(); - default boolean isDone() { + default boolean isDone() + { if (getJobs().size() == 0) return false; - for (WSJob job : getJobs()) { + for (WSJob job : getJobs()) + { if (!job.getStatus().isDone()) return false; } diff --git a/src/jalview/ws2/operations/Operation.java b/src/jalview/ws2/operations/Operation.java index 062d036..e4df008 100644 --- a/src/jalview/ws2/operations/Operation.java +++ b/src/jalview/ws2/operations/Operation.java @@ -5,9 +5,14 @@ import jalview.ws2.MenuEntryProviderI; public interface Operation { public int getMinSequences(); + public int getMaxSequences(); + public boolean canSubmitGaps(); + public boolean isProteinOperation(); - public boolean isNucleotideOperation(); + + public boolean isNucleotideOperation(); + public MenuEntryProviderI getMenuBuilder(); } diff --git a/src/jalview/ws2/slivka/SlivkaWSDiscoverer.java b/src/jalview/ws2/slivka/SlivkaWSDiscoverer.java index fe5c040..98ce079 100644 --- a/src/jalview/ws2/slivka/SlivkaWSDiscoverer.java +++ b/src/jalview/ws2/slivka/SlivkaWSDiscoverer.java @@ -37,7 +37,8 @@ public class SlivkaWSDiscoverer implements WebServiceDiscoverer } @Override - public List getUrls() { + public List getUrls() + { String surls = Cache.getDefault(SLIVKA_HOST_URLS, DEFAULT_URL); String urls[] = surls.split(","); ArrayList valid = new ArrayList<>(urls.length); @@ -47,13 +48,13 @@ public class SlivkaWSDiscoverer implements WebServiceDiscoverer { new URL(url); valid.add(url); - } - catch (MalformedURLException e) + } catch (MalformedURLException e) { - Cache.log.warn("Problem whilst trying to make a URL from '" + - Objects.toString(url, "") + "'. " + - "This was probably due to malformed comma-separated-list " + - "in the " + SLIVKA_HOST_URLS + " entry of ${HOME}/.jalview_properties"); + Cache.log.warn("Problem whilst trying to make a URL from '" + + Objects.toString(url, "") + "'. " + + "This was probably due to malformed comma-separated-list " + + "in the " + SLIVKA_HOST_URLS + + " entry of ${HOME}/.jalview_properties"); Cache.log.debug("Exception occurred while reading url list", e); } } @@ -61,53 +62,63 @@ public class SlivkaWSDiscoverer implements WebServiceDiscoverer } @Override - public void setUrls(List wsUrls) { + public void setUrls(List wsUrls) + { if (wsUrls != null && !wsUrls.isEmpty()) { Cache.setProperty(SLIVKA_HOST_URLS, String.join(",", wsUrls)); } - else { + else + { Cache.removeProperty(SLIVKA_HOST_URLS); } } @Override - public boolean testUrl(URL url) { + public boolean testUrl(URL url) + { return getStatusForUrl(url.toString()) == STATUS_OK; } @Override - public int getStatusForUrl(String url) { + public int getStatusForUrl(String url) + { try { List services = new SlivkaClient(url).getServices(); return services.isEmpty() ? STATUS_NO_SERVICES : STATUS_OK; - } - catch (IOException e) + } catch (IOException e) { - Cache.log.error("Slivka could not retrieve services list from " + url, e); + Cache.log.error("Slivka could not retrieve services list from " + url, + e); return STATUS_INVALID; } } - public List getServices() { + public List getServices() + { return Collections.unmodifiableList(services); } - public boolean hasServices() { + public boolean hasServices() + { return !isRunning() && services.size() > 0; } - public boolean isRunning() { - for (Future task : discoveryTasks) { - if (!task.isDone()) { + public boolean isRunning() + { + for (Future task : discoveryTasks) + { + if (!task.isDone()) + { return false; } } return true; } - public boolean isDone() { + public boolean isDone() + { return !isRunning() && discoveryTasks.size() > 0; } @@ -130,17 +141,22 @@ public class SlivkaWSDiscoverer implements WebServiceDiscoverer Cache.log.info("Reloading Slivka services"); fireServicesChanged(Collections.emptyList()); ArrayList allServices = new ArrayList<>(); - for (String url : getUrls()) { + for (String url : getUrls()) + { SlivkaClient client = new SlivkaClient(url); List services; - try { + try + { services = client.getServices(); - } catch (IOException e) { + } catch (IOException e) + { Cache.log.error("Unable to fetch services from " + url, e); continue; } - for (SlivkaService service : services) { - SlivkaWebService instance = new SlivkaWebService(client, service, service.getName()); + for (SlivkaService service : services) + { + SlivkaWebService instance = new SlivkaWebService(client, service, + service.getName()); for (String classifier : service.classifiers) { String[] path = classifier.split("\\s*::\\s*"); @@ -148,7 +164,8 @@ public class SlivkaWSDiscoverer implements WebServiceDiscoverer && path[1].toLowerCase().equals("analysis")) { Operation op = null; - switch (path[path.length - 1].toLowerCase()) { + switch (path[path.length - 1].toLowerCase()) + { case "multiple sequence alignment": op = new AlignmentOperation(instance, instance::getAlignment); } @@ -156,7 +173,8 @@ public class SlivkaWSDiscoverer implements WebServiceDiscoverer instance.addOperation(op); } } - if (instance.operations.size() > 0) { + if (instance.operations.size() > 0) + { allServices.add(instance); } } @@ -168,7 +186,8 @@ public class SlivkaWSDiscoverer implements WebServiceDiscoverer } @Override - public String getErrorMessages() { + public String getErrorMessages() + { return ""; } diff --git a/src/jalview/ws2/slivka/SlivkaWebService.java b/src/jalview/ws2/slivka/SlivkaWebService.java index e7800ad..2cd9385 100644 --- a/src/jalview/ws2/slivka/SlivkaWebService.java +++ b/src/jalview/ws2/slivka/SlivkaWebService.java @@ -38,13 +38,19 @@ import uk.ac.dundee.compbio.slivkaclient.SlivkaService; public class SlivkaWebService implements WebServiceI { protected final SlivkaClient client; + protected final SlivkaService service; + protected SlivkaDatastore store = null; + protected final String operation; + protected final ArrayList operations = new ArrayList<>(); + protected int typeFlags = 0; - protected static final EnumMap stateMap = new EnumMap<>(Job.Status.class); + protected static final EnumMap stateMap = new EnumMap<>( + Job.Status.class); { stateMap.put(Job.Status.PENDING, WsJob.JobState.QUEUED); stateMap.put(Job.Status.REJECTED, WsJob.JobState.INVALID); @@ -58,68 +64,96 @@ public class SlivkaWebService implements WebServiceI stateMap.put(Job.Status.ERROR, WsJob.JobState.SERVERERROR); stateMap.put(Job.Status.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 - )); - public SlivkaWebService(SlivkaClient client, SlivkaService service, String operation) { + protected final Set failedStates = new HashSet<>( + Arrays.asList(WsJob.JobState.INVALID, WsJob.JobState.BROKEN, + WsJob.JobState.FAILED, WsJob.JobState.SERVERERROR, + WsJob.JobState.CANCELLED)); + + public SlivkaWebService(SlivkaClient client, SlivkaService service, + String operation) + { this.client = client; this.service = service; this.operation = operation; } @Override - public String getHostName() { return client.getUrl().toString(); } + public String getHostName() + { + return client.getUrl().toString(); + } @Override - public String getProviderName() { return "slivka"; } + public String getProviderName() + { + return "slivka"; + } @Override - public String getName() { return service.getName(); } + public String getName() + { + return service.getName(); + } @Override - public String getDescription() { return service.getDescription(); } + public String getDescription() + { + return service.getDescription(); + } @Override - public String getOperationType() { return operation; } + public String getOperationType() + { + return operation; + } @Override - public List getOperations() { + public List getOperations() + { return operations; } - void addOperation(Operation operation) { + void addOperation(Operation operation) + { operations.add(operation); } - void removeOperation(Operation operation) { + void removeOperation(Operation operation) + { operations.remove(operation); } @Override - public boolean hasParameters() { + public boolean hasParameters() + { return getParamStore().getServiceParameters().size() > 0; } @Override - public ParamDatastoreI getParamStore() { - if (store == null) { + public ParamDatastoreI getParamStore() + { + if (store == null) + { store = new SlivkaDatastore(service); } return store; } @Override - public String submit(List sequences, List args) throws IOException + public String submit(List sequences, List args) + throws IOException { var request = new uk.ac.dundee.compbio.slivkaclient.JobRequest(); - for (Parameter param : service.getParameters()) { - if (param instanceof Parameter.FileParameter) { + for (Parameter param : service.getParameters()) + { + if (param instanceof Parameter.FileParameter) + { // if finds a file input, gives it sequences stream Parameter.FileParameter fileParam = (Parameter.FileParameter) param; FileFormat format; - switch (fileParam.getMediaType()) { + switch (fileParam.getMediaType()) + { case "application/pfam": format = FileFormat.Pfam; break; @@ -134,20 +168,22 @@ public class SlivkaWebService implements WebServiceI format = FileFormat.Fasta; break; } - InputStream stream = new ByteArrayInputStream( - format.getWriter(null) + InputStream stream = new ByteArrayInputStream(format.getWriter(null) .print(sequences.toArray(new SequenceI[0]), false) .getBytes()); request.addFile(param.getId(), stream); } } - if (args != null) { - for (ArgumentI arg : args) { + if (args != null) + { + for (ArgumentI arg : args) + { // multiple choice field names are name$number to avoid duplications // the number is stripped here String paramId = arg.getName().split("\\$", 2)[0]; Parameter param = service.getParameter(paramId); - if (param instanceof Parameter.FlagParameter) { + if (param instanceof Parameter.FlagParameter) + { if (arg.getValue() != null && !arg.getValue().isBlank()) request.addData(paramId, true); else @@ -164,8 +200,7 @@ public class SlivkaWebService implements WebServiceI } @Override - public void updateProgress(WSJob job) - throws IOException + public void updateProgress(WSJob job) throws IOException { // TODO Auto-generated method stub @@ -180,7 +215,8 @@ public class SlivkaWebService implements WebServiceI @Override public boolean handleSubmissionError(WSJob job, Exception ex) { - if (ex instanceof ClientProtocolException) { + if (ex instanceof ClientProtocolException) + { Cache.log.error("Job submission failed due to exception.", ex); return true; } @@ -194,25 +230,30 @@ public class SlivkaWebService implements WebServiceI return false; } - public AlignmentI getAlignment(WSJob job) throws IOException { + public AlignmentI getAlignment(WSJob job) throws IOException + { Collection files; var slivkaJob = client.getJob(job.getJobId()); files = slivkaJob.getResults(); - for (RemoteFile f : files) { - if (f.getMediaType().equals("application/clustal")) { - return new FormatAdapter().readFile( - f.getContentUrl().toString(), DataSourceType.URL, FileFormat.Clustal); + for (RemoteFile f : files) + { + if (f.getMediaType().equals("application/clustal")) + { + return new FormatAdapter().readFile(f.getContentUrl().toString(), + DataSourceType.URL, FileFormat.Clustal); } - else if (f.getMediaType().equals("application/fasta")) { - return new FormatAdapter().readFile( - f.getContentUrl().toString(), DataSourceType.URL, FileFormat.Fasta); + else if (f.getMediaType().equals("application/fasta")) + { + return new FormatAdapter().readFile(f.getContentUrl().toString(), + DataSourceType.URL, FileFormat.Fasta); } } return null; } @Override - public String toString() { + public String toString() + { return String.format("SlivkaWebService[%s]", getName()); } } -- 1.7.10.2