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.

JavaCommand.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.process.monitor;
  21. import org.apache.commons.lang.StringUtils;
  22. import javax.annotation.Nullable;
  23. import java.io.File;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.LinkedHashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Properties;
  31. public class JavaCommand {
  32. // unique key among the group of commands to launch
  33. private final String key;
  34. private File workDir;
  35. // for example -Xmx1G
  36. private final List<String> javaOptions = new ArrayList<String>();
  37. // entry point
  38. private String className;
  39. // relative path to JAR files
  40. private final List<String> classpath = new ArrayList<String>();
  41. // program arguments (parameters of main(String[])
  42. private final Map<String, String> arguments = new LinkedHashMap<String, String>();
  43. private final Map<String, String> envVariables = new HashMap<String, String>(System.getenv());
  44. private File tempDir = null;
  45. public JavaCommand(String key) {
  46. this.key = key;
  47. }
  48. public String getKey() {
  49. return key;
  50. }
  51. public File getWorkDir() {
  52. return workDir;
  53. }
  54. public JavaCommand setWorkDir(File workDir) {
  55. this.workDir = workDir;
  56. return this;
  57. }
  58. public File getTempDir() {
  59. return tempDir;
  60. }
  61. public JavaCommand setTempDir(File tempDir) {
  62. this.tempDir = tempDir;
  63. return this;
  64. }
  65. public List<String> getJavaOptions() {
  66. return javaOptions;
  67. }
  68. public JavaCommand addJavaOption(String s) {
  69. if (StringUtils.isNotBlank(s)) {
  70. javaOptions.add(s);
  71. }
  72. return this;
  73. }
  74. public JavaCommand addJavaOptions(String s) {
  75. for (String opt : s.split(" ")) {
  76. addJavaOption(opt);
  77. }
  78. return this;
  79. }
  80. public String getClassName() {
  81. return className;
  82. }
  83. public JavaCommand setClassName(String className) {
  84. this.className = className;
  85. return this;
  86. }
  87. public List<String> getClasspath() {
  88. return classpath;
  89. }
  90. public JavaCommand addClasspath(String s) {
  91. classpath.add(s);
  92. return this;
  93. }
  94. public Map<String, String> getArguments() {
  95. return arguments;
  96. }
  97. public JavaCommand setArgument(String key, @Nullable String value) {
  98. if (value == null) {
  99. arguments.remove(key);
  100. } else {
  101. arguments.put(key, value);
  102. }
  103. return this;
  104. }
  105. public JavaCommand setArguments(Properties args) {
  106. for (Map.Entry<Object, Object> entry : args.entrySet()) {
  107. setArgument(entry.getKey().toString(), entry.getValue() != null ? entry.getValue().toString() : null);
  108. }
  109. return this;
  110. }
  111. public Map<String, String> getEnvVariables() {
  112. return envVariables;
  113. }
  114. public JavaCommand setEnvVariable(String key, @Nullable String value) {
  115. if (value == null) {
  116. envVariables.remove(key);
  117. } else {
  118. envVariables.put(key, value);
  119. }
  120. return this;
  121. }
  122. @Override
  123. public String toString() {
  124. final StringBuilder sb = new StringBuilder("JavaCommand{");
  125. sb.append("workDir=").append(workDir);
  126. sb.append(", javaOptions=").append(javaOptions);
  127. sb.append(", className='").append(className).append('\'');
  128. sb.append(", classpath=").append(classpath);
  129. sb.append(", arguments=").append(arguments);
  130. sb.append(", envVariables=").append(envVariables);
  131. sb.append('}');
  132. return sb.toString();
  133. }
  134. }