Community

ClassLoading delegation

created by rafael liu in JBoss Microcontainer - View the full discussion

It's actually not a MC thing, but it's on MC's expertise...

 

I'm trying to do a classloader which will chain 2 other classloaders. I need this since I'm making a ContainerApp that will get another web application's context (another WAR), say BookingApp, to dispatch requests.

 

So whenever ContainerApp has to load classes it will look first at BookingApp, then at ContainerApp classpash itself. My first naive approach was:

 

public class DelegatorClassLoader extends ClassLoader {
 
        @Override
        public synchronized Class<?> loadClass(String name) throws ClassNotFoundException {
                try { 
                        return bookingClassLoader.loadClass(name);
                } catch (Exception e) {
                        return containerClassLoader.loadClass(name);
                }
        }
 
...
 
}

 

Which works OK unless classes BookingApp reference ContainerApp, or vice-versa. That is, supposing the WARs had the following classes:

 

ContainerApp.war:

ContainerOnlyClass

 

BookingApp .war:

BookingOnlyClass

BookingContainerClass extends  ContainerOnlyClass

 

The results I get are:

 

DelegatorClassLoader.loadClass(ContainerOnlyClass) works

DelegatorClassLoader.loadClass(BookingOnlyClass) works

DelegatorClassLoader.loadClass(BookingContainerClass) doesn't work

 

The associated classloader is either ContainerApp or BookingApp, never DelegatorClassLoader. Reading the VM spec it's clear that the reference the class holds in to it's defining classloader, while in my code  DelegatorClassLoader is the initiator classloader.

 

My question is: doesn't MC VFS has something like this already so I could reuse it? This seems precisely what JBoss does.. Could you point me to some source code?

 

thanks a lot! cheers

Reply to this message by going to Community

Start a new discussion in JBoss Microcontainer at Community