Skip to content

Commit 517b8b4

Browse files
committed
[grid] Fixing hub and node configuration loading from json file
To be able to read hub, hubHost and hubPort from json file we need to know what of there properties were read, and what are not. It is impossible if InstanceCoercer keeps default values in non-read fields. So I've changed it to fill non-read fields with null values. No test were broken, but it can break Simon's intention. In this case we have to parametrize InstanceCoercer to either keep default values or write nulls to non-read fields.
1 parent bab2de3 commit 517b8b4

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

java/client/src/org/openqa/selenium/json/InstanceCoercer.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.reflect.Modifier;
2424
import java.lang.reflect.ParameterizedType;
2525
import java.lang.reflect.Type;
26+
import java.util.ArrayList;
2627
import java.util.Arrays;
2728
import java.util.LinkedList;
2829
import java.util.List;
@@ -71,6 +72,8 @@ public BiFunction<JsonInput, PropertySetting, Object> apply(Type type) {
7172

7273
jsonInput.beginObject();
7374

75+
List<TypeAndWriter> usedWriters = new ArrayList<>();
76+
7477
while (jsonInput.hasNext()) {
7578
String key = jsonInput.nextName();
7679

@@ -79,13 +82,24 @@ public BiFunction<JsonInput, PropertySetting, Object> apply(Type type) {
7982
jsonInput.skipValue();
8083
continue;
8184
}
85+
usedWriters.add(writer);
8286

8387
Object value = coercer.coerce(jsonInput, writer.type, setter);
8488
writer.writer.accept(instance, value);
8589
}
8690

8791
jsonInput.endObject();
8892

93+
// all unset fields should be set to null
94+
allWriters.values().stream()
95+
.filter(w -> ! usedWriters.contains(w))
96+
.forEach(w -> {
97+
try {
98+
w.writer.accept(instance, null);
99+
} catch (IllegalArgumentException ignore) {
100+
}
101+
});
102+
89103
return instance;
90104
} catch (ReflectiveOperationException e) {
91105
throw new JsonException(e);
@@ -145,6 +159,18 @@ private Map<String, TypeAndWriter> getBeanWriters(Constructor<?> constructor) {
145159
}
146160

147161
private Constructor<?> getConstructor(Type type) {
162+
Class target = getClss(type);
163+
164+
try {
165+
@SuppressWarnings("unchecked") Constructor<?> constructor = target.getConstructor();
166+
constructor.setAccessible(true);
167+
return constructor;
168+
} catch (ReflectiveOperationException e) {
169+
throw new JsonException(e);
170+
}
171+
}
172+
173+
private Class getClss(Type type) {
148174
Class target = null;
149175

150176
if (type instanceof Class) {
@@ -159,14 +185,7 @@ private Constructor<?> getConstructor(Type type) {
159185
if (target == null) {
160186
throw new JsonException("Cannot determine base class");
161187
}
162-
163-
try {
164-
@SuppressWarnings("unchecked") Constructor<?> constructor = target.getConstructor();
165-
constructor.setAccessible(true);
166-
return constructor;
167-
} catch (ReflectiveOperationException e) {
168-
throw new JsonException(e);
169-
}
188+
return target;
170189
}
171190

172191
private class TypeAndWriter {

java/server/src/org/openqa/grid/internal/utils/configuration/GridHubConfiguration.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
package org.openqa.grid.internal.utils.configuration;
1919

20+
import org.openqa.grid.common.exception.GridConfigurationException;
2021
import org.openqa.grid.internal.listeners.Prioritizer;
2122
import org.openqa.grid.internal.utils.CapabilityMatcher;
2223
import org.openqa.grid.internal.utils.DefaultCapabilityMatcher;
24+
import org.openqa.selenium.json.JsonInput;
2325

2426
import java.util.Map;
2527

@@ -110,7 +112,28 @@ public GridHubConfiguration() {
110112
* @param filePath hub config json file to load configuration from
111113
*/
112114
public static GridHubConfiguration loadFromJSON(String filePath) {
113-
return StandaloneConfiguration.loadFromJson(filePath, GridHubConfiguration.class);
115+
return loadFromJSON(StandaloneConfiguration.loadJsonFromResourceOrFile(filePath));
116+
}
117+
118+
public static GridHubConfiguration loadFromJSON(JsonInput jsonInput) {
119+
try {
120+
GridHubConfiguration config = StandaloneConfiguration.loadFromJson(
121+
jsonInput,
122+
GridHubConfiguration.class);
123+
124+
GridHubConfiguration result = new GridHubConfiguration();
125+
result.merge(config);
126+
// copy non-mergeable fields
127+
if (config.host != null) {
128+
result.host = config.host;
129+
}
130+
if (config.port != null) {
131+
result.port = config.port;
132+
}
133+
return result;
134+
} catch (Throwable e) {
135+
throw new GridConfigurationException("Error with the JSON of the config : " + e.getMessage(), e);
136+
}
114137
}
115138

116139
/**

java/server/src/org/openqa/grid/internal/utils/configuration/GridNodeConfiguration.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,21 @@ public static GridNodeConfiguration loadFromJSON(JsonInput jsonInput) {
403403
" the file to work with Selenium 3.See https://github.com" +
404404
"/SeleniumHQ/selenium/wiki/Grid2#configuring-the-nodes-by-json" +
405405
" for more details.");
406-
}
406+
}
407407

408-
return config;
408+
GridNodeConfiguration result = new GridNodeConfiguration();
409+
result.merge(config);
410+
if (config.hub == null && (config.hubHost != null || config.hubPort != null)) {
411+
result.hub = String.format("http://%s:%s", config.getHubHost(), config.getHubPort());
412+
}
413+
// copy non-mergeable fields
414+
if (config.host != null) {
415+
result.host = config.host;
416+
}
417+
if (config.port != null) {
418+
result.port = config.port;
419+
}
420+
return result;
409421
} catch (Throwable e) {
410422
throw new GridConfigurationException("Error with the JSON of the config : " + e.getMessage(),
411423
e);

java/server/test/org/openqa/grid/internal/utils/configuration/GridHubConfigurationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void testLoadFromJson() throws IOException {
102102

103103
try (Reader reader = new StringReader("{ \"host\": \"dummyhost\", \"port\": 1234 }");
104104
JsonInput jsonInput = new Json().newInput(reader)) {
105-
ghc = GridHubConfiguration.loadFromJson(jsonInput, GridHubConfiguration.class);
105+
ghc = GridHubConfiguration.loadFromJSON(jsonInput);
106106
}
107107

108108
assertEquals("hub", ghc.role);

0 commit comments

Comments
 (0)