On Aug 15, 2008, at 10:02 AM, Grzegorz Kossakowski wrote:
I'm just playing around with Cocoon's RCL plug-in in some more
complicated scenario and stumbled upon a problem when the same class
is loaded by two different classloaders.
In brief, following steps are performed:
1. Class from my block is loaded using ResourceStoreClassLoader.
Let's call this class MAIN.
2. This class accesses some static method of another class loaded by
ResourceStoreClassLoader (because this class comes from my block as
well) and sets some value on static field. Let's call this class
STATIC
3. Then another class (3RDPARTY) is being loaded which is not part
of my block, it's from third-party jar. It's loaded by parent
classloader to ResourceStoreClassLoader. As part of configuration of
this class I pass a name of class which is some kind of extension
point and is implemented by class coming from my block; let's call
this class EXTENSION.
4. 3RDPARTY creates new instance of EXTENSION class (and this class
is again not loaded by ResourceStoreClassLoader but by parent), but
in EXTENSION class one tries to access field set in STATIC.
What I don't get is why you have STATIC class in two places. You
should have it only in one place. E.g., EXTENSION as well as STATIC
and all other required classes should be in single jar file which
should be placed side by side with the 3RDPARTY jar file.
RCL will not have STATIC file and will have to use the one loaded by
parent. Right?
Vadim
5. While EXTENSION tries to access STATIC, STATIC is being loaded
again, using parent classloader. The value in STATIC is gone,
obviously.
So the basic problem is that, this STATIC class is loaded by two
different classloaders.
I'm wondering if there is any kind of general solution to such
problems. The only one I can think of is to enforce loading of
3RDPARTY class by ResourceStoreClassLoader so everything stays
within one classloader and even reflection tricks are not dangerous.
However, that would mean that one needs to modify commons-jci project.
Is there any simpler method?
--
Grzegorz Kossakowski