From 7b4d7d7ad27d1321b4aa49c6d03e14e1365cbc63 Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Fri, 13 Dec 2024 20:29:36 -0500 Subject: [PATCH] Ensure only single instance for each classpath extension Signed-off-by: Craig Perkins --- .../java/org/opensearch/plugins/PluginsService.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/opensearch/plugins/PluginsService.java b/server/src/main/java/org/opensearch/plugins/PluginsService.java index a6298b8b4da6f..c3db32874d260 100644 --- a/server/src/main/java/org/opensearch/plugins/PluginsService.java +++ b/server/src/main/java/org/opensearch/plugins/PluginsService.java @@ -159,8 +159,8 @@ public PluginsService( pluginsLoaded.add(new Tuple<>(pluginInfo, plugin)); pluginsList.add(pluginInfo); pluginsNames.add(pluginInfo.getName()); - loadExtensions(pluginsLoaded); } + loadExtensions(pluginsLoaded); Set seenBundles = new LinkedHashSet<>(); List modulesList = new ArrayList<>(); @@ -571,9 +571,17 @@ private static void loadExtensionsForPlugin(ExtensiblePlugin extensiblePlugin, L ExtensiblePlugin.ExtensionLoader extensionLoader = new ExtensiblePlugin.ExtensionLoader() { @Override public List loadExtensions(Class extensionPointType) { + Set> seenClasses = new LinkedHashSet<>(); List result = new ArrayList<>(); + for (Plugin extendingPlugin : extendingPlugins) { - result.addAll(createExtensions(extensionPointType, extendingPlugin)); + List extensions = createExtensions(extensionPointType, extendingPlugin); + for (T extension : extensions) { + // Only add if we haven't seen this class before, needed for classpath extensions for testing + if (seenClasses.add(extension.getClass())) { + result.add(extension); + } + } } return Collections.unmodifiableList(result); }