|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.grid.selenium; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | + |
| 22 | +import org.openqa.grid.common.CommandLineOptionHelper; |
| 23 | +import org.openqa.grid.common.GridDocHelper; |
| 24 | +import org.openqa.grid.common.GridRole; |
| 25 | +import org.openqa.grid.common.RegistrationRequest; |
| 26 | +import org.openqa.grid.common.exception.GridConfigurationException; |
| 27 | +import org.openqa.grid.internal.utils.GridHubConfiguration; |
| 28 | +import org.openqa.grid.internal.utils.SelfRegisteringRemote; |
| 29 | +import org.openqa.grid.shared.CliUtils; |
| 30 | +import org.openqa.grid.web.Hub; |
| 31 | +import org.openqa.selenium.remote.server.SeleniumServer; |
| 32 | +import org.openqa.selenium.remote.server.log.LoggingOptions; |
| 33 | +import org.openqa.selenium.remote.server.log.TerseFormatter; |
| 34 | + |
| 35 | +import java.io.File; |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.logging.ConsoleHandler; |
| 38 | +import java.util.logging.FileHandler; |
| 39 | +import java.util.logging.Handler; |
| 40 | +import java.util.logging.Level; |
| 41 | +import java.util.logging.Logger; |
| 42 | + |
| 43 | +public class GridLauncherV3 { |
| 44 | + |
| 45 | + private static final Logger log = Logger.getLogger(GridLauncherV3.class.getName()); |
| 46 | + |
| 47 | + public interface GridItemLauncher { |
| 48 | + void launch(String[] args, Logger log) throws Exception; |
| 49 | + void printUsage(); |
| 50 | + } |
| 51 | + |
| 52 | + private static ImmutableMap<GridRole, GridItemLauncher> launchers |
| 53 | + = new ImmutableMap.Builder<GridRole, GridItemLauncher>() |
| 54 | + .put(GridRole.NOT_GRID, new GridItemLauncher() { |
| 55 | + @Override |
| 56 | + public void launch(String[] args, Logger log) throws Exception { |
| 57 | + log.info("Launching a standalone Selenium Server"); |
| 58 | + SeleniumServer.main(args); |
| 59 | + log.info("Selenium Server is up and running"); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void printUsage() { |
| 64 | + String separator = "\n-------------------------------\n"; |
| 65 | + SeleniumServer.usage(separator + "Running as a standalone server:" + separator); |
| 66 | + } |
| 67 | + }) |
| 68 | + .put(GridRole.HUB, new GridItemLauncher() { |
| 69 | + @Override |
| 70 | + public void launch(String[] args, Logger log) throws Exception { |
| 71 | + log.info("Launching Selenium Grid hub"); |
| 72 | + GridHubConfiguration c = GridHubConfiguration.build(args); |
| 73 | + Hub h = new Hub(c); |
| 74 | + h.start(); |
| 75 | + log.info("Nodes should register to " + h.getRegistrationURL()); |
| 76 | + log.info("Selenium Grid hub is up and running"); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void printUsage() { |
| 81 | + String separator = "\n-------------------------------\n"; |
| 82 | + GridDocHelper.printHubHelp(separator + "Running as a grid hub:" + separator, false); |
| 83 | + } |
| 84 | + }) |
| 85 | + .put(GridRole.NODE, new GridItemLauncher() { |
| 86 | + @Override |
| 87 | + public void launch(String[] args, Logger log) throws Exception { |
| 88 | + log.info("Launching a Selenium Grid node"); |
| 89 | + RegistrationRequest c = RegistrationRequest.build(args); |
| 90 | + SelfRegisteringRemote remote = new SelfRegisteringRemote(c); |
| 91 | + remote.setRemoteServer(new SeleniumServer(c.getConfigAsInt("port", 4444))); |
| 92 | + remote.startRemoteServer(); |
| 93 | + log.info("Selenium Grid node is up and ready to register to the hub"); |
| 94 | + remote.startRegistrationProcess(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void printUsage() { |
| 99 | + String separator = "\n-------------------------------\n"; |
| 100 | + GridDocHelper.printNodeHelp(separator + "Running as a grid node:" + separator, false); |
| 101 | + } |
| 102 | + }) |
| 103 | + .build(); |
| 104 | + |
| 105 | + public static void main(String[] args) throws Exception { |
| 106 | + CommandLineOptionHelper helper = new CommandLineOptionHelper(args); |
| 107 | + GridRole role = GridRole.find(args); |
| 108 | + |
| 109 | + if (role == null) { |
| 110 | + printInfoAboutRoles(helper); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + if (helper.isParamPresent("-help") || helper.isParamPresent("-h")) { |
| 115 | + if (launchers.containsKey(role)) { |
| 116 | + launchers.get(role).printUsage(); |
| 117 | + } else { |
| 118 | + printInfoAboutRoles(helper); |
| 119 | + } |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + configureLogging(helper); |
| 124 | + |
| 125 | + if (launchers.containsKey(role)) { |
| 126 | + try { |
| 127 | + launchers.get(role).launch(args, log); |
| 128 | + } catch (Exception e) { |
| 129 | + launchers.get(role).printUsage(); |
| 130 | + e.printStackTrace(); |
| 131 | + } |
| 132 | + } else { |
| 133 | + throw new GridConfigurationException("Unknown role: " + role); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static void printInfoAboutRoles(CommandLineOptionHelper helper) { |
| 138 | + if (helper.hasParamValue("-role")) { |
| 139 | + CliUtils.printWrappedLine( |
| 140 | + "", |
| 141 | + "Error: the role '" + helper.getParamValue("-role") + "' does not match a recognized server role\n"); |
| 142 | + } else { |
| 143 | + CliUtils.printWrappedLine( |
| 144 | + "", |
| 145 | + "Error: -role option needs to be followed by the value that defines role of this component in the grid\n"); |
| 146 | + } |
| 147 | + System.out.println( |
| 148 | + "Selenium server can run in one of the following roles:\n" + |
| 149 | + " hub as a hub of a Selenium grid\n" + |
| 150 | + " node as a node of a Selenium grid\n" + |
| 151 | + " standalone as a standalone server not being a part of a grid\n" + |
| 152 | + "\n" + |
| 153 | + "If -role option is omitted the server runs standalone\n"); |
| 154 | + CliUtils.printWrappedLine( |
| 155 | + "", |
| 156 | + "To get help on the options available for a specific role run the server" |
| 157 | + + " with -help option and the corresponding -role option value"); |
| 158 | + } |
| 159 | + |
| 160 | + private static void configureLogging(CommandLineOptionHelper helper) { |
| 161 | + Level logLevel = |
| 162 | + helper.isParamPresent("-debug") |
| 163 | + ? Level.FINE |
| 164 | + : LoggingOptions.getDefaultLogLevel(); |
| 165 | + if (logLevel == null) { |
| 166 | + logLevel = Level.INFO; |
| 167 | + } |
| 168 | + Logger.getLogger("").setLevel(logLevel); |
| 169 | + Logger.getLogger("org.openqa.jetty").setLevel(Level.WARNING); |
| 170 | + |
| 171 | + String logFilename = |
| 172 | + helper.isParamPresent("-log") |
| 173 | + ? helper.getParamValue("-log") |
| 174 | + : LoggingOptions.getDefaultLogOutFile(); |
| 175 | + if (logFilename != null) { |
| 176 | + for (Handler handler : Logger.getLogger("").getHandlers()) { |
| 177 | + if (handler instanceof ConsoleHandler) { |
| 178 | + Logger.getLogger("").removeHandler(handler); |
| 179 | + } |
| 180 | + } |
| 181 | + try { |
| 182 | + Handler logFile = new FileHandler(new File(logFilename).getAbsolutePath(), true); |
| 183 | + logFile.setFormatter(new TerseFormatter(true)); |
| 184 | + logFile.setLevel(logLevel); |
| 185 | + Logger.getLogger("").addHandler(logFile); |
| 186 | + } catch (IOException e) { |
| 187 | + throw new RuntimeException(e); |
| 188 | + } |
| 189 | + } else { |
| 190 | + boolean logLongForm = helper.isParamPresent("-logLongForm"); |
| 191 | + for (Handler handler : Logger.getLogger("").getHandlers()) { |
| 192 | + if (handler instanceof ConsoleHandler) { |
| 193 | + handler.setLevel(logLevel); |
| 194 | + handler.setFormatter(new TerseFormatter(logLongForm)); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments