<commit_before>/*
Copyright 2007-2011 WebDriver committers

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.
 */
package org.openqa.grid.web;

import java.io.InputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.Servlet;

import org.openqa.grid.internal.Registry;
import org.openqa.grid.web.servlet.ConsoleServlet;
import org.openqa.grid.web.servlet.DriverServlet;
import org.openqa.grid.web.servlet.Grid1HeartbeatServlet;
import org.openqa.grid.web.servlet.ProxyStatusServlet;
import org.openqa.grid.web.servlet.RegistrationServlet;
import org.openqa.grid.web.servlet.ResourceServlet;
import org.openqa.grid.web.utils.ExtraServletUtil;
import org.openqa.jetty.http.SocketListener;
import org.openqa.jetty.jetty.Server;
import org.openqa.jetty.jetty.servlet.WebApplicationContext;
import org.yaml.snakeyaml.Yaml;

import com.google.common.collect.Maps;

/**
 * 
 * Jetty server. Main entry point for everything about the grid.
 * 
 * Except for unit tests, this should be a singleton.
 * 
 * 
 */
public class Hub {

	private static final Logger log = Logger.getLogger(Hub.class.getName());

	private int port;
	private String host;
	private Server server;
	private Registry registry;
	private Map<String, Class<? extends Servlet>> extraServlet = Maps.newHashMap();
	private static Map<String, String> grid1Mapping = Maps.newHashMap();
	private static Hub INSTANCE = new Hub(4444, Registry.getInstance());

	public static Hub getInstance() {
		return INSTANCE;
	}

	/**
	 * Starts the hub and the default port. Can register some custom servlet to
	 * manage the remote via a web interface.
	 * 
	 * @param args
	 * @throws Exception
	 */
	// TODO freynaud : have a separate config file with the servlet and their
	// name to use for their path. param isn't convenient.
	/*
	 * public static void main(String[] args) throws Exception { Hub hub =
	 * Hub.getInstance(); for (String s : args) { Class<? extends Servlet>
	 * servletClass = ExtraServletUtil.createServlet(s); if (s != null) { String
	 * path = "/grid/admin/" + servletClass.getSimpleName() + "/*";
	 * log.info("binding " + servletClass.getCanonicalName() + " to " + path);
	 * hub.addServlet(path, servletClass); } } hub.start();
	 * 
	 * }
	 */

	private void addServlet(String key, Class<? extends Servlet> s) {
		extraServlet.put(key, s);
	}

	/**
	 * get the registry backing up the hub state.
	 * 
	 * @return
	 */
	public Registry getRegistry() {
		return registry;
	}

	/**
	 * Create a new instance on the given port, with the given registry. Allow
	 * several hub to run on the same machine without interacting with each
	 * other. Useless in a normal use. Convenient for testing
	 * 
	 * @param port
	 * @param registry
	 */
	public static Hub getNewInstanceForTest(int port, Registry registry) {
		return new Hub(port, registry);
	}

	private Hub(int port, Registry registry) {
		InetAddress addr;
		try {
			addr = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			throw new InstantiationError("cannot find hub ip");
		}
		host = addr.getHostAddress();
		this.port = port;
		this.registry = registry;
		registry.setHub(this);

		// Load up the old Selenium Grid 1.0 configuration if it exists.
		loadGrid1Config();
	}

