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.

OrchestratorSettingsUtils.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.db;
  21. import java.io.File;
  22. import java.io.InputStream;
  23. import java.net.HttpURLConnection;
  24. import java.net.URI;
  25. import java.util.Map;
  26. import java.util.Properties;
  27. import org.apache.commons.io.FileUtils;
  28. import org.apache.commons.io.IOUtils;
  29. import org.apache.commons.lang.text.StrSubstitutor;
  30. import org.sonar.api.config.internal.Settings;
  31. import static org.apache.commons.lang.StringUtils.isEmpty;
  32. public class OrchestratorSettingsUtils {
  33. private OrchestratorSettingsUtils() {
  34. // prevents instantiation
  35. }
  36. public static void loadOrchestratorSettings(Settings settings) {
  37. String url = settings.getString("orchestrator.configUrl");
  38. if (isEmpty(url)) {
  39. return;
  40. }
  41. InputStream input = null;
  42. try {
  43. URI uri = new URI(url);
  44. if (url.startsWith("file:")) {
  45. File file = new File(uri);
  46. input = FileUtils.openInputStream(file);
  47. } else {
  48. HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
  49. int responseCode = connection.getResponseCode();
  50. if (responseCode >= 400) {
  51. throw new IllegalStateException("Fail to request: " + uri + ". Status code=" + responseCode);
  52. }
  53. input = connection.getInputStream();
  54. }
  55. Properties props = new Properties();
  56. props.load(input);
  57. settings.addProperties(props);
  58. for (Map.Entry<String, String> entry : settings.getProperties().entrySet()) {
  59. String interpolatedValue = StrSubstitutor.replace(entry.getValue(), System.getenv(), "${", "}");
  60. settings.setProperty(entry.getKey(), interpolatedValue);
  61. }
  62. } catch (Exception e) {
  63. throw new IllegalStateException("Cannot load Orchestrator properties from:" + url, e);
  64. } finally {
  65. IOUtils.closeQuietly(input);
  66. }
  67. }
  68. }