Class ReloadingClassLoader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class ReloadingClassLoader
    extends java.net.URLClassLoader
    Custom ClassLoader that reverses the classloader lookups, and that is able to notify a listener when a class file is changed.
    Author:
    Jean-Baptiste Quenot
    • Constructor Summary

      Constructors 
      Constructor Description
      ReloadingClassLoader​(java.lang.ClassLoader parent)
      Create a new reloading ClassLoader from a list of URLs, and initialize the ModificationWatcher to detect class file modifications
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static void addLocation​(java.net.URL url)
      Add the location of a directory containing class files
      void destroy()
      Remove the ModificationWatcher from the current reloading class loader
      static void excludePattern​(java.lang.String pattern)
      Exclude a pattern
      static java.util.Set<java.net.URL> getLocations()
      Returns the list of all configured locations of directories containing class files
      static java.util.List<java.lang.String> getPatterns()
      Returns the list of all configured inclusion or exclusion patterns
      java.net.URL getResource​(java.lang.String name)
      Gets a resource from this ClassLoader.
      static void includePattern​(java.lang.String pattern)
      Include a pattern
      java.lang.Class<?> loadClass​(java.lang.String name, boolean resolve)
      Loads the class from this ClassLoader.
      void setListener​(org.apache.wicket.util.listener.IChangeListener<java.lang.Class<?>> listener)
      Sets the listener that will be notified when a class changes
      protected boolean tryClassHere​(java.lang.String name)  
      • Methods inherited from class java.net.URLClassLoader

        addURL, close, definePackage, findClass, 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, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, 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
    • Constructor Detail

      • ReloadingClassLoader

        public ReloadingClassLoader​(java.lang.ClassLoader parent)
        Create a new reloading ClassLoader from a list of URLs, and initialize the ModificationWatcher to detect class file modifications
        Parameters:
        parent - the parent classloader in case the class file cannot be loaded from the above locations
    • Method Detail

      • tryClassHere

        protected boolean tryClassHere​(java.lang.String name)
        Parameters:
        name -
        Returns:
        true if class if found, false otherwise
      • includePattern

        public static void includePattern​(java.lang.String pattern)
        Include a pattern
        Parameters:
        pattern - the pattern to include
      • excludePattern

        public static void excludePattern​(java.lang.String pattern)
        Exclude a pattern
        Parameters:
        pattern - the pattern to exclude
      • getPatterns

        public static java.util.List<java.lang.String> getPatterns()
        Returns the list of all configured inclusion or exclusion patterns
        Returns:
        list of patterns as String
      • addLocation

        public static void addLocation​(java.net.URL url)
        Add the location of a directory containing class files
        Parameters:
        url - the URL for the directory
      • getLocations

        public static java.util.Set<java.net.URL> getLocations()
        Returns the list of all configured locations of directories containing class files
        Returns:
        list of locations as URL
      • getResource

        public final java.net.URL getResource​(java.lang.String name)
        Gets a resource from this ClassLoader. If the resource does not exist in this one, we check the parent. Please note that this is the exact opposite of the ClassLoader spec. We use it to work around inconsistent class loaders from third party vendors.
        Overrides:
        getResource in class java.lang.ClassLoader
        Parameters:
        name - of resource
      • loadClass

        public final java.lang.Class<?> loadClass​(java.lang.String name,
                                                  boolean resolve)
                                           throws java.lang.ClassNotFoundException
        Loads the class from this ClassLoader. If the class does not exist in this one, we check the parent. Please note that this is the exact opposite of the ClassLoader spec. We use it to load the class from the same classloader as WicketFilter or WicketServlet. When found, the class file is watched for modifications.
        Overrides:
        loadClass in class java.lang.ClassLoader
        Parameters:
        name - the name of the class
        resolve - if true then resolve the class
        Returns:
        the resulting Class object
        Throws:
        java.lang.ClassNotFoundException - if the class could not be found
      • setListener

        public void setListener​(org.apache.wicket.util.listener.IChangeListener<java.lang.Class<?>> listener)
        Sets the listener that will be notified when a class changes
        Parameters:
        listener - the listener to notify upon class change
      • destroy

        public void destroy()
        Remove the ModificationWatcher from the current reloading class loader