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.

MetadataLoader.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.internal;
  21. import java.io.IOException;
  22. import java.net.URL;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.Locale;
  25. import java.util.Scanner;
  26. import org.sonar.api.SonarEdition;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.api.utils.Version;
  29. import static java.lang.String.format;
  30. import static org.apache.commons.lang3.StringUtils.trimToEmpty;
  31. /**
  32. * For internal use
  33. *
  34. * @since 7.8
  35. */
  36. public class MetadataLoader {
  37. private static final String SQ_VERSION_FILE_PATH = "/sq-version.txt";
  38. private static final String SONAR_API_VERSION_FILE_PATH = "/sonar-api-version.txt";
  39. private static final String EDITION_FILE_PATH = "/sonar-edition.txt";
  40. private static final String SQ_VERSION_EOL_FILE_PATH = "/sq-version-eol.txt";
  41. public static final String CAN_NOT_LOAD_FROM_CLASSPATH = "Can not load %s from classpath";
  42. private MetadataLoader() {
  43. // only static methods
  44. }
  45. public static Version loadApiVersion(System2 system) {
  46. return getVersion(system, SONAR_API_VERSION_FILE_PATH);
  47. }
  48. public static Version loadSQVersion(System2 system) {
  49. return getVersion(system, SQ_VERSION_FILE_PATH);
  50. }
  51. public static String loadSqVersionEol(System2 system) {
  52. return getParamFromFile(system, SQ_VERSION_EOL_FILE_PATH);
  53. }
  54. private static Version getVersion(System2 system, String versionFilePath) {
  55. return Version.parse(getParamFromFile(system, versionFilePath));
  56. }
  57. private static String getParamFromFile(System2 system, String filePath) {
  58. URL url = system.getResource(filePath);
  59. try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8)) {
  60. return scanner.nextLine();
  61. } catch (IOException e) {
  62. throw new IllegalStateException(format(CAN_NOT_LOAD_FROM_CLASSPATH, filePath), e);
  63. }
  64. }
  65. public static SonarEdition loadEdition(System2 system) {
  66. URL url = system.getResource(EDITION_FILE_PATH);
  67. if (url == null) {
  68. return SonarEdition.COMMUNITY;
  69. }
  70. try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8)) {
  71. String editionInFile = scanner.nextLine();
  72. return parseEdition(editionInFile);
  73. } catch (IOException e) {
  74. throw new IllegalStateException(format(CAN_NOT_LOAD_FROM_CLASSPATH, EDITION_FILE_PATH), e);
  75. }
  76. }
  77. static SonarEdition parseEdition(String edition) {
  78. String str = trimToEmpty(edition.toUpperCase(Locale.ENGLISH));
  79. try {
  80. return SonarEdition.valueOf(str);
  81. } catch (IllegalArgumentException e) {
  82. throw new IllegalStateException(format("Invalid edition found in '%s': '%s'", EDITION_FILE_PATH, str));
  83. }
  84. }
  85. }