Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update command validation for mode value and use an enum for possible values #900

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/emissary/admin/Startup.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import emissary.pickup.PickUpPlace;
import emissary.place.CoordinationPlace;
import emissary.place.IServiceProviderPlace;
import emissary.server.EmissaryServer;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -287,7 +288,7 @@ protected boolean localDirectorySetup(final Map<String, String> localDirectories

final long start = System.currentTimeMillis();
final Map<String, String> dirStarts = new HashMap<>();
EmissaryNode emissaryNode = new EmissaryNode();
EmissaryNode emissaryNode = EmissaryServer.getInstance().getNode();
for (final String thePlaceLocation : hostParameters) {

final String host = placeHost(thePlaceLocation);
Expand Down
43 changes: 16 additions & 27 deletions src/main/java/emissary/command/ServerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

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

Expand All @@ -27,14 +26,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")
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 = 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 @@ -58,7 +52,7 @@ public int getDefaultPort() {
return DEFAULT_PORT;
}

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

Expand Down Expand Up @@ -87,20 +81,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 @@ -122,12 +111,12 @@ public void setupServer() throws EmissaryException {

@Override
protected void startService() {
try {
LOG.info("Running Emissary Server");
new EmissaryServer(this).startServer();
} catch (EmissaryException e) {
LOG.error("Unable to start server", e);
}
// try {
LOG.info("Running Emissary Server");
EmissaryServer.init(this).startServer();
// } catch (EmissaryException e) {
// LOG.error("Unable to start server", e);
// }
}

@Override
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);
}
}
}
21 changes: 0 additions & 21 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.

31 changes: 17 additions & 14 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 All @@ -197,14 +200,14 @@ public Configurator getNodeConfigurator() throws IOException {
}

/**
* Get the peer configuration stream for this noed
* Get the peer configuration stream for this node
*/
public Configurator getPeerConfigurator() throws IOException {
if (isStandalone()) {
// return a configurator here with just standalone, don't actually read the peer.cfg
// This is a hack until we can TODO: refactor all this so standalone doesn't need peers
// maybe even warn if there is a peer.cfg
logger.debug("Node is standalone, ignoring any peer.cfg and only constructing one rendevous peer with the local node");
logger.debug("Node is standalone, ignoring any peer.cfg and only constructing one rendezvous peer with the local node");
Configurator cfg = new ServiceConfigGuide();
cfg.addEntry("RENDEZVOUS_PEER", this.asUrlKey());
return cfg;
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/emissary/pickup/WorkSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ public WorkSpace(FeedCommand feedCommand) {
this.outbound = new PriorityQueue<>(11, this.feedCommand.getSort());
}

configure();
startJetty();
register();
initializeService();
}

protected void startJetty() {
if (!EmissaryServer.isStarted()) {
if (!EmissaryServer.isInitialized() || !EmissaryServer.getInstance().isServerRunning()) {
// TODO investigate passing the feedCommand object directly to the serverCommand
List<String> args = new ArrayList<>();
args.add("-b");
Expand All @@ -224,7 +224,7 @@ protected void startJetty() {
try {
// To ensure the feed command starts correctly, depends on a node-{feedCommand.getPort}.cfg file
ServerCommand cmd = BaseCommand.parse(ServerCommand.class, args);
Server server = new EmissaryServer(cmd).startServer();
Server server = EmissaryServer.init(cmd).startServer();
final boolean jettyStatus = server.isStarted();
if (!jettyStatus) {
logger.error("Cannot start the Workspace due to EmissaryServer not starting!");
Expand Down Expand Up @@ -293,13 +293,10 @@ public void stop() {
public void shutDown() {
stop();
if (this.jettyStartedHere) {
final EmissaryNode node = new EmissaryNode();
final EmissaryNode node = EmissaryServer.getInstance().getNode();
if (node.isValid()) {
try {
final EmissaryServer s = EmissaryServer.lookup();
s.getServer().stop();
} catch (NamespaceException ex) {
logger.error("Cannot find jetty server", ex);
EmissaryServer.getInstance().stop();
} catch (Exception ex) {
logger.error("Jetty cannot be shutdown", ex);
}
Expand Down Expand Up @@ -523,8 +520,8 @@ public void setPattern(@Nullable final String thePattern) throws Exception {
/**
* Configure the Processor. The *.cfg file is optional
*/
protected void configure() {
final EmissaryNode node = new EmissaryNode();
protected void register() {
final EmissaryNode node = EmissaryServer.getInstance().getNode();
if (node.isValid()) {
this.workSpaceUrl = node.getNodeScheme() + "://" + node.getNodeName() + ":" + node.getNodePort() + "/" + this.WORK_SPACE_NAME;
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/emissary/place/ServiceProviderPlace.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,14 @@ protected void setupPlace(@Nullable String theDir, String placeLocation) throws
* @return true if it worked
*/
private boolean localizeDirectory(@Nullable String theDir) {
// Get a local (non proxy) copy of the directory if possible!
// Get a local (non-proxy) copy of the directory if possible!
// Looking up both if nothing is provided
if (theDir == null) {
try {
localDirPlace = DirectoryPlace.lookup();
dirPlace = localDirPlace.toString();
} catch (EmissaryException ex) {
if (EmissaryServer.exists() && !(this instanceof DirectoryPlace)) {
if (EmissaryServer.getInstance().isServerRunning() && !(this instanceof DirectoryPlace)) {
logger.warn("Unable to find DirectoryPlace in local namespace", ex);
return false;
}
Expand Down Expand Up @@ -422,7 +422,7 @@ protected void configureServicePlace(@Nullable String placeLocation) throws IOEx
keys.add(placeLocation); // save as first in list
locationPart = KeyManipulator.getServiceLocation(placeLocation);
} else if (!placeLocation.contains("://")) {
EmissaryNode node = new EmissaryNode();
EmissaryNode node = EmissaryServer.getInstance().getNode();
locationPart = "http://" + node.getNodeName() + ":" + node.getNodePort() + "/" + placeLocation;
}

Expand Down
Loading