Class ProxyClassLoader

java.lang.Object
java.lang.ClassLoader
net.thevpc.common.prs.classloader.ProxyClassLoader

public class ProxyClassLoader extends ClassLoader
A class loader that has multiple parents and uses them for loading classes and resources. It can be used in tree hierarchy, where it can exploit its capability to not throw ClassNotFoundException when communicating with other ProxyClassLoader. It itself doesn't load classes or resources, but allows subclasses to add such functionality.
Author:
Petr Nejedly, Jesse Glick (ProxyClassLoader)
  • Constructor Details

    • ProxyClassLoader

      public ProxyClassLoader(ClassLoader[] parents)
      Create a multi-parented classloader. Loads recursively from parents.
      Parameters:
      parents - list of parent classloaders.
      Throws:
      IllegalArgumentException - if there are any nulls or duplicate parent loaders or cycles.
    • ProxyClassLoader

      public ProxyClassLoader(String name, ClassLoader[] parents)
    • ProxyClassLoader

      public ProxyClassLoader(String name, ClassLoader[] parents, boolean transitive)
      Create a multi-parented classloader.
      Parameters:
      parents - list of parent classloaders.
      transitive - whether other PCLs depending on this one will automatically search through its parent list
      Throws:
      IllegalArgumentException - if there are any nulls or duplicate parent loaders or cycles.
      Since:
      org.netbeans.core/1 > 1.6
  • Method Details

    • getName

      public String getName()
      Overrides:
      getName in class ClassLoader
    • append

      public void append(ClassLoader[] classLoaders) throws IllegalArgumentException
      Add new parents dynamically.
      Parameters:
      classLoaders - the new parents to add (append to list)
      Throws:
      IllegalArgumentException - in case of a null or cyclic parent (duplicate OK)
    • destroy

      public void destroy()
      Try to destroy this classloader. Subsequent attempts to use it will log an error (at most one though).
    • loadClass

      protected final Class loadClass(String name, boolean resolve) throws ClassNotFoundException
      Loads the class with the specified name. The implementation of this method searches for classes in the following order: Calls ClassLoader.findLoadedClass(String) to check if the class has * already been loaded. * Checks the caches whether another class from the same package * was already loaded and uses the same classloader * Tries to find the class using parent loaders in their order. * Calls the simpleFindClass(String, String, String) method to find * the class using this class loader. *
      Overrides:
      loadClass in class ClassLoader
      Parameters:
      name - the name of the class
      resolve - if true then resolve the class
      Returns:
      the resulting Class object
      Throws:
      ClassNotFoundException - if the class could not be found
    • simpleFindClass

      protected Class simpleFindClass(String name, String fileName, String pkg)
      This ClassLoader can't load anything itself. Subclasses may override this method to do some class loading themselves. The implementation should not throw any exception, just return null if it can't load required class.
      Parameters:
      name - the name of the class
      fileName - the expected filename of the classfile, like java/lang/Object.class for java.lang.Object The ClassLoader implementation may or may not use it, depending whether it is usefull to it.
      pkg - the package name, in the format org/netbeans/modules/foo/
      Returns:
      the resulting Class object or null
    • getResource

      public final URL getResource(String name)
      Finds the resource with the given name. The implementation of this method searches for resources in the following order: Checks the caches whether another resource or class from the same * package was already loaded and uses the same classloader. * Tries to find the resources using parent loaders in their order. * Calls the findResource(String) method to find * the resources using this class loader. *
      Overrides:
      getResource in class ClassLoader
      Parameters:
      name - a "/"-separated path name that identifies the resource.
      Returns:
      a URL for reading the resource, or null if the resource could not be found.
      See Also:
    • findResource

      protected URL findResource(String name)
      This ClassLoader can't load anything itself. Subclasses may override this method to do some resource loading themselves.
      Overrides:
      findResource in class ClassLoader
      Parameters:
      name - the resource name
      Returns:
      a URL for reading the resource, or null if the resource could not be found.
    • findResources

      protected final Enumeration findResources(String name) throws IOException
      Finds all the resource with the given name. The implementation of this method uses the simpleFindResources(String) method to find all the resources available from this classloader and adds all the resources from all the parents.
      Overrides:
      findResources in class ClassLoader
      Parameters:
      name - the resource name
      Returns:
      an Enumeration of URLs for the resources
      Throws:
      IOException - if I/O errors occur
    • simpleFindResources

      protected Enumeration simpleFindResources(String name) throws IOException
      This ClassLoader can't load anything itself. Subclasses may override this method to do some resource loading themselves, this implementation simply delegates to findResources method of the superclass that should return empty Enumeration.
      Parameters:
      name - the resource name
      Returns:
      an Enumeration of URLs for the resources
      Throws:
      IOException - if I/O errors occur
    • getPackage

      protected Package getPackage(String name)
      Returns a Package that has been defined by this class loader or any of its parents.
      Overrides:
      getPackage in class ClassLoader
      Parameters:
      name - the package name
      Returns:
      the Package corresponding to the given name, or null if not found
    • getPackageFast

      protected Package getPackageFast(String name, String sname, boolean recurse)
      Faster way to find a package.
      Parameters:
      name - package name in org.netbeans.modules.foo format
      sname - package name in org/netbeans/modules/foo/ format
      recurse - whether to also ask parents
    • definePackage

      protected Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) throws IllegalArgumentException
      This is here just for locking serialization purposes. Delegates to super.definePackage with proper locking. Also tracks the package in our private cache, since getPackageFast(...,...,false) will not call super.getPackage.
      Overrides:
      definePackage in class ClassLoader
      Throws:
      IllegalArgumentException
    • getPackages

      protected Package[] getPackages()
      Returns all of the Packages defined by this class loader and its parents.
      Overrides:
      getPackages in class ClassLoader
      Returns:
      the array of Package objects defined by this ClassLoader
    • getPackageAccessibly

      public Package getPackageAccessibly(String name)
    • getPackagesAccessibly

      public Package[] getPackagesAccessibly()
    • isSpecialResource

      protected boolean isSpecialResource(String pkg)
      Test whether a given resource name is something that any JAR might have, and for which the domain cache should be disabled. The result must not change from one call to the next with the same argument. By default the domain cache is disabled only for META-INF/* JAR information.
      Parameters:
      pkg - the package component of the resource path ending with a slash, e.g. "org/netbeans/modules/foo/"
      Returns:
      true if it is a special resource, false for normal domain-cached resource
      Since:
      org.netbeans.core/1 1.3
    • shouldDelegateResource

      protected boolean shouldDelegateResource(String pkg, ClassLoader parent)
      Test whether a given resource request (for a class or not) should be searched for in the specified parent classloader or not. The result must not change from one call to the next with the same arguments. By default, always true. Subclasses may override to "mask" certain packages from view, possibly according to the classloader chain.
      Parameters:
      pkg - the package component of the resource path ending with a slash, e.g. "org/netbeans/modules/foo/"
      parent - a classloader which is a direct or indirect parent of this one
      Returns:
      true if the request should be delegated to this parent; false to only search elsewhere (other parents, this loader's own namespace)
      Since:
      org.netbeans.core/1 1.3