Dapeng has posted 1 posts at DZone. View Full User Profile

Extending Tomcat WebappLoader to Share Library jars

03.14.2010
| 14272 views |
  • submit to reddit
Using Tomcat + Spring + Hibernate can quickly result in a 20M war. It took me 10mins to deploy such a war so to fix a typo in the HTML code. Wouldn't it be nice if we can somehow externalise the lib jars and different applications can share the same jars.

Reading Tomcat docs, I find out there is a shared lib folder for all the applications, provided each app is using exactly the same version. So this would not be very helpful either.

Finally I came across the Loader Component.

With a bit help from tomcat's src, I managed to extend org.apache.catalina.loader.WebappLoader that is able to load addtional lib by reading the external lib config settings from application's context.xml.

The idea is to intercept Tomcat's normal web app loading process and check if there are any tags in the context.xml. If so treat each tag as a folder that contains a set of jars, then make those jars available to the application. Then proceed with normal Tomcat web app loading.

There are 2 things to change

1. build the TomcatWebappLoader.jar (can be found in the attached zip) and put it to ${catalina.home}/lib, restart tomcat (this only needs to be done once). So the new WebappLoader is available to Tomcat.

2. change your context.xml

from
  <Context path="/testapp" />
to
  <Context path="/testapp">
    <Loader className='com.spiralcomms.tomcat.WebappLoader' />
    <lib>spring_2.5.6</lib>
    <lib>hibernate_3.3.0</lib>
  </Context>

and copy the required jars to ${catalina.home}/repo/spring_2.5.6 and ${catalina.home}/repo/hibernate_3.3.0 folders

DONE.

now the war size goes from 20M down to 300K, and the resulting war only takes a few secs to upload and deploy using Tomcat manager

I have included both the project src as well the compiled jar in the attached zip

AttachmentSize
com.spiralcomms.tomcat.WebappLoader.zip7.96 KB
Published at DZone with permission of its author, Dapeng Liu.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Manjuka Soysa replied on Sun, 2010/03/14 - 7:16pm

Why didn't you just put the jars in common/lib?

Sebastian Herold replied on Sun, 2010/03/14 - 9:00pm in response to: Manjuka Soysa

The answer is between the lines: You can use common/lib if all your webapps use the same spring and hibernate versions, but imagine you have 5 legacy apps that depend on spring 1.2 and 5 new webapps that depend on spring 2.5 and hibernate. You shouldn't put all these jars into common/lib. So the author's solution is quite nice, decreases upload time and is quiet similar to a local maven repository, which contains versioned, shared dependencies too.

Pedro Bachiega replied on Wed, 2010/03/24 - 5:39pm

Seems that VirtualWebappLoader do the same thing...
But on Javadoc, it has a restriction to not be used on production. Someone know why?

Thanks in advance!

Rares Pop replied on Fri, 2010/03/26 - 2:01am

Has anybody confirmed this to be working with Spring 3.0.x?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.