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.

ConfigurationProvider.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.config;
  21. import java.util.Optional;
  22. import java.util.function.Function;
  23. import org.apache.commons.lang.ArrayUtils;
  24. import org.picocontainer.injectors.ProviderAdapter;
  25. import org.sonar.api.config.Configuration;
  26. import org.sonar.api.config.PropertyDefinition;
  27. import org.sonar.api.config.internal.Settings;
  28. import static java.util.function.Function.identity;
  29. import static org.sonar.api.config.internal.MultivalueProperty.parseAsCsv;
  30. public class ConfigurationProvider extends ProviderAdapter {
  31. private Configuration configuration;
  32. public Configuration provide(Settings settings) {
  33. if (configuration == null) {
  34. configuration = new ServerConfigurationAdapter(settings);
  35. }
  36. return configuration;
  37. }
  38. private static class ServerConfigurationAdapter implements Configuration {
  39. private static final Function<String, String> REPLACE_ENCODED_COMMAS = value -> value.replace("%2C", ",");
  40. private final Settings settings;
  41. private ServerConfigurationAdapter(Settings settings) {
  42. this.settings = settings;
  43. }
  44. @Override
  45. public Optional<String> get(String key) {
  46. return Optional.ofNullable(settings.getString(key));
  47. }
  48. @Override
  49. public boolean hasKey(String key) {
  50. return settings.hasKey(key);
  51. }
  52. @Override
  53. public String[] getStringArray(String key) {
  54. boolean multiValue = settings.getDefinition(key)
  55. .map(PropertyDefinition::multiValues)
  56. .orElse(false);
  57. return get(key)
  58. .map(v -> parseAsCsv(key, v, multiValue ? REPLACE_ENCODED_COMMAS : identity()))
  59. .orElse(ArrayUtils.EMPTY_STRING_ARRAY);
  60. }
  61. }
  62. }