Google
 
Web unafbapune.blogspot.com

Wednesday, June 13, 2007

 

Hacking JGroups Util.java

Recently I encountered a resource loading problem in JGroups from within a Tomcat webapp when trying to construct a JChannel when so triggered from a JMX MBean via the jconsole:
    channel = new JChannel("tcp.xml");
Basically if the JChannel is constructed from the webapp, it can locate the "tcp.xml" as a resource and works just fine. However if the JChannel is constructed from an MBean call stack via the jconsole, it failed to locate the resource due to the fact that the context classloading of the executing thread is different. So I hacked org.jgroups.util.Util to make it work in both cases. I made the hack on jgroups 2.2.9.4 , but I tried the latest 2.5.0CR1 which also seemed to exhibit the same problem/behavior. More details below.

My question is: should the hack I made be incorporated into the official version ? Guess what ? The next day, Bela Ban, the JGroups Lead / JBoss Clustering team, unconditionally accepted this change into the official JGroups 2.4 branch and the CVS head. Yeah!
public class Util {
...
public static InputStream getResourceAsStream(String name, Class clazz) {
ClassLoader loader;

try {
loader=Thread.currentThread().getContextClassLoader();
if(loader != null) {
// hack: returns only if resource is found
// return loader.getResourceAsStream(name);
InputStream is = loader.getResourceAsStream(name);

if (is != null)
return is;
}
}
catch(Throwable t) {
}

if(clazz != null) {
try {
loader=clazz.getClassLoader();
if(loader != null) {
// hack: returns only if resource is found
// return loader.getResourceAsStream(name);
InputStream is = loader.getResourceAsStream(name);

if (is != null)
return is;
}
}
catch(Throwable t) {
}
}
try {
loader= ClassLoader.getSystemClassLoader();
if(loader != null) {
return loader.getResourceAsStream(name);
}
}
catch(Throwable t) {
}

return null;
}
...
}

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?