KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > console > plugins > monitor > ManageSnapshotServlet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.console.plugins.monitor;
23
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
45  * @author Bill Burke
46  * @version $Revision: 37459 $
47  */

48 public class ManageSnapshotServlet extends javax.servlet.http.HttpServlet JavaDoc
49 {
50    private static final Logger log = Logger.getLogger(ManageSnapshotServlet.class);
51    
52    protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
53            throws ServletException JavaDoc, IOException JavaDoc
54    {
55       doit(req, resp);
56    }
57
58    protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
59            throws ServletException JavaDoc, IOException JavaDoc
60    {
61       doit(req, resp);
62    }
63
64    protected void error(String JavaDoc msg, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
65            throws ServletException JavaDoc, IOException JavaDoc
66    {
67       req.setAttribute("error", "Error: " + msg);
68       req.getRequestDispatcher("/manageSnapshot.jsp").forward(req, resp);
69       return;
70    }
71
72    protected void doit(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
73            throws ServletException JavaDoc, IOException JavaDoc
74    {
75       String JavaDoc action = req.getParameter("action");
76       if (action == null)
77       {
78          error("unknown action: ", req, resp);
79          return;
80       }
81       action = action.trim();
82       MBeanServer JavaDoc mbeanServer = MBeanServerLocator.locateJBoss();
83       ObjectName JavaDoc monitorObjectName;
84       String JavaDoc attribute = null;
85       try
86       {
87          monitorObjectName = new ObjectName JavaDoc(req.getParameter("monitorObjectName"));
88          attribute = (String JavaDoc)mbeanServer.getAttribute(monitorObjectName, "ObservedAttribute");
89       }
90       catch (Exception JavaDoc ex)
91       {
92          error("Malformed Monitor ObjectName: " + req.getParameter("monitorObjectName"), req, resp);
93          return;
94       }
95       if (action.equals("Start Snapshot"))
96       {
97          Object JavaDoc[] nullArgs = {};
98          String JavaDoc[] nullSig = {};
99          try
100          {
101             mbeanServer.invoke(monitorObjectName, "startSnapshot", nullArgs, nullSig);
102          }
103          catch (Exception JavaDoc 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 JavaDoc[] nullArgs = {};
114          String JavaDoc[] nullSig = {};
115          try
116          {
117             mbeanServer.invoke(monitorObjectName, "endSnapshot", nullArgs, nullSig);
118          }
119          catch (Exception JavaDoc 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 JavaDoc[] nullArgs = {};
130          String JavaDoc[] nullSig = {};
131          try
132          {
133             mbeanServer.invoke(monitorObjectName, "clearData", nullArgs, nullSig);
134          }
135          catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc data = null;
161          long start, end = 0;
162          try
163          {
164             data = (ArrayList JavaDoc)mbeanServer.getAttribute(monitorObjectName, "Data");
165             start = ((Long JavaDoc)mbeanServer.getAttribute(monitorObjectName, "StartTime")).longValue();
166             end = ((Long JavaDoc)mbeanServer.getAttribute(monitorObjectName, "EndTime")).longValue();
167          }
168          catch (Exception JavaDoc ex)
169          {
170             error("Problem invoking getData: " + ex.toString(), req, resp);
171             return;
172          }
173          resp.setContentType("text/html");
174          PrintWriter JavaDoc 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 JavaDoc data = null;
191          long start, end = 0;
192          try
193          {
194             data = (ArrayList JavaDoc)mbeanServer.getAttribute(monitorObjectName, "Data");
195             start = ((Long JavaDoc)mbeanServer.getAttribute(monitorObjectName, "StartTime")).longValue();
196             end = ((Long JavaDoc)mbeanServer.getAttribute(monitorObjectName, "EndTime")).longValue();
197          }
198          catch (Exception JavaDoc 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 JavaDoc(i), (Number JavaDoc)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 JavaDoc 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