1 /*------------------------------------------------------------------------------
  2 Name:      SystemInfo.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Servlet to monitor system load on web server
  6 Version:   $Id: SystemInfo.java 14846 2006-03-07 17:14:22Z ruff $
  7 Author:    xmlBlaster@marcelruff.info
  8 ------------------------------------------------------------------------------*/
  9 package http.dhtml.systemInfo;
 10 
 11 import java.util.logging.Logger;
 12 import java.util.logging.Level;
 13 import org.xmlBlaster.util.StopWatch;
 14 import org.xmlBlaster.client.I_XmlBlasterAccess;
 15 import org.xmlBlaster.util.XmlBlasterException;
 16 import org.xmlBlaster.protocol.http.Util;
 17 import org.xmlBlaster.protocol.http.BlasterHttpProxy;
 18 import org.xmlBlaster.protocol.http.HttpPushHandler;
 19 import org.xmlBlaster.client.key.SubscribeKey;
 20 import org.xmlBlaster.client.qos.SubscribeQos;
 21 
 22 import javax.servlet.*;
 23 import javax.servlet.http.*;
 24 
 25 import java.io.*;
 26 
 27 /**
 28  * A servlet demonstrating the browser callback framework, using a persistent
 29  * http connection.
 30  * <p />
 31  * This servlets subscribes to messages in xmlBlaster, which are requested
 32  * from the browser, currently 'cpuinfo' and 'meminfo' messages.<br />
 33  * You see in your browser two bars displaying the current load of the
 34  * xmlBlaster.org web server.<br />
 35  * Inside the browser the bars are updated with DHTML.
 36  * <p />
 37  * If you want to do something similar, you can use this as a base
 38  * for your application.
 39  * <p />
 40  * See xmlBlaster/demo/http/README for further informations
 41  */
 42 public class SystemInfo extends HttpServlet
 43 {
 44    private static final String ME = "SystemInfo";
 45    private static Logger log = Logger.getLogger(SystemInfo.class.getName());
 46 
 47 
 48    /**
 49     * This method is invoked only once when the servlet is started.
 50     * @param conf init parameter of the servlet
 51     */
 52    public void init(ServletConfig conf) throws ServletException
 53    {
 54       super.init(conf);
 55 
 56    }
 57 
 58 
 59    /**
 60     * dummy
 61     */
 62    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException
 63    {
 64       doGet(request, response);
 65    }
 66 
 67 
 68    /**
 69     * Subscribes to xmlBlaster messages 'cpuinfo' and 'meminfo'.
 70     * <p />
 71     * The message updates are received asynchronous over the callbackFrame.
 72     * <br />
 73     * The return from this doGet() may be ignored
 74     * <p />
 75     * Invoking example:
 76     * <br />
 77     * "/servlet/SystemInfo?ActionType=cpuinfo"
 78     * @param request
 79     * @param response
 80     */
 81    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException
 82    {
 83       if (log.isLoggable(Level.FINER)) log.finer("Entering SystemInfo.doRequest() ...");
 84       StopWatch stop = new StopWatch();
 85 
 86       String sessionId = request.getRequestedSessionId();
 87       String actionType = Util.getParameter(request, "ActionType", null);
 88       String output = "Subscribing to " + actionType + " message ...";
 89 
 90       try {
 91          if (actionType == null) {
 92             String str = "Please call servlet with some ActionType e.g. xmlBlaster/dhtml/systemInfo?ActionType=cpuinfo";
 93             log.severe(str);
 94             htmlOutput(str, response);
 95             return;
 96          }
 97 
 98          I_XmlBlasterAccess corbaConnection = BlasterHttpProxy.getXmlBlasterAccess(sessionId);
 99          if (corbaConnection == null) {
100             String text = "Your Session ID is not valid, please try again with cookies enabled";
101             log.severe(text);
102             popupError(response, text);
103             return;
104          }
105 
106          // Expecting actionType = "cpuinfo" or "meminfo" but it could be
107          // any valid key oid.
108          log.info("Got request for " + actionType + ", sessionId=" + sessionId + " ...");
109 
110          SubscribeKey xmlKey = new SubscribeKey(null, actionType);
111          SubscribeQos xmlQos = new SubscribeQos(null);
112 
113          String ret = corbaConnection.subscribe(xmlKey.toXml(), xmlQos.toXml()).getSubscriptionId();
114          log.info("Subscribed to " + actionType + "=" + ret);
115 
116          // NOTE: The callback messages (update()) are handled by our
117          // BlasterHttpProxyServlet framework and pushed to the browser.
118          // Typically a browser (or applet) can subscribe itself directly at BlasterHttpProxyServlet
119          // so there is no need for this servlet
120       }
121       catch (XmlBlasterException e) {
122          String text = "Error from xmlBlaster: " + e.getMessage();
123          log.severe(text);
124          popupError(response, text);
125          return;
126       }
127       catch (Exception e) {
128          e.printStackTrace();
129          log.severe("Error in doGet(): " + e.toString());
130          popupError(response, e.toString());
131          return;
132       }
133 
134       htmlOutput(output, response);
135       log.fine("Leaving SystemInfo.doRequest() ..."+stop.nice());
136       System.gc();
137    }
138 
139 
140    /**
141     * Returns a HTML file to the Browser.
142     * @param htmlData the complete HTML page
143     * @param response the servlet response-object
144     * @see HttpServletResponse
145     */
146    public void htmlOutput(String htmlData, HttpServletResponse response) throws ServletException
147    {
148       response.setContentType("text/html");
149       try {
150          PrintWriter pw;
151          pw = response.getWriter();
152          pw.println(htmlData);
153          pw.close();
154       }
155       catch(IOException e) {
156          log.warning("Could not deliver HTML page to browser:"+e.toString());
157          throw new ServletException(e.toString());
158       }
159    }
160 
161 
162    /**
163     * Report an error to the browser, which displays it in an alert() message.
164     * @param sessionId The browser
165     * @param error The text to display
166     */
167    public void popupError(HttpServletResponse response, String error)
168    {
169       try {
170          response.setContentType("text/html");
171          PrintWriter pw;
172          pw = response.getWriter();
173          pw.println(HttpPushHandler.alert(error));
174          pw.close();
175       }
176       catch(IOException e) {
177          log.severe("Sending of error failed: " + error + "\n Reason=" + e.toString());
178       }
179    }
180 
181 
182    /**
183     * Send XML-Data to browser.
184     * The browser needs to handle the data.
185     * @param xmlData XML data
186     * @param response servlet response
187     */
188    public void xmlOutput( String xmlData, HttpServletResponse response ) throws ServletException
189    {
190       response.setContentType("text/xml");
191 
192       try {
193          PrintWriter pw;
194          pw = response.getWriter();
195          pw.println(xmlData);
196          pw.close();
197       }
198       catch(IOException e) {
199          String text = "Sending XML data to browser failed: " + e.toString();
200          log.warning(text);
201          PrintWriter pw;
202          try { pw = response.getWriter(); } catch(IOException e2) { log.severe("2.xml send problem"); return; }
203          pw.println("<html><body>Request Problems" + text + "</body></html>");
204          pw.close();
205       }
206    }
207 }


syntax highlighted by Code2HTML, v. 0.9.1