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.

DefaultServer.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.scanner.platform;
  21. import java.util.Date;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.sonar.api.CoreProperties;
  24. import org.sonar.api.SonarRuntime;
  25. import org.sonar.api.config.Configuration;
  26. import org.sonar.api.platform.Server;
  27. import org.sonar.api.utils.DateUtils;
  28. import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
  29. import static org.apache.commons.lang.StringUtils.trimToEmpty;
  30. public class DefaultServer extends Server {
  31. private final Configuration settings;
  32. private final DefaultScannerWsClient client;
  33. private final SonarRuntime runtime;
  34. public DefaultServer(Configuration settings, DefaultScannerWsClient client, SonarRuntime runtime) {
  35. this.settings = settings;
  36. this.client = client;
  37. this.runtime = runtime;
  38. }
  39. @Override
  40. public String getId() {
  41. return settings.get(CoreProperties.SERVER_ID).orElseThrow(() -> new IllegalStateException("Mandatory"));
  42. }
  43. @Override
  44. public String getVersion() {
  45. return runtime.getApiVersion().toString();
  46. }
  47. @Override
  48. public Date getStartedAt() {
  49. String dateString = settings.get(CoreProperties.SERVER_STARTTIME).orElseThrow(() -> new IllegalStateException("Mandatory"));
  50. return DateUtils.parseDateTime(dateString);
  51. }
  52. @Override
  53. public String getContextPath() {
  54. return null;
  55. }
  56. @Override
  57. public String getPublicRootUrl() {
  58. String baseUrl = trimToEmpty(settings.get(CoreProperties.SERVER_BASE_URL).orElse(""));
  59. if (baseUrl.isEmpty()) {
  60. // If server base URL was not configured in Sonar server then it is better to take URL configured on batch side
  61. baseUrl = client.baseUrl();
  62. }
  63. return StringUtils.removeEnd(baseUrl, "/");
  64. }
  65. @Override
  66. public boolean isSecured() {
  67. return false;
  68. }
  69. @Override
  70. public String getPermanentServerId() {
  71. return getId();
  72. }
  73. }