db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oyvind.Bakk...@Sun.COM
Subject Re: Question about using URLClassLoader and Derby
Date Thu, 10 Nov 2005 14:01:18 GMT
Knut Anders Hatlen wrote:
> Oyvind.Bakksjo@Sun.COM writes:
> 
>>Your URLClassLoader will delegate loading to its parent before it
>>tries to load anything itself. You need to be sure that your
>>URLClassLoader's parents have no Derby system to load - if they do,
>>the result is not what you want.
>>
>>If you cannot assume anything about what's in the parent classloaders'
>>search path, you will need to create your own custom classloader which
>>"cuts off" the delegation for classes whose names follow a certain
>>pattern - for instance, start with "org.apache.derby." I have done
>>this successfully in other projects.
> 
> 
> Overloading the loadClass method like this is enough, isn't it?
> 
> // untested code follows
> protected Class loadClass(String name, boolean resolve)
>                    throws ClassNotFoundException {
>     Class cl = findLoadedClass(name);
>     // uncomment the next line to use parent classloader
>     //if (cl == null) cl = getParent().loadClass(name);
>     if (cl == null) cl = findClass(name);
>     if (cl == null) throw new ClassNotFoundException();
>     if (resolve) resolveClass(cl);
>     return cl;
> }

Except that you cannot unconditionally disable delegation to the parent 
- code using your classloader will never be able to find 
java.lang.String, for example.

Something like

// untested code follows
protected Class loadClass(String name, boolean resolve)
     throws ClassNotFoundException
{
     Class cl = findLoadedClass(name);
     if (cl == null) {
         // cut off delegation to parent for certain classes
         // to ensure loading from the desired source
         if (!name.startsWith("org.apache.derby")) {
             cl = getParent().loadClass(name);
         }
     }
     if (cl == null) cl = findClass(name);
     if (cl == null) throw new ClassNotFoundException();
     if (resolve) resolveClass(cl);
     return cl;
}


You could subclass URLClassLoader and override this method.

-- 
Oyvind Bakksjo
Sun Microsystems, Database Technology Group
Trondheim, Norway
http://weblogs.java.net/blog/bakksjo/

Mime
View raw message