From 31d849e57ea589411655f214a75eee16326bc25f Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 18 Jul 2017 16:04:33 +0200 Subject: [PATCH] SONAR-8798 extract AbstractCommand from JavaCommand --- .../application/process/AbstractCommand.java | 92 +++++++++++++++++++ .../application/process/JavaCommand.java | 79 ++-------------- .../process/AbstractCommandTest.java | 59 ++++++++++++ 3 files changed, 158 insertions(+), 72 deletions(-) create mode 100644 server/sonar-process-monitor/src/main/java/org/sonar/application/process/AbstractCommand.java create mode 100644 server/sonar-process-monitor/src/test/java/org/sonar/application/process/AbstractCommandTest.java diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/application/process/AbstractCommand.java b/server/sonar-process-monitor/src/main/java/org/sonar/application/process/AbstractCommand.java new file mode 100644 index 00000000000..ad4486514bc --- /dev/null +++ b/server/sonar-process-monitor/src/main/java/org/sonar/application/process/AbstractCommand.java @@ -0,0 +1,92 @@ +/* + * 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.process; + +import java.io.File; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; +import javax.annotation.Nullable; +import org.sonar.process.ProcessId; + +public abstract class AbstractCommand { + // unique key among the group of commands to launch + private final ProcessId id; + // program arguments + private final Map arguments = new LinkedHashMap<>(); + private final Map envVariables = new HashMap<>(System.getenv()); + private File workDir; + + protected AbstractCommand(ProcessId id) { + this.id = id; + } + + public ProcessId getProcessId() { + return id; + } + + public File getWorkDir() { + return workDir; + } + + public T setWorkDir(File workDir) { + this.workDir = workDir; + return castThis(); + } + + @SuppressWarnings("unchecked") + private T castThis() { + return (T) this; + } + + public Map getArguments() { + return arguments; + } + + public T setArgument(String key, @Nullable String value) { + if (value == null) { + arguments.remove(key); + } else { + arguments.put(key, value); + } + return castThis(); + } + + public T setArguments(Properties args) { + for (Map.Entry entry : args.entrySet()) { + setArgument(entry.getKey().toString(), entry.getValue() != null ? entry.getValue().toString() : null); + } + return castThis(); + } + + public Map getEnvVariables() { + return envVariables; + } + + public T setEnvVariable(String key, @Nullable String value) { + if (value == null) { + envVariables.remove(key); + } else { + envVariables.put(key, value); + } + return castThis(); + } +} diff --git a/server/sonar-process-monitor/src/main/java/org/sonar/application/process/JavaCommand.java b/server/sonar-process-monitor/src/main/java/org/sonar/application/process/JavaCommand.java index b5311087de7..f22bd7bc8e7 100644 --- a/server/sonar-process-monitor/src/main/java/org/sonar/application/process/JavaCommand.java +++ b/server/sonar-process-monitor/src/main/java/org/sonar/application/process/JavaCommand.java @@ -19,52 +19,20 @@ */ package org.sonar.application.process; -import java.io.File; import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; -import java.util.Properties; -import javax.annotation.Nullable; import org.sonar.process.ProcessId; -public class JavaCommand { - - // unique key among the group of commands to launch - private final ProcessId id; - - private File workDir; - - // for example -Xmx1G - private final List javaOptions = new ArrayList<>(); - +public class JavaCommand extends AbstractCommand { // entry point private String className; - + // for example -Xmx1G + private final List javaOptions = new ArrayList<>(); // relative path to JAR files private final List classpath = new ArrayList<>(); - // program arguments (parameters of main(String[]) - private final Map arguments = new LinkedHashMap<>(); - - private final Map envVariables = new HashMap<>(System.getenv()); - public JavaCommand(ProcessId id) { - this.id = id; - } - - public ProcessId getProcessId() { - return id; - } - - public File getWorkDir() { - return workDir; - } - - public JavaCommand setWorkDir(File workDir) { - this.workDir = workDir; - return this; + super(id); } public List getJavaOptions() { @@ -103,48 +71,15 @@ public class JavaCommand { return this; } - public Map getArguments() { - return arguments; - } - - public JavaCommand setArgument(String key, @Nullable String value) { - if (value == null) { - arguments.remove(key); - } else { - arguments.put(key, value); - } - return this; - } - - public JavaCommand setArguments(Properties args) { - for (Map.Entry entry : args.entrySet()) { - setArgument(entry.getKey().toString(), entry.getValue() != null ? entry.getValue().toString() : null); - } - return this; - } - - public Map getEnvVariables() { - return envVariables; - } - - public JavaCommand setEnvVariable(String key, @Nullable String value) { - if (value == null) { - envVariables.remove(key); - } else { - envVariables.put(key, value); - } - return this; - } - @Override public String toString() { StringBuilder sb = new StringBuilder("JavaCommand{"); - sb.append("workDir=").append(workDir); + sb.append("workDir=").append(getWorkDir()); sb.append(", javaOptions=").append(javaOptions); sb.append(", className='").append(className).append('\''); sb.append(", classpath=").append(classpath); - sb.append(", arguments=").append(arguments); - sb.append(", envVariables=").append(envVariables); + sb.append(", arguments=").append(getArguments()); + sb.append(", envVariables=").append(getEnvVariables()); sb.append('}'); return sb.toString(); } diff --git a/server/sonar-process-monitor/src/test/java/org/sonar/application/process/AbstractCommandTest.java b/server/sonar-process-monitor/src/test/java/org/sonar/application/process/AbstractCommandTest.java new file mode 100644 index 00000000000..327f7b73c0c --- /dev/null +++ b/server/sonar-process-monitor/src/test/java/org/sonar/application/process/AbstractCommandTest.java @@ -0,0 +1,59 @@ +/* + * 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.process; + +import java.io.File; +import java.util.Properties; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.process.ProcessId; + +import static org.assertj.core.api.Assertions.assertThat; + +public class AbstractCommandTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void test_command_with_complete_information() throws Exception { + AbstractCommand command = new AbstractCommand(ProcessId.ELASTICSEARCH) { + + }; + + command.setArgument("first_arg", "val1"); + Properties args = new Properties(); + args.setProperty("second_arg", "val2"); + command.setArguments(args); + + command.setEnvVariable("JAVA_COMMAND_TEST", "1000"); + File workDir = temp.newFolder(); + command.setWorkDir(workDir); + + assertThat(command.toString()).isNotNull(); + assertThat(command.getWorkDir()).isSameAs(workDir); + + // copy current env variables + assertThat(command.getEnvVariables().get("JAVA_COMMAND_TEST")).isEqualTo("1000"); + assertThat(command.getEnvVariables().size()).isEqualTo(System.getenv().size() + 1); + } + +} -- 2.39.5