1 22 package org.jboss.console.plugins.monitor; 23 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.io.PrintWriter ; 27 import java.util.ArrayList ; 28 29 import javax.management.MBeanServer ; 30 import javax.management.ObjectName ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 35 import org.jboss.logging.Logger; 36 import org.jboss.mx.util.MBeanServerLocator; 37 import org.jfree.chart.ChartFactory; 38 import org.jfree.chart.ChartUtilities; 39 import org.jfree.chart.JFreeChart; 40 import org.jfree.chart.plot.PlotOrientation; 41 import org.jfree.data.DefaultTableXYDataset; 42 import org.jfree.data.XYSeries; 43 44 48 public class ManageSnapshotServlet extends javax.servlet.http.HttpServlet 49 { 50 private static final Logger log = Logger.getLogger(ManageSnapshotServlet.class); 51 52 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 53 throws ServletException , IOException 54 { 55 doit(req, resp); 56 } 57 58 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 59 throws ServletException , IOException 60 { 61 doit(req, resp); 62 } 63 64 protected void error(String msg, HttpServletRequest req, HttpServletResponse resp) 65 throws ServletException , IOException 66 { 67 req.setAttribute("error", "Error: " + msg); 68 req.getRequestDispatcher("/manageSnapshot.jsp").forward(req, resp); 69 return; 70 } 71 72 protected void doit(HttpServletRequest req, HttpServletResponse resp) 73 throws ServletException , IOException 74 { 75 String action = req.getParameter("action"); 76 if (action == null) 77 { 78 error("unknown action: ", req, resp); 79 return; 80 } 81 action = action.trim(); 82 MBeanServer mbeanServer = MBeanServerLocator.locateJBoss(); 83 ObjectName monitorObjectName; 84 String attribute = null; 85 try 86 { 87 monitorObjectName = new ObjectName (req.getParameter("monitorObjectName")); 88 attribute = (String )mbeanServer.getAttribute(monitorObjectName, "ObservedAttribute"); 89 } 90 catch (Exception ex) 91 { 92 error("Malformed Monitor ObjectName: " + req.getParameter("monitorObjectName"), req, resp); 93 return; 94 } 95 if (action.equals("Start Snapshot")) 96 { 97 Object [] nullArgs = {}; 98 String [] nullSig = {}; 99 try 100 { 101 mbeanServer.invoke(monitorObjectName, "startSnapshot", nullArgs, nullSig); 102 } 103 catch (Exception ex) 104 { 105 error("Problem invoking startSnapshot: " + ex.toString(), req, resp); 106 return; 107 } 108 req.getRequestDispatcher("/manageSnapshot.jsp").forward(req, resp); 109 return; 110 } 111 else if (action.equals("Stop Snapshot")) 112 { 113 Object [] nullArgs = {}; 114 String [] nullSig = {}; 115 try 116 { 117 mbeanServer.invoke(monitorObjectName, "endSnapshot", nullArgs, nullSig); 118 } 119 catch (Exception ex) 120 { 121 error("Problem invoking endSnapshot: " + ex.toString(), req, resp); 122 return; 123 } 124 req.getRequestDispatcher("/manageSnapshot.jsp").forward(req, resp); 125 return; 126 } 127 else if (action.equals("Clear Dataset")) 128 { 129 Object [] nullArgs = {}; 130 String [] nullSig = {}; 131 try 132 { 133 mbeanServer.invoke(monitorObjectName, "clearData", nullArgs, nullSig); 134 } 135 catch (Exception ex) 136 { 137 error("Problem invoking clearData: " + ex.toString(), req, resp); 138 return; 139 } 140 req.setAttribute("error", "Dataset Cleared!"); 141 req.getRequestDispatcher("/manageSnapshot.jsp").forward(req, resp); 142 return; 143 } 144 else if (action.equals("Remove Snapshot")) 145 { 146 try 147 { 148 log.debug("removing snapshot: " + monitorObjectName.toString()); 149 mbeanServer.unregisterMBean(monitorObjectName); 150 req.getRequestDispatcher("/ServerInfo.jsp").forward(req, resp); 151 } 152 catch (Exception ex) 153 { 154 error("Failed to Remove Monitor: " + ex.toString(), req, resp); 155 } 156 return; 157 } 158 else if (action.equals("Show Dataset")) 159 { 160 ArrayList data = null; 161 long start, end = 0; 162 try 163 { 164 data = (ArrayList )mbeanServer.getAttribute(monitorObjectName, "Data"); 165 start = ((Long )mbeanServer.getAttribute(monitorObjectName, "StartTime")).longValue(); 166 end = ((Long )mbeanServer.getAttribute(monitorObjectName, "EndTime")).longValue(); 167 } 168 catch (Exception ex) 169 { 170 error("Problem invoking getData: " + ex.toString(), req, resp); 171 return; 172 } 173 resp.setContentType("text/html"); 174 PrintWriter writer = resp.getWriter(); 175 writer.println("<html>"); 176 writer.println("<body>"); 177 writer.println("<b>Start Time:</b> " + start + "ms<br>"); 178 writer.println("<b>End Time:</b> " + end + "ms<br>"); 179 writer.println("<b>Total Time:</b> " + (end - start) + "ms<br>"); 180 writer.println("<br><table border=\"0\">"); 181 for (int i = 0; i < data.size(); i++) 182 { 183 writer.println("<tr><td>" + data.get(i) + "</td></tr"); 184 } 185 writer.println("</table></body></html>"); 186 return; 187 } 188 else if (action.equals("Graph Dataset")) 189 { 190 ArrayList data = null; 191 long start, end = 0; 192 try 193 { 194 data = (ArrayList )mbeanServer.getAttribute(monitorObjectName, "Data"); 195 start = ((Long )mbeanServer.getAttribute(monitorObjectName, "StartTime")).longValue(); 196 end = ((Long )mbeanServer.getAttribute(monitorObjectName, "EndTime")).longValue(); 197 } 198 catch (Exception ex) 199 { 200 error("Problem invoking getData: " + ex.toString(), req, resp); 201 return; 202 } 203 XYSeries set = new XYSeries(attribute, false, false); 204 for (int i = 0; i < data.size(); i++) 205 { 206 set.add(new Integer (i), (Number )data.get(i)); 207 } 208 DefaultTableXYDataset dataset = new DefaultTableXYDataset(false); 209 dataset.addSeries(set); 210 JFreeChart chart = ChartFactory.createXYLineChart( 211 "JMX Attribute: " + attribute, "count", attribute, dataset, 212 PlotOrientation.VERTICAL, 213 true, 214 true, 215 false 216 ); 217 resp.setContentType("image/png"); 218 OutputStream out = resp.getOutputStream(); 219 ChartUtilities.writeChartAsPNG(out, chart, 400, 300); 220 out.close(); 221 return; 222 } 223 error("Unknown Action", req, resp); 224 return; 225 } 226 227 } 228 | Popular Tags |