Additional Code - Zip file 193 KB

Listing 1: admin.js

var url = './admin?reqid=0';
var baseCtx;
var callBack=null;
var callbackTimeout=null;

function trapAlert() {
  if (window.XMLHttpRequest) {
      req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
      req = new ActiveXObject("Microsoft.XMLHTTP");
  }

  req.onreadystatechange = processRequest;
  req.open("GET", url, true);
  req.send(null);
}

//
// readyState Status Codes:
//	0 = uninitialized
//	1 = loading
//	2 = loaded
//	3 = interactive
//	4 = complete

function processRequest() {
    if (req.readyState == 4) {
        if (req.status == 200) {
          parseMessages();
        } else {
          updateStatus(null)
        }
        setCallback(); // only do this when complete
    }
}

function parseMessages() {
	response  = req.responseXML;
	itemStatus = response.getElementsByTagName('status')[0].firstChild.nodeValue;
        itemText = response.getElementsByTagName('textBody')[0].firstChild.nodeValue;
        paintAlertText(itemText);
        callbackTimeout =
		parseInt(response.getElementsByTagName('callBack')[0].firstChild.nodeValue);
        updateStatus(itemText);

}

function paintAlertText(itemText) {
    document.getElementById("adminBanner").innerHTML = itemText;

}
function setCallback() {
  callBack = setTimeout('trapAlert()',callbackTimeout);
}

function initAdmin(uri) {
  setCallback();
}

function updateStatus(msg) {
  if (msg == null ) {
    window.status = "No Alerts";
    document.getElementById("adminBanner").innerHTML = " ";
  } else {
    window.status = "Example Alert: " + msg
  }
}


Listing 2: admin.java (servlet)

package com.grahamh.management.servlet;
/**
 * Admin
 * Processes a request to get various admin states for the OLTP user
 * @author GH
 */

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

import com.grahamh.management.UserWeb.UserWebMBeanHelper;
import com.grahamh.management.utils.MBeanHelperFactory;


public class Admin extends HttpServlet
{
  static final private String CONTENT_TYPE = "text/xml";

  public void init() throws ServletException
  {
  }
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
  ServletException, IOException
  {
    /**
     * use requestId in future to switch on admin events
     */
    String requestId = request.getParameter("reqid");
    if ((requestId == null) || (!MBeanHelperFactory.getWebHelper().isAlertReady()) )
    {
        //nothing to show
        // there re two models - if nothing to show :
        // 1. dont send anything, or
        // 2. send a message and let the client interpret the alertStatus.
        // (1) used, so client just displays an alert if one exists.
       response.setStatus(HttpServletResponse.SC_NO_CONTENT);


    } else {
      // only send a message if there is an alert notification to send
      StringBuffer sb = new StringBuffer();
      sb.append("<message><status>");
      sb.append(MBeanHelperFactory.getWebHelper().getAlertStatus());
      sb.append("</status><textBody><![CDATA[");
      sb.append(MBeanHelperFactory.getWebHelper().getAlertMessage());
      sb.append("]]></textBody>");
      sb.append("<callBack>");
      sb.append(MBeanHelperFactory.getWebHelper().getCallBack());
      sb.append("</callBack></message>");

      response.setContentType( "text/xml" );
      response.setHeader( "Cache-Control", "no-cache" );
      PrintWriter out = response.getWriter();
      out.write(sb.toString());
      System.out.println(sb.toString());
    }
  }
  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
  ServletException, IOException
  {
    doGet(request, response);
  }
  //Clean up resources
  public void destroy()
  {
  }
}

Additional Code - Zip file 193 KB