You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractCommand.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.application.command;
  21. import java.io.File;
  22. import java.util.HashMap;
  23. import java.util.HashSet;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import javax.annotation.CheckForNull;
  27. import org.sonar.application.es.EsInstallation;
  28. import org.sonar.process.ProcessId;
  29. import org.sonar.process.System2;
  30. import static java.util.Objects.requireNonNull;
  31. public abstract class AbstractCommand<T extends AbstractCommand> {
  32. // unique key among the group of commands to launch
  33. private final ProcessId id;
  34. private final Map<String, String> envVariables;
  35. private final Set<String> suppressedEnvVariables = new HashSet<>();
  36. private final File workDir;
  37. private EsInstallation esInstallation;
  38. protected AbstractCommand(ProcessId id, File workDir, System2 system2) {
  39. this.id = requireNonNull(id, "ProcessId can't be null");
  40. this.workDir = requireNonNull(workDir, "workDir can't be null");
  41. this.envVariables = new HashMap<>(system2.getenv());
  42. }
  43. public ProcessId getProcessId() {
  44. return id;
  45. }
  46. public File getWorkDir() {
  47. return workDir;
  48. }
  49. @SuppressWarnings("unchecked")
  50. private T castThis() {
  51. return (T) this;
  52. }
  53. public Map<String, String> getEnvVariables() {
  54. return envVariables;
  55. }
  56. public Set<String> getSuppressedEnvVariables() {
  57. return suppressedEnvVariables;
  58. }
  59. public T suppressEnvVariable(String key) {
  60. requireNonNull(key, "key can't be null");
  61. suppressedEnvVariables.add(key);
  62. envVariables.remove(key);
  63. return castThis();
  64. }
  65. public T setEnvVariable(String key, String value) {
  66. envVariables.put(
  67. requireNonNull(key, "key can't be null"),
  68. requireNonNull(value, "value can't be null"));
  69. return castThis();
  70. }
  71. public T setEsInstallation(EsInstallation esInstallation) {
  72. this.esInstallation = esInstallation;
  73. return castThis();
  74. }
  75. @CheckForNull
  76. public EsInstallation getEsInstallation() {
  77. return esInstallation;
  78. }
  79. }