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, org.refcodes.component.Startable

public class Loader<CTX> extends Object implements org.refcodes.component.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.
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.refcodes.component.Startable

    org.refcodes.component.Startable.StartAutomaton, org.refcodes.component.Startable.StartBuilder<B extends org.refcodes.component.Startable.StartBuilder<B>>, org.refcodes.component.Startable.UncheckedStartable
  • Constructor Summary

    Constructors
    Constructor
    Description
    Loader(Class<? extends Plugin<CTX>> aPluginType, Path aPluginsDir, CTX aContext)
    Create a new HotPlug manager.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Stop watching and destroy all loaded plugins.
    void
    Starts the background watcher thread and performs an initial scan.

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface org.refcodes.component.Startable

    startUnchecked
  • 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 org.refcodes.component.Startable
    • close

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