@@ -118,15 +118,30 @@ public static LayerAndLoader ofLoader(ClassLoader loader) {
118118 * @param modulesDirectory The directory modules exist in, or null if modules should not be loaded from the filesystem
119119 * @param pluginsDirectory The directory plugins exist in, or null if plugins should not be loaded from the filesystem
120120 */
121- @ SuppressWarnings ("this-escape" )
122- public PluginsLoader (Path modulesDirectory , Path pluginsDirectory ) {
121+ public static PluginsLoader createPluginsLoader (Path modulesDirectory , Path pluginsDirectory ) {
122+ return createPluginsLoader (modulesDirectory , pluginsDirectory , true );
123+ }
123124
124- Map <String , List <ModuleQualifiedExportsService >> qualifiedExports = new HashMap <>(ModuleQualifiedExportsService .getBootServices ());
125- addServerExportsService (qualifiedExports );
125+ /**
126+ * Constructs a new PluginsLoader
127+ *
128+ * @param modulesDirectory The directory modules exist in, or null if modules should not be loaded from the filesystem
129+ * @param pluginsDirectory The directory plugins exist in, or null if plugins should not be loaded from the filesystem
130+ * @param withServerExports {@code true} to add server module exports
131+ */
132+ public static PluginsLoader createPluginsLoader (Path modulesDirectory , Path pluginsDirectory , boolean withServerExports ) {
133+ Map <String , List <ModuleQualifiedExportsService >> qualifiedExports ;
134+ if (withServerExports ) {
135+ qualifiedExports = new HashMap <>(ModuleQualifiedExportsService .getBootServices ());
136+ addServerExportsService (qualifiedExports );
137+ } else {
138+ qualifiedExports = Collections .emptyMap ();
139+ }
126140
127141 Set <PluginBundle > seenBundles = new LinkedHashSet <>();
128142
129143 // load (elasticsearch) module layers
144+ List <PluginDescriptor > moduleDescriptors ;
130145 if (modulesDirectory != null ) {
131146 try {
132147 Set <PluginBundle > modules = PluginsUtils .getModuleBundles (modulesDirectory );
@@ -140,6 +155,7 @@ public PluginsLoader(Path modulesDirectory, Path pluginsDirectory) {
140155 }
141156
142157 // load plugin layers
158+ List <PluginDescriptor > pluginDescriptors ;
143159 if (pluginsDirectory != null ) {
144160 try {
145161 // TODO: remove this leniency, but tests bogusly rely on it
@@ -158,7 +174,28 @@ public PluginsLoader(Path modulesDirectory, Path pluginsDirectory) {
158174 pluginDescriptors = Collections .emptyList ();
159175 }
160176
161- this .loadedPluginLayers = Collections .unmodifiableMap (loadPluginLayers (seenBundles , qualifiedExports ));
177+ Map <String , LoadedPluginLayer > loadedPluginLayers = new LinkedHashMap <>();
178+ Map <String , Set <URL >> transitiveUrls = new HashMap <>();
179+ List <PluginBundle > sortedBundles = PluginsUtils .sortBundles (seenBundles );
180+ if (sortedBundles .isEmpty () == false ) {
181+ Set <URL > systemLoaderURLs = JarHell .parseModulesAndClassPath ();
182+ for (PluginBundle bundle : sortedBundles ) {
183+ PluginsUtils .checkBundleJarHell (systemLoaderURLs , bundle , transitiveUrls );
184+ loadPluginLayer (bundle , loadedPluginLayers , qualifiedExports );
185+ }
186+ }
187+
188+ return new PluginsLoader (moduleDescriptors , pluginDescriptors , loadedPluginLayers );
189+ }
190+
191+ PluginsLoader (
192+ List <PluginDescriptor > moduleDescriptors ,
193+ List <PluginDescriptor > pluginDescriptors ,
194+ Map <String , LoadedPluginLayer > loadedPluginLayers
195+ ) {
196+ this .moduleDescriptors = moduleDescriptors ;
197+ this .pluginDescriptors = pluginDescriptors ;
198+ this .loadedPluginLayers = loadedPluginLayers ;
162199 }
163200
164201 public List <PluginDescriptor > moduleDescriptors () {
@@ -173,25 +210,7 @@ public Stream<PluginLayer> pluginLayers() {
173210 return loadedPluginLayers .values ().stream ().map (Function .identity ());
174211 }
175212
176- private Map <String , LoadedPluginLayer > loadPluginLayers (
177- Set <PluginBundle > bundles ,
178- Map <String , List <ModuleQualifiedExportsService >> qualifiedExports
179- ) {
180- Map <String , LoadedPluginLayer > loaded = new LinkedHashMap <>();
181- Map <String , Set <URL >> transitiveUrls = new HashMap <>();
182- List <PluginBundle > sortedBundles = PluginsUtils .sortBundles (bundles );
183- if (sortedBundles .isEmpty () == false ) {
184- Set <URL > systemLoaderURLs = JarHell .parseModulesAndClassPath ();
185- for (PluginBundle bundle : sortedBundles ) {
186- PluginsUtils .checkBundleJarHell (systemLoaderURLs , bundle , transitiveUrls );
187- loadPluginLayer (bundle , loaded , qualifiedExports );
188- }
189- }
190-
191- return loaded ;
192- }
193-
194- private void loadPluginLayer (
213+ private static void loadPluginLayer (
195214 PluginBundle bundle ,
196215 Map <String , LoadedPluginLayer > loaded ,
197216 Map <String , List <ModuleQualifiedExportsService >> qualifiedExports
@@ -211,7 +230,7 @@ private void loadPluginLayer(
211230 }
212231
213232 final ClassLoader parentLoader = ExtendedPluginsClassLoader .create (
214- getClass () .getClassLoader (),
233+ PluginsLoader . class .getClassLoader (),
215234 extendedPlugins .stream ().map (LoadedPluginLayer ::spiClassLoader ).toList ()
216235 );
217236 LayerAndLoader spiLayerAndLoader = null ;
@@ -427,7 +446,7 @@ private static List<ModuleLayer> parentLayersOrBoot(List<ModuleLayer> parentLaye
427446 }
428447 }
429448
430- protected void addServerExportsService (Map <String , List <ModuleQualifiedExportsService >> qualifiedExports ) {
449+ private static void addServerExportsService (Map <String , List <ModuleQualifiedExportsService >> qualifiedExports ) {
431450 var exportsService = new ModuleQualifiedExportsService (serverModule ) {
432451 @ Override
433452 protected void addExports (String pkg , Module target ) {
0 commit comments