Listed below are the entire contents of the sample HTTP partitioning
facility application servlet.
public class ListPartitions extends HttpServlet implements Servlet,
HttpPartitionNotification {
private static String className = "ListPartitions";
private static String appName = "http.wpf.sample";
private static HttpPartitionManager httpPartitionManager =
HttpPartitionManager.instance;
public void init() throws ServletException {
System.out.println(className+": Registering notification ");
httpPartitionManager.registerNotification(appName, this);
}
public void destroy() {
System.out.println(className+": Deregistering notification ");
httpPartitionManager.deregisterNotification(appName, this);
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
ServletOutputStream out = resp.getOutputStream();
out.println("<html>");
out.println("<head><title>Hello World</title></head>");
out.println("<body>");
out.println("<h1>Hello World</h1><h1>");
out.println(listPartitions());
out.println("</h1>");
out.println("");
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
public static String listPartitions() {
HttpPartitionManager httpPartitionManager =
HttpPartitionManager.instance;
Vector partitions = httpPartitionManager.getActivePartitions
(appName);
String output = "Number of Partitions: "+partitions.size()+":
\nPartitions:\n";
for (int i=0; i< partitions.size(); i++)
output = output.concat(partitions.elementAt(i) + "\n");
return output;
}
public Vector getPartitions() {
System.out.println(className+": getPartitions ");
Vector myVect = new Vector();
myVect.add("jian");
myVect.add("lou");
System.out.println(className+": getPartitions number of partitions:
"+myVect.size());
return myVect; // Do not override partitions
}
public HttpPartitionExpression[] getExpressions() {
System.out.println(className+": getExpressions ");
HttpPartitionExpression[] expressions = new HttpPartitionExpression[2];
expressions[0] = httpPartitionManager.createHttpPartitionExpression
("(user=)(.*)&", "$2");
expressions[1] = httpPartitionManager.createHttpPartitionExpression
("(user=)(.*)$", "$2");
System.out.println(className+": getExpressions number of expressions
"+expressions.length);
return expressions; // Do not override expressions
}
public boolean loadEvent(String partitionName) {
/*
* now is a good time to start caching relevant data
*/
System.out.println(className+": load "+partitionName);
return true;
}
public void unloadEvent(String partitionName)
{
/*
* now is a good time to flush relevant cached data
*/
System.out.println(className+": unload "+partitionName);
}
public boolean isPartitionAlive(String partitionName)
{
/*
* can check if a partition is still active
*/
System.out.println(className+": isPartitionAlive ");
return true;
}
}
This example illustrates how the HttpPartitionManager is used to specify
two partition names (lou and jian in this case) and two request expressions
using the getPartitions() and getExpressions() methods of the notification
interface. If this servlet were not to specify these (if, for example, they
were provided in an accompanying partitions.xml file or specified by the EJB),
these methods would return null.