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.

ServerExtensionInstaller.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.server.plugins;
  21. import com.google.common.collect.ArrayListMultimap;
  22. import com.google.common.collect.ImmutableSet;
  23. import com.google.common.collect.ListMultimap;
  24. import java.lang.annotation.Annotation;
  25. import java.util.Collection;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Set;
  29. import java.util.stream.Collectors;
  30. import org.sonar.api.ExtensionProvider;
  31. import org.sonar.api.Plugin;
  32. import org.sonar.api.SonarRuntime;
  33. import org.sonar.api.config.Configuration;
  34. import org.sonar.api.internal.PluginContextImpl;
  35. import org.sonar.api.utils.AnnotationUtils;
  36. import org.sonar.api.utils.MessageException;
  37. import org.sonar.core.platform.ComponentContainer;
  38. import org.sonar.core.platform.PluginInfo;
  39. import org.sonar.core.platform.PluginRepository;
  40. import static java.util.Objects.requireNonNull;
  41. import static org.sonar.core.extension.ExtensionProviderSupport.isExtensionProvider;
  42. /**
  43. * Loads the plugins server extensions and injects them to DI container
  44. */
  45. public abstract class ServerExtensionInstaller {
  46. private static final Set<String> NO_MORE_COMPATIBLE_PLUGINS = ImmutableSet.of("authgithub", "authgitlab");
  47. private final SonarRuntime sonarRuntime;
  48. private final PluginRepository pluginRepository;
  49. private final Set<Class<? extends Annotation>> supportedAnnotationTypes;
  50. protected ServerExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository,
  51. Collection<Class<? extends Annotation>> supportedAnnotationTypes) {
  52. requireNonNull(supportedAnnotationTypes, "At least one supported annotation type must be specified");
  53. this.sonarRuntime = sonarRuntime;
  54. this.pluginRepository = pluginRepository;
  55. this.supportedAnnotationTypes = ImmutableSet.copyOf(supportedAnnotationTypes);
  56. }
  57. public void installExtensions(ComponentContainer container) {
  58. failWhenNoMoreCompatiblePlugins();
  59. ListMultimap<PluginInfo, Object> installedExtensionsByPlugin = ArrayListMultimap.create();
  60. for (PluginInfo pluginInfo : pluginRepository.getPluginInfos()) {
  61. try {
  62. String pluginKey = pluginInfo.getKey();
  63. Plugin plugin = pluginRepository.getPluginInstance(pluginKey);
  64. container.addExtension(pluginInfo, plugin);
  65. Plugin.Context context = new PluginContextImpl.Builder()
  66. .setSonarRuntime(sonarRuntime)
  67. .setBootConfiguration(container.getComponentByType(Configuration.class))
  68. .build();
  69. plugin.define(context);
  70. for (Object extension : context.getExtensions()) {
  71. if (installExtension(container, pluginInfo, extension, true) != null) {
  72. installedExtensionsByPlugin.put(pluginInfo, extension);
  73. } else {
  74. container.declareExtension(pluginInfo, extension);
  75. }
  76. }
  77. } catch (Throwable e) {
  78. // catch Throwable because we want to catch Error too (IncompatibleClassChangeError, ...)
  79. throw new IllegalStateException(String.format("Fail to load plugin %s [%s]", pluginInfo.getName(), pluginInfo.getKey()), e);
  80. }
  81. }
  82. for (Map.Entry<PluginInfo, Object> entry : installedExtensionsByPlugin.entries()) {
  83. PluginInfo pluginInfo = entry.getKey();
  84. try {
  85. Object extension = entry.getValue();
  86. if (isExtensionProvider(extension)) {
  87. ExtensionProvider provider = (ExtensionProvider) container.getComponentByKey(extension);
  88. installProvider(container, pluginInfo, provider);
  89. }
  90. } catch (Throwable e) {
  91. // catch Throwable because we want to catch Error too (IncompatibleClassChangeError, ...)
  92. throw new IllegalStateException(String.format("Fail to load plugin %s [%s]", pluginInfo.getName(), pluginInfo.getKey()), e);
  93. }
  94. }
  95. }
  96. private void failWhenNoMoreCompatiblePlugins() {
  97. List<String> noMoreCompatiblePluginNames = pluginRepository.getPluginInfos()
  98. .stream()
  99. .filter(pluginInfo -> NO_MORE_COMPATIBLE_PLUGINS.contains(pluginInfo.getKey()))
  100. .map(PluginInfo::getName)
  101. .collect(Collectors.toList());
  102. if (!noMoreCompatiblePluginNames.isEmpty()) {
  103. throw MessageException.of(String.format("Plugins '%s' are no more compatible with SonarQube", String.join(",", noMoreCompatiblePluginNames)));
  104. }
  105. }
  106. private void installProvider(ComponentContainer container, PluginInfo pluginInfo, ExtensionProvider provider) {
  107. Object obj = provider.provide();
  108. if (obj != null) {
  109. if (obj instanceof Iterable) {
  110. for (Object ext : (Iterable) obj) {
  111. installExtension(container, pluginInfo, ext, false);
  112. }
  113. } else {
  114. installExtension(container, pluginInfo, obj, false);
  115. }
  116. }
  117. }
  118. private Object installExtension(ComponentContainer container, PluginInfo pluginInfo, Object extension, boolean acceptProvider) {
  119. for (Class<? extends Annotation> supportedAnnotationType : supportedAnnotationTypes) {
  120. if (AnnotationUtils.getAnnotation(extension, supportedAnnotationType) != null) {
  121. if (!acceptProvider && isExtensionProvider(extension)) {
  122. throw new IllegalStateException("ExtensionProvider can not include providers itself: " + extension);
  123. }
  124. container.addExtension(pluginInfo, extension);
  125. return extension;
  126. }
  127. }
  128. return null;
  129. }
  130. }