Class PluginManager

java.lang.Object
org.omegazero.common.plugins.PluginManager
All Implemented Interfaces:
Iterable<Plugin>

public class PluginManager extends Object implements Iterable<Plugin>
Manages and loads Plugins.
Since:
2.2
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Allow loading plugins that are not contained within a single file but within a directory.
    static final int
    Try to load any files, not only files ending with .jar.
    static final int
    Exit the method when an error occurs while loading a plugin, rethrowing the caught exception.
    static final int
    Search for additional plugins in subdirectories.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
     
    int
    loadFromDirectory(String path, int flags)
    Loads plugins from the given directory.
    int
    loadFromDirectory(Path path, int flags)
    Searches for plugins in the given directory and loads them using loadPlugin(Path).
    int
    loadFromJar(Path jarFilePath, String innerPath, int flags)
    Searches for plugins in the JAR file at the given path, by attempting to load any nested JAR files, whose relative path in the given JAR file start with innerPath.
    int
    loadFromPath(String path, int flags)
    Searches for plugins at the given path.
    void
    Loads and initializes the plugin at path and registers it with this PluginManager instance.
    int
    Returns the number of plugins registered in this PluginManager.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

    • EXIT_ON_ERROR

      public static final int EXIT_ON_ERROR
      Exit the method when an error occurs while loading a plugin, rethrowing the caught exception.
      See Also:
    • ALLOW_NONJAR

      public static final int ALLOW_NONJAR
      Try to load any files, not only files ending with .jar.
      See Also:
    • RECURSIVE

      public static final int RECURSIVE
      Search for additional plugins in subdirectories.
      See Also:
    • ALLOW_DIRS

      public static final int ALLOW_DIRS
      Allow loading plugins that are not contained within a single file but within a directory. This flag is not affected by ALLOW_NONJAR.
      See Also:
  • Constructor Details

    • PluginManager

      public PluginManager()
  • Method Details

    • loadFromDirectory

      public int loadFromDirectory(String path, int flags) throws IOException
      Loads plugins from the given directory. See loadFromDirectory(Path, int).
      Parameters:
      path - The directory to load plugins from
      flags - Bit field of flags
      Returns:
      The number of plugins loaded
      Throws:
      IOException - If an IO error occurs
    • loadFromDirectory

      public int loadFromDirectory(Path path, int flags) throws IOException
      Searches for plugins in the given directory and loads them using loadPlugin(Path).

      The search behavior may be configured using the flags parameter. This parameter is a bit field of EXIT_ON_ERROR, ALLOW_NONJAR, RECURSIVE and ALLOW_DIRS. RECURSIVE and ALLOW_DIRS are mutually exclusive. If both are specified, the function behaves as if only RECURSIVE were set.

      Parameters:
      path - The directory to load plugins from
      flags - Bit field of flags
      Returns:
      The number of plugins loaded
      Throws:
      IOException - If an IO error occurs
    • loadFromJar

      public int loadFromJar(Path jarFilePath, String innerPath, int flags) throws IOException
      Searches for plugins in the JAR file at the given path, by attempting to load any nested JAR files, whose relative path in the given JAR file start with innerPath.

      The search behavior may be configured using the flags parameter. This parameter is a bit field of EXIT_ON_ERROR and ALLOW_NONJAR.

      Parameters:
      jarFilePath - The path of the wrapping JAR file
      innerPath - The relative path prefix in the JAR file
      flags - Bit field of flags
      Returns:
      The number of plugins loaded
      Throws:
      IOException - If an IO error occurs
      Since:
      2.8
    • loadFromPath

      public int loadFromPath(String path, int flags) throws IOException
      Searches for plugins at the given path.

      If the given path contains the separator !/, the path is assumed to point inside a JAR file, with the format <path to JAR file>!/<directory inside JAR file>, and loadFromJar(Path, String, int) is called. Otherwise, the path is passed unchanged to loadFromDirectory(String, int).

      Parameters:
      path - The path
      flags - Bit field of flags
      Returns:
      The number of plugins loaded
      Throws:
      IOException - If an IO error occurs
      Since:
      2.8
    • loadPlugin

      public void loadPlugin(Path path) throws IOException
      Loads and initializes the plugin at path and registers it with this PluginManager instance.

      If the given path is a directory, the plugin is loaded as a directory plugin, otherwise, it is loaded as a JAR plugin.

      Parameters:
      path - The path of the plugin
      Throws:
      IOException - If an IO error occurs
    • pluginCount

      public int pluginCount()
      Returns the number of plugins registered in this PluginManager.
      Returns:
      The number of plugins
    • iterator

      public Iterator<Plugin> iterator()
      Specified by:
      iterator in interface Iterable<Plugin>