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.

JvmOptions.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.LinkedHashSet;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.stream.Collectors;
  29. import javax.annotation.CheckForNull;
  30. import javax.annotation.Nullable;
  31. import org.sonar.process.MessageException;
  32. import org.sonar.process.Props;
  33. import static java.lang.String.format;
  34. import static java.util.Objects.requireNonNull;
  35. import static java.util.stream.Collectors.joining;
  36. public class JvmOptions<T extends JvmOptions> {
  37. private static final String JVM_OPTION_NOT_NULL_ERROR_MESSAGE = "a JVM option can't be null";
  38. private final HashMap<String, String> mandatoryOptions = new HashMap<>();
  39. private final LinkedHashSet<String> options = new LinkedHashSet<>();
  40. public JvmOptions() {
  41. this(Collections.emptyMap());
  42. }
  43. public JvmOptions(Map<String, String> mandatoryJvmOptions) {
  44. requireNonNull(mandatoryJvmOptions, JVM_OPTION_NOT_NULL_ERROR_MESSAGE)
  45. .entrySet()
  46. .stream()
  47. .filter(e -> {
  48. requireNonNull(e.getKey(), "JVM option prefix can't be null");
  49. if (e.getKey().trim().isEmpty()) {
  50. throw new IllegalArgumentException("JVM option prefix can't be empty");
  51. }
  52. requireNonNull(e.getValue(), "JVM option value can't be null");
  53. return true;
  54. }).forEach(e -> {
  55. String key = e.getKey().trim();
  56. String value = e.getValue().trim();
  57. mandatoryOptions.put(key, value);
  58. add(key + value);
  59. });
  60. }
  61. public T addFromMandatoryProperty(Props props, String propertyName) {
  62. String value = props.nonNullValue(propertyName);
  63. if (!value.isEmpty()) {
  64. List<String> jvmOptions = Arrays.stream(value.split(" (?=-)")).map(String::trim).collect(Collectors.toList());
  65. checkOptionFormat(propertyName, jvmOptions);
  66. checkMandatoryOptionOverwrite(propertyName, jvmOptions);
  67. options.addAll(jvmOptions);
  68. }
  69. return castThis();
  70. }
  71. private static void checkOptionFormat(String propertyName, List<String> jvmOptionsFromProperty) {
  72. List<String> invalidOptions = jvmOptionsFromProperty.stream()
  73. .filter(JvmOptions::isInvalidOption)
  74. .collect(Collectors.toList());
  75. if (!invalidOptions.isEmpty()) {
  76. throw new MessageException(format(
  77. "a JVM option can't be empty and must start with '-'. The following JVM options defined by property '%s' are invalid: %s",
  78. propertyName,
  79. invalidOptions.stream()
  80. .collect(joining(", "))));
  81. }
  82. }
  83. private void checkMandatoryOptionOverwrite(String propertyName, List<String> jvmOptionsFromProperty) {
  84. List<Match> matches = jvmOptionsFromProperty.stream()
  85. .map(jvmOption -> new Match(jvmOption, mandatoryOptionFor(jvmOption)))
  86. .filter(match -> match.getMandatoryOption() != null)
  87. .collect(Collectors.toList());
  88. if (!matches.isEmpty()) {
  89. throw new MessageException(format(
  90. "a JVM option can't overwrite mandatory JVM options. The following JVM options defined by property '%s' are invalid: %s",
  91. propertyName,
  92. matches.stream()
  93. .map(m -> m.getOption() + " overwrites " + m.mandatoryOption.getKey() + m.mandatoryOption.getValue())
  94. .collect(joining(", "))));
  95. }
  96. }
  97. /**
  98. * Add an option.
  99. * Argument is trimmed before being added.
  100. *
  101. * @throws IllegalArgumentException if argument is empty or does not start with {@code -}.
  102. */
  103. public T add(String str) {
  104. requireNonNull(str, JVM_OPTION_NOT_NULL_ERROR_MESSAGE);
  105. String value = str.trim();
  106. if (isInvalidOption(value)) {
  107. throw new IllegalArgumentException("a JVM option can't be empty and must start with '-'");
  108. }
  109. checkMandatoryOptionOverwrite(value);
  110. options.add(value);
  111. return castThis();
  112. }
  113. private void checkMandatoryOptionOverwrite(String value) {
  114. Map.Entry<String, String> overriddenMandatoryOption = mandatoryOptionFor(value);
  115. if (overriddenMandatoryOption != null) {
  116. throw new MessageException(String.format(
  117. "a JVM option can't overwrite mandatory JVM options. %s overwrites %s",
  118. value,
  119. overriddenMandatoryOption.getKey() + overriddenMandatoryOption.getValue()));
  120. }
  121. }
  122. @CheckForNull
  123. private Map.Entry<String, String> mandatoryOptionFor(String jvmOption) {
  124. return mandatoryOptions.entrySet().stream()
  125. .filter(s -> jvmOption.startsWith(s.getKey()) && !jvmOption.equals(s.getKey() + s.getValue()))
  126. .findFirst()
  127. .orElse(null);
  128. }
  129. private static boolean isInvalidOption(String value) {
  130. return value.isEmpty() || !value.startsWith("-");
  131. }
  132. @SuppressWarnings("unchecked")
  133. private T castThis() {
  134. return (T) this;
  135. }
  136. public List<String> getAll() {
  137. return new ArrayList<>(options);
  138. }
  139. @Override
  140. public String toString() {
  141. return options.toString();
  142. }
  143. private static final class Match {
  144. private final String option;
  145. private final Map.Entry<String, String> mandatoryOption;
  146. private Match(String option, @Nullable Map.Entry<String, String> mandatoryOption) {
  147. this.option = option;
  148. this.mandatoryOption = mandatoryOption;
  149. }
  150. String getOption() {
  151. return option;
  152. }
  153. @CheckForNull
  154. Map.Entry<String, String> getMandatoryOption() {
  155. return mandatoryOption;
  156. }
  157. }
  158. }