Class Loader<CTX>
java.lang.Object
org.refcodes.loader.Loader<CTX>
- Type Parameters:
CTX- The _context type for thePlugininstances to operate on when being initialized.
- All Implemented Interfaces:
Closeable,AutoCloseable,org.refcodes.component.Startable
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 -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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 ServiceLoaderaPluginsDir- directory to watch for plugin jarsaContext- _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:
startin interfaceorg.refcodes.component.Startable
-
close
public void close()Stop watching and destroy all loaded plugins.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-