	private void initServer() {
		try {
			server = new Server();
			SocketListener socketListener = new SocketListener();
			socketListener.setMaxIdleTimeMs(60000);
			socketListener.setPort(port);
			server.addListener(socketListener);

			WebApplicationContext root = server.addWebApplication("", ".");
			root.setAttribute(Registry.KEY, registry);<commit_msg>FrancoisReynaud: using the networkutils to find the info about the hub.It should fix the "cannot find ip" issues for the machines with more complex network settings.<commit_after>/*
Copyright 2007-2011 WebDriver committers

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.
 */
package org.openqa.grid.web;

import java.io.InputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.Servlet;

import org.openqa.grid.internal.Registry;
import org.openqa.grid.web.servlet.ConsoleServlet;
import org.openqa.grid.web.servlet.DriverServlet;
import org.openqa.grid.web.servlet.Grid1HeartbeatServlet;
import org.openqa.grid.web.servlet.ProxyStatusServlet;
import org.openqa.grid.web.servlet.RegistrationServlet;
import org.openqa.grid.web.servlet.ResourceServlet;
import org.openqa.grid.web.utils.ExtraServletUtil;
import org.openqa.jetty.http.SocketListener;
import org.openqa.jetty.jetty.Server;
import org.openqa.jetty.jetty.servlet.WebApplicationContext;
import org.openqa.selenium.net.NetworkUtils;
import org.yaml.snakeyaml.Yaml;

import com.google.common.collect.Maps;

/**
 * 
 * Jetty server. Main entry point for everything about the grid.
 * 
 * Except for unit tests, this should be a singleton.
 * 
 * 
 */
public class Hub {

	private static final Logger log = Logger.getLogger(Hub.class.getName());

	private int port;
	private String host;
	private Server server;
	private Registry registry;
	private Map<String, Class<? extends Servlet>> extraServlet = Maps.newHashMap();
	private static Map<String, String> grid1Mapping = Maps.newHashMap();
	private static Hub INSTANCE = new Hub(4444, Registry.getInstance());
	private NetworkUtils utils = new NetworkUtils();
	public static Hub getInstance() {
		return INSTANCE;
	}

	/**
	 * Starts the hub and the default port. Can register some custom servlet to
	 * manage the remote via a web interface.
	 * 
	 * @param args
	 * @throws Exception
	 */
	// TODO freynaud : have a separate config file with the servlet and their
	// name to use for their path. param isn't convenient.
	/*
	 * public static void main(String[] args) throws Exception { Hub hub =
	 * Hub.getInstance(); for (String s : args) { Class<? extends Servlet>
	 * servletClass = ExtraServletUtil.createServlet(s); if (s != null) { String
	 * path = "/grid/admin/" + servletClass.getSimpleName() + "/*";
	 * log.info("binding " + servletClass.getCanonicalName() + " to " + path);
	 * hub.addServlet(path, servletClass); } } hub.start();
	 * 
	 * }
	 */

	private void addServlet(String key, Class<? extends Servlet> s) {
		extraServlet.put(key, s);
	}

	/**
	 * get the registry backing up the hub state.
	 * 
	 * @return
	 */
	public Registry getRegistry() {
		return registry;
	}

	/**
	 * Create a new instance on the given port, with the given registry. Allow
	 * several hub to run on the same machine without interacting with each
	 * other. Useless in a normal use. Convenient for testing
	 * 
	 * @param port
	 * @param registry
	 */
	public static Hub getNewInstanceForTest(int port, Registry registry) {
		return new Hub(port, registry);
	}

	private Hub(int port, Registry registry) {
		host = utils.getIp4NonLoopbackAddressOfThisMachine().getHostAddress();
		this.port = port;
		this.registry = registry;
		registry.setHub(this);

		// Load up the old Selenium Grid 1.0 configuration if it exists.
		loadGrid1Config();
	}

	private void initServer() {
		try {
			server = new Server();
			SocketListener socketListener = new SocketListener();
			socketListener.setMaxIdleTimeMs(60000);
			socketListener.setPort(port);
			server.addListener(socketListener);

			WebApplicationContext root = server.addWebApplication("", ".");
			root.setAttribute(Registry.KEY, registry);
{'fraction_non_alphanumeric': 0.10217618992202956, 'fraction_numerical': 0.006167811008960782, 'mean_word_length': 4.199032062915911, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1205370', 'n_tokens_mistral': 2657, 'n_tokens_neox': 2450, 'n_words': 824}