Package org.datanucleus
Class ClassLoaderResolverImpl
- java.lang.Object
-
- org.datanucleus.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.
-
-
Field Summary
Fields Modifier and Type Field Description protected ClassLoader
contextLoader
ClassLoader initialised by the context (ExecutionContext).protected int
contextLoaderHashCode
Hash code cache for performance improvementprotected Map<String,Class>
jreClasses
protected Map<String,Class>
loadedClasses
Cache for loaded classesprotected Map<String,URL>
resources
Cache for resourcesprotected ClassLoader
runtimeLoader
ClassLoader registered to load runtime created classes.protected int
runtimeLoaderHashCode
Hash code cache for performance improvementprotected Map<String,Class>
unloadedClasses
Cache for loaded classesprotected ClassLoader
userRegisteredLoader
ClassLoader registered to load classes (e.g set in the persistence properties as the primary loader).protected int
userRegisteredLoaderHashCode
Hash code cache for performance improvement
-
Constructor Summary
Constructors Constructor Description ClassLoaderResolverImpl()
Constructor for non-PersistenceManager cases so there is no PM context loader.ClassLoaderResolverImpl(ClassLoader ctxLoader)
Constructor for ExecutionContext cases.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Class
classForName(String name)
JDO's Class Loading mechanism (Spec 1.0.1 Chapter 12.5).Class
classForName(String name, boolean initialize)
JDO's Class Loading mechanism (Spec 1.0.1 Chapter 12.5).Class
classForName(String name, ClassLoader primary)
JDO's Class Loading mechanism (Spec 1.0.1 Chapter 12.5).Class
classForName(String name, ClassLoader primary, boolean initialize)
JDO's Class Loading mechanism (Spec 1.0.1 Chapter 12.5).URL
getResource(String resourceName, ClassLoader primary)
Finds the resource with the given name.Enumeration
getResources(String resourceName, ClassLoader primary)
Finds all the resources with the given name.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.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.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.protected void
loadJreClasses()
void
registerUserClassLoader(ClassLoader loader)
ClassLoader registered by users to load classes.void
setPrimary(ClassLoader primary)
Sets the primary classloader for the current threadvoid
setRuntimeClassLoader(ClassLoader loader)
ClassLoader registered to load classes created at runtime.String
toString()
void
unsetPrimary()
Unsets the primary classloader for the current thread
-
-
-
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
-
-
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 interfaceClassLoaderResolver
- Parameters:
name
- Name of the Class to be loadedprimary
- 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 interfaceClassLoaderResolver
- Parameters:
name
- Name of the Class to be loadedprimary
- 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 interfaceClassLoaderResolver
- 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 interfaceClassLoaderResolver
- Parameters:
name
- Name of the Class to be loadedinitialize
- 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 interfaceClassLoaderResolver
- Parameters:
class_name_1
- Name of first classclass_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 interfaceClassLoaderResolver
- Parameters:
class_name_1
- Name of first classclass_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 interfaceClassLoaderResolver
- Parameters:
class_1
- First classclass_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 interfaceClassLoaderResolver
- 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 byloader
.- Specified by:
registerUserClassLoader
in interfaceClassLoaderResolver
- 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 interfaceClassLoaderResolver
- Parameters:
resourceName
- the resource name. IfresourceName
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 interfaceClassLoaderResolver
- Parameters:
resourceName
- the path to resource name relative to the classloader root path. IfresourceName
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 interfaceClassLoaderResolver
- Parameters:
primary
- the primary classloader
-
unsetPrimary
public void unsetPrimary()
Unsets the primary classloader for the current thread- Specified by:
unsetPrimary
in interfaceClassLoaderResolver
-
-