|
Testing the Status of Java[tm] Servlet Dynamic Class Reloading in iPlanet[tm] Application Server 6.0/6.5
By Amanda Waite
We want to hear from you! Please send us your FEEDBACK. The following article may contain actual software programs in source code form. This source code is made available for developers to use as needed, pursuant to the terms and conditions of this license. During the testing of a Web module deployed on iPlanet[tm] Application Server 6.0 (iAS6.0) recently, I became aware that there is no easy way to find out if dynamic reloading has been enabled in the application server. The process of enabling dynamic class reloading is fairly simple. In iAS6.0, you edit a registry key and restart the application server. In iAS6.5, dynamic class reloading is enabled through a checkbox in the iAS Administration Tool (iASAT), which again requires a restart of the application server. The problem is that because a restart is needed in order for a change to the setting to take effect, the fact that registry key has been changed, or in the case of iAS6.5 the check box has been checked, does not necessarily indicate that dynamic class reloading has been enabled. Another problem is that there are several different ways to shutdown and restart iAS, and you can't always be sure that the process or processes that would be affected by a configuration change have actually restarted.
Dynamic class reloading is most commonly used with Java[tm] Servlet and JavaServer Pages[tm] (JSP[tm]) components. JSPs that have not been registered (that is, do not have a GUID) will always be dynamically reloaded. While for Servlets and registered JSPs, dynamic class reloading has to be enabled. The following procedure is designed to help determine whether dynamic class reloading has been enabled for Servlets and registered JSPs.
With dynamic class reloading disabled, the Servlet Container uses a single instance of com.iplanet.ias.classloader.IasAppClassLoader to load all of the classes in a Web module, this includes the Servlet classes, compiled JSP Servlet classes and any helper classes. With dynamic class reloading enabled, the Servlet container loads helper classes in exactly the same way. But for each Servlet class (or Compiled JSP Servlet class) it creates a new classloader whose parent is the classloader that was used to load the helper classes. This new classloader is an instance of com.iplanet.ias.classloader.IasWebClassLoader, a special classloader specifically designed for the dynamic reloading of Servlet classes. During normal operations the Servlet container monitors the timestamp on all of the Servlet class files. If a class file is updated, the Servlet Container will create a new instance of IasWebClassLoader (also a child classloader used to load the helper classes) and will use it to load the updated Servlet class file.
From this we can see that it is possible to determine whether dynamic class reloading is enabled or not, simply by looking at the class name of the classloader used to load a Servlet. What we can do is write a very simple Servlet that can look at the name of the classloader that was used to load it and then return the result to the user as a HTML page with a status message.
In the code that follows we examine the actual name of the classloader instance in order to determine the type of the classloader that is being used. If we wanted to, we could get the class of the classloader and test to see if it was an instance of a specific class using the Java[tm] programming language 'instanceof' operator. However, in order to do that we would need to have that class or classes available on our classpath to enable us to compile the code. The method we have chosen simplifies things a little.
Here is the beginning of the doGet() method of our test Servlet: public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException {
String clName = getClass().getClassLoader().toString();
String status = "";
if(clName.startsWith("com.iplanet.ias.classloader.IasWebClassLoader"))
status = "Dynamic reloading is enabled for Servlets";
else if(clName.startsWith("com.iplanet.ias.classloader.IasAppClassLoader"))
status = "Dynamic reloading is not enabled for Servlets";
else
status = "Unknown classloader found: " + clName;
...
}
Firstly, we get the current classloader using getClass().getClassLoader() and then call toString() on the result and store this in the String variable clName. This returns a string representation of the classloader instance. We have chosen this method to make the code simpler and more readable. We could alternatively have called getClass() on the classloader to get its class and then called getName() to get the name of the class. We then compare the start of clName with the name of the two possible classloader classes. This tells us what type of classloader was used to load the Servlet instance, and we set the status String variable based on the results of the comparison. We expect to find two types of classloader classes as described earlier, but if another class was found then we would simply set the status to indicate the name of the class that was found. Now all that remains to be done is to display the status string. To do this we store the status string in the current session and then forward the current request to a JSP which can then be used to present the results. We do this at the end of the Servlet doGet() method as follows: Session session = request.getSession(true);
session.setAttribute("status", status);
getServletContext().getRequestDispatcher("/StatusDisplay.jsp").forward(request, response);
This gets the current session (or creates one if one does not already exist) and then adds the status string object to the session as an attribute named "status". We then forward the request and response to the JSP "/StatusDisplay.jsp". StatusDisplay.jsp can be as simple or as complicated as you like. In our code sample we simply extract the status string from the session and display it in the center of the Web Browser. The code for the StatusDisplay.jsp is as follows: <%@ page session="true" %>
<html>
<title>Status of Dynamic Servlet Reloading</title>
<body>
<h4 align="center"><%= session.getAttribute("status") %></h4>
</body>
</html>
In the 'page' directive we enable this JSP to be part of a session using session="true". The rest of the code is basic HTML except for the JSP scriplet that we use to extract the status string from the session. The important code here is: <%= session.getAttribute("status") %>
This retrieves the attribute named "status" from the session object and inserts the result into the <h4> tag ready to be displayed by a browser. The complete Servlet code is as follows: /*
* DynamicReloadTestServlet.java
*
*/
package servletutils;
import javax.servlet.*;
import javax.servlet.http.*;
public class DynamicReloadStateServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
HttpSession session = request.getSession(true);
String clName = getClass().getClassLoader().toString();
String status = "";
if(clName.startsWith("com.iplanet.ias.classloader.IasWebClassLoader"))
status = "Dynamic reloading is enabled for Servlets";
else if(clName.startsWith("com.iplanet.ias.classloader.IasAppClassLoader"))
status = "Dynamic reloading is not enabled for Servlets";
else
status = "Unknown classloader found: " + clName;
session.setAttribute("status", status);
getServletContext().getRequestDispatcher("/StatusDisplay.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doGet(request, response);
}
public String getServletInfo() {
return "Servlet for getting Dynamic reload status";
}
}
The complete code for StatusDisplay.jsp is as follows: <%@ page session="true" %>
<html>
<title>Status of Dynamic Servlet Reloading</title>
<body>
<h4 align="center"><%= session.getAttribute("status") %></h4>
</body>
</html>
The JSP and Servlet class file need to be packaged as a Web application ARchive file (WAR file) and deployed on the iPlanet Application Server. This process is well documented and isn't covered in this article. For completeness an assembled WAR file has also been provided as DrWebApp.war. If deployed on iAS 6.0 as is, it can be accessed from a browser using the following URI: http://<iAS ServerName>:<port number>/NASApp/DRwebApp/DRstatus Points to Note: In the example, the session object was used to store the status string. This should work even when Cookies are disabled in the caller's browser. An alternative would be to use the ServletContext to store the status string. While in this example we have used a JSP to display the status string we could just have easily have generated the output inside the Servlet. The method chosen was used in order to seperate the functional logic from the presentation. | |||||||||||||||||||||||||||||||||||||||