Class PluginClassLoader

All Implemented Interfaces:
Closeable, AutoCloseable

public class PluginClassLoader extends URLClassLoader

Class loader which loads the classes requested by the plugins. It first tries to search in the classpath, then it requests the class to the parent classloader, then it searches in the owns plugins' library dir, and if all these methods fail, it tries to load the class from any of the depended plugins. Finally, if this also fails, the other classloaders provided in the addLoaders method are requested to load the class.

The class loader can also be used to load resources from the plugin's directory by using the getResource() method.

Author:
Fernando Gonz�lez Cort�s
  • Constructor Details

    • PluginClassLoader

      public PluginClassLoader(URL[] jars, String baseDir, ClassLoader cl, PluginClassLoader[] pluginLoaders) throws IOException
      Creates a new PluginClassLoader object.
      Parameters:
      jars - Array with the search paths where classes will be searched
      baseDir - Base directory for this plugin. This is the directory which will be used as basedir in the getResources method.
      cl - The parent classloader of this classloader. It will be used to search classes before trying to search in the plugin's directory
      pluginLoaders - The classloaders of the depended plugins.
      Throws:
      IOException
    • PluginClassLoader

      public PluginClassLoader(URL[] jars, String baseDir, ClassLoader cl, List<PluginClassLoader> pluginLoaders) throws IOException
      Throws:
      IOException
  • Method Details

    • singleLoadClass

      protected Class singleLoadClass(String name) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • loadClass

      protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException
      Carga la clase
      Overrides:
      loadClass in class ClassLoader
      Parameters:
      name - Nombre de la clase
      resolve - Si se ha de resolver la clase o no
      Returns:
      Clase cargada
      Throws:
      ClassNotFoundException - Si no se pudo encontrar la clase
    • getResource

      public URL getResource(String res)
      Gets the requested resource. If the path is relative, its base directory will be the one provided in the PluginClassLoader's constructor. If the resource is not found, search in dependents plugins, otherwise the parent classloader will be invoked to try to get it. If it is not found, it will return null.
      Overrides:
      getResource in class ClassLoader
      Parameters:
      res - An absolute or relative path to the requested resource.
      Returns:
      Resource's URL if it was found, nul otherwise.
    • getResources

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

      protected Enumeration<URL> getResources(String name, HashSet<PluginClassLoader> visitedPlugins) throws IOException
      Throws:
      IOException
    • getPluginName

      public String getPluginName()
      Returns the name of the plugin (the name of the directory containing the plugin).
      Returns:
      An String containing the plugin's name.
    • getPermissions

      protected PermissionCollection getPermissions(CodeSource codesource)
      Overrides:
      getPermissions in class URLClassLoader
    • getBaseDir

      public String getBaseDir()
      Gets the plugin's base Iteratorinvalid input: '<'Map.Entryinvalid input: '<'Integer, Integer>>dir, the directory which will be used to search resources.
      Returns:
      Returns the baseDir.
    • addLoaders

      public static void addLoaders(ArrayList classLoaders)
      Adds other classloader to use when all the normal methods fail.
      Parameters:
      classLoaders - An ArrayList of ClassLoaders which will be used to load classes when all the normal methods fail.
    • toString

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

      public void addPluginClassLoader(PluginClassLoader pluginClassLoader)
    • addURL

      public void addURL(URL url)
      Overrides:
      addURL in class URLClassLoader