Class PluginManager
plugins directory.
This class provides functionality for scanning the plugins directory,
searching for .jar files, and loading them into a URLClassLoader.
It also provides functionality to load the current JAR file into a URLClassLoader.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.slf4j.LoggerLogger instance for logging plugin management operations, including scanning for plugins, loading them, and handling errors related to file operations. -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull URLClassLoaderRetrieves aURLClassLoaderthat loads plugins from thepluginsdirectory.static <T extends Core>
@NotNull URLClassLoadergetSelfLoader(@NotNull Class<T> type) Retrieves aURLClassLoaderthat loads the current JAR file.
-
Field Details
-
log
public static final org.slf4j.Logger logLogger instance for logging plugin management operations, including scanning for plugins, loading them, and handling errors related to file operations.This logger is used to record significant events during the plugin loading process, such as:
- Start and completion of plugin loading operations.
- Warnings when expected directories or files are not found or when there are issues creating directories.
- Errors encountered during scanning, loading, or converting plugin JAR files.
- Information about the number of plugins found in the plugins directory.
The logger uses the SLF4J logging framework, allowing the application to configure different log levels (e.g., INFO, WARN, ERROR) and direct output to different logging sinks (console, files, etc.). This makes it easy to track the state of plugin loading during development, debugging, and production deployment.
Example log outputs:
- Informational log when the plugin loading starts: "Plugin loading starts."
- Warning log when the plugins folder doesn't exist and is being created: "Plugins folder not found. Creating it now."
- Error log when a failure occurs while creating the plugins directory: "Failed to create plugins folder:
error-message" - Error log when a failure occurs while scanning for plugin JAR files: "Failed to scan plugin directory:
error-message" - Informational log when no plugins are found: "No plugins found in the plugins folder."
- Informational log when the plugin loading finishes: "Plugin loading finished."
Using this log, developers and administrators can track the success or failure of plugin loading and identify specific issues related to missing plugins, directory creation, or file reading operations.
-
-
Method Details
-
getPluginLoader
Retrieves aURLClassLoaderthat loads plugins from thepluginsdirectory.If the
pluginsdirectory does not exist, it will be created. The method then scans the directory for.jarfiles and adds them to theURLClassLoader.- Returns:
- a
URLClassLoadercontaining plugins found in thepluginsdirectory.
-
getSelfLoader
@Contract("_ -> new") @NotNull public static <T extends Core> @NotNull URLClassLoader getSelfLoader(@NotNull @NotNull Class<T> type) throws RuntimeException Retrieves aURLClassLoaderthat loads the current JAR file.This method is useful for loading the current application as a plugin.
- Type Parameters:
T- the type ofCore.- Parameters:
type- theCoreclass used to retrieve the location of the JAR file.- Returns:
- a
URLClassLoaderfor the current JAR file. - Throws:
RuntimeException- if the current JAR file cannot be loaded.
-