From 43b1e1627f60ca4e3cab2ef60b4efef9d81030df Mon Sep 17 00:00:00 2001 From: jpdahlke Date: Fri, 17 Jan 2025 12:56:26 -0500 Subject: [PATCH] change EmissaryServer to use an enum for mode (#1042) --- .../java/emissary/command/ServerCommand.java | 32 ++++++------------- .../command/converter/ModeConverter.java | 22 +++++++++++++ .../validator/ServerModeValidator.java | 21 ------------ .../command/validator/package-info.java | 4 --- .../java/emissary/directory/EmissaryNode.java | 23 +++++++------ .../java/emissary/server/api/PeersIT.java | 20 ++---------- 6 files changed, 46 insertions(+), 76 deletions(-) create mode 100644 src/main/java/emissary/command/converter/ModeConverter.java delete mode 100644 src/main/java/emissary/command/validator/ServerModeValidator.java delete mode 100644 src/main/java/emissary/command/validator/package-info.java diff --git a/src/main/java/emissary/command/ServerCommand.java b/src/main/java/emissary/command/ServerCommand.java index 9b6041105e..771ae95914 100644 --- a/src/main/java/emissary/command/ServerCommand.java +++ b/src/main/java/emissary/command/ServerCommand.java @@ -1,10 +1,9 @@ package emissary.command; import emissary.client.EmissaryResponse; +import emissary.command.converter.ModeConverter; import emissary.command.converter.ProjectBaseConverter; -import emissary.command.validator.ServerModeValidator; -import emissary.core.EmissaryException; -import emissary.core.EmissaryRuntimeException; +import emissary.directory.EmissaryNode; import emissary.server.EmissaryServer; import emissary.server.api.Pause; @@ -28,15 +27,9 @@ public class ServerCommand extends ServiceCommand { public static final int DEFAULT_PORT = 8001; - private String mode = "standalone"; - - @Option(names = {"-m", "--mode"}, description = "mode: standalone or cluster\nDefault: ${DEFAULT-VALUE}", defaultValue = "standalone") - @SuppressWarnings("unused") - private void setMode(String value) { - ServerModeValidator smv = new ServerModeValidator(); - smv.validate("mode", value); - mode = value; - } + @Option(names = {"-m", "--mode"}, description = "mode: standalone or cluster\nDefault: ${DEFAULT-VALUE}", converter = ModeConverter.class, + defaultValue = "standalone") + private EmissaryNode.Mode mode; @Option(names = "--staticDir", description = "path to static assets, loaded from classpath otherwise", converter = ProjectBaseConverter.class) private Path staticDir; @@ -60,7 +53,7 @@ public int getDefaultPort() { return DEFAULT_PORT; } - public String getMode() { + public EmissaryNode.Mode getMode() { return mode; } @@ -89,20 +82,15 @@ public boolean shouldStrictMode() { public void setupCommand() { setupHttp(); reinitLogback(); - try { - setupServer(); - } catch (EmissaryException e) { - LOG.error("Got an exception", e); - throw new EmissaryRuntimeException(e); - } + setupServer(); } - public void setupServer() throws EmissaryException { + public void setupServer() { String flavorMode; if (getFlavor() == null) { - flavorMode = getMode().toUpperCase(Locale.getDefault()); + flavorMode = getMode().toString(); } else { - flavorMode = getMode().toUpperCase(Locale.getDefault()) + "," + getFlavor(); + flavorMode = getMode().toString() + "," + getFlavor(); } if (shouldStrictMode()) { diff --git a/src/main/java/emissary/command/converter/ModeConverter.java b/src/main/java/emissary/command/converter/ModeConverter.java new file mode 100644 index 0000000000..b34e7183dd --- /dev/null +++ b/src/main/java/emissary/command/converter/ModeConverter.java @@ -0,0 +1,22 @@ +package emissary.command.converter; + +import emissary.directory.EmissaryNode; + +import picocli.CommandLine.ITypeConverter; + +import java.util.Locale; + +public class ModeConverter implements ITypeConverter { + + @Override + public EmissaryNode.Mode convert(String s) throws Exception { + switch (s.toLowerCase(Locale.getDefault())) { + case "cluster": + return EmissaryNode.Mode.CLUSTER; + case "standalone": + return EmissaryNode.Mode.STANDALONE; + default: + throw new IllegalArgumentException("Unknown mode: " + s); + } + } +} diff --git a/src/main/java/emissary/command/validator/ServerModeValidator.java b/src/main/java/emissary/command/validator/ServerModeValidator.java deleted file mode 100644 index 5642ea004c..0000000000 --- a/src/main/java/emissary/command/validator/ServerModeValidator.java +++ /dev/null @@ -1,21 +0,0 @@ -package emissary.command.validator; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ServerModeValidator { - - private static final Logger LOG = LoggerFactory.getLogger(ServerModeValidator.class); - - public void validate(String name, String value) { - switch (value) { - case "cluster": - case "standalone": - break; - default: - LOG.error("Unknown mode: {}", value); - throw new IllegalArgumentException("Unknown mode: " + value); - } - } - -} diff --git a/src/main/java/emissary/command/validator/package-info.java b/src/main/java/emissary/command/validator/package-info.java deleted file mode 100644 index 4e89fb2a6d..0000000000 --- a/src/main/java/emissary/command/validator/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Provides classes to validate parameters {@link emissary.command.EmissaryCommand}s. - */ -package emissary.command.validator; diff --git a/src/main/java/emissary/directory/EmissaryNode.java b/src/main/java/emissary/directory/EmissaryNode.java index b837edfa37..ab7b55d53a 100644 --- a/src/main/java/emissary/directory/EmissaryNode.java +++ b/src/main/java/emissary/directory/EmissaryNode.java @@ -64,9 +64,9 @@ public class EmissaryNode { /** Property that determines if server will shut down in the event a place fails to start */ public static final String STRICT_STARTUP_MODE = "strict.mode"; - // types are feeder, worker, standalone - // TODO: make an enum for these - private static final String DEFAULT_NODE_MODE = "standalone"; + public enum Mode { + STANDALONE, CLUSTER; + } @Nullable protected String nodeName = null; @@ -76,8 +76,7 @@ public class EmissaryNode { // this is the OS for all practical purposes @Nullable protected String nodeType = null; - @Nullable - protected String nodeMode = null; // probably better as nodeType, but that requires a refactor + protected Mode nodeMode; protected boolean nodeNameIsDefault = false; @Nullable protected String nodeServiceType = null; @@ -85,14 +84,14 @@ public class EmissaryNode { protected boolean strictStartupMode = false; public EmissaryNode() { - this(DEFAULT_NODE_MODE); + this(Mode.STANDALONE); } /** * Construct the node. The node name and port are from system properties. The node type is based on the os.name in this * implementation */ - public EmissaryNode(String nodeMode) { + public EmissaryNode(Mode nodeMode) { this.nodeMode = nodeMode; this.nodeName = System.getProperty(NODE_NAME_PROPERTY); if (this.nodeName == null) { @@ -113,6 +112,10 @@ public EmissaryNode(String nodeMode) { this.strictStartupMode = Boolean.parseBoolean(System.getProperty(STRICT_STARTUP_MODE, String.valueOf(false))); } + public Mode getNodeMode() { + return nodeMode; + } + /** * The node name */ @@ -173,11 +176,7 @@ public boolean isValidStandalone() { * True if this node appears to be a stand-alone (non P2P) node */ public boolean isStandalone() { - return isValidStandalone() && getNodeMode().equals("standalone"); - } - - private Object getNodeMode() { - return nodeMode; + return isValidStandalone() && getNodeMode().equals(Mode.STANDALONE); } public boolean isStrictStartupMode() { diff --git a/src/test/java/emissary/server/api/PeersIT.java b/src/test/java/emissary/server/api/PeersIT.java index 51f4bc162a..fb05a48a51 100644 --- a/src/test/java/emissary/server/api/PeersIT.java +++ b/src/test/java/emissary/server/api/PeersIT.java @@ -44,7 +44,7 @@ public void setup() throws Exception { String projectBase = System.getenv(ConfigUtil.PROJECT_BASE_ENV); ServerCommand cmd = ServerCommand.parse(ServerCommand.class, "-b ", projectBase, "-m", "cluster", "-p", "123456"); cmd.setupServer(); - EmissaryServer server = EmissaryServer.init(cmd, new TestEmissaryNode()); + EmissaryServer server = EmissaryServer.init(cmd, new TestEmissaryNode(EmissaryNode.Mode.CLUSTER)); Namespace.bind("EmissaryServer", server); DirectoryPlace directoryPlace = new DirectoryPlace(DIRNAME, server.getNode()); @@ -108,25 +108,11 @@ void peersNoDirectoryPlace() throws NamespaceException { static class TestEmissaryNode extends EmissaryNode { - public TestEmissaryNode() { + public TestEmissaryNode(EmissaryNode.Mode mode) { + super(mode); nodeNameIsDefault = true; } - @Override - public int getNodePort() { - return TEST_PORT; - } - - @Override - public String getNodeName() { - return TEST_NODE; - } - - @Override - public boolean isStandalone() { - return false; - } - @Override public Configurator getPeerConfigurator() throws IOException { // just go get this from the src/test/resources directory