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.

ChildSettings.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.server.setting;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.Optional;
  25. import org.sonar.api.config.Configuration;
  26. import org.sonar.api.config.internal.Settings;
  27. import org.sonar.api.config.internal.ConfigurationBridge;
  28. import static java.util.Objects.requireNonNull;
  29. public class ChildSettings extends Settings {
  30. private final Settings parentSettings;
  31. private final Map<String, String> localProperties = new HashMap<>();
  32. public ChildSettings(Settings parentSettings) {
  33. super(parentSettings.getDefinitions(), parentSettings.getEncryption());
  34. this.parentSettings = parentSettings;
  35. }
  36. @Override
  37. protected Optional<String> get(String key) {
  38. String value = localProperties.get(key);
  39. if (value != null) {
  40. return Optional.of(value);
  41. }
  42. return parentSettings.getRawString(key);
  43. }
  44. @Override
  45. protected void set(String key, String value) {
  46. localProperties.put(
  47. requireNonNull(key, "key can't be null"),
  48. requireNonNull(value, "value can't be null").trim());
  49. }
  50. @Override
  51. protected void remove(String key) {
  52. localProperties.remove(key);
  53. }
  54. /**
  55. * Only returns the currently loaded properties.
  56. *
  57. * <p>
  58. * On the Web Server, global properties are loaded lazily when requested by name. Therefor,
  59. * this will return only global properties which have been requested using
  60. * {@link #get(String)} at least once prior to this call.
  61. */
  62. @Override
  63. public Map<String, String> getProperties() {
  64. // order is important. local properties override parent properties.
  65. ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
  66. builder.putAll(parentSettings.getProperties());
  67. builder.putAll(localProperties);
  68. return builder.build();
  69. }
  70. public Configuration asConfiguration() {
  71. return new ConfigurationBridge(this);
  72. }
  73. }