Class ClassLoaderResolverImpl

  • All Implemented Interfaces:
    ClassLoaderResolver

    public class ClassLoaderResolverImpl
    extends Object
    implements ClassLoaderResolver
    A basic implementation of a ClassLoaderResolver. A ClassLoaderResolver provides a series of methods for resolving classes from their names. It supports up to 3 class loaders, based loosely on JDO Spec section 12.5. The class loaders will be used in this order:
    • The loader that loaded the class or instance referred to in the API that caused this class to be loaded.
      • In case of query, this is the loader of the candidate class, or the loader of the object passed to the newQuery
      • In case of navigation from a persistent instance, this is the loader of the class of the instance.
      • In the case of getExtent with subclasses, this is the loader of the candidate class.
      • In the case of getObjectById, this is the loader of the object id instance.
      • Other cases do not have an explicit loader.
    • The loader returned in the current context by Thread.getContextClassLoader().
    • The loader returned by Thread.getContextClassLoader() at the time of creating an ExecutionContext.
    • The loader registered for dynamically creating and loading classes at runtime.
    TODO Provide a way of flushing a classname from the cached classes so we can reload a class
    • Field Detail

      • contextLoader

        protected final ClassLoader contextLoader
        ClassLoader initialised by the context (ExecutionContext).
      • contextLoaderHashCode

        protected int contextLoaderHashCode
        Hash code cache for performance improvement
      • runtimeLoader

        protected ClassLoader runtimeLoader
        ClassLoader registered to load runtime created classes.
      • runtimeLoaderHashCode

        protected int runtimeLoaderHashCode
        Hash code cache for performance improvement
      • userRegisteredLoader

        protected ClassLoader userRegisteredLoader
        ClassLoader registered to load classes (e.g set in the persistence properties as the primary loader).
      • userRegisteredLoaderHashCode

        protected int userRegisteredLoaderHashCode
        Hash code cache for performance improvement
      • loadedClasses

        protected Map<String,​Class> loadedClasses
        Cache for loaded classes
      • unloadedClasses

        protected Map<String,​Class> unloadedClasses
        Cache for loaded classes
      • resources

        protected Map<String,​URL> resources
        Cache for resources
    • Constructor Detail

      • ClassLoaderResolverImpl

        public ClassLoaderResolverImpl​(ClassLoader ctxLoader)
        Constructor for ExecutionContext cases.
        Parameters:
        ctxLoader - Loader from ExecutionContext initialisation time.
      • ClassLoaderResolverImpl

        public ClassLoaderResolverImpl()
        Constructor for non-PersistenceManager cases so there is no PM context loader.
    • Method Detail

      • loadJreClasses

        protected void loadJreClasses()
      • classForName

        public Class classForName​(String name,
                                  ClassLoader primary)
        JDO's Class Loading mechanism (Spec 1.0.1 Chapter 12.5). Try 3 loaders, starting with user-supplied loader, then try the current thread's loader, and finally try the PM context loader. This method does not initialize the class itself.
        Specified by:
        classForName in interface ClassLoaderResolver
        Parameters:
        name - Name of the Class to be loaded
        primary - primary ClassLoader to use (or null)
        Returns:
        The class given the name, using the required loader.
        Throws:
        ClassNotResolvedException - if the class can't be found in the classpath
      • classForName

        public Class classForName​(String name,
                                  ClassLoader primary,
                                  boolean initialize)
        JDO's Class Loading mechanism (Spec 1.0.1 Chapter 12.5).
        Specified by:
        classForName in interface ClassLoaderResolver
        Parameters:
        name - Name of the Class to be loaded
        primary - the primary ClassLoader to use (or null)
        initialize - whether to initialize the class or not.
        Returns:
        The Class given the name, using the specified ClassLoader
        Throws:
        ClassNotResolvedException - if the class can't be found in the classpath
      • classForName

        public Class classForName​(String name)
        JDO's Class Loading mechanism (Spec 1.0.1 Chapter 12.5). This method does not initialize the class itself.
        Specified by:
        classForName in interface ClassLoaderResolver
        Parameters:
        name - Name of the Class to be loaded
        Returns:
        The class given the name, using the required loader.
      • classForName

        public Class classForName​(String name,
                                  boolean initialize)
        JDO's Class Loading mechanism (Spec 1.0.1 Chapter 12.5).
        Specified by:
        classForName in interface ClassLoaderResolver
        Parameters:
        name - Name of the Class to be loaded
        initialize - whether to initialize the class or not.
        Returns:
        The Class given the name, using the specified ClassLoader
        Throws:
        ClassNotResolvedException - if the class can't be found in the classpath
      • isAssignableFrom

        public boolean isAssignableFrom​(String class_name_1,
                                        String class_name_2)
        Utility to check the assignability of 2 classes in accordance with JDO's Class Loading mechanism. This will check class_1.isAssignableFrom(class_2);
        Specified by:
        isAssignableFrom in interface ClassLoaderResolver
        Parameters:
        class_name_1 - Name of first class
        class_name_2 - Name of second class
        Returns:
        Whether Class 2 is assignable from Class 1
      • isAssignableFrom

        public boolean isAssignableFrom​(String class_name_1,
                                        Class class_2)
        Utility to check the assignability of 2 classes in accordance with JDO's Class Loading mechanism. This will check class_1.isAssignableFrom(class_2);
        Specified by:
        isAssignableFrom in interface ClassLoaderResolver
        Parameters:
        class_name_1 - Name of first class
        class_2 - Second class
        Returns:
        Whether Class 2 is assignable from Class 1
      • isAssignableFrom

        public boolean isAssignableFrom​(Class class_1,
                                        String class_name_2)
        Utility to check the assignability of 2 classes in accordance with JDO's Class Loading mechanism. This will check class_1.isAssignableFrom(class_2);
        Specified by:
        isAssignableFrom in interface ClassLoaderResolver
        Parameters:
        class_1 - First class
        class_name_2 - Name of second class
        Returns:
        Whether Class 2 is assignable from Class 1
      • setRuntimeClassLoader

        public void setRuntimeClassLoader​(ClassLoader loader)
        ClassLoader registered to load classes created at runtime.
        Specified by:
        setRuntimeClassLoader in interface ClassLoaderResolver
        Parameters:
        loader - The ClassLoader in which classes are defined
      • registerUserClassLoader

        public void registerUserClassLoader​(ClassLoader loader)
        ClassLoader registered by users to load classes. One ClassLoader can be registered, and if one ClassLoader is already registered, the registered ClassLoader is replaced by loader.
        Specified by:
        registerUserClassLoader in interface ClassLoaderResolver
        Parameters:
        loader - The ClassLoader in which classes are loaded
      • getResources

        public Enumeration getResources​(String resourceName,
                                        ClassLoader primary)
                                 throws IOException
        Finds all the resources with the given name.
        Specified by:
        getResources in interface ClassLoaderResolver
        Parameters:
        resourceName - the resource name. If resourceName starts with "/", remove it before searching.
        primary - the primary ClassLoader to use (or null)
        Returns:
        An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty. Resources that the class loader doesn't have access to will not be in the enumeration.
        Throws:
        IOException - If I/O errors occur
        See Also:
        ClassLoader.getResources(java.lang.String)
      • getResource

        public URL getResource​(String resourceName,
                               ClassLoader primary)
        Finds the resource with the given name.
        Specified by:
        getResource in interface ClassLoaderResolver
        Parameters:
        resourceName - the path to resource name relative to the classloader root path. If resourceName starts with "/", remove it before searching.
        primary - the primary ClassLoader to use (or null)
        Returns:
        A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
        See Also:
        ClassLoader.getResource(java.lang.String)
      • setPrimary

        public void setPrimary​(ClassLoader primary)
        Sets the primary classloader for the current thread
        Specified by:
        setPrimary in interface ClassLoaderResolver
        Parameters:
        primary - the primary classloader
      • unsetPrimary

        public void unsetPrimary()
        Unsets the primary classloader for the current thread
        Specified by:
        unsetPrimary in interface ClassLoaderResolver