From cfe7a59ac8d45c4d1d7def3923c67c5b48f70161 Mon Sep 17 00:00:00 2001 From: Sébastien Lesaint Date: Thu, 23 Feb 2017 11:40:30 +0100 Subject: SONAR-7937 extract JavaCommandFactory from App and fix missing coverage on this part --- .../src/main/java/org/sonar/application/App.java | 98 ++---------------- .../org/sonar/application/JavaCommandFactory.java | 32 ++++++ .../sonar/application/JavaCommandFactoryImpl.java | 114 +++++++++++++++++++++ 3 files changed, 155 insertions(+), 89 deletions(-) create mode 100644 sonar-application/src/main/java/org/sonar/application/JavaCommandFactory.java create mode 100644 sonar-application/src/main/java/org/sonar/application/JavaCommandFactoryImpl.java (limited to 'sonar-application/src/main/java') diff --git a/sonar-application/src/main/java/org/sonar/application/App.java b/sonar-application/src/main/java/org/sonar/application/App.java index 7271d593ad8..d6c3b843a85 100644 --- a/sonar-application/src/main/java/org/sonar/application/App.java +++ b/sonar-application/src/main/java/org/sonar/application/App.java @@ -27,7 +27,6 @@ import org.apache.commons.io.FilenameUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.process.Lifecycle; -import org.sonar.process.ProcessId; import org.sonar.process.ProcessProperties; import org.sonar.process.Props; import org.sonar.process.Stoppable; @@ -36,29 +35,13 @@ import org.sonar.process.monitor.Monitor; import static org.sonar.process.Lifecycle.State; import static org.sonar.process.ProcessId.APP; -import static org.sonar.process.ProcessProperties.HTTPS_PROXY_HOST; -import static org.sonar.process.ProcessProperties.HTTPS_PROXY_PORT; -import static org.sonar.process.ProcessProperties.HTTP_PROXY_HOST; -import static org.sonar.process.ProcessProperties.HTTP_PROXY_PORT; /** * Entry-point of process that starts and monitors ElasticSearch, the Web Server and the Compute Engine. */ public class App implements Stoppable { - /** - * Properties about proxy that must be set as system properties - */ - private static final String[] PROXY_PROPERTY_KEYS = new String[] { - HTTP_PROXY_HOST, - HTTP_PROXY_PORT, - "http.nonProxyHosts", - HTTPS_PROXY_HOST, - HTTPS_PROXY_PORT, - "http.auth.ntlm.domain", - "socksProxyHost", - "socksProxyPort"}; - + private final JavaCommandFactory javaCommandFactory; private final Monitor monitor; public App(AppFileSystem appFileSystem, boolean watchForHardStop) { @@ -68,10 +51,12 @@ public class App implements Stoppable { .setWatchForHardStop(watchForHardStop) .setWaitForOperational() .addListener(new AppLifecycleListener()) - .build()); + .build(), + new JavaCommandFactoryImpl()); } - App(Monitor monitor) { + App(Monitor monitor, JavaCommandFactory javaCommandFactory) { + this.javaCommandFactory = javaCommandFactory; this.monitor = monitor; } @@ -80,19 +65,19 @@ public class App implements Stoppable { monitor.awaitTermination(); } - private static List createCommands(Props props) { + private List createCommands(Props props) { File homeDir = props.nonNullValueAsFile(ProcessProperties.PATH_HOME); List commands = new ArrayList<>(3); if (isProcessEnabled(props, ProcessProperties.CLUSTER_SEARCH_DISABLED)) { - commands.add(createESCommand(props, homeDir)); + commands.add(javaCommandFactory.createESCommand(props, homeDir)); } if (isProcessEnabled(props, ProcessProperties.CLUSTER_WEB_DISABLED)) { - commands.add(createWebServerCommand(props, homeDir)); + commands.add(javaCommandFactory.createWebCommand(props, homeDir)); } if (isProcessEnabled(props, ProcessProperties.CLUSTER_CE_DISABLED)) { - commands.add(createCeServerCommand(props, homeDir)); + commands.add(javaCommandFactory.createCeCommand(props, homeDir)); } return commands; @@ -103,71 +88,6 @@ public class App implements Stoppable { !props.valueAsBoolean(disabledPropertyKey); } - private static JavaCommand createESCommand(Props props, File homeDir) { - return newJavaCommand(ProcessId.ELASTICSEARCH, props, homeDir) - .addJavaOptions("-Djava.awt.headless=true") - .addJavaOptions(props.nonNullValue(ProcessProperties.SEARCH_JAVA_OPTS)) - .addJavaOptions(props.nonNullValue(ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS)) - .setClassName("org.sonar.search.SearchServer") - .addClasspath("./lib/common/*") - .addClasspath("./lib/search/*"); - } - - private static JavaCommand createWebServerCommand(Props props, File homeDir) { - JavaCommand command = newJavaCommand(ProcessId.WEB_SERVER, props, homeDir) - .addJavaOptions(ProcessProperties.WEB_ENFORCED_JVM_ARGS) - .addJavaOptions(props.nonNullValue(ProcessProperties.WEB_JAVA_OPTS)) - .addJavaOptions(props.nonNullValue(ProcessProperties.WEB_JAVA_ADDITIONAL_OPTS)) - // required for logback tomcat valve - .setEnvVariable(ProcessProperties.PATH_LOGS, props.nonNullValue(ProcessProperties.PATH_LOGS)) - .setClassName("org.sonar.server.app.WebServer") - .addClasspath("./lib/common/*") - .addClasspath("./lib/server/*"); - String driverPath = props.value(ProcessProperties.JDBC_DRIVER_PATH); - if (driverPath != null) { - command.addClasspath(driverPath); - } - return command; - } - - private static JavaCommand createCeServerCommand(Props props, File homeDir) { - JavaCommand command = newJavaCommand(ProcessId.COMPUTE_ENGINE, props, homeDir) - .addJavaOptions(ProcessProperties.CE_ENFORCED_JVM_ARGS) - .addJavaOptions(props.nonNullValue(ProcessProperties.CE_JAVA_OPTS)) - .addJavaOptions(props.nonNullValue(ProcessProperties.CE_JAVA_ADDITIONAL_OPTS)) - .setClassName("org.sonar.ce.app.CeServer") - .addClasspath("./lib/common/*") - .addClasspath("./lib/server/*") - .addClasspath("./lib/ce/*"); - String driverPath = props.value(ProcessProperties.JDBC_DRIVER_PATH); - if (driverPath != null) { - command.addClasspath(driverPath); - } - return command; - } - - private static JavaCommand newJavaCommand(ProcessId id, Props props, File homeDir) { - JavaCommand command = new JavaCommand(id) - .setWorkDir(homeDir) - .setArguments(props.rawProperties()); - - for (String key : PROXY_PROPERTY_KEYS) { - if (props.contains(key)) { - command.addJavaOption("-D" + key + "=" + props.value(key)); - } - } - // defaults of HTTPS are the same than HTTP defaults - setSystemPropertyToDefaultIfNotSet(command, props, HTTPS_PROXY_HOST, HTTP_PROXY_HOST); - setSystemPropertyToDefaultIfNotSet(command, props, HTTPS_PROXY_PORT, HTTP_PROXY_PORT); - return command; - } - - private static void setSystemPropertyToDefaultIfNotSet(JavaCommand command, Props props, String httpsProperty, String httpProperty) { - if (!props.contains(httpsProperty) && props.contains(httpProperty)) { - command.addJavaOption("-D" + httpsProperty + "=" + props.value(httpProperty)); - } - } - static String starPath(File homeDir, String relativePath) { File dir = new File(homeDir, relativePath); return FilenameUtils.concat(dir.getAbsolutePath(), "*"); diff --git a/sonar-application/src/main/java/org/sonar/application/JavaCommandFactory.java b/sonar-application/src/main/java/org/sonar/application/JavaCommandFactory.java new file mode 100644 index 00000000000..0071a3619d8 --- /dev/null +++ b/sonar-application/src/main/java/org/sonar/application/JavaCommandFactory.java @@ -0,0 +1,32 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.application; + +import java.io.File; +import org.sonar.process.Props; +import org.sonar.process.monitor.JavaCommand; + +public interface JavaCommandFactory { + JavaCommand createESCommand(Props props, File homeDir); + + JavaCommand createWebCommand(Props props, File homeDir); + + JavaCommand createCeCommand(Props props, File homeDir); +} diff --git a/sonar-application/src/main/java/org/sonar/application/JavaCommandFactoryImpl.java b/sonar-application/src/main/java/org/sonar/application/JavaCommandFactoryImpl.java new file mode 100644 index 00000000000..4f9ebd80ed8 --- /dev/null +++ b/sonar-application/src/main/java/org/sonar/application/JavaCommandFactoryImpl.java @@ -0,0 +1,114 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.application; + +import java.io.File; +import org.sonar.process.ProcessId; +import org.sonar.process.ProcessProperties; +import org.sonar.process.Props; +import org.sonar.process.monitor.JavaCommand; + +import static org.sonar.process.ProcessProperties.HTTPS_PROXY_HOST; +import static org.sonar.process.ProcessProperties.HTTPS_PROXY_PORT; +import static org.sonar.process.ProcessProperties.HTTP_PROXY_HOST; +import static org.sonar.process.ProcessProperties.HTTP_PROXY_PORT; + +public class JavaCommandFactoryImpl implements JavaCommandFactory { + /** + * Properties about proxy that must be set as system properties + */ + private static final String[] PROXY_PROPERTY_KEYS = new String[] { + HTTP_PROXY_HOST, + HTTP_PROXY_PORT, + "http.nonProxyHosts", + HTTPS_PROXY_HOST, + HTTPS_PROXY_PORT, + "http.auth.ntlm.domain", + "socksProxyHost", + "socksProxyPort"}; + + @Override + public JavaCommand createESCommand(Props props, File workDir) { + return newJavaCommand(ProcessId.ELASTICSEARCH, props, workDir) + .addJavaOptions("-Djava.awt.headless=true") + .addJavaOptions(props.nonNullValue(ProcessProperties.SEARCH_JAVA_OPTS)) + .addJavaOptions(props.nonNullValue(ProcessProperties.SEARCH_JAVA_ADDITIONAL_OPTS)) + .setClassName("org.sonar.search.SearchServer") + .addClasspath("./lib/common/*") + .addClasspath("./lib/search/*"); + } + + @Override + public JavaCommand createWebCommand(Props props, File workDir) { + JavaCommand command = newJavaCommand(ProcessId.WEB_SERVER, props, workDir) + .addJavaOptions(ProcessProperties.WEB_ENFORCED_JVM_ARGS) + .addJavaOptions(props.nonNullValue(ProcessProperties.WEB_JAVA_OPTS)) + .addJavaOptions(props.nonNullValue(ProcessProperties.WEB_JAVA_ADDITIONAL_OPTS)) + // required for logback tomcat valve + .setEnvVariable(ProcessProperties.PATH_LOGS, props.nonNullValue(ProcessProperties.PATH_LOGS)) + .setClassName("org.sonar.server.app.WebServer") + .addClasspath("./lib/common/*") + .addClasspath("./lib/server/*"); + String driverPath = props.value(ProcessProperties.JDBC_DRIVER_PATH); + if (driverPath != null) { + command.addClasspath(driverPath); + } + return command; + } + + @Override + public JavaCommand createCeCommand(Props props, File workDir) { + JavaCommand command = newJavaCommand(ProcessId.COMPUTE_ENGINE, props, workDir) + .addJavaOptions(ProcessProperties.CE_ENFORCED_JVM_ARGS) + .addJavaOptions(props.nonNullValue(ProcessProperties.CE_JAVA_OPTS)) + .addJavaOptions(props.nonNullValue(ProcessProperties.CE_JAVA_ADDITIONAL_OPTS)) + .setClassName("org.sonar.ce.app.CeServer") + .addClasspath("./lib/common/*") + .addClasspath("./lib/server/*") + .addClasspath("./lib/ce/*"); + String driverPath = props.value(ProcessProperties.JDBC_DRIVER_PATH); + if (driverPath != null) { + command.addClasspath(driverPath); + } + return command; + } + + private static JavaCommand newJavaCommand(ProcessId id, Props props, File workDir) { + JavaCommand command = new JavaCommand(id) + .setWorkDir(workDir) + .setArguments(props.rawProperties()); + + for (String key : PROXY_PROPERTY_KEYS) { + if (props.contains(key)) { + command.addJavaOption("-D" + key + "=" + props.value(key)); + } + } + // defaults of HTTPS are the same than HTTP defaults + setSystemPropertyToDefaultIfNotSet(command, props, HTTPS_PROXY_HOST, HTTP_PROXY_HOST); + setSystemPropertyToDefaultIfNotSet(command, props, HTTPS_PROXY_PORT, HTTP_PROXY_PORT); + return command; + } + + private static void setSystemPropertyToDefaultIfNotSet(JavaCommand command, Props props, String httpsProperty, String httpProperty) { + if (!props.contains(httpsProperty) && props.contains(httpProperty)) { + command.addJavaOption("-D" + httpsProperty + "=" + props.value(httpProperty)); + } + } +} -- cgit v1.2.3