Friday, July 29, 2005

What version of a class or resource am I using?

I end up working with lots and lots of different Web applications, fat-client applications, and server extensions, meaning that I get various versions of various bits of code scattered all over the damned place. This becomes a real problem when you're basically learning things on the fly and modifying stuff heavily. A really good example is working with log4j. After lots of changes and extensions, I'm reasonably happy with our current configuration. However there are lots of log4j.properties files floating around and sometimes my apps end up getting one of these older configurations, which leads to heartache. So how can you figure out which version of a particular resource you're getting?

The code below shows how you can use the Java VM itself to tell you where items are coming from:

ClassLoader cl = Thread.currentThread().getContextClassLoader();
while(cl != null)
{
   java.net.URL loc = cl.getResource("/log4j.properties");
   System.out.println("Search and destroy --> " + loc);
   cl = cl.getParent();
}

It's worth noting that your classes will end up using the first version found in the class loader (assuming all classes in the same process space). The other versions found, if any, will be obscured by the first version.

No comments: