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.

CoreExtensionsInstaller.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.core.extension;
  21. import java.lang.annotation.Annotation;
  22. import java.util.Arrays;
  23. import java.util.Collection;
  24. import java.util.Optional;
  25. import java.util.function.Predicate;
  26. import org.sonar.api.SonarRuntime;
  27. import org.sonar.api.config.Configuration;
  28. import org.sonar.api.config.internal.MapSettings;
  29. import org.sonar.api.utils.AnnotationUtils;
  30. import org.sonar.api.utils.log.Logger;
  31. import org.sonar.api.utils.log.Loggers;
  32. import org.sonar.core.platform.ExtensionContainer;
  33. import static java.util.Objects.requireNonNull;
  34. public abstract class CoreExtensionsInstaller {
  35. private static final Logger LOG = Loggers.get(CoreExtensionsInstaller.class);
  36. private final SonarRuntime sonarRuntime;
  37. private final CoreExtensionRepository coreExtensionRepository;
  38. private final Class<? extends Annotation> supportedAnnotationType;
  39. protected CoreExtensionsInstaller(SonarRuntime sonarRuntime, CoreExtensionRepository coreExtensionRepository, Class<? extends Annotation> supportedAnnotationType) {
  40. this.sonarRuntime = sonarRuntime;
  41. this.coreExtensionRepository = coreExtensionRepository;
  42. this.supportedAnnotationType = supportedAnnotationType;
  43. }
  44. public static Predicate<Object> noExtensionFilter() {
  45. return t -> true;
  46. }
  47. public static Predicate<Object> noAdditionalSideFilter() {
  48. return t -> true;
  49. }
  50. /**
  51. * @param container the container into which extensions will be installed
  52. * @param extensionFilter filters extensions added to {@link CoreExtension.Context}. When it returns false, the
  53. * extension is ignored as if it had never been added to the context.
  54. * @param additionalSideFilter applied on top of filtering on {@link #supportedAnnotationType} to decide whether
  55. * extension should be added to container as an object or only as a PropertyDefinition.
  56. */
  57. public void install(ExtensionContainer container, Predicate<Object> extensionFilter, Predicate<Object> additionalSideFilter) {
  58. coreExtensionRepository.loadedCoreExtensions()
  59. .forEach(coreExtension -> install(container, extensionFilter, additionalSideFilter, coreExtension));
  60. }
  61. private void install(ExtensionContainer container, Predicate<Object> extensionFilter, Predicate<Object> additionalSideFilter, CoreExtension coreExtension) {
  62. String coreExtensionName = coreExtension.getName();
  63. try {
  64. addDeclaredExtensions(container, extensionFilter, additionalSideFilter, coreExtension);
  65. LOG.debug("Installed core extension: " + coreExtensionName);
  66. coreExtensionRepository.installed(coreExtension);
  67. } catch (Exception e) {
  68. throw new RuntimeException("Failed to load core extension " + coreExtensionName, e);
  69. }
  70. }
  71. private void addDeclaredExtensions(ExtensionContainer container, Predicate<Object> extensionFilter,
  72. Predicate<Object> additionalSideFilter, CoreExtension coreExtension) {
  73. ContextImpl context = new ContextImpl(container, extensionFilter, additionalSideFilter, coreExtension.getName());
  74. coreExtension.load(context);
  75. }
  76. private <T> boolean hasSupportedAnnotation(T component) {
  77. return AnnotationUtils.getAnnotation(component, supportedAnnotationType) != null;
  78. }
  79. private class ContextImpl implements CoreExtension.Context {
  80. private final ExtensionContainer container;
  81. private final Predicate<Object> extensionFilter;
  82. private final Predicate<Object> additionalSideFilter;
  83. private final String extensionCategory;
  84. public ContextImpl(ExtensionContainer container, Predicate<Object> extensionFilter, Predicate<Object> additionalSideFilter, String extensionCategory) {
  85. this.container = container;
  86. this.extensionFilter = extensionFilter;
  87. this.additionalSideFilter = additionalSideFilter;
  88. this.extensionCategory = extensionCategory;
  89. }
  90. @Override
  91. public SonarRuntime getRuntime() {
  92. return sonarRuntime;
  93. }
  94. @Override
  95. public Configuration getBootConfiguration() {
  96. return Optional.ofNullable(container.getComponentByType(Configuration.class))
  97. .orElseGet(() -> new MapSettings().asConfig());
  98. }
  99. @Override
  100. public CoreExtension.Context addExtension(Object component) {
  101. requireNonNull(component, "component can't be null");
  102. if (!extensionFilter.test(component)) {
  103. return this;
  104. }
  105. if (!addSupportedExtension(container, additionalSideFilter, extensionCategory, component)) {
  106. container.declareExtension(extensionCategory, component);
  107. }
  108. return this;
  109. }
  110. @Override
  111. public final CoreExtension.Context addExtensions(Object component, Object... otherComponents) {
  112. addExtension(component);
  113. Arrays.stream(otherComponents).forEach(this::addExtension);
  114. return this;
  115. }
  116. @Override
  117. public <T> CoreExtension.Context addExtensions(Collection<T> components) {
  118. requireNonNull(components, "components can't be null");
  119. components.forEach(this::addExtension);
  120. return this;
  121. }
  122. private <T> boolean addSupportedExtension(ExtensionContainer container, Predicate<Object> additionalSideFilter,
  123. String extensionCategory, T component) {
  124. if (hasSupportedAnnotation(component) && additionalSideFilter.test(component)) {
  125. container.addExtension(extensionCategory, component);
  126. return true;
  127. }
  128. return false;
  129. }
  130. }
  131. }