aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-main/src/main/java/org/sonar/application/command/AbstractCommand.java
blob: b8e5bc2e996e0fbbcfc52c903b21dacc28b80490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * SonarQube
 * Copyright (C) 2009-2025 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.command;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import org.sonar.application.es.EsInstallation;
import org.sonar.process.ProcessId;
import org.sonar.process.System2;

import static java.util.Objects.requireNonNull;

public abstract class AbstractCommand<T extends AbstractCommand> {
  // unique key among the group of commands to launch
  private final ProcessId id;
  private final Map<String, String> envVariables;
  private final Set<String> suppressedEnvVariables = new HashSet<>();
  private final File workDir;
  private EsInstallation esInstallation;

  protected AbstractCommand(ProcessId id, File workDir, System2 system2) {
    this.id = requireNonNull(id, "ProcessId can't be null");
    this.workDir = requireNonNull(workDir, "workDir can't be null");
    this.envVariables = new HashMap<>(system2.getenv());
  }

  public ProcessId getProcessId() {
    return id;
  }

  public File getWorkDir() {
    return workDir;
  }

  @SuppressWarnings("unchecked")
  private T castThis() {
    return (T) this;
  }

  public Map<String, String> getEnvVariables() {
    return envVariables;
  }

  public Set<String> getSuppressedEnvVariables() {
    return suppressedEnvVariables;
  }

  public T suppressEnvVariable(String key) {
    requireNonNull(key, "key can't be null");
    suppressedEnvVariables.add(key);
    envVariables.remove(key);
    return castThis();
  }

  public T setEnvVariable(String key, String value) {
    envVariables.put(
      requireNonNull(key, "key can't be null"),
      requireNonNull(value, "value can't be null"));
    return castThis();
  }


  public T setEsInstallation(EsInstallation esInstallation) {
    this.esInstallation = esInstallation;
    return castThis();
  }

  @CheckForNull
  public EsInstallation getEsInstallation() {
    return esInstallation;
  }
}