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.

ServerPluginRepository.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Optional;
  27. import java.util.stream.Collectors;
  28. import javax.annotation.CheckForNull;
  29. import org.sonar.api.Plugin;
  30. import org.sonar.core.platform.PluginInfo;
  31. import org.sonar.core.platform.PluginRepository;
  32. import static com.google.common.base.Preconditions.checkArgument;
  33. import static java.lang.String.format;
  34. public class ServerPluginRepository implements PluginRepository {
  35. private final Map<String, ServerPlugin> pluginByKey = new HashMap<>();
  36. private final Map<ClassLoader, String> keysByClassLoader = new HashMap<>();
  37. public void addPlugins(List<ServerPlugin> plugins) {
  38. pluginByKey.putAll(plugins.stream().collect(Collectors.toMap(p -> p.getPluginInfo().getKey(), p -> p)));
  39. for (ServerPlugin p : plugins) {
  40. keysByClassLoader.put(p.getClassloader(), p.getPluginInfo().getKey());
  41. }
  42. }
  43. public void addPlugin(ServerPlugin plugin) {
  44. pluginByKey.put(plugin.getPluginInfo().getKey(), plugin);
  45. if (plugin.getInstance() != null) {
  46. keysByClassLoader.put(plugin.getInstance().getClass().getClassLoader(), plugin.getPluginInfo().getKey());
  47. }
  48. }
  49. @CheckForNull
  50. public String getPluginKey(Object extension) {
  51. return keysByClassLoader.get(extension.getClass().getClassLoader());
  52. }
  53. @Override
  54. public Collection<PluginInfo> getPluginInfos() {
  55. return Collections.unmodifiableCollection(pluginByKey.values().stream().map(ServerPlugin::getPluginInfo).collect(Collectors.toList()));
  56. }
  57. @Override
  58. public PluginInfo getPluginInfo(String key) {
  59. return getPlugin(key).getPluginInfo();
  60. }
  61. public ServerPlugin getPlugin(String key) {
  62. ServerPlugin plugin = pluginByKey.get(key);
  63. if (plugin == null) {
  64. throw new IllegalArgumentException(format("Plugin [%s] does not exist", key));
  65. }
  66. return plugin;
  67. }
  68. public Collection<ServerPlugin> getPlugins() {
  69. return Collections.unmodifiableCollection(pluginByKey.values());
  70. }
  71. public Optional<ServerPlugin> findPlugin(String key) {
  72. return Optional.ofNullable(pluginByKey.get(key));
  73. }
  74. @Override
  75. public Plugin getPluginInstance(String key) {
  76. ServerPlugin plugin = pluginByKey.get(key);
  77. checkArgument(plugin != null, "Plugin [%s] does not exist", key);
  78. return plugin.getInstance();
  79. }
  80. @Override
  81. public Collection<Plugin> getPluginInstances() {
  82. return pluginByKey.values().stream()
  83. .map(ServerPlugin::getInstance)
  84. .collect(Collectors.toList());
  85. }
  86. @Override
  87. public boolean hasPlugin(String key) {
  88. return pluginByKey.containsKey(key);
  89. }
  90. }