FileDocCategorySizeDatePackage
ManageSnapshotServlet.javaAPI DocJBoss 4.2.18003Fri Jul 13 21:02:14 BST 2007org.jboss.console.plugins.monitor

ManageSnapshotServlet

public class ManageSnapshotServlet extends HttpServlet
author
Bill Burke
version
$Revision: 57191 $

Fields Summary
static final long
serialVersionUID
private static final Logger
log
Constructors Summary
Methods Summary
protected voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)

   
        
             
   
      doit(req, resp);
   
protected voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)

      doit(req, resp);
   
protected voiddoit(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)

      String action = req.getParameter("action");
      if (action == null)
      {
         error("unknown action: ", req, resp);
         return;
      }
      action = action.trim();
      MBeanServer mbeanServer = MBeanServerLocator.locateJBoss();
      ObjectName monitorObjectName;
      String attribute = null;
      try
      {
         monitorObjectName = new ObjectName(req.getParameter("monitorObjectName"));
         attribute = (String)mbeanServer.getAttribute(monitorObjectName, "ObservedAttribute");
      }
      catch (Exception ex)
      {
         error("Malformed Monitor ObjectName: " + req.getParameter("monitorObjectName"), req, resp);
         return;
      }
      if (action.equals("Start Snapshot"))
      {
         Object[] nullArgs = {};
         String[] nullSig = {};
         try
         {
            mbeanServer.invoke(monitorObjectName, "startSnapshot", nullArgs, nullSig);
         }
         catch (Exception ex)
         {
            error("Problem invoking startSnapshot: " + ex.toString(), req, resp);
            return;
         }
         req.getRequestDispatcher("/manageSnapshot.jsp").forward(req, resp);
         return;
      }
      else if (action.equals("Stop Snapshot"))
      {
         Object[] nullArgs = {};
         String[] nullSig = {};
         try
         {
            mbeanServer.invoke(monitorObjectName, "endSnapshot", nullArgs, nullSig);
         }
         catch (Exception ex)
         {
            error("Problem invoking endSnapshot: " + ex.toString(), req, resp);
            return;
         }
         req.getRequestDispatcher("/manageSnapshot.jsp").forward(req, resp);
         return;
      }
      else if (action.equals("Clear Dataset"))
      {
         Object[] nullArgs = {};
         String[] nullSig = {};
         try
         {
            mbeanServer.invoke(monitorObjectName, "clearData", nullArgs, nullSig);
         }
         catch (Exception ex)
         {
            error("Problem invoking clearData: " + ex.toString(), req, resp);
            return;
         }
         req.setAttribute("error", "Dataset Cleared!");
         req.getRequestDispatcher("/manageSnapshot.jsp").forward(req, resp);
         return;
      }
      else if (action.equals("Remove Snapshot"))
      {
         try
         {
            log.debug("removing snapshot: " + monitorObjectName.toString());
            mbeanServer.unregisterMBean(monitorObjectName);
            req.getRequestDispatcher("/ServerInfo.jsp").forward(req, resp);
         }
         catch (Exception ex)
         {
            error("Failed to Remove Monitor: " + ex.toString(), req, resp);
         }
         return;
      }
      else if (action.equals("Show Dataset"))
      {
         ArrayList data = null;
         long start, end = 0;
         try
         {
            data = (ArrayList)mbeanServer.getAttribute(monitorObjectName, "Data");
            start = ((Long)mbeanServer.getAttribute(monitorObjectName, "StartTime")).longValue();
            end = ((Long)mbeanServer.getAttribute(monitorObjectName, "EndTime")).longValue();
         }
         catch (Exception ex)
         {
            error("Problem invoking getData: " + ex.toString(), req, resp);
            return;
         }
         resp.setContentType("text/html");
         PrintWriter writer = resp.getWriter();
         writer.println("<html>");
         writer.println("<body>");
         writer.println("<b>Start Time:</b> " + start + "ms<br>");
         writer.println("<b>End Time:</b> " + end + "ms<br>");
         writer.println("<b>Total Time:</b> " + (end - start) + "ms<br>");
         writer.println("<br><table border=\"0\">");
         for (int i = 0; i < data.size(); i++)
         {
            writer.println("<tr><td>" + data.get(i) + "</td></tr");
         }
         writer.println("</table></body></html>");
         return;
      }
      else if (action.equals("Graph Dataset"))
      {
         ArrayList data = null;
         long start, end = 0;
         try
         {
            data = (ArrayList)mbeanServer.getAttribute(monitorObjectName, "Data");
            start = ((Long)mbeanServer.getAttribute(monitorObjectName, "StartTime")).longValue();
            end = ((Long)mbeanServer.getAttribute(monitorObjectName, "EndTime")).longValue();
         }
         catch (Exception ex)
         {
            error("Problem invoking getData: " + ex.toString(), req, resp);
            return;
         }
         XYSeries set = new XYSeries(attribute, false, false);
         for (int i = 0; i < data.size(); i++)
         {
            set.add(new Integer(i), (Number)data.get(i));
         }
         DefaultTableXYDataset dataset = new DefaultTableXYDataset(false);
         dataset.addSeries(set);
         JFreeChart chart = ChartFactory.createXYLineChart(
                 "JMX Attribute: " + attribute, "count", attribute, dataset,
                 PlotOrientation.VERTICAL,
                 true,
                 true,
                 false
         );
         resp.setContentType("image/png");
         OutputStream out = resp.getOutputStream();
         ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
         out.close();
         return;
      }
      error("Unknown Action", req, resp);
      return;
   
protected voiderror(java.lang.String msg, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)

      req.setAttribute("error", "Error: " + msg);
      req.getRequestDispatcher("/manageSnapshot.jsp").forward(req, resp);
      return;