tomcat自动加载

有专门针对struct的解决方案

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.)

原文地址:https://www.cnblogs.com/macula7/p/1960728.html