Class PluginClassLoader

All Implemented Interfaces:
Closeable, AutoCloseable

public class PluginClassLoader extends URLClassLoader

Each Plugin has classes loaded from its own DefaultPluginClassLoader. These should inherit from the Engine's class loader, so that the plugins can access the core classes. Plugins can also depend on other plugins, which allows classes for this plugin to be loaded/linked from another plugin's class loader.

TODO add some integration tests for the plugin mechanism?

  • Constructor Details

    • PluginClassLoader

      public PluginClassLoader(ClassLoader engineClassLoader, URL jarUrl, URL... dependencies)

      Create a new plugin class loader

      Parameters:
      jarUrl - primary library that constitutes this plugin's code
      dependencies - Array of urls to subsidiary dependencies with own libraries and all exported libraries of plugins that are required for this plugin.
      engineClassLoader - the parent class loader, expected to be the engine's class loader
  • Method Details

    • newDummy

      public static PluginClassLoader newDummy()

      Returns a plugin class loader to use in test situations. Likely to go bang if you try and load classes from it

    • setPluginDescriptor

      public void setPluginDescriptor(nz.org.riskscape.engine.plugin.PluginDescriptor toSet)

      Assign the PluginDescriptor that uses this class loader - this should be a strictly 1-1 relationship.

    • addDependency

      public void addDependency(PluginClassLoader classLoader)

      Add a plugin dependency to this plugin. The given class loader will be searched for classes once loading fails from this classloader's resources, the engine's classloader, and finally the system class path.

    • freeze

      public void freeze()

      Prevent further plugins from being added to this one, just in case... who knows what that will do?

    • loadClass

      protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException
      Overrides:
      loadClass in class ClassLoader
      Throws:
      ClassNotFoundException
    • loadClassLocally

      protected Class<?> loadClassLocally(String name, boolean resolve) throws ClassNotFoundException

      Attempt to load a class directly from this classloader's classpath, without delegating the search anywhere else. This is used by other PluginClassLoader's to search for their dependencies, and is never called from this.

      NB it does end up searching the engine's classpath as well, which is harmless but wasteful. I'm a little shy of changing this class loader too much, due to general ignorance and inexperience with implementing custom classloaders

      Throws:
      ClassNotFoundException
    • getResource

      public URL getResource(String name)

      Get a resource from local resources, engine resources, then falling back to plugin resources

      Overrides:
      getResource in class ClassLoader
    • getResources

      public Enumeration<URL> getResources(String name) throws IOException
      Overrides:
      getResources in class ClassLoader
      Throws:
      IOException
    • getResourceAsStream

      public InputStream getResourceAsStream(String name)
      Overrides:
      getResourceAsStream in class URLClassLoader
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getI18nResourceLoader

      public ResourceClassLoader getI18nResourceLoader()
      Returns:
      a lazily-created ResourceClassLoader that is configured to load resources (and not classes) from the i18n directory next to the code plugin code.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getDependencies

      public List<PluginClassLoader> getDependencies()
    • getJarUrl

      public URL getJarUrl()