Class OSGiWebappClassLoader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, ClassVisibilityChecker, org.osgi.framework.BundleReference

    public class OSGiWebappClassLoader
    extends WebAppClassLoader
    implements org.osgi.framework.BundleReference
    OSGiWebappClassLoader Extends the webapp classloader to also use the classloader of the Bundle defining the webapp.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.Set<java.lang.String> JAR_WITH_SUCH_CLASS_MUST_BE_EXCLUDED
      when a logging framework is setup in the osgi classloaders, it can access this and register the classes that must not be found in the jar.
    • Constructor Summary

      Constructors 
      Constructor Description
      OSGiWebappClassLoader​(java.lang.ClassLoader parent, WebAppContext context, org.osgi.framework.Bundle contributor)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addClassPath​(java.lang.String classPath)
      Parse the classpath ourselves to be able to filter things.
      static void addClassThatIdentifiesAJarThatMustBeRejected​(java.lang.Class<?> zclass)  
      static void addClassThatIdentifiesAJarThatMustBeRejected​(java.lang.String zclassName)  
      protected java.lang.Class<?> findClass​(java.lang.String name)  
      java.net.URL findResource​(java.lang.String name)  
      org.osgi.framework.Bundle getBundle()
      Returns the Bundle that defined this web-application.
      java.net.URL getResource​(java.lang.String name)
      Get a resource from the classloader NOTE: this method provides a convenience of hacking off a leading / should one be present.
      java.util.Enumeration<java.net.URL> getResources​(java.lang.String name)  
      protected java.lang.Class<?> loadAsResource​(java.lang.String name, boolean checkSystemResource)
      Try to load the class from the bundle classloader.
      void setWebappContext​(WebAppContext webappContext)
      In the case of the generation of a webapp via a jetty context file we need a proper classloader to setup the app before we have the WebappContext So we place a fake one there to start with.
      • Methods inherited from class java.net.URLClassLoader

        addURL, definePackage, findResources, 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, getPackage, getPackages, getParent, getPlatformClassLoader, 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, wait, wait, wait
    • Field Detail

      • JAR_WITH_SUCH_CLASS_MUST_BE_EXCLUDED

        public static final java.util.Set<java.lang.String> JAR_WITH_SUCH_CLASS_MUST_BE_EXCLUDED
        when a logging framework is setup in the osgi classloaders, it can access this and register the classes that must not be found in the jar.
    • Constructor Detail

      • OSGiWebappClassLoader

        public OSGiWebappClassLoader​(java.lang.ClassLoader parent,
                                     WebAppContext context,
                                     org.osgi.framework.Bundle contributor)
                              throws java.io.IOException
        Parameters:
        parent - The parent classloader.
        context - The WebAppContext
        contributor - The bundle that defines this web-application.
        Throws:
        java.io.IOException - if unable to cerate the OSGiWebappClassLoader
    • Method Detail

      • addClassThatIdentifiesAJarThatMustBeRejected

        public static void addClassThatIdentifiesAJarThatMustBeRejected​(java.lang.Class<?> zclass)
      • addClassThatIdentifiesAJarThatMustBeRejected

        public static void addClassThatIdentifiesAJarThatMustBeRejected​(java.lang.String zclassName)
      • findClass

        protected java.lang.Class<?> findClass​(java.lang.String name)
                                        throws java.lang.ClassNotFoundException
        Overrides:
        findClass in class WebAppClassLoader
        Throws:
        java.lang.ClassNotFoundException
      • getBundle

        public org.osgi.framework.Bundle getBundle()
        Returns the Bundle that defined this web-application.
        Specified by:
        getBundle in interface org.osgi.framework.BundleReference
        Returns:
        The Bundle object associated with this BundleReference.
      • getResources

        public java.util.Enumeration<java.net.URL> getResources​(java.lang.String name)
                                                         throws java.io.IOException
        Overrides:
        getResources in class WebAppClassLoader
        Throws:
        java.io.IOException
      • getResource

        public java.net.URL getResource​(java.lang.String name)
        Description copied from class: WebAppClassLoader
        Get a resource from the classloader NOTE: this method provides a convenience of hacking off a leading / should one be present. This is non-standard and it is recommended to not rely on this behavior
        Overrides:
        getResource in class WebAppClassLoader
      • findResource

        public java.net.URL findResource​(java.lang.String name)
        Overrides:
        findResource in class java.net.URLClassLoader
      • loadAsResource

        protected java.lang.Class<?> loadAsResource​(java.lang.String name,
                                                    boolean checkSystemResource)
                                             throws java.lang.ClassNotFoundException
        Try to load the class from the bundle classloader. We do NOT load it as a resource as the WebAppClassLoader does because the url that is returned is an osgi-special url that does not play properly with WebAppClassLoader's method of extracting the class from the resource. This implementation directly asks the osgi bundle classloader to load the given class name.
        Overrides:
        loadAsResource in class WebAppClassLoader
        Parameters:
        name - the name of the class to load
        checkSystemResource - if true and the class isn't a system class we return it
        Returns:
        the loaded class
        Throws:
        java.lang.ClassNotFoundException - if the class cannot be found
        See Also:
        WebAppClassLoader.loadAsResource(java.lang.String, boolean)
      • addClassPath

        public void addClassPath​(java.lang.String classPath)
                          throws java.io.IOException
        Parse the classpath ourselves to be able to filter things. This is a derivative work of the super class
        Overrides:
        addClassPath in class WebAppClassLoader
        Parameters:
        classPath - Comma or semicolon separated path of filenames or URLs pointing to directories or jar files. Directories should end with '/'.
        Throws:
        java.io.IOException - if unable to add classpath
      • setWebappContext

        public void setWebappContext​(WebAppContext webappContext)
        In the case of the generation of a webapp via a jetty context file we need a proper classloader to setup the app before we have the WebappContext So we place a fake one there to start with. We replace it with the actual webapp context with this method. We also apply the extraclasspath there at the same time.
        Parameters:
        webappContext - the web app context