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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.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.Set;
  27. import org.sonar.api.Plugin;
  28. import org.sonar.api.SonarRuntime;
  29. import org.sonar.api.config.Configuration;
  30. import org.sonar.api.internal.PluginContextImpl;
  31. import org.sonar.api.utils.AnnotationUtils;
  32. import org.sonar.core.platform.ComponentContainer;
  33. import org.sonar.core.platform.PluginInfo;
  34. import org.sonar.core.platform.PluginRepository;
  35. import static java.lang.String.format;
  36. import static java.util.Objects.requireNonNull;
  37. /**
  38. * Loads the plugins server extensions and injects them to DI container
  39. */
  40. public abstract class ServerExtensionInstaller {
  41. private final SonarRuntime sonarRuntime;
  42. private final PluginRepository pluginRepository;
  43. private final Set<Class<? extends Annotation>> supportedAnnotationTypes;
  44. protected ServerExtensionInstaller(SonarRuntime sonarRuntime, PluginRepository pluginRepository, Collection<Class<? extends Annotation>> supportedAnnotationTypes) {
  45. requireNonNull(supportedAnnotationTypes, "At least one supported annotation type must be specified");
  46. this.sonarRuntime = sonarRuntime;
  47. this.pluginRepository = pluginRepository;
  48. this.supportedAnnotationTypes = ImmutableSet.copyOf(supportedAnnotationTypes);
  49. }
  50. public void installExtensions(ComponentContainer container) {
  51. ListMultimap<PluginInfo, Object> installedExtensionsByPlugin = ArrayListMultimap.create();
  52. for (PluginInfo pluginInfo : pluginRepository.getPluginInfos()) {
  53. try {
  54. String pluginKey = pluginInfo.getKey();
  55. Plugin plugin = pluginRepository.getPluginInstance(pluginKey);
  56. container.addExtension(pluginInfo, plugin);
  57. Plugin.Context context = new PluginContextImpl.Builder()
  58. .setSonarRuntime(sonarRuntime)
  59. .setBootConfiguration(container.getComponentByType(Configuration.class))
  60. .build();
  61. plugin.define(context);
  62. for (Object extension : context.getExtensions()) {
  63. if (installExtension(container, pluginInfo, extension) != null) {
  64. installedExtensionsByPlugin.put(pluginInfo, extension);
  65. } else {
  66. container.declareExtension(pluginInfo, extension);
  67. }
  68. }
  69. } catch (Throwable e) {
  70. // catch Throwable because we want to catch Error too (IncompatibleClassChangeError, ...)
  71. throw new IllegalStateException(format("Fail to load plugin %s [%s]", pluginInfo.getName(), pluginInfo.getKey()), e);
  72. }
  73. }
  74. }
  75. private Object installExtension(ComponentContainer container, PluginInfo pluginInfo, Object extension) {
  76. for (Class<? extends Annotation> supportedAnnotationType : supportedAnnotationTypes) {
  77. if (AnnotationUtils.getAnnotation(extension, supportedAnnotationType) != null) {
  78. container.addExtension(pluginInfo, extension);
  79. return extension;
  80. }
  81. }
  82. return null;
  83. }
  84. }