Skip to content

Commit

Permalink
[grid] Add a flag to allow the config to be dumped to the console
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Mar 30, 2020
1 parent 2f4cf5b commit 6371c31
Show file tree
Hide file tree
Showing 17 changed files with 291 additions and 13 deletions.
4 changes: 4 additions & 0 deletions java/server/src/org/openqa/selenium/grid/commands/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public Executable configure(String... args) {
configFlags.readConfigFiles(),
new DefaultHubConfig());

if (help.dumpConfig(config, System.out)) {
return;
}

LoggingOptions loggingOptions = new LoggingOptions(config);
loggingOptions.configureLogging();
Tracer tracer = loggingOptions.getTracer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public Executable configure(String... args) {
"server", ImmutableMap.of(
"port", 5557))));

if (help.dumpConfig(config, System.out)) {
return;
}

LoggingOptions loggingOptions = new LoggingOptions(config);
loggingOptions.configureLogging();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public Executable configure(String... args) {
new AnnotatedConfig(standaloneFlags),
new DefaultStandaloneConfig());

if (help.dumpConfig(config, System.out)) {
return;
}

LoggingOptions loggingOptions = new LoggingOptions(config);
loggingOptions.configureLogging();
Tracer tracer = loggingOptions.getTracer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.openqa.selenium.grid.config;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;

import java.lang.reflect.Field;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -159,4 +161,15 @@ public Optional<List<String>> getAll(String section, String option) {

return Optional.of(ImmutableList.copyOf(values));
}

@Override
public Set<String> getSectionNames() {
return ImmutableSortedSet.copyOf(config.keySet());
}

@Override
public Set<String> getOptions(String section) {
Objects.requireNonNull(section, "Section name to get options for must be set.");
return ImmutableSortedSet.copyOf(config.getOrDefault(section, ImmutableMap.of()).keySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static java.util.Comparator.naturalOrder;

public class CompoundConfig implements Config {

Expand All @@ -44,12 +47,30 @@ public Optional<List<String>> getAll(String section, String option) {
Objects.requireNonNull(option, "Option name not set");

List<String> values = allConfigs.stream()
.map(config -> config.getAll(section, option))
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(Collection::stream)
.collect(toImmutableList());
.map(config -> config.getAll(section, option))
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(Collection::stream)
.collect(toImmutableList());

return values.isEmpty() ? Optional.empty() : Optional.of(values);
}

@Override
public Set<String> getSectionNames() {
return allConfigs.stream()
.map(Config::getSectionNames)
.flatMap(Collection::stream)
.collect(toImmutableSortedSet(naturalOrder()));
}

@Override
public Set<String> getOptions(String section) {
Objects.requireNonNull(section, "Section name to get options for must be set.");

return allConfigs.stream()
.map(config -> config.getOptions(section))
.flatMap(Collection::stream)
.collect(toImmutableSortedSet(naturalOrder()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;

import java.util.AbstractMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static java.util.Comparator.naturalOrder;

public class ConcatenatingConfig implements Config {

Expand Down Expand Up @@ -58,4 +63,32 @@ public Optional<List<String>> getAll(String section, String option) {
.findFirst()
.map(ImmutableList::of);
}

@Override
public Set<String> getSectionNames() {
String actualPrefix = prefix.toLowerCase(Locale.ENGLISH);

return values.keySet().stream()
.filter(key -> key.toLowerCase(Locale.ENGLISH).startsWith(actualPrefix))
.filter(key -> key.length() > (actualPrefix.length() + 1))
.map(key -> key.substring(actualPrefix.length()))
.filter(key -> key.indexOf(separator) > -1)
.map(key -> key.substring(0, key.indexOf(separator)))
.map(key -> key.toLowerCase(Locale.ENGLISH))
.collect(ImmutableSortedSet.toImmutableSortedSet(naturalOrder()));
}

@Override
public Set<String> getOptions(String section) {
Objects.requireNonNull(section, "Section name to get options for must be set.");

String actualPrefix = String.format("%s%s_", prefix, section).toLowerCase(Locale.ENGLISH);

return values.keySet().stream()
.filter(key -> key.toLowerCase(Locale.ENGLISH).startsWith(actualPrefix))
.filter(key -> key.length() > actualPrefix.length() + 1)
.map(key -> key.substring(actualPrefix.length()))
.map(key -> key.toLowerCase(Locale.ENGLISH))
.collect(ImmutableSortedSet.toImmutableSortedSet(naturalOrder()));
}
}
5 changes: 5 additions & 0 deletions java/server/src/org/openqa/selenium/grid/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@

import java.util.List;
import java.util.Optional;
import java.util.Set;

public interface Config {

Set<String> getSectionNames();

Set<String> getOptions(String section);

Optional<List<String>> getAll(String section, String option);

default Optional<String> get(String section, String option) {
Expand Down
32 changes: 31 additions & 1 deletion java/server/src/org/openqa/selenium/grid/config/EnvConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@

package org.openqa.selenium.grid.config;

import com.google.common.collect.Comparators;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;

import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static java.util.Comparator.naturalOrder;

/**
* Exposes environment variables as config settings by mapping
Expand All @@ -38,7 +46,7 @@ public Optional<List<String>> getAll(String section, String option) {
Objects.requireNonNull(option, "Option name not set");

String key = String.format("%s_%s", section, option)
.toUpperCase(Locale.US)
.toUpperCase(Locale.ENGLISH)
.replace("-", "_")
.replace(".", "_");

Expand All @@ -53,4 +61,26 @@ public Optional<List<String>> getAll(String section, String option) {

return Optional.ofNullable(value).map(ImmutableList::of);
}

@Override
public Set<String> getSectionNames() {
return System.getenv().keySet().stream()
// We need at least two "_" characters
.filter(key -> key.split("_").length > 1)
.map(key -> key.substring(0, key.indexOf("_")))
.map(key -> key.toLowerCase(Locale.ENGLISH))
.collect(toImmutableSortedSet(naturalOrder()));
}

@Override
public Set<String> getOptions(String section) {
Objects.requireNonNull(section, "Section name to get options for must be set.");

String prefix = String.format("%s_", section).toUpperCase(Locale.ENGLISH);
return System.getenv().keySet().stream()
.filter(key -> key.startsWith(prefix))
.map(key -> key.substring(prefix.length()))
.map(key -> key.toLowerCase(Locale.ENGLISH))
.collect(toImmutableSortedSet(naturalOrder()));
}
}
13 changes: 13 additions & 0 deletions java/server/src/org/openqa/selenium/grid/config/JsonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static org.openqa.selenium.json.Json.MAP_TYPE;

Expand Down Expand Up @@ -57,4 +58,16 @@ public static Config from(Path path) {
public Optional<List<String>> getAll(String section, String option) {
return delegate.getAll(section, option);
}

@Override
public Set<String> getSectionNames() {
return delegate.getSectionNames();
}

@Override
public Set<String> getOptions(String section) {
Objects.requireNonNull(section, "Section name to get options for must be set.");

return delegate.getOptions(section);
}
}
42 changes: 37 additions & 5 deletions java/server/src/org/openqa/selenium/grid/config/MapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,63 @@
package org.openqa.selenium.grid.config;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

public class MapConfig implements Config {

private final Map<String, Object> raw;
private final Map<String, Map<String, Object>> raw;

public MapConfig(Map<String, Object> raw) {
this.raw = raw;
Objects.requireNonNull(raw, "Underlying map must be set.");

ImmutableMap.Builder<String, Map<String, Object>> builder = ImmutableMap.builder();
for (Map.Entry<String, Object> entry : raw.entrySet()) {
if (!(entry.getValue() instanceof Map)) {
continue;
}

ImmutableMap<String, Object> values = ((Map<?, ?>) entry.getValue()).entrySet().stream()
.filter(e -> e.getKey() instanceof String)
.collect(ImmutableMap.toImmutableMap(e -> String.valueOf(e.getKey()), Map.Entry::getValue));

builder.put(entry.getKey(), values);
}

this.raw = builder.build();
}

@Override
public Optional<List<String>> getAll(String section, String option) {
Objects.requireNonNull(section, "Section name not set");
Objects.requireNonNull(option, "Option name not set");

Object rawSection = raw.get(section);
if (!(rawSection instanceof Map)) {
Map<String, Object> rawSection = raw.get(section);
if (rawSection == null) {
return Optional.empty();
}

Object value = ((Map<?, ?>) rawSection).get(option);
Object value = rawSection.get(option);
return value == null ? Optional.empty() : Optional.of(ImmutableList.of(String.valueOf(value)));
}

@Override
public Set<String> getSectionNames() {
return ImmutableSet.copyOf(raw.keySet());
}

@Override
public Set<String> getOptions(String section) {
Objects.requireNonNull(section, "Section name to get options for must be set.");

Map<String, Object> values = raw.getOrDefault(section, ImmutableMap.of());
return ImmutableSortedSet.copyOf(values.keySet());
}
}
19 changes: 19 additions & 0 deletions java/server/src/org/openqa/selenium/grid/config/TomlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.grid.config;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;
import io.ous.jtoml.JToml;
import io.ous.jtoml.Toml;
import io.ous.jtoml.TomlTable;
Expand All @@ -30,6 +31,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

public class TomlConfig implements Config {

Expand Down Expand Up @@ -85,4 +87,21 @@ public Optional<List<String>> getAll(String section, String option) {

return Optional.of(ImmutableList.of(String.valueOf(value)));
}

@Override
public Set<String> getSectionNames() {
return ImmutableSortedSet.copyOf(toml.keySet());
}

@Override
public Set<String> getOptions(String section) {
Objects.requireNonNull(section, "Section name to get options for must be set.");

Object raw = toml.get(section);
if (!(raw instanceof TomlTable)) {
return ImmutableSortedSet.of();
}

return ImmutableSortedSet.copyOf(((TomlTable) raw).keySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public Executable configure(String... args) {
configFlags.readConfigFiles(),
new DefaultDistributorConfig());

if (help.dumpConfig(config, System.out)) {
return;
}

LoggingOptions loggingOptions = new LoggingOptions(config);
loggingOptions.configureLogging();
Tracer tracer = loggingOptions.getTracer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ public Executable configure(String... args) {
configFlags.readConfigFiles(),
new DefaultNodeConfig());

if (help.dumpConfig(config, System.out)) {
return;
}

LoggingOptions loggingOptions = new LoggingOptions(config);
loggingOptions.configureLogging();
Tracer tracer = loggingOptions.getTracer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public Executable configure(String... args) {
configFlags.readConfigFiles(),
new MapConfig(ImmutableMap.of("server", ImmutableMap.of("port", 4444))));

if (help.dumpConfig(config, System.out)) {
return;
}

LoggingOptions loggingOptions = new LoggingOptions(config);
loggingOptions.configureLogging();
Tracer tracer = loggingOptions.getTracer();
Expand Down
Loading

0 comments on commit 6371c31

Please sign in to comment.