Custom ClassLoader - changing order of loading to look in my loaderfirst

A

amw

Problem: my J2EE ear file uses a third-party library (bunch of jar
files) to do SNMP stuff. However, the J2EE vendor also uses this
third-party library to do its own SNMP stuff. The problem is the vendor
uses a different version of the third-party library, so my code tries to
run and finds the wrong version of the library (of the jar files) that
the J2EE vendor has supplied.

Possible solution: I plan to have a custom classloader (derived from
java.lang.ClassLoader) that gets hooked in to my threads using
Thread.setContextClassLoader(). This custom classloader would look for
the third-party library jars in a spot I specify so it would find the
correct ones. Since I want to change the usual classloader behavior of
"look in parent classloaders first, then child classloader" to "look in
child classloader first, then in parent classloaders" I can't just
override ClassLoader.findClass(). I, at a minimum, have to override
ClassLoader.loadClass() to look in my classloader first. I have
questions about this:

1. What other methods do I have to override? For example, which of the
resource-related methods do I need to override so that resources are
searched for first in my classloader then in parent classloaders?

2. I was thinking of using a URLClassLoader as a helper to my
classloader, invoking its method(s) from my custom classloader's
method(s) to actually load the classes from URLs that are not on the
standard classpath. I was planning to set it up with a custom parent
classloader that can't find anything, so that the helper URLClassLoader
would only ever find classes/resources in the URLs I provide to it.
Does this approach make sense? Have you seen anything like this done
before?

Thanks in advance for any help you can provide.
 
C

Chris Smith

amw said:
Possible solution: I plan to have a custom classloader (derived from
java.lang.ClassLoader) that gets hooked in to my threads using
Thread.setContextClassLoader(). This custom classloader would look for
the third-party library jars in a spot I specify so it would find the
correct ones. Since I want to change the usual classloader behavior of
"look in parent classloaders first, then child classloader" to "look in
child classloader first, then in parent classloaders" I can't just
override ClassLoader.findClass(). I, at a minimum, have to override
ClassLoader.loadClass() to look in my classloader first. I have
questions about this:

1. What other methods do I have to override? For example, which of the
resource-related methods do I need to override so that resources are
searched for first in my classloader then in parent classloaders?

Looks like loadClass, getResource, and getResourceAsStream would be good
enough. You should be aware that this non-standard order can cause
problems unless usage of the classes to be loaded by your out-of-order
loader is sufficiently isolated. That is, it's problematic if:

ClassLoader1 is the system classloader.
ClassLoader2 has ClassLoader1 as a parent.
However, ClassLoader2 checks for ClassX elsewhere before delegating to
ClassLoader1.

ClassLoader1 loads ClassA.
ClassLoader2 loads ClassB.
ClassA has a method foo(ClassX).
ClassB wants to call ClassA's foo(ClassX) method.

However, as long as usage of ClassX is isolated across that boundary,
you can avoid the problem. Just make sure you understand and document
this limitation with regard to the class library that is loaded out-of-
order.
2. I was thinking of using a URLClassLoader as a helper to my
classloader, invoking its method(s) from my custom classloader's
method(s) to actually load the classes from URLs that are not on the
standard classpath. I was planning to set it up with a custom parent
classloader that can't find anything, so that the helper URLClassLoader
would only ever find classes/resources in the URLs I provide to it.
Does this approach make sense? Have you seen anything like this done
before?

No, I wouldn't do it like that. I'd set up a URLClassLoader as a
"friend" classloader, and write a new sort of composition classloader
that delegates to its friend, and then to its parent only if the friend
can't help... kinda like your average adolescent. So there'd be three
ClassLoaders in all, but the actual ClassLoader that you write would
only exist to delegate to others. Does that make sense to you?

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top