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

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