From d47e74d6f220efaed486ed89dfcb97e54683ad9e Mon Sep 17 00:00:00 2001 From: Greg Fraley Date: Sun, 10 Feb 2019 17:20:18 +0100 Subject: [PATCH] [grid] Implement deep copy on NodeJsonConfiguration Signed-off-by: Diego Molina --- .../configuration/GridNodeConfiguration.java | 40 +++++++++++-------- .../json/CommonJsonConfiguration.java | 13 ++++++ .../json/GridJsonConfiguration.java | 11 +++++ .../json/NodeJsonConfiguration.java | 26 +++++++++++- 4 files changed, 72 insertions(+), 18 deletions(-) diff --git a/java/server/src/org/openqa/grid/internal/utils/configuration/GridNodeConfiguration.java b/java/server/src/org/openqa/grid/internal/utils/configuration/GridNodeConfiguration.java index e9ed714acbcbc..8e0444b5710ba 100644 --- a/java/server/src/org/openqa/grid/internal/utils/configuration/GridNodeConfiguration.java +++ b/java/server/src/org/openqa/grid/internal/utils/configuration/GridNodeConfiguration.java @@ -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; @@ -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); @@ -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; } diff --git a/java/server/src/org/openqa/grid/internal/utils/configuration/json/CommonJsonConfiguration.java b/java/server/src/org/openqa/grid/internal/utils/configuration/json/CommonJsonConfiguration.java index bab3e6ad98b3f..96217c02d633e 100644 --- a/java/server/src/org/openqa/grid/internal/utils/configuration/json/CommonJsonConfiguration.java +++ b/java/server/src/org/openqa/grid/internal/utils/configuration/json/CommonJsonConfiguration.java @@ -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; diff --git a/java/server/src/org/openqa/grid/internal/utils/configuration/json/GridJsonConfiguration.java b/java/server/src/org/openqa/grid/internal/utils/configuration/json/GridJsonConfiguration.java index 45b733099c6dd..c949ce5329c45 100644 --- a/java/server/src/org/openqa/grid/internal/utils/configuration/json/GridJsonConfiguration.java +++ b/java/server/src/org/openqa/grid/internal/utils/configuration/json/GridJsonConfiguration.java @@ -27,6 +27,17 @@ public abstract class GridJsonConfiguration extends CommonJsonConfiguration { private Map custom = new HashMap<>(); private List servlets = new ArrayList<>(); private List 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. diff --git a/java/server/src/org/openqa/grid/internal/utils/configuration/json/NodeJsonConfiguration.java b/java/server/src/org/openqa/grid/internal/utils/configuration/json/NodeJsonConfiguration.java index e779c84e5ca76..7e8681613863a 100644 --- a/java/server/src/org/openqa/grid/internal/utils/configuration/json/NodeJsonConfiguration.java +++ b/java/server/src/org/openqa/grid/internal/utils/configuration/json/NodeJsonConfiguration.java @@ -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); @@ -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;