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 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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 org.apache.commons.lang.StringUtils.trimToEmpty;
  30. /**
  31. * For internal use
  32. *
  33. * @since 7.8
  34. */
  35. public class MetadataLoader {
  36. private static final String SQ_VERSION_FILE_PATH = "/sq-version.txt";
  37. private static final String EDITION_FILE_PATH = "/sonar-edition.txt";
  38. private MetadataLoader() {
  39. // only static methods
  40. }
  41. public static Version loadVersion(System2 system) {
  42. URL url = system.getResource(SQ_VERSION_FILE_PATH);
  43. try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8.name())) {
  44. String versionInFile = scanner.nextLine();
  45. return Version.parse(versionInFile);
  46. } catch (IOException e) {
  47. throw new IllegalStateException("Can not load " + SQ_VERSION_FILE_PATH + " from classpath ", e);
  48. }
  49. }
  50. public static SonarEdition loadEdition(System2 system) {
  51. URL url = system.getResource(EDITION_FILE_PATH);
  52. if (url == null) {
  53. return SonarEdition.COMMUNITY;
  54. }
  55. try (Scanner scanner = new Scanner(url.openStream(), StandardCharsets.UTF_8.name())) {
  56. String editionInFile = scanner.nextLine();
  57. return parseEdition(editionInFile);
  58. } catch (IOException e) {
  59. throw new IllegalStateException("Can not load " + EDITION_FILE_PATH + " from classpath", e);
  60. }
  61. }
  62. static SonarEdition parseEdition(String edition) {
  63. String str = trimToEmpty(edition.toUpperCase(Locale.ENGLISH));
  64. try {
  65. return SonarEdition.valueOf(str);
  66. } catch (IllegalArgumentException e) {
  67. throw new IllegalStateException(String.format("Invalid edition found in '%s': '%s'", EDITION_FILE_PATH, str));
  68. }
  69. }
  70. }