Class WatchResourcesClassLoader

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class WatchResourcesClassLoader
    extends URLClassLoader
    Special URL classloader to get only changed resources from URL. Use this classloader to support watchResources property. This classloader checks if the resource was modified after application startup and in that case delegates getResource()/getResources() to custom URL classloader. Otherwise returns null or resource from paren classloader (depending on searchParent property).
    Author:
    Jiri Bubnik
    • Constructor Detail

      • WatchResourcesClassLoader

        public WatchResourcesClassLoader()
      • WatchResourcesClassLoader

        public WatchResourcesClassLoader​(boolean searchParent)
      • WatchResourcesClassLoader

        public WatchResourcesClassLoader​(ClassLoader classLoader)
    • Method Detail

      • setSearchParent

        public void setSearchParent​(boolean searchParent)
      • initExtraPath

        public void initExtraPath​(URL[] extraPath)
        Configure new instance with urls and watcher service.
        Parameters:
        extraPath - the URLs from which to load resources
      • initWatchResources

        public void initWatchResources​(URL[] watchResources,
                                       Watcher watcher)
        Configure new instance with urls and watcher service.
        Parameters:
        watchResources - the URLs from which to load resources
        watcher - watcher service to register watch events
      • isResourceChanged

        public boolean isResourceChanged​(URL url)
        Check if the resource was changed after this classloader instantiaton.
        Parameters:
        url - full URL of the file
        Returns:
        true if was changed after instantiation
      • getResource

        public URL getResource​(String name)
        Returns URL only if the resource is found in changedURL and was actually changed after instantiation of this classloader.
        Overrides:
        getResource in class ClassLoader
      • getResources

        public Enumeration<URL> getResources​(String name)
                                      throws IOException
        Returns only a single instance of the changed resource. There are conflicting requirements for other resources inclusion. This class should "hide" the original resource, hence it should not be included in the resoult. On the other hand, there may be resource with the same name in other JAR which should be included and now is hidden (for example multiple persistence.xml). Maybe a new property to influence this behaviour?
        Overrides:
        getResources in class ClassLoader
        Throws:
        IOException
      • getClasspath

        public String getClasspath()
        Support for classpath builder on Tomcat.