The Source for Java Technology Collaboration
User: Password:



Budi Kurniawan

Budi Kurniawan's Blog

Free Module for More Rapid Struts Development with Tomcat

Posted by budi on April 05, 2005 at 12:27 AM | Comments (2)

Both Tomcat and Struts are successful open source projects. The former is a world class servlet/JSP container, the latter a very popular framework for building Model 2 Web applications. And, lots of people use the combination of both. Here is a module that helps develop Struts applications more rapidly.

As you may know, you can configure Tomcat to automatically reload an application if the web.xml file has been modified or a class/library has been added/modified. This really saves time because you don't have to reload your application yourself. Even if you are using the Tomcat Admin application, this feature still saves you many clicks of mouse buttons. However, Tomcat cannot be configured to reload a Struts application if the Struts configuration file (struts-config.xml) file has changed. This is unfortunate because the struts-config.xml changes frequently during development.

This module, which you can download from BrainySoftware.com, is a Tomcat plugin that does not require you to do anything other than copying it to a directory. It works by overriding the WebappLoader, a Tomcat component responsible for loading Web applications. The Context component in Tomcat supports auto-reload. When this feature is enabled, the WebappLoader's modified method gets called regularly. This method checks if any class under WEB-INF/classes or any JAR file under WEB-INF/lib has been changed. If it has, the modified method returns true and the Context is reloaded.
The Tomcat-Struts module adds a new method called checkStrutsConfig to the WebappLoader class. This method returns true if the struts-config.xml file in WEB-INF has been modified. It also changes the original modified method from:

public boolean modified() {
return (classLoader.modified());
}

to

public boolean modified() {
return (checkStrutsConfig() || classLoader.modified());
}

which means, the modified method will return true if the struts-config.xml has been modified since the last time it was checked.
Now, if you've been a Java programmer for a while, you must know how easy it is to check the timestamp of a file. In fact, modifying the WebappLoader class is a matter of adding these 33 lines of code:

long strutsConfigLastModified = getStrutsConfigTimestamp();

private String getContextPath() {
Container container = getContainer();
if (!(container instanceof Context))
return "";
return ((Context) container).getPath();
}

protected long getStrutsConfigTimestamp() {
// return -1 if struts-config.xml is not available
Container container = getContainer();
if (!(container instanceof Context))
return -1L;

ServletContext servletContext = ((Context) container).getServletContext();


if (servletContext == null)
return -1L;
String realPath = servletContext.getRealPath("/");
File strutsConfig = new File(realPath, "/WEB-INF/struts-config.xml");
if (!strutsConfig.exists())
return -1L;
else
return strutsConfig.lastModified();
}

protected boolean checkStrutsConfig() {
long currentTimestamp = getStrutsConfigTimestamp();
if (currentTimestamp!=-1L && currentTimestamp!=strutsConfigLastModified) {
strutsConfigLastModified = currentTimestamp;
return true;
}
return false;
}


I hope you find this useful. (This is also my answer to readers of my "How Tomcat Works" book who, after learning to be better programmers by examining how the Tomcat Development Team designed and developed Tomcat, asked if I had an example of how to extend Tomcat.)


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Hi there,

    Thanks for the article. useful info.

    why did you publish "How Tomcat Works" under your own publishing house ? Why not through, say O'Reilly or manning ? How about sourcebeat ? Sourcebeat would be best since Tomcat is a ever moving target and that investment would be considered by users as safer than other types of books.

    Also, the price of the book is on the higher end -- going through sourcebeat/manning would give you more prints/publicity and the users a better price. By NOT having a low-priced PDF version of the book, you're keeping the book out of reach of most book buyers (mind you, I invest $300 - $1000/year on tech books but still consider this book to be expensive).

    thank you,

    BR,
    ~A

    Posted by: anjanb2 on April 06, 2005 at 01:16 PM

  • PDF versions of both "How Tomcat Works" and "Struts Design and Programming" are available from my site www.brainysoftware.com, at half price.
    Other than not being able to force booksellers to pass huge discounts to readers, I think my books are reasonable priced. For example, How Tomcat Works, which took 2 years to complete, is $49.99, the same as my Java for the Web book which took me 4 months to write. Also, I used to sell direct with a 45% discount, but some booksellers complained so I removed the page. Now, a 45% discount is only available to those who contact me direct.

    Thanks for your support.

    Posted by: budi on April 08, 2005 at 06:21 PM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds