Class PluginManager

java.lang.Object
net.regsirius06.engine.plugins.files.PluginManager

public final class PluginManager extends Object
Manages plugin loading from the 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 Details

    • log

      public static final org.slf4j.Logger log
      Logger 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

      @Contract(" -> new") @NotNull public static @NotNull URLClassLoader getPluginLoader()
      Retrieves a URLClassLoader that loads plugins from the plugins directory.

      If the plugins directory does not exist, it will be created. The method then scans the directory for .jar files and adds them to the URLClassLoader.

      Returns:
      a URLClassLoader containing plugins found in the plugins directory.
    • getSelfLoader

      @Contract("_ -> new") @NotNull public static <T extends Core> @NotNull URLClassLoader getSelfLoader(@NotNull @NotNull Class<T> type) throws RuntimeException
      Retrieves a URLClassLoader that loads the current JAR file.

      This method is useful for loading the current application as a plugin.

      Type Parameters:
      T - the type of Core.
      Parameters:
      type - the Core class used to retrieve the location of the JAR file.
      Returns:
      a URLClassLoader for the current JAR file.
      Throws:
      RuntimeException - if the current JAR file cannot be loaded.