diff --git a/server/src/main/java/org/elasticsearch/plugins/PluginsService.java b/server/src/main/java/org/elasticsearch/plugins/PluginsService.java index be39afebe203d..be8f9b303f4eb 100644 --- a/server/src/main/java/org/elasticsearch/plugins/PluginsService.java +++ b/server/src/main/java/org/elasticsearch/plugins/PluginsService.java @@ -33,7 +33,6 @@ import org.elasticsearch.plugins.spi.SPIClassIterator; import java.io.IOException; -import java.io.UncheckedIOException; import java.lang.ModuleLayer.Controller; import java.lang.module.Configuration; import java.lang.module.ModuleFinder; @@ -99,6 +98,17 @@ record LoadedPlugin(PluginDescriptor descriptor, Plugin instance, ClassLoader lo private static final Logger logger = LogManager.getLogger(PluginsService.class); private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(PluginsService.class); + private static final Map> exportsServices; + + static { + Map> qualifiedExports = new HashMap<>(); + var loader = ServiceLoader.load(ModuleQualifiedExportsService.class, PluginsService.class.getClassLoader()); + for (var exportsService : loader) { + addExportsService(qualifiedExports, exportsService, exportsService.getClass().getModule().getName()); + } + exportsServices = Map.copyOf(qualifiedExports); + } + private final Settings settings; private final Path configPath; @@ -123,8 +133,7 @@ public PluginsService(Settings settings, Path configPath, Path modulesDirectory, this.settings = settings; this.configPath = configPath; - Map> qualifiedExports = new HashMap<>(); - loadExportsServices(qualifiedExports, PluginsService.class.getClassLoader()); + Map> qualifiedExports = new HashMap<>(exportsServices); addServerExportsService(qualifiedExports); Set seenBundles = new LinkedHashSet<>(); @@ -202,12 +211,21 @@ static void checkMandatoryPlugins(Set existingPlugins, Set manda } } + private static final Set officialPlugins; + + static { + try (var stream = PluginsService.class.getResourceAsStream("/plugins.txt")) { + officialPlugins = Streams.readAllLines(stream).stream().map(String::trim).collect(Collectors.toUnmodifiableSet()); + } catch (final IOException e) { + throw new AssertionError(e); + } + } + private static List getRuntimeInfos( PluginIntrospector inspector, List pluginDescriptors, Map plugins ) { - var officialPlugins = getOfficialPlugins(); List runtimeInfos = new ArrayList<>(); for (PluginDescriptor descriptor : pluginDescriptors) { LoadedPlugin plugin = plugins.get(descriptor.getName()); @@ -223,14 +241,6 @@ private static List getRuntimeInfos( return runtimeInfos; } - private static Set getOfficialPlugins() { - try (var stream = PluginsService.class.getResourceAsStream("/plugins.txt")) { - return Streams.readAllLines(stream).stream().map(String::trim).collect(Sets.toUnmodifiableSortedSet()); - } catch (final IOException e) { - throw new UncheckedIOException(e); - } - } - /** * Map a function over all plugins * @param function a function that takes a plugin and returns a result @@ -788,13 +798,6 @@ private static void exposeQualifiedExportsAndOpens(Module target, Map exportService.addExportsAndOpens(target)); } - private static void loadExportsServices(Map> qualifiedExports, ClassLoader classLoader) { - var loader = ServiceLoader.load(ModuleQualifiedExportsService.class, classLoader); - for (var exportsService : loader) { - addExportsService(qualifiedExports, exportsService, exportsService.getClass().getModule().getName()); - } - } - private static void addExportsService( Map> qualifiedExports, ModuleQualifiedExportsService exportsService,