org.openqa.grid.internal.utils.GridHubConfiguration Maven / Gradle / Ivy
// 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.grid.internal.utils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.openqa.grid.common.CommandLineOptionHelper;
import org.openqa.grid.common.JSONConfigurationUtils;
import org.openqa.grid.common.RegistrationRequest;
import org.openqa.grid.common.exception.GridConfigurationException;
import org.openqa.grid.internal.listeners.Prioritizer;
import org.openqa.selenium.remote.JsonToBeanConverter;
import org.yaml.snakeyaml.Yaml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GridHubConfiguration {
/**
* The hub needs to know its hostname in order to write the proper Location header for the request
* being forwarded. Usually this can be guessed correctly, but in case it cannot it can be passed
* via this config param.
*/
private String host = null;
/**
* port for the hub.
*/
private int port;
/**
* how often in ms each proxy will detect that a session has timed out. All new proxy registering
* will have that value if they don't specifically mention the parameter.
*/
private int cleanupCycle;
/**
* how long a new session request can stay in the queue without being assigned before being
* rejected. -1 = forever.
*/
private int newSessionWaitTimeout;
/**
* list of extra servlets this hub will display. Allows to present custom view of the hub for
* monitoring and management purpose
*/
private List servlets = new ArrayList<>();
/**
* name <-> browser mapping from grid1
*/
private Map grid1Mapping = new HashMap<>();
/**
* to specify the order in which the new session request will be handled.
*/
private Prioritizer prioritizer = null;
/**
* to specify how new request and nodes will be matched.
*/
private CapabilityMatcher matcher = new DefaultCapabilityMatcher();
/**
* true by default.If true, the hub will throw exception as soon as a request not supported by the
* grid is received. If set to false, the request will be queued, hoping that a node will be
* registered at some point, supporting that capability.
*/
private boolean throwOnCapabilityNotPresent = true;
/**
* The filename to use for logging. Default value is null
and indicates logging to STDOUT.
*/
private String logFilename;
/**
* max number of thread for Jetty. Default is normally 255.
*/
private int jettyMaxThreads = -1;
private Map allParams = new HashMap<>();
/**
* original command line param, useful for debugging
*/
private String[] args = {};
private String grid1Yml = null;
private String grid2JSON = null;
public GridHubConfiguration() {
loadDefault();
}
/**
* builds a grid configuration from the parameters passed command line.
*
* @param args command line arguments
* @return A GridHubConfiguration object with options from the grid1 and/or
* grid2 config file(s), plus any command line option overrides.
*/
public static GridHubConfiguration build(String[] args) {
GridHubConfiguration res = new GridHubConfiguration();
CommandLineOptionHelper helper = new CommandLineOptionHelper(args);
res.args = args;
// is there a grid1 config file to load ?
if (helper.isParamPresent("-grid1Yml")) {
String value = helper.getParamValue("-grid1Yml");
res.grid1Yml = value;
res.loadFromGridYml(value);
}
// is there a grid2 config file ?
if (helper.isParamPresent("-hubConfig")) {
String value = helper.getParamValue("-hubConfig");
res.grid2JSON = value;
res.loadFromJSON(value);
}
// are there some command line param to overwrite the config file ?
res.loadFromCommandLine(args);
return res;
}
public String getGrid1Yml() {
return grid1Yml;
}
public String getGrid2JSON() {
return grid2JSON;
}
public void loadDefault() {
loadFromJSON("defaults/DefaultHub.json");
}
public void loadFromCommandLine(String[] args) {
CommandLineOptionHelper helper = new CommandLineOptionHelper(args);
// storing them all.
List params = helper.getKeys();
for (String param : params) {
String cleanParam = param.replaceFirst("-", "");
String value = helper.getParamValue(param);
allParams.put(cleanParam, value);
}
// handle the core config.
if (helper.isParamPresent("-host")) {
host = helper.getParamValue("-host");
}
if (helper.isParamPresent("-port")) {
port = Integer.parseInt(helper.getParamValue("-port"));
}
if (helper.isParamPresent("-jettyMaxThreads")) {
jettyMaxThreads = Integer.parseInt(helper.getParamValue("-jettyMaxThreads"));
}
if (helper.isParamPresent("-cleanUpCycle")) {
cleanupCycle = Integer.parseInt(helper.getParamValue("-cleanUpCycle"));
}
if (helper.isParamPresent("-timeout")) {
setTimeout(Integer.parseInt(helper.getParamValue("-timeout")) * 1000);
}
if (helper.isParamPresent("-browserTimeout")) {
setBrowserTimeout(Integer.parseInt(helper.getParamValue("-browserTimeout")) * 1000);
}
if (helper.isParamPresent("-newSessionWaitTimeout")) {
newSessionWaitTimeout = Integer.parseInt(helper.getParamValue("-newSessionWaitTimeout"));
}
if (helper.isParamPresent("-throwOnCapabilityNotPresent")) {
throwOnCapabilityNotPresent =
Boolean.parseBoolean(helper.getParamValue("-throwOnCapabilityNotPresent"));
}
if (helper.isParamPresent("-prioritizer")) {
setPrioritizer(helper.getParamValue("-prioritizer"));
}
if (helper.isParamPresent("-capabilityMatcher")) {
setCapabilityMatcher(helper.getParamValue("-capabilityMatcher"));
}
if (helper.isParamPresent("-servlets")) {
servlets = helper.getParamValues("-servlets");
}
if (helper.isParamPresent("-log")) {
logFilename = helper.getParamValue("-log");
}
}
/**
* @param resource /grid_configuration.yml for instance
*/
public void loadFromGridYml(String resource) {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
if (in == null) {
try {
in = new FileInputStream(resource);
} catch (FileNotFoundException e) {
// ignore
}
}
if (in == null) {
throw new InvalidParameterException(resource + " is not a valid resource.");
}
Yaml yaml = new Yaml();
Map config = (Map) yaml.load(in);
Map hub = (Map) config.get("hub");
List