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.

Settings.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.api.config.internal;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Optional;
  27. import java.util.Properties;
  28. import java.util.stream.Collectors;
  29. import javax.annotation.CheckForNull;
  30. import javax.annotation.Nullable;
  31. import org.apache.commons.lang.ArrayUtils;
  32. import org.apache.commons.lang.StringUtils;
  33. import org.sonar.api.config.PropertyDefinition;
  34. import org.sonar.api.config.PropertyDefinitions;
  35. import org.sonar.api.utils.DateUtils;
  36. import static java.util.Objects.requireNonNull;
  37. import static org.apache.commons.lang.StringUtils.trim;
  38. /**
  39. * Implementation of the deprecated Settings interface
  40. */
  41. public abstract class Settings extends org.sonar.api.config.Settings {
  42. private final PropertyDefinitions definitions;
  43. private final Encryption encryption;
  44. protected Settings(PropertyDefinitions definitions, Encryption encryption) {
  45. this.definitions = requireNonNull(definitions);
  46. this.encryption = requireNonNull(encryption);
  47. }
  48. protected abstract Optional<String> get(String key);
  49. /**
  50. * Add the settings with the specified key and value, both are trimmed and neither can be null.
  51. *
  52. * @throws NullPointerException if {@code key} and/or {@code value} is {@code null}.
  53. */
  54. protected abstract void set(String key, String value);
  55. protected abstract void remove(String key);
  56. /**
  57. * Immutable map of the properties that have non-default values.
  58. * The default values defined by {@link PropertyDefinitions} are ignored,
  59. * so the returned values are not the effective values. Basically only
  60. * the non-empty results of {@link #getRawString(String)} are returned.
  61. * <p>
  62. * Values are not decrypted if they are encrypted with a secret key.
  63. * </p>
  64. */
  65. public abstract Map<String, String> getProperties();
  66. public Encryption getEncryption() {
  67. return encryption;
  68. }
  69. /**
  70. * The value that overrides the default value. It
  71. * may be encrypted with a secret key. Use {@link #getString(String)} to get
  72. * the effective and decrypted value.
  73. *
  74. * @since 6.1
  75. */
  76. public Optional<String> getRawString(String key) {
  77. return get(definitions.validKey(requireNonNull(key)));
  78. }
  79. /**
  80. * All the property definitions declared by core and plugins.
  81. */
  82. public PropertyDefinitions getDefinitions() {
  83. return definitions;
  84. }
  85. /**
  86. * The definition related to the specified property. It may
  87. * be empty.
  88. *
  89. * @since 6.1
  90. */
  91. public Optional<PropertyDefinition> getDefinition(String key) {
  92. return Optional.ofNullable(definitions.get(key));
  93. }
  94. /**
  95. * @return {@code true} if the property has a non-default value, else {@code false}.
  96. */
  97. @Override
  98. public boolean hasKey(String key) {
  99. return getRawString(key).isPresent();
  100. }
  101. @CheckForNull
  102. public String getDefaultValue(String key) {
  103. return definitions.getDefaultValue(key);
  104. }
  105. public boolean hasDefaultValue(String key) {
  106. return StringUtils.isNotEmpty(getDefaultValue(key));
  107. }
  108. /**
  109. * The effective value of the specified property. Can return
  110. * {@code null} if the property is not set and has no
  111. * defined default value.
  112. * <p>
  113. * If the property is encrypted with a secret key,
  114. * then the returned value is decrypted.
  115. * </p>
  116. *
  117. * @throws IllegalStateException if value is encrypted but fails to be decrypted.
  118. */
  119. @CheckForNull
  120. @Override
  121. public String getString(String key) {
  122. String effectiveKey = definitions.validKey(key);
  123. Optional<String> value = getRawString(effectiveKey);
  124. if (!value.isPresent()) {
  125. // default values cannot be encrypted, so return value as-is.
  126. return getDefaultValue(effectiveKey);
  127. }
  128. if (encryption.isEncrypted(value.get())) {
  129. try {
  130. return encryption.decrypt(value.get());
  131. } catch (Exception e) {
  132. throw new IllegalStateException("Fail to decrypt the property " + effectiveKey + ". Please check your secret key.", e);
  133. }
  134. }
  135. return value.get();
  136. }
  137. /**
  138. * Effective value as boolean. It is {@code false} if {@link #getString(String)}
  139. * does not return {@code "true"}, even if it's not a boolean representation.
  140. *
  141. * @return {@code true} if the effective value is {@code "true"}, else {@code false}.
  142. */
  143. @Override
  144. public boolean getBoolean(String key) {
  145. String value = getString(key);
  146. return StringUtils.isNotEmpty(value) && Boolean.parseBoolean(value);
  147. }
  148. /**
  149. * Effective value as {@code int}.
  150. *
  151. * @return the value as {@code int}. If the property does not have value nor default value, then {@code 0} is returned.
  152. * @throws NumberFormatException if value is not empty and is not a parsable integer
  153. */
  154. @Override
  155. public int getInt(String key) {
  156. String value = getString(key);
  157. if (StringUtils.isNotEmpty(value)) {
  158. return Integer.parseInt(value);
  159. }
  160. return 0;
  161. }
  162. /**
  163. * Effective value as {@code long}.
  164. *
  165. * @return the value as {@code long}. If the property does not have value nor default value, then {@code 0L} is returned.
  166. * @throws NumberFormatException if value is not empty and is not a parsable {@code long}
  167. */
  168. @Override
  169. public long getLong(String key) {
  170. String value = getString(key);
  171. if (StringUtils.isNotEmpty(value)) {
  172. return Long.parseLong(value);
  173. }
  174. return 0L;
  175. }
  176. /**
  177. * Effective value as {@link Date}, without time fields. Format is {@link DateUtils#DATE_FORMAT}.
  178. *
  179. * @return the value as a {@link Date}. If the property does not have value nor default value, then {@code null} is returned.
  180. * @throws RuntimeException if value is not empty and is not in accordance with {@link DateUtils#DATE_FORMAT}.
  181. */
  182. @CheckForNull
  183. @Override
  184. public Date getDate(String key) {
  185. String value = getString(key);
  186. if (StringUtils.isNotEmpty(value)) {
  187. return DateUtils.parseDate(value);
  188. }
  189. return null;
  190. }
  191. /**
  192. * Effective value as {@link Date}, with time fields. Format is {@link DateUtils#DATETIME_FORMAT}.
  193. *
  194. * @return the value as a {@link Date}. If the property does not have value nor default value, then {@code null} is returned.
  195. * @throws RuntimeException if value is not empty and is not in accordance with {@link DateUtils#DATETIME_FORMAT}.
  196. */
  197. @CheckForNull
  198. @Override
  199. public Date getDateTime(String key) {
  200. String value = getString(key);
  201. if (StringUtils.isNotEmpty(value)) {
  202. return DateUtils.parseDateTime(value);
  203. }
  204. return null;
  205. }
  206. /**
  207. * Effective value as {@code Float}.
  208. *
  209. * @return the value as {@code Float}. If the property does not have value nor default value, then {@code null} is returned.
  210. * @throws NumberFormatException if value is not empty and is not a parsable number
  211. */
  212. @CheckForNull
  213. @Override
  214. public Float getFloat(String key) {
  215. String value = getString(key);
  216. if (StringUtils.isNotEmpty(value)) {
  217. try {
  218. return Float.valueOf(value);
  219. } catch (NumberFormatException e) {
  220. throw new IllegalStateException(String.format("The property '%s' is not a float value", key));
  221. }
  222. }
  223. return null;
  224. }
  225. /**
  226. * Effective value as {@code Double}.
  227. *
  228. * @return the value as {@code Double}. If the property does not have value nor default value, then {@code null} is returned.
  229. * @throws NumberFormatException if value is not empty and is not a parsable number
  230. */
  231. @CheckForNull
  232. @Override
  233. public Double getDouble(String key) {
  234. String value = getString(key);
  235. if (StringUtils.isNotEmpty(value)) {
  236. try {
  237. return Double.valueOf(value);
  238. } catch (NumberFormatException e) {
  239. throw new IllegalStateException(String.format("The property '%s' is not a double value", key));
  240. }
  241. }
  242. return null;
  243. }
  244. /**
  245. * Value is split by comma and trimmed. Never returns null.
  246. * <br>
  247. * Examples :
  248. * <ul>
  249. * <li>"one,two,three " -&gt; ["one", "two", "three"]</li>
  250. * <li>" one, two, three " -&gt; ["one", "two", "three"]</li>
  251. * <li>"one, , three" -&gt; ["one", "", "three"]</li>
  252. * </ul>
  253. */
  254. @Override
  255. public String[] getStringArray(String key) {
  256. String effectiveKey = definitions.validKey(key);
  257. Optional<PropertyDefinition> def = getDefinition(effectiveKey);
  258. if ((def.isPresent()) && (def.get().multiValues())) {
  259. String value = getString(key);
  260. if (value == null) {
  261. return ArrayUtils.EMPTY_STRING_ARRAY;
  262. }
  263. return Arrays.stream(value.split(",", -1)).map(String::trim)
  264. .map(s -> s.replace("%2C", ","))
  265. .toArray(String[]::new);
  266. }
  267. return getStringArrayBySeparator(key, ",");
  268. }
  269. /**
  270. * Value is split by carriage returns.
  271. *
  272. * @return non-null array of lines. The line termination characters are excluded.
  273. * @since 3.2
  274. */
  275. @Override
  276. public String[] getStringLines(String key) {
  277. String value = getString(key);
  278. if (StringUtils.isEmpty(value)) {
  279. return new String[0];
  280. }
  281. return value.split("\r?\n|\r", -1);
  282. }
  283. /**
  284. * Value is split and trimmed.
  285. */
  286. @Override
  287. public String[] getStringArrayBySeparator(String key, String separator) {
  288. String value = getString(key);
  289. if (value != null) {
  290. String[] strings = StringUtils.splitByWholeSeparator(value, separator);
  291. String[] result = new String[strings.length];
  292. for (int index = 0; index < strings.length; index++) {
  293. result[index] = trim(strings[index]);
  294. }
  295. return result;
  296. }
  297. return ArrayUtils.EMPTY_STRING_ARRAY;
  298. }
  299. public Settings appendProperty(String key, @Nullable String value) {
  300. Optional<String> existingValue = getRawString(definitions.validKey(key));
  301. String newValue;
  302. if (!existingValue.isPresent()) {
  303. newValue = trim(value);
  304. } else {
  305. newValue = existingValue.get() + "," + trim(value);
  306. }
  307. return setProperty(key, newValue);
  308. }
  309. public Settings setProperty(String key, @Nullable String[] values) {
  310. requireNonNull(key, "key can't be null");
  311. String effectiveKey = key.trim();
  312. Optional<PropertyDefinition> def = getDefinition(effectiveKey);
  313. if (!def.isPresent() || (!def.get().multiValues())) {
  314. throw new IllegalStateException("Fail to set multiple values on a single value property " + key);
  315. }
  316. String text = null;
  317. if (values != null) {
  318. List<String> escaped = new ArrayList<>();
  319. for (String value : values) {
  320. if (null != value) {
  321. escaped.add(value.replace(",", "%2C"));
  322. } else {
  323. escaped.add("");
  324. }
  325. }
  326. String escapedValue = escaped.stream().collect(Collectors.joining(","));
  327. text = trim(escapedValue);
  328. }
  329. return setProperty(key, text);
  330. }
  331. /**
  332. * Change a property value in a restricted scope only, depending on execution context. New value
  333. * is <b>never</b> persisted. New value is ephemeral and kept in memory only:
  334. * <ul>
  335. * <li>during current analysis in the case of scanner stack</li>
  336. * <li>during processing of current HTTP request in the case of web server stack</li>
  337. * <li>during execution of current task in the case of Compute Engine stack</li>
  338. * </ul>
  339. * Property is temporarily removed if the parameter {@code value} is {@code null}
  340. */
  341. public Settings setProperty(String key, @Nullable String value) {
  342. String validKey = definitions.validKey(key);
  343. if (value == null) {
  344. removeProperty(validKey);
  345. } else {
  346. set(validKey, trim(value));
  347. }
  348. return this;
  349. }
  350. /**
  351. * @see #setProperty(String, String)
  352. */
  353. public Settings setProperty(String key, @Nullable Boolean value) {
  354. return setProperty(key, value == null ? null : String.valueOf(value));
  355. }
  356. /**
  357. * @see #setProperty(String, String)
  358. */
  359. public Settings setProperty(String key, @Nullable Integer value) {
  360. return setProperty(key, value == null ? null : String.valueOf(value));
  361. }
  362. /**
  363. * @see #setProperty(String, String)
  364. */
  365. public Settings setProperty(String key, @Nullable Long value) {
  366. return setProperty(key, value == null ? null : String.valueOf(value));
  367. }
  368. /**
  369. * @see #setProperty(String, String)
  370. */
  371. public Settings setProperty(String key, @Nullable Double value) {
  372. return setProperty(key, value == null ? null : String.valueOf(value));
  373. }
  374. /**
  375. * @see #setProperty(String, String)
  376. */
  377. public Settings setProperty(String key, @Nullable Float value) {
  378. return setProperty(key, value == null ? null : String.valueOf(value));
  379. }
  380. /**
  381. * @see #setProperty(String, String)
  382. */
  383. public Settings setProperty(String key, @Nullable Date date) {
  384. return setProperty(key, date, false);
  385. }
  386. public Settings addProperties(Map<String, String> props) {
  387. for (Map.Entry<String, String> entry : props.entrySet()) {
  388. setProperty(entry.getKey(), entry.getValue());
  389. }
  390. return this;
  391. }
  392. public Settings addProperties(Properties props) {
  393. for (Map.Entry<Object, Object> entry : props.entrySet()) {
  394. setProperty(entry.getKey().toString(), entry.getValue().toString());
  395. }
  396. return this;
  397. }
  398. /**
  399. * @see #setProperty(String, String)
  400. */
  401. public Settings setProperty(String key, @Nullable Date date, boolean includeTime) {
  402. if (date == null) {
  403. return removeProperty(key);
  404. }
  405. return setProperty(key, includeTime ? DateUtils.formatDateTime(date) : DateUtils.formatDate(date));
  406. }
  407. public Settings removeProperty(String key) {
  408. remove(key);
  409. return this;
  410. }
  411. @Override
  412. public List<String> getKeysStartingWith(String prefix) {
  413. return getProperties().keySet().stream()
  414. .filter(key -> StringUtils.startsWith(key, prefix))
  415. .collect(Collectors.toList());
  416. }
  417. }