-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the ability to configure things.
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
Showing
17 changed files
with
411 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# BUILD FILE SYNTAX: SKYLARK | ||
|
||
export_file( | ||
name = 'changelog', | ||
out = "CHANGELOG", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/...", | ||
] | ||
) |
44 changes: 44 additions & 0 deletions
44
java/server/src/org/openqa/selenium/grid/config/CompoundConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
java/server/src/org/openqa/selenium/grid/config/ConcatenatingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
java/server/src/org/openqa/selenium/grid/config/Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
java/server/src/org/openqa/selenium/grid/config/ConfigException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
java/server/src/org/openqa/selenium/grid/config/EnvConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
java/server/src/org/openqa/selenium/grid/config/MapConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
java/server/src/org/openqa/selenium/grid/server/BaseServerOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
java/server/src/org/openqa/selenium/grid/server/CommonConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
) |
Oops, something went wrong.