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

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