Skip to content

Commit

Permalink
[grid] Add support for configs to be from TOML files
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Mar 30, 2020
1 parent d1b63a3 commit 0173ece
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 1 deletion.
1 change: 1 addition & 0 deletions java/maven_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def selenium_java_deps():
"io.opentelemetry:opentelemetry-exporters-inmemory:%s" % opentelemetry_version,
"io.opentelemetry:opentelemetry-exporters-logging:%s" % opentelemetry_version,
"io.opentelemetry:opentelemetry-sdk:%s" % opentelemetry_version,
"io.ous:jtoml:2.0.0",
"it.ozimov:embedded-redis:0.7.2",
"javax.servlet:javax.servlet-api:3.1.0",
maven.artifact(
Expand Down
38 changes: 37 additions & 1 deletion java/maven_install.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependency_tree": {
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": -306069303,
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": -1465542858,
"conflict_resolution": {},
"dependencies": [
{
Expand Down Expand Up @@ -2149,6 +2149,42 @@
"sha256": "76804889ff788c1612256289eecbb09cdc955e908fc210e138448c2fc2bbb346",
"url": "https://repo1.maven.org/maven2/io/opentelemetry/opentelemetry-sdk/0.2.4/opentelemetry-sdk-0.2.4-sources.jar"
},
{
"coord": "io.ous:jtoml:2.0.0",
"dependencies": [],
"directDependencies": [],
"exclusions": [
"org.hamcrest:hamcrest-all",
"org.hamcrest:hamcrest-core",
"io.netty:netty-all"
],
"file": "v1/https/repo1.maven.org/maven2/io/ous/jtoml/2.0.0/jtoml-2.0.0.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/io/ous/jtoml/2.0.0/jtoml-2.0.0.jar",
"https://jcenter.bintray.com/io/ous/jtoml/2.0.0/jtoml-2.0.0.jar",
"https://maven.google.com/io/ous/jtoml/2.0.0/jtoml-2.0.0.jar"
],
"sha256": "3cabdae2244c999addebb8c31ae452fbdc874b4f26a163539954b8eeb5d6acc6",
"url": "https://repo1.maven.org/maven2/io/ous/jtoml/2.0.0/jtoml-2.0.0.jar"
},
{
"coord": "io.ous:jtoml:jar:sources:2.0.0",
"dependencies": [],
"directDependencies": [],
"exclusions": [
"org.hamcrest:hamcrest-all",
"org.hamcrest:hamcrest-core",
"io.netty:netty-all"
],
"file": "v1/https/repo1.maven.org/maven2/io/ous/jtoml/2.0.0/jtoml-2.0.0-sources.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/io/ous/jtoml/2.0.0/jtoml-2.0.0-sources.jar",
"https://jcenter.bintray.com/io/ous/jtoml/2.0.0/jtoml-2.0.0-sources.jar",
"https://maven.google.com/io/ous/jtoml/2.0.0/jtoml-2.0.0-sources.jar"
],
"sha256": "f479f2acdf7a362dc86a5c9310ddaec7b34a87f0a8a6f46dde41c1069b2a2138",
"url": "https://repo1.maven.org/maven2/io/ous/jtoml/2.0.0/jtoml-2.0.0-sources.jar"
},
{
"coord": "io.projectreactor:reactor-core:3.3.2.RELEASE",
"dependencies": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ java_library(
],
deps = [
artifact("com.google.guava:guava"),
artifact("io.ous:jtoml"),
],
)
88 changes: 88 additions & 0 deletions java/server/src/org/openqa/selenium/grid/config/TomlConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.config;

import com.google.common.collect.ImmutableList;
import io.ous.jtoml.JToml;
import io.ous.jtoml.Toml;
import io.ous.jtoml.TomlTable;

import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class TomlConfig implements Config {

private final Toml toml;

public TomlConfig(Reader reader) {
try {
toml = JToml.parse(reader);
} catch (IOException e) {
throw new ConfigException("Unable to read TOML.", e);
}
}

public static Config from(Path path) {
Objects.requireNonNull(path, "Path to read must be set.");

try (Reader reader = Files.newBufferedReader(path)) {
return new TomlConfig(reader);
} catch (IOException e) {
throw new ConfigException(String.format("Unable to parse: %s", path), e);
}
}

@Override
public Optional<List<String>> getAll(String section, String option) {
Objects.requireNonNull(section, "Section to read must be set.");
Objects.requireNonNull(option, "Option to read must be set.");

if (!toml.containsKey(section)) {
return Optional.empty();
}

Object raw = toml.get(section);
if (!(raw instanceof TomlTable)) {
throw new ConfigException(String.format("Section %s is not a section! %s", section, raw));
}

TomlTable table = toml.getTomlTable(section);

Object value = table.getOrDefault(option, null);
if (value == null) {
return Optional.empty();
}

if (value instanceof Collection) {
ImmutableList<String> values = ((Collection<?>) value).stream()
.filter(item -> (!(item instanceof Collection)))
.map(String::valueOf)
.collect(ImmutableList.toImmutableList());

return Optional.of(values);
}

return Optional.of(ImmutableList.of(String.valueOf(value)));
}
}
2 changes: 2 additions & 0 deletions java/server/test/org/openqa/selenium/grid/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ java_test_suite(
"//java/server/src/org/openqa/selenium/grid/config",
artifact("com.beust:jcommander"),
artifact("com.google.guava:guava"),
artifact("io.ous:jtoml"),
artifact("org.assertj:assertj-core"),
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.config;

import org.junit.Test;

import java.io.StringReader;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

public class TomlConfigTest {

@Test
public void shouldUseATableAsASection() {
String raw = "[cheeses]\nselected=brie";
Config config = new TomlConfig(new StringReader(raw));

assertThat(config.get("cheeses", "selected")).isEqualTo(Optional.of("brie"));
}
}

0 comments on commit 0173ece

Please sign in to comment.