views:

2426

answers:

1

I've got a foo.war file from a third-party vendor. I've defined a context in my Tomcat configuration by creating conf/Catalina/localhost/foo.xml that contains:

<Context docBase="/path/to/foo.war" ...> ... </Context>

I want Tomcat to load up the foo context at startup. But the WEB-INF/web.xml (deployment descriptor) in the foo.war file does not include a <load-on-startup>, so Tomcat waits until the first request. I'd really rather not unpack the third-party foo.war to edit their web.xml. Plus, I'd have to do it every time the vendor releases a new version of their .war.

Is there any way within Tomcat configuration to tell Tomcat to load the foo context at startup? I know that within the <Context> element you can set paramaters, env vars, etc without editing the web.xml. But I can't find anything tin the Tomcat docs about loading on startup.

+2  A: 

This is tricky. You're limited by the conventions of Tomcat and other containers, so there's no straightforward solution.

You could use the global web.xml to initialize specific servlets and/or JSPs from the .war using the <load-on-startup> element. This is the only way I know of to force load-on-startup without modifying the .war file or the WEB-INF/web.xml inside it. Note that you may need to initialize the servlets and JSPs using different names/paths to avoid conflicts.

Of course, doing it that way means you have to know enough about the .war to initialize the app, which might mean looking at its web.xml to determine what to load. This might defeat the purpose, since it's not exactly a hands-off approach to loading just any .war on startup. But with a little extra work, you could write a script that extracts the necessary information from the .war file's web.xml and adds it to your global web.xml automatically.

Now, if you're willing to consider script writing to modify the .war file, you could just write a script that extracts WEB-INF/web.xml from the .war file, adds <load-on-startup> child elements to all the <servlet> elements, and updates the .war with the new copy. I'm not sure what environment you're using to run Tomcat, but here's an example bash script that would do the job:

#!/bin/sh

TEMPDIR=/tmp/temp$$
WARFILE=/path-to-tomcat/webapps/foo.war

mkdir -p $TEMPDIR/WEB-INF
pushd $TEMPDIR
unzip -qq -c $WARFILE WEB-INF/web.xml \
    | sed 's#</servlet>.*#<load-on-startup>99</load-on-startup></servlet>#' \
    > WEB-INF/web.xml
zip -f $WARFILE WEB-INF/web.xml
popd
rm -rf $TEMPDIR

You could run this script or something similar as part of your Tomcat startup. Hope this helps.

David Crow