Class Loader<CTX>

java.lang.Object
org.refcodes.loader.Loader<CTX>
Type Parameters:
CTX - The _context type for the Plugin instances to operate on when being initialized.
All Implemented Interfaces:
Closeable, AutoCloseable, Startable

public class Loader<CTX> extends Object implements Startable, Closeable
Generic runtime loader that discovers, initializes, and destroys plugins at runtime. It watches a configurable plugins directory, loads new JARs via a dedicated class loader, discovers plugin implementations through Java's ServiceLoader, and invokes their lifecycle methods with a user supplied _context object. When a plugin JAR is removed or the JVM shuts down, the loader calls the destroy method on all affected plugin instances and releases their class loader.
  • Constructor Details

    • Loader

      public Loader(Class<? extends Plugin<CTX>> aPluginType, Path aPluginsDir, CTX aContext) throws IOException
      Create a new HotPlug manager.
      Parameters:
      aPluginType - the SPI interface class used with ServiceLoader
      aPluginsDir - directory to watch for plugin jars
      aContext - _context object handed into plugin.initialize(_context)
      Throws:
      IOException
  • Method Details

    • start

      public void start()
      Starts the background watcher thread and performs an initial scan.
      Specified by:
      start in interface Startable
    • close

      public void close()
      Stop watching and destroy all loaded plugins.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable