Skip to content

Commit

Permalink
change to use enum
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdahlke committed Aug 28, 2024
1 parent 22de37d commit 2732099
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 104 deletions.
44 changes: 0 additions & 44 deletions clipboard

This file was deleted.

26 changes: 10 additions & 16 deletions src/main/java/emissary/command/ServerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import emissary.client.EmissaryResponse;
import emissary.command.converter.ProjectBaseConverter;
import emissary.command.validator.ServerModeValidator;
import emissary.command.converter.ServerModeConverter;
import emissary.core.EmissaryException;
import emissary.core.EmissaryRuntimeException;
import emissary.directory.EmissaryNode;
import emissary.server.EmissaryServer;
import emissary.server.api.Pause;

Expand All @@ -27,8 +27,9 @@ public class ServerCommand extends ServiceCommand {

public static final int DEFAULT_PORT = 8001;

@Option(names = {"-m", "--mode"}, description = "mode: standalone or cluster\nDefault: ${DEFAULT-VALUE}", defaultValue = "standalone")
private String mode = "standalone";
@Option(names = {"-m", "--mode"}, description = "mode: standalone or cluster\nDefault: ${DEFAULT-VALUE}", converter = ServerModeConverter.class,
defaultValue = "standalone")
private EmissaryNode.EmissaryMode mode;

@Option(names = "--staticDir", description = "path to static assets, loaded from classpath otherwise", converter = ProjectBaseConverter.class)
private Path staticDir;
Expand All @@ -52,7 +53,7 @@ public int getDefaultPort() {
return DEFAULT_PORT;
}

public String getMode() {
public EmissaryNode.EmissaryMode getMode() {
return mode;
}

Expand Down Expand Up @@ -81,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();
flavorMode = getMode().toString();
} else {
flavorMode = getMode().toUpperCase() + "," + getFlavor();
flavorMode = getMode().toString() + "," + getFlavor();
}

if (shouldStrictMode()) {
Expand All @@ -107,8 +103,6 @@ public void setupServer() throws EmissaryException {
flavorSet.add(f.toUpperCase());
}

ServerModeValidator.validate(getMode());

if (flavorSet.contains("STANDALONE") && flavorSet.contains("CLUSTER")) {
throw new IllegalArgumentException("Can not run a server in both STANDALONE and CLUSTER");
} else {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/emissary/command/converter/ServerModeConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package emissary.command.converter;

import emissary.directory.EmissaryNode;

import picocli.CommandLine.ITypeConverter;

public class ServerModeConverter implements ITypeConverter<EmissaryNode.EmissaryMode> {

@Override
public EmissaryNode.EmissaryMode convert(String s) throws Exception {
switch (s.toLowerCase()) {
case "cluster":
return EmissaryNode.EmissaryMode.CLUSTER;
case "standalone":
return EmissaryNode.EmissaryMode.STANDALONE;
default:
throw new IllegalArgumentException("Unknown mode: " + s);
}
}
}
23 changes: 0 additions & 23 deletions src/main/java/emissary/command/validator/ServerModeValidator.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/java/emissary/command/validator/package-info.java

This file was deleted.

27 changes: 15 additions & 12 deletions src/main/java/emissary/directory/EmissaryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,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 EmissaryMode {
STANDALONE, CLUSTER;
}

@Nullable
protected String nodeName = null;
Expand All @@ -75,19 +75,23 @@ 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 EmissaryMode nodeMode;
protected boolean nodeNameIsDefault = false;
@Nullable
protected String nodeServiceType = null;

protected boolean strictStartupMode = false;

public EmissaryNode() {
this(EmissaryMode.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() {
public EmissaryNode(EmissaryMode nodeMode) {
this.nodeMode = nodeMode;
this.nodeName = System.getProperty(NODE_NAME_PROPERTY);
if (this.nodeName == null) {
// Use IP Address for default node name since it is
Expand All @@ -103,11 +107,14 @@ public EmissaryNode() {
this.nodeScheme = System.getProperty(NODE_SCHEME_PROPERTY, "http");
this.nodePort = Integer.getInteger(NODE_PORT_PROPERTY, -1).intValue();
this.nodeType = System.getProperty("os.name", DEFAULT_NODE_TYPE).toLowerCase().replace(' ', '_');
this.nodeMode = System.getProperty("node.mode", DEFAULT_NODE_MODE).toLowerCase();
this.nodeServiceType = System.getProperty(NODE_SERVICE_TYPE_PROPERTY, DEFAULT_NODE_SERVICE_TYPE);
this.strictStartupMode = Boolean.parseBoolean(System.getProperty(STRICT_STARTUP_MODE, String.valueOf(false)));
}

public EmissaryMode getNodeMode() {
return nodeMode;
}

/**
* The node name
*/
Expand Down Expand Up @@ -168,11 +175,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(EmissaryMode.STANDALONE);
}

public boolean isStrictStartupMode() {
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/emissary/server/EmissaryServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,12 @@ public class EmissaryServer {
private final EmissaryNode emissaryNode;

public EmissaryServer(ServerCommand cmd) throws EmissaryException {
this(cmd, new EmissaryNode());
this(cmd, new EmissaryNode(cmd.getMode()));
}

@VisibleForTesting
public EmissaryServer(ServerCommand cmd, EmissaryNode node) throws EmissaryException {
this.cmd = cmd;
// See if we are an emissary node, but first setup node type
if (cmd.getMode() != null) {
System.setProperty("node.mode", cmd.getMode()); // TODO: clean this crap up
}
emissaryNode = node;

if (!emissaryNode.isValid()) {
Expand Down

0 comments on commit 2732099

Please sign in to comment.