JPOX
JPOX
JPOX  |  Version 1.0  |  Version 1.1  |  Version 1.2  |  JDO 
1.1 | Preparation | Runtime | Extensions | Tutorials and Examples | Developer
JPOX 1.1 Runtime
Runtime Tools
Queries
RDBMS Datastores
JPOX Class Loading

At runtime, JPOX will need to load the classes being persisted. To do this it needs to utilise a particular class loading mechanism. The JDO2 specification defines how class-loading is to operate in a JDO implementation and this is what JPOX uses by default. In brief the JDO2 class-loading mechanism utilises 3 class loaders

  • When creating the PersistenceManagerFactory you can specify a class loader. This is used first if specified
  • The second class loader to try is the class loader for the current thread.
  • The third class loader to try is the class loader for the PMF context.

If a class cannot be loaded using these three loaders then an exception is typically thrown depending on the operation.



Custom Class Loading

JPOX (from version 1.1.1) allows the user to specify their own class-loading mechanism. This is enabled via specifying the PersistenceManagerFactory property org.jpox.classLoaderResolverName to be the name of a class that implements the JPOX interface org.jpox.ClassLoaderResolver. This interface is shown below

public interface ClassLoaderResolver
{
    /**
     * Class loading method, allowing specification of a primary loader. This method does not initialize the class
     * @param name Name of the Class to be loaded
     * @param primary the primary ClassLoader to use (or null)
     * @return The Class given the name, using the specified ClassLoader
     * @throws ClassNotResolvedException if the class can't be found in the classpath
     */
    public Class classForName(String name, ClassLoader primary);

    /**
     * Class loading method, allowing specification of a primary loader
     * and whether the class should be initialised or not.
     * @param name Name of the Class to be loaded
     * @param primary the primary ClassLoader to use (or null)
     * @param initialize whether to initialize the class or not.
     * @return The Class given the name, using the specified ClassLoader
     * @throws ClassNotResolvedException if the class can't be found in the classpath
     */
    public Class classForName(String name, ClassLoader primary, boolean initialize);

    /**
     * Class loading method. This method does not initialize the class
     * @param name Name of the Class to be loaded
     * @return The Class given the name, using the specified ClassLoader
     */
    public Class classForName(String name);

    /**
     * Class loading method, allowing for initialisation of the class.
     * @param name Name of the Class to be loaded
     * @param initialize whether to initialize the class or not.
     * @return The Class given the name, using the specified ClassLoader
     */
    public Class classForName(String name, boolean initialize);

    /**
     * Method to test whether the type represented by the specified class_2 
     * parameter can be converted to the type represented by class_name_1 parameter.
     * @param class_name_1 Class name
     * @param class_2 Class to compare against
     * @return Whether they are assignable
     */
    public boolean isAssignableFrom(String class_name_1, Class class_2);

    /**
     * Method to test whether the type represented by the specified class_name_2 
     * parameter can be converted to the type represented by class_1 parameter.
     * @param class_1 First class
     * @param class_name_2 Class name to compare against
     * @return Whether they are assignable
     */
    public boolean isAssignableFrom(Class class_1, String class_name_2);

    /**
     * Method to test whether the type represented by the specified class_name_2 
     * parameter can be converted to the type represented by class_name_1 parameter.
     * @param class_name_1 Class name
     * @param class_name_2 Class name to compare against
     * @return Whether they are assignable
     */
    public boolean isAssignableFrom(String class_name_1, String class_name_2);
    
    /**
     * ClassLoader registered to load classes created at runtime. One ClassLoader can
     * be registered, and if one ClassLoader is already registered, the registered ClassLoader
     * is replaced by loader.
     * @param loader The ClassLoader in which classes are defined
     */
    public void registerClassLoader(ClassLoader loader);

    /**
     * Finds all the resources with the given name.
     * @param resourceName the resource name. If resourceName starts with "/", remove it before searching.
     * @return 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 ClassLoader#getResources(java.lang.String)
     */
    public Enumeration getResources(String resourceName) throws IOException;
    
    /**
     * Finds the resource with the given name.
     * @param resourceName the path to resource name relative to the classloader root path. If resourceName starts with "/", remove it.   
     * @return 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. 
     * @throws IOException If I/O errors occur
     * @see ClassLoader#getResource(java.lang.String)
     */
    public URL getResource(String resourceName);