
org.openqa.grid.internal.utils.GridHubConfiguration Maven / Gradle / Ivy
Go to download
Selenium automates browsers. That's it! What you do with that power is entirely up to you.
package org.openqa.grid.internal.utils;
/*
Copyright 2011 Selenium committers
Copyright 2011 Software Freedom Conservancy
Licensed 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.
*/
import org.json.JSONArray;
import org.json.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.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.Iterator;
import java.util.List;
import java.util.Map;
import static org.openqa.grid.internal.utils.ServerJsonValues.*;
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;
/**
* to specify that logging level should be set to Level.DEBUG
*/
private boolean isDebug = false;
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
* @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("-cleanUpCycle")) {
cleanupCycle = Integer.parseInt(helper.getParamValue("-cleanUpCycle"));
}
if (helper.isParamPresent(CLIENT_TIMEOUT.getAsParam())) {
setTimeout(Integer.parseInt(helper.getParamValue(CLIENT_TIMEOUT.getAsParam())) * 1000);
}
if (helper.isParamPresent(BROWSER_TIMEOUT.getAsParam())) {
setBrowserTimeout(Integer.parseInt(helper.getParamValue(BROWSER_TIMEOUT.getAsParam())) * 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");
}
if (helper.isParamPresent("-debug")) {
isDebug = true;
}
}
/**
* @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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy