Class PluginLoader<T extends Plugin>
- Type Parameters:
T- the type of plugin to load, constrained to implement thePlugininterface
Each plugin must implement the Plugin interface and provide a non-null, non-blank identity via the
Plugin.identity() method. Plugins are loaded from individual JAR files, each loaded in its own
URLClassLoader, enabling isolated class loading for plugin dependencies.
This class supports custom ClassLoader hierarchies to accommodate complex deployment environments, and
aggregates errors encountered during plugin discovery and loading into a single LoaderException
if no plugins are successfully loaded.
It supports one-time classloading, but no reloading of changed JARs at runtime. An application restart is required to pick up changes to plugin JARs.
-
Constructor Summary
ConstructorsConstructorDescriptionPluginLoader(Class<T> pluginClass) Constructs a new PluginLoader that will load plugins of the specified typeT.PluginLoader(Class<T> pluginClass, LoaderConfiguration configuration) Constructs a new PluginLoader that will load plugins of the specified typeT.PluginLoader(Class<T> pluginClass, LoaderConfiguration configuration, ClassLoader parentClassLoader) Constructs a new instance of the PluginLoader, which is responsible for loading plugins of the specified typeT.PluginLoader(Class<T> pluginClass, ClassLoader parentClassLoader) Constructs a new PluginLoader that will load plugins of the specified typeT. -
Method Summary
Modifier and TypeMethodDescriptionLoads all plugins of typeTfrom JAR files located in the specified directory.
-
Constructor Details
-
PluginLoader
Constructs a new PluginLoader that will load plugins of the specified typeT. The parent ClassLoader is set to the current thread's context ClassLoader, which allows plugins to access classes and resources on the core's classpath. It uses the system default configuration for plugin loading behaviors, seeLoaderConfiguration.defaults().- Parameters:
pluginClass- the Class object representing the plugin typeTto load
-
PluginLoader
Constructs a new PluginLoader that will load plugins of the specified typeT. It uses the system default configuration for plugin loading behaviors, seeLoaderConfiguration.defaults().- Parameters:
pluginClass- the Class object representing the plugin typeTto loadparentClassLoader- the ClassLoader to be used as the parent for class loading of plugins
-
PluginLoader
Constructs a new PluginLoader that will load plugins of the specified typeT. The parent ClassLoader is set to the current thread's context ClassLoader, which allows plugins to access classes and resources on the core's classpath.- Parameters:
pluginClass- the Class object representing the type of pluginTto loadconfiguration- the LoaderConfiguration specifying custom plugin loading behaviors
-
PluginLoader
public PluginLoader(Class<T> pluginClass, LoaderConfiguration configuration, ClassLoader parentClassLoader) Constructs a new instance of the PluginLoader, which is responsible for loading plugins of the specified typeT.- Parameters:
pluginClass- the Class object representing the type of pluginTto loadconfiguration- the LoaderConfiguration specifying custom plugin loading behaviorsparentClassLoader- the ClassLoader to be used as the parent for loading plugin classes and resources
-
-
Method Details
-
load
Loads all plugins of type
Tfrom JAR files located in the specified directory. Each JAR file is loaded using a dedicatedURLClassLoader, and plugins are discovered via the JavaServiceLoadermechanism (META-INF/services/package.plus.service.ClassName file).For each discovered plugin, its
Plugin.identity()must be non-null and non-blank; otherwise, it is skipped and an error is recorded.- Parameters:
pluginJarsLocation- the directory containing JAR files to scan for plugins- Returns:
- a list of
PluginHandle, linking each plugin's metadata to the corresponding plugin instance - Throws:
LoaderException- if one or more errors occur during loading, if no plugins could be successfully loaded, or if there are any duplicates. Note: The exception may contain multiple causes, each associated with a specific file or failure point
-