Skip to content

Commit

Permalink
[grid] Implement deep copy on NodeJsonConfiguration
Browse files Browse the repository at this point in the history
Signed-off-by: Diego Molina <[email protected]>
  • Loading branch information
gsfraley authored and diemol committed Feb 25, 2019
1 parent 0911c88 commit d47e74d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ private static class HostPort {
this.port = port;
}
}

private static NodeJsonConfiguration getDefaultConfigFromJson() {
synchronized(DEFAULT_CONFIG_FROM_JSON) {
return new NodeJsonConfiguration(DEFAULT_CONFIG_FROM_JSON);
}
}

private HostPort hubHostPort;

Expand Down Expand Up @@ -161,47 +167,48 @@ private static class HostPort {
* Creates a new configuration using the default values.
*/
public GridNodeConfiguration() {
this(DEFAULT_CONFIG_FROM_JSON);
this(getDefaultConfigFromJson());
}

public GridNodeConfiguration(NodeJsonConfiguration jsonConfig) {
super(jsonConfig);
NodeJsonConfiguration defaultConfig = getDefaultConfigFromJson();
role = ROLE;
capabilities = new ArrayList<>(ofNullable(jsonConfig.getCapabilities())
.orElse(DEFAULT_CONFIG_FROM_JSON.getCapabilities()));
.orElse(defaultConfig.getCapabilities()));
maxSession = ofNullable(jsonConfig.getMaxSession())
.orElse(DEFAULT_CONFIG_FROM_JSON.getMaxSession());
.orElse(defaultConfig.getMaxSession());
register = ofNullable(jsonConfig.getRegister())
.orElse(DEFAULT_CONFIG_FROM_JSON.getRegister());
.orElse(defaultConfig.getRegister());
registerCycle = ofNullable(jsonConfig.getRegisterCycle())
.orElse(DEFAULT_CONFIG_FROM_JSON.getRegisterCycle());
.orElse(defaultConfig.getRegisterCycle());
nodeStatusCheckTimeout = ofNullable(jsonConfig.getNodeStatusCheckTimeout())
.orElse(DEFAULT_CONFIG_FROM_JSON.getNodeStatusCheckTimeout());
.orElse(defaultConfig.getNodeStatusCheckTimeout());
nodePolling = ofNullable(jsonConfig.getNodePolling())
.orElse(DEFAULT_CONFIG_FROM_JSON.getNodePolling());
.orElse(defaultConfig.getNodePolling());
unregisterIfStillDownAfter = ofNullable(jsonConfig.getUnregisterIfStillDownAfter())
.orElse(DEFAULT_CONFIG_FROM_JSON.getUnregisterIfStillDownAfter());
.orElse(defaultConfig.getUnregisterIfStillDownAfter());
downPollingLimit = ofNullable(jsonConfig.getDownPollingLimit())
.orElse(DEFAULT_CONFIG_FROM_JSON.getDownPollingLimit());
.orElse(defaultConfig.getDownPollingLimit());
proxy = ofNullable(jsonConfig.getProxy())
.orElse(DEFAULT_CONFIG_FROM_JSON.getProxy());
.orElse(defaultConfig.getProxy());
enablePlatformVerification = jsonConfig.isEnablePlatformVerification();
if (jsonConfig.getHub() != null) {
// -hub has precedence
hub = jsonConfig.getHub();

} else if (jsonConfig.getHubHost() != null && jsonConfig.getHubPort() != null) {
hubHost = ofNullable(jsonConfig.getHubHost()).orElse(DEFAULT_CONFIG_FROM_JSON.getHubHost());
hubPort = ofNullable(jsonConfig.getHubPort()).orElse(DEFAULT_CONFIG_FROM_JSON.getHubPort());
hubHost = ofNullable(jsonConfig.getHubHost()).orElse(defaultConfig.getHubHost());
hubPort = ofNullable(jsonConfig.getHubPort()).orElse(defaultConfig.getHubPort());

} else {
hub = DEFAULT_CONFIG_FROM_JSON.getHub();
hub = defaultConfig.getHub();
}
}

public GridNodeConfiguration(GridNodeCliOptions cliConfig) {
this(ofNullable(cliConfig.getConfigFile()).map(NodeJsonConfiguration::loadFromResourceOrFile)
.orElse(DEFAULT_CONFIG_FROM_JSON));
.orElse(getDefaultConfigFromJson()));
super.merge(cliConfig.getCommonGridOptions());
ofNullable(cliConfig.getCapabilities()).ifPresent(v -> capabilities = v);
ofNullable(cliConfig.getRegister()).ifPresent(v -> register = v);
Expand Down Expand Up @@ -242,8 +249,9 @@ private HostPort getHubHostPort() {
throw new RuntimeException("-hub must be a valid url: " + hub, mURLe);
}
} else if (hubHost != null || hubPort != null) {
hubHostPort = new HostPort(ofNullable(hubHost).orElse(DEFAULT_CONFIG_FROM_JSON.getHubHost()),
ofNullable(hubPort).orElse(DEFAULT_CONFIG_FROM_JSON.getHubPort()));
NodeJsonConfiguration defaultConfig = getDefaultConfigFromJson();
hubHostPort = new HostPort(ofNullable(hubHost).orElse(defaultConfig.getHubHost()),
ofNullable(hubPort).orElse(defaultConfig.getHubPort()));
}
return hubHostPort;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ private static Reader readFileOrResource(String source) {
private Integer timeout;
private Integer browserTimeout;
private Integer jettyMaxThreads;

public CommonJsonConfiguration() {}

public CommonJsonConfiguration(CommonJsonConfiguration commonJsonConfig) {
role = commonJsonConfig.role;
debug = commonJsonConfig.debug;
log = commonJsonConfig.log;
host = commonJsonConfig.host;
port = commonJsonConfig.port;
timeout = commonJsonConfig.timeout;
browserTimeout = commonJsonConfig.browserTimeout;
jettyMaxThreads = commonJsonConfig.jettyMaxThreads;
}

protected String getRole() {
return role;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ public abstract class GridJsonConfiguration extends CommonJsonConfiguration {
private Map<String, String> custom = new HashMap<>();
private List<String> servlets = new ArrayList<>();
private List<String> withoutServlets = new ArrayList<>();

public GridJsonConfiguration() {
super();
}

public GridJsonConfiguration(GridJsonConfiguration gridJsonConfig) {
super(gridJsonConfig);
custom = new HashMap<>(gridJsonConfig.custom);
servlets = new ArrayList<>(gridJsonConfig.servlets);
withoutServlets = new ArrayList<>(gridJsonConfig.withoutServlets);
}

/**
* Custom key/value pairs for the hub registry. Default empty.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

package org.openqa.grid.internal.utils.configuration.json;

import static java.util.Optional.ofNullable;

import org.openqa.grid.common.exception.GridConfigurationException;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.json.JsonInput;

import java.util.List;
import java.util.stream.Collectors;

public class NodeJsonConfiguration extends GridJsonConfiguration {

private NodeJsonConfiguration() {}

public static NodeJsonConfiguration loadFromJson(JsonInput source) {
NodeJsonConfiguration config = fromJson(source, NodeJsonConfiguration.class);

Expand Down Expand Up @@ -76,6 +77,27 @@ public static NodeJsonConfiguration loadFromResourceOrFile(String source) {
private Integer unregisterIfStillDownAfter;
private boolean enablePlatformVerification = true;

public NodeJsonConfiguration() {}

public NodeJsonConfiguration(NodeJsonConfiguration nodeJsonConfig) {
super(nodeJsonConfig);
hubHost = nodeJsonConfig.hubHost;
hubPort = nodeJsonConfig.hubPort;
id = nodeJsonConfig.id;
capabilities = ofNullable(nodeJsonConfig.capabilities).map(v -> v.stream()
.map(MutableCapabilities::new).collect(Collectors.toList())).orElse(null);
maxSession = nodeJsonConfig.maxSession;
downPollingLimit = nodeJsonConfig.downPollingLimit;
hub = nodeJsonConfig.hub;
nodePolling = nodeJsonConfig.nodePolling;
nodeStatusCheckTimeout = nodeJsonConfig.nodeStatusCheckTimeout;
proxy = nodeJsonConfig.proxy;
register = nodeJsonConfig.register;
registerCycle = nodeJsonConfig.registerCycle;
unregisterIfStillDownAfter = nodeJsonConfig.unregisterIfStillDownAfter;
enablePlatformVerification = nodeJsonConfig.enablePlatformVerification;
}

// used to read a Selenium 2.x nodeConfig.json file and throw a friendly exception
private Object configuration;

Expand Down

0 comments on commit d47e74d

Please sign in to comment.