From 6ed95e10095d9b0a31fe6912818fb81d980fdc05 Mon Sep 17 00:00:00 2001 From: Mateusz Warowny Date: Tue, 8 Mar 2022 18:07:14 +0100 Subject: [PATCH] JAL-3878 Add web service reference to actions. --- src/jalview/ws2/actions/BaseAction.java | 27 +++++-- .../ws2/actions/alignment/AlignmentAction.java | 2 +- src/jalview/ws2/actions/api/ActionI.java | 16 +++- src/jalview/ws2/api/WebService.java | 85 +++++++++----------- 4 files changed, 72 insertions(+), 58 deletions(-) diff --git a/src/jalview/ws2/actions/BaseAction.java b/src/jalview/ws2/actions/BaseAction.java index 37fbe39..0fc7aa0 100644 --- a/src/jalview/ws2/actions/BaseAction.java +++ b/src/jalview/ws2/actions/BaseAction.java @@ -1,9 +1,11 @@ package jalview.ws2.actions; import java.util.EnumSet; +import java.util.Objects; import jalview.ws2.actions.api.ActionI; import jalview.ws2.api.CredentialType; +import jalview.ws2.api.WebService; /** * An abstract base class storing common data and implementing their getters @@ -12,14 +14,15 @@ import jalview.ws2.api.CredentialType; * {@code isActive} implementations. * * @author mmwarowny - * - * @param + * @param * task result type */ -public abstract class BaseAction implements ActionI +public abstract class BaseAction implements ActionI { - public static class Builder + public static abstract class Builder> { + protected WebService webService; + protected String name; protected String tooltip = ""; @@ -41,6 +44,11 @@ public abstract class BaseAction implements ActionI this.name = name; } + public void webService(WebService val) + { + this.webService = val; + } + public void tooltip(String val) { tooltip = val; @@ -82,6 +90,8 @@ public abstract class BaseAction implements ActionI } } + protected final WebService> webService; + protected final String name; protected final String tooltip; @@ -98,8 +108,10 @@ public abstract class BaseAction implements ActionI protected final EnumSet requiredCredentials; - protected BaseAction(Builder builder) + protected BaseAction(Builder> builder) { + Objects.requireNonNull(builder.webService); + this.webService = builder.webService; this.name = builder.name; this.tooltip = builder.tooltip; this.subcategory = builder.subcategory; @@ -110,9 +122,10 @@ public abstract class BaseAction implements ActionI this.requiredCredentials = builder.requiredCredentials; } - public static Builder newBuilder(String name) + @Override + public WebService> getWebService() { - return new Builder(name); + return webService; } @Override diff --git a/src/jalview/ws2/actions/alignment/AlignmentAction.java b/src/jalview/ws2/actions/alignment/AlignmentAction.java index d8f5a1d..040e6ef 100644 --- a/src/jalview/ws2/actions/alignment/AlignmentAction.java +++ b/src/jalview/ws2/actions/alignment/AlignmentAction.java @@ -27,7 +27,7 @@ public class AlignmentAction extends BaseAction * * @author mmwarowny */ - public static class Builder extends BaseAction.Builder + public static class Builder extends BaseAction.Builder { protected AlignmentWebServiceClientI client; diff --git a/src/jalview/ws2/actions/api/ActionI.java b/src/jalview/ws2/actions/api/ActionI.java index 0fbbe22..bcf7678 100644 --- a/src/jalview/ws2/actions/api/ActionI.java +++ b/src/jalview/ws2/actions/api/ActionI.java @@ -7,6 +7,7 @@ import jalview.viewmodel.AlignmentViewport; import jalview.ws.params.ArgumentI; import jalview.ws2.api.CredentialType; import jalview.ws2.api.Credentials; +import jalview.ws2.api.WebService; /** * {@code Action} object represents an executable action that the web service @@ -21,12 +22,19 @@ import jalview.ws2.api.Credentials; * * @author mmwarowny * - * @param + * @param * task result type */ -public interface ActionI +public interface ActionI { /** + * Get the web service containing this action. + * + * @return containing web service + */ + WebService> getWebService(); + + /** * Get the name of the action. Typically, it should be the same as the name of * the service. * @@ -105,8 +113,8 @@ public interface ActionI * event handler attached to the new task * @return new running task */ - TaskI perform(AlignmentViewport viewport, List args, - Credentials credentials, TaskEventListener handler); + TaskI perform(AlignmentViewport viewport, List args, + Credentials credentials, TaskEventListener handler); /** * Return if the action is currently active for the given viewport. Active diff --git a/src/jalview/ws2/api/WebService.java b/src/jalview/ws2/api/WebService.java index 6831541..4a101fd 100644 --- a/src/jalview/ws2/api/WebService.java +++ b/src/jalview/ws2/api/WebService.java @@ -2,16 +2,16 @@ package jalview.ws2.api; import java.net.URL; import java.util.ArrayList; -import java.util.Collections; +import java.util.Collection; import java.util.List; import jalview.ws.params.ParamDatastoreI; import jalview.ws2.actions.api.ActionI; import static java.util.Objects.requireNonNull; -public class WebService +public class WebService> { - public static class Builder + public static class Builder> { private URL url; @@ -27,82 +27,59 @@ public class WebService private ParamDatastoreI paramDatastore; - private List actions = new ArrayList<>(); + private Class actionClass; - private Class actionClass; - - public Builder url(URL val) + public Builder url(URL val) { url = val; return this; } - public Builder clientName(String val) + public Builder clientName(String val) { clientName = val; return this; } - public Builder category(String val) + public Builder category(String val) { category = val; return this; } - public Builder name(String val) + public Builder name(String val) { name = val; return this; } - public Builder description(String val) + public Builder description(String val) { description = val; return this; } - public Builder interactive(boolean val) + public Builder interactive(boolean val) { interactive = val; return this; } - public Builder paramDatastore(ParamDatastoreI val) + public Builder paramDatastore(ParamDatastoreI val) { paramDatastore = val; return this; } - public Builder actions(List val) - { - actions = val; - return this; - } - - public Builder action(T val) - { - actions.add(val); - return this; - } - - public Builder actionClass(Class val) + public Builder actionClass(Class val) { actionClass = val; return this; } - public WebService build() + public WebService build() { - requireNonNull(url); - requireNonNull(clientName); - requireNonNull(category); - requireNonNull(name); - requireNonNull(paramDatastore); - requireNonNull(actions); - if (actions.size() == 0) - throw new IllegalArgumentException("Empty actions list"); - requireNonNull(actionClass); - return new WebService(this); + return new WebService(this); } } @@ -120,12 +97,18 @@ public class WebService private final ParamDatastoreI paramDatastore; - private final List actions; + private final List actions; - private final Class actionClass; + private final Class actionClass; - WebService(Builder builder) + protected WebService(Builder builder) { + requireNonNull(builder.url); + requireNonNull(builder.clientName); + requireNonNull(builder.category); + requireNonNull(builder.name); + requireNonNull(builder.paramDatastore); + requireNonNull(builder.actionClass); this.url = builder.url; this.clientName = builder.clientName; this.category = builder.category; @@ -133,13 +116,23 @@ public class WebService this.description = builder.description; this.interactive = builder.interactive; this.paramDatastore = builder.paramDatastore; - this.actions = Collections.unmodifiableList(builder.actions); + this.actions = new ArrayList<>(); this.actionClass = builder.actionClass; } - - public static Builder newBuilder() + + public static > Builder newBuilder() + { + return new Builder(); + } + + public void addAction(A action) + { + this.actions.add(action); + } + + public void addActions(Collection actions) { - return new Builder(); + this.actions.addAll(actions); } public URL getUrl() @@ -177,12 +170,12 @@ public class WebService return paramDatastore; } - public List getActions() + public List getActions() { return actions; } - public Class getActionClass() + public Class getActionClass() { return actionClass; } -- 1.7.10.2