Class Loader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    Loader.NondelegatingLoader

    public class Loader
    extends java.net.URLClassLoader
    Binding classloader. This is intended to substitute for the System classloader (i.e., the one used for loading user classes). It first processes one or more binding definitions, caching the binary classes modified by the bindings. It then uses these modified forms of the classes when they're requested for loading.
    Author:
    Dennis M. Sosnoski
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Loader.NondelegatingLoader
      Version of bind-on-demand loader which will not delegate handling of classes included in the binding definition.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.ArrayList m_bindings
      Binding definitions used by loader.
      private java.util.HashMap m_classMap
      Map of classes modified by binding.
      private boolean m_isBound
      Flag for bindings compiled into class code.
    • Constructor Summary

      Constructors 
      Constructor Description
      Loader()
      Default constructor.
      Loader​(java.net.URL[] paths)
      Constructor with classpath URLs supplied.
      Loader​(java.net.URL[] paths, java.lang.ClassLoader parent)
      Constructor with classpath URLs and parent classloader supplied.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.Class findClass​(java.lang.String name)
      Find and load class by name.
      static java.net.URL[] getClassPaths()
      Method builds an array of URL for items in the class path.
      protected boolean isBoundClass​(java.lang.String name)
      Check if a class has been modified by a binding.
      void loadBinding​(java.lang.String fname, java.lang.String sname, java.io.InputStream is, java.net.URL url)
      Load binding definition.
      void loadFileBinding​(java.lang.String path)
      Load binding definition from file path.
      void loadResourceBinding​(java.lang.String path)
      Load binding definition from file path.
      void processBindings()
      Process the binding definitions.
      void reset()
      Reset loader information.
      • Methods inherited from class java.net.URLClassLoader

        addURL, close, definePackage, findResource, findResources, getPermissions, getResourceAsStream, getURLs, newInstance, newInstance
      • Methods inherited from class java.security.SecureClassLoader

        defineClass, defineClass
      • Methods inherited from class java.lang.ClassLoader

        clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • m_bindings

        private java.util.ArrayList m_bindings
        Binding definitions used by loader.
      • m_isBound

        private boolean m_isBound
        Flag for bindings compiled into class code.
      • m_classMap

        private java.util.HashMap m_classMap
        Map of classes modified by binding.
    • Constructor Detail

      • Loader

        public Loader​(java.net.URL[] paths,
                      java.lang.ClassLoader parent)
        Constructor with classpath URLs and parent classloader supplied. Sets up the paths for both actual classloading and finding classes to be bound.
        Parameters:
        paths - array of classpath URLs
        parent - classloader used for delegation loading
      • Loader

        public Loader​(java.net.URL[] paths)
        Constructor with classpath URLs supplied. This uses the supplied classpaths, delegating directly to the parent classloader of the normal System classloader.
        Parameters:
        paths - array of classpath URLs
      • Loader

        public Loader()
               throws java.net.MalformedURLException
        Default constructor. This reads the standard class path and uses it for locating classes used by the binding, delegating directly to the parent classloader of the normal System classloader.
        Throws:
        java.net.MalformedURLException - on error in classpath URLs
    • Method Detail

      • reset

        public void reset()
        Reset loader information. This discards all prior bindings and clears the internal state in preparation for loading a different set of bindings. It is not possible to clear the loaded classes, though, so any new bindings must refer to different classes from those previously loaded.
      • getClassPaths

        public static java.net.URL[] getClassPaths()
                                            throws java.net.MalformedURLException
        Method builds an array of URL for items in the class path.
        Returns:
        array of classpath URLs
        Throws:
        java.net.MalformedURLException
      • loadBinding

        public void loadBinding​(java.lang.String fname,
                                java.lang.String sname,
                                java.io.InputStream is,
                                java.net.URL url)
                         throws JiBXException,
                                java.io.IOException
        Load binding definition. This may be called multiple times to load multiple bindings, but only prior to the bindings being compiled. The reader form of the call is generally preferred, since the document encoding may not be properly interpreted from a stream.
        Parameters:
        fname - binding definition full name
        sname - short form of name to use as the default name of the binding
        is - input stream for binding definition document
        url - URL for binding definition (null if not available)
        Throws:
        java.lang.IllegalStateException - if called after bindings have been compiled
        java.io.IOException - if error reading the binding
        JiBXException - if error in processing the binding definition
      • loadFileBinding

        public void loadFileBinding​(java.lang.String path)
                             throws JiBXException,
                                    java.io.IOException
        Load binding definition from file path. This may be called multiple times to load multiple bindings, but only prior to the bindings being compiled.
        Parameters:
        path - binding definition file path
        Throws:
        java.lang.IllegalStateException - if called after bindings have been compiled
        java.io.IOException - if error reading the file
        JiBXException - if error in processing the binding definition
      • loadResourceBinding

        public void loadResourceBinding​(java.lang.String path)
                                 throws JiBXException,
                                        java.io.IOException
        Load binding definition from file path. This may be called multiple times to load multiple bindings, but only prior to the bindings being compiled.
        Parameters:
        path - binding definition file path
        Throws:
        java.lang.IllegalStateException - if called after bindings have been compiled
        java.io.IOException - if error reading the file
        JiBXException - if error in processing the binding definition
      • processBindings

        public void processBindings()
                             throws JiBXException
        Process the binding definitions. This compiles the bindings into the classes, saving the modified classes for loading when needed.
        Throws:
        JiBXException - if error in processing the binding definition
      • isBoundClass

        protected boolean isBoundClass​(java.lang.String name)
        Check if a class has been modified by a binding. If bindings haven't been compiled prior to this call they will be compiled automatically when this method is called.
        Parameters:
        name - fully qualified package and class name to be found
        Returns:
        true if class modified by binding, false if not
      • findClass

        protected java.lang.Class findClass​(java.lang.String name)
                                     throws java.lang.ClassNotFoundException
        Find and load class by name. If the named class has been modified by a binding this loads the modified binary class; otherwise, it just uses the base class implementation to do the loading. If bindings haven't been compiled prior to this call they will be compiled automatically when this method is called.
        Overrides:
        findClass in class java.net.URLClassLoader
        Parameters:
        name - fully qualified package and class name to be found
        Returns:
        the loaded class
        Throws:
        java.lang.ClassNotFoundException - if the class cannot be found
        See Also:
        ClassLoader.findClass(java.lang.String)