Unstable Terrain

Software development in the real world

Posts Tagged ‘Tomcat

Document base /path/to/exploded/war/ does not exist or is not a readable directory

leave a comment »

I discovered the easy technique where one can point Tomcat at a directory instead of copying stuff into the /webapps dir.

So, I created $CATALINA_HOME/conf/Catalina/localhost/ma.xml like so:

<Context path="/ma" docBase="/workspace/ma/target/ma" reloadable="true" />

After a few false starts (turns out that Tomcat on Windows doesn’t accept cygwin-style paths even when started from sygwin), I thought I had it sorted.

<Context path="/ma" docBase="d:\workspace\ma\target\ma" reloadable="true" />

but Tomcat was having none of it.

16/06/2011 5:04:23 PM org.apache.catalina.core.StandardContext resourcesStart
 SEVERE: Error starting static Resources
 java.lang.IllegalArgumentException: Document base D:\workspace\ma\target\ma does not exist or is not a readable directory

Turns out I was being stupid and forgot the trailing slash in the docBase.

Derp.

Written by Trent

June 16, 2011 at 6:04 pm

Posted in Software Development, Tools

Tagged with

Tomcat 6.0.26 released

leave a comment »

Changelog says that Bug 41059 has been fixed, meaning that tomcat won’t hang inconveniently when it tries to shut down. Neat.

Written by Trent

April 16, 2010 at 8:12 pm

Posted in Software Development, Tools

Tagged with , ,

Java Reflection Api: More Drawbacks

with one comment

Vinay wrote on DZone about the Java Reflection API and mentioned some drawbacks. Here’s one he missed:

The Reflection API Loads Classes as a Side-Effect

I encountered an error message recently in the Tomcat application I currently develop. I upgraded Log4J to 1.2.15 and suddenly, I was unable to stop Tomcat cleanly.

log4j:ERROR LogMananger.repositorySelector was null likely due to error in class reloading, using NOPLoggerRepository.

I found mention of this error in Log4J Bug 43867, but it turns out that the error lies inside Tomcat instead, and is due to the way that Tomcat unloads classes when WARs are undeployed. But to understand the bug, a short intro on class loading is in order.

Classloading 100

Classes are loaded in multiple stages. The main reason for this is to decrease program startup time and avoid using memory until the class is actually used.

The three stages of classloading are:

  1. Loading
  2. Linking
  3. Initialising

and only then is the class instantiated. Classes tend to be linked fairly eagerly, but lazily initialised. (For more details, please see this TechJava article)

Back to the bug

When Tomcat unloads a WAR, it helpfully tries to compensate for bad programming inside the WAR by clearing out any static fields it can find. It’s a crude but effective way to dereference fields that might have strong references to stuff you are trying to unload.

Inside Tomcat, org.apache.catalina.loader.WebappClassLoader.clearReferences() triggers the bug
by overwriting all static variables in your webapp (classes and JARs) with null. It does this even with the standard Log4J LogFactory. However, getting the field of a class that hasn’t been initialised will cause it to be initialised, which instantiates all the static fields…. including those static fields which depend on LogFactory! Hilarity ensues.

Luckily, Log4J can detect when something (i.e. Tomcat) has pulled the rug out from underneath it and will substitute a NOPLoggerRepository which won’t do any logging but at least won’t throw Null Pointer Exceptions. It’s interesting to note that Log4J is often used in static fields, so in hindsight, this is obviously not the first time they’ve had to contend with this issue. At any rate, it spits out the error message, but Tomcat is still broken and the WAR unloading fails.

The Solution

The solution is fairly easy. First, Tomcat should make a list of all the fields it’s about to null out. This will trigger class instantiation on any class that hasn’t been completely loaded. Once this is complete, Tomcat can then null out the fields safely. Tomcat Bug 41059 has this fix, and you can expect it soon in Tomcat 6.0.21.

Written by Trent

October 27, 2009 at 9:00 am