Skip to content

Commit

Permalink
Adding the ability to configure things.
Browse files Browse the repository at this point in the history
Configuration is split into two separate layers. The first
are implementations of `Config`. These provide raw access
to the config options, and can be implemented any way that
people choose.

The second layer are app-specific views of the `Config`s
available. These allow all configuration options to be put
into one place, and easily accessed and reasoned about.
  • Loading branch information
shs96c committed Aug 7, 2018
1 parent 63ebe33 commit ee8e6d4
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 8 deletions.
1 change: 1 addition & 0 deletions java/BUCK
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# BUILD FILE SYNTAX: SKYLARK

export_file(
name = 'changelog',
out = "CHANGELOG",
Expand Down
12 changes: 12 additions & 0 deletions java/server/src/org/openqa/selenium/grid/config/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# BUILD FILE SYNTAX: SKYLARK

java_library(
name = "config",
srcs = glob(["*.java"]),
deps = [
"//third_party/java/guava:guava",
],
visibility = [
"//java/server/...",
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 java.util.List;
import java.util.Optional;

public class CompoundConfig implements Config {

private final List<Config> allConfigs;

public CompoundConfig(Config mostImportant, Config... othersInDescendingOrderOfImportance) {
this.allConfigs = ImmutableList.<Config>builder()
.add(mostImportant)
.add(othersInDescendingOrderOfImportance)
.build();
}

@Override
public Optional<String> get(String section, String option) {
return allConfigs.stream()
.map(config -> config.get(section, option))
.filter(Optional::isPresent)
.findFirst()
.orElse(Optional.empty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.ImmutableMap;

import java.util.AbstractMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public class ConcatenatingConfig implements Config {

private final Map<String, Object> values;

public ConcatenatingConfig(Map<?, ?> values) {
this.values = Objects.requireNonNull(values).entrySet().stream()
.peek(entry -> Objects.requireNonNull(entry.getKey(), "Key has not been set"))
.peek(entry -> Objects.requireNonNull(entry.getValue(), "Value has not been set"))
.map(entry -> new AbstractMap.SimpleImmutableEntry<>(
String.valueOf(entry.getKey()),
String.valueOf(entry.getValue())))
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
}

@Override
public Optional<String> get(String section, String option) {
return Optional.ofNullable(values.get(section + "." + option)).map(String::valueOf);
}
}
33 changes: 33 additions & 0 deletions java/server/src/org/openqa/selenium/grid/config/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 java.util.Optional;

public interface Config {

Optional<String> get(String section, String option);

default Optional<Integer> getInt(String section, String option) {
return get(section, option).map(Integer::parseInt);
}

default Optional<Boolean> getBool(String section, String option) {
return get(section, option).map(Boolean::parseBoolean);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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;

public class ConfigException extends RuntimeException {

public ConfigException(String message) {
super(message);
}
}
28 changes: 28 additions & 0 deletions java/server/src/org/openqa/selenium/grid/config/EnvConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 java.util.Optional;

public class EnvConfig implements Config {

@Override
public Optional<String> get(String section, String option) {
return Optional.ofNullable(System.getenv().get(section + "." + option));
}
}
41 changes: 41 additions & 0 deletions java/server/src/org/openqa/selenium/grid/config/MapConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 java.util.Map;
import java.util.Optional;

public class MapConfig implements Config {

private final Map<String, Object> raw;

public MapConfig(Map<String, Object> raw) {
this.raw = raw;
}

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

Object value = ((Map<?, ?>) rawSection).get(option);
return value == null ? Optional.empty() : Optional.of(String.valueOf(value));
}
}
2 changes: 2 additions & 0 deletions java/server/src/org/openqa/selenium/grid/server/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ java_library(
deps = [
"//java/client/src/org/openqa/selenium/remote:remote",
"//java/server/src/org/openqa/selenium/injector:injector",
"//java/server/src/org/openqa/selenium/grid/config:config",
"//java/server/src/org/openqa/selenium/grid/web:web",
"//third_party/java/beust:jcommander",
"//third_party/java/guava:guava",
"//third_party/java/jetty:jetty",
],
Expand Down
12 changes: 5 additions & 7 deletions java/server/src/org/openqa/selenium/grid/server/BaseServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.seleniumhq.jetty9.server.ServerConnector;
import org.seleniumhq.jetty9.servlet.ServletContextHandler;
import org.seleniumhq.jetty9.servlet.ServletHolder;
import org.seleniumhq.jetty9.util.thread.QueuedThreadPool;

import java.net.URL;
import java.util.LinkedHashMap;
Expand All @@ -54,13 +55,10 @@ public class BaseServer implements Server<BaseServer> {
private final Injector injector;
private URL url;

public BaseServer() {
this(0);
}

public BaseServer(int port) {
this.port = port;
this.server = new org.seleniumhq.jetty9.server.Server();
public BaseServer(BaseServerOptions options) {
this.port = options.getPort();
this.server = new org.seleniumhq.jetty9.server.Server(
new QueuedThreadPool(options.getMaxServerThreads()));

// Insertion order may matter
this.handlers = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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.server;

import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.ConfigException;

public class BaseServerOptions {

private final Config config;

public BaseServerOptions(Config config) {
this.config = config;
}

public int getPort() {
int port = config.getInt("server", "port")
.orElse(0);

if (port < 0) {
throw new ConfigException("Port cannot be less than 0: " + port);
}

return port;
}

public int getMaxServerThreads() {
int count = config.getInt("server", "max-threads")
.orElse(Runtime.getRuntime().availableProcessors() * 3);

if (count < 0) {
throw new ConfigException("Maximum number of server threads cannot be less than 0: " + count);
}

return count;
}
}
50 changes: 50 additions & 0 deletions java/server/src/org/openqa/selenium/grid/server/CommonConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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.server;

import com.beust.jcommander.Parameter;

import org.openqa.selenium.grid.config.Config;

import java.util.Optional;

public class CommonConfig implements Config {

@Parameter(description = "Port to listen on", names = {"-p", "--port"})
private int port = 0;

@Parameter(description = "Maximum number of listener threads", names = "--max-threads")
private int maxThreads = Runtime.getRuntime().availableProcessors() * 3;

@Override
public Optional<String> get(String section, String option) {
switch (section) {
case "server":
switch (option) {
case "max-threads":
return Optional.of(String.valueOf(maxThreads));

case "port":
return Optional.of(String.valueOf(port));
}
break;
}

return Optional.empty();
}
}
14 changes: 14 additions & 0 deletions java/server/test/org/openqa/selenium/grid/config/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# BUILD FILE SYNTAX: SKYLARK

java_test(
name = "config",
srcs = glob(["*.java"]),
labels = [
"small",
],
deps = [
"//java/server/src/org/openqa/selenium/grid/config:config",
"//third_party/java/guava:guava",
"//third_party/java/junit:junit",
],
)
Loading

0 comments on commit ee8e6d4

Please sign in to comment.