KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
26
27 import javax.management.Attribute JavaDoc;
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.jboss.logging.Logger;
35 import org.jboss.mx.util.MBeanServerLocator;
36
37 /**
38  * Created by IntelliJ IDEA.
39  * User: wburke
40  * Date: Nov 25, 2003
41  * Time: 5:53:01 PM
42  * To change this template use Options | File Templates.
43  */

44 public class ManageThresholdMonitorServlet extends javax.servlet.http.HttpServlet JavaDoc
45 {
46    private static final Logger log = Logger.getLogger(ManageThresholdMonitorServlet.class);
47    
48    protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
49            throws ServletException JavaDoc, IOException JavaDoc
50    {
51       doit(req, resp);
52    }
53
54    protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
55            throws ServletException JavaDoc, IOException JavaDoc
56    {
57       doit(req, resp);
58    }
59
60    protected void error(String JavaDoc msg, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
61            throws ServletException JavaDoc, IOException JavaDoc
62    {
63       req.setAttribute("error", "Error: " + msg);
64       //this.getServletContext().getRequestDispatcher("/createThresholdMonitor.jsp").forward(req, resp);
65
req.getRequestDispatcher("/manageThresholdMonitor.jsp").forward(req, resp);
66       return;
67    }
68
69    protected void doit(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
70            throws ServletException JavaDoc, IOException JavaDoc
71    {
72       String JavaDoc action = req.getParameter("action");
73       if (action == null)
74       {
75          error("unknown action: ", req, resp);
76          return;
77       }
78       String JavaDoc monitorName = req.getParameter("monitorName").trim();
79       MBeanServer JavaDoc mbeanServer = MBeanServerLocator.locateJBoss();
80       ObjectName JavaDoc monitorObjectName, oname = null;
81       String JavaDoc attribute = null;
82       try
83       {
84          monitorObjectName = new ObjectName JavaDoc(req.getParameter("monitorObjectName"));
85       }
86       catch (Exception JavaDoc ex)
87       {
88          error("Malformed Monitor ObjectName: " + req.getParameter("monitorObjectName"), req, resp);
89          return;
90       }
91       if (action.trim().equals("Clear Alert"))
92       {
93          try
94          {
95             log.debug("Clearing Alert for monitor: " + monitorObjectName.toString());
96             String JavaDoc[] signature = {};
97             Object JavaDoc[] args = {};
98             mbeanServer.invoke(monitorObjectName, "clearAlert", args, signature);
99             req.setAttribute("error", "Alert cleared");
100             req.getRequestDispatcher("/manageThresholdMonitor.jsp").forward(req, resp);
101             return;
102          }
103          catch (Exception JavaDoc ex)
104          {
105             error("Failed to Clear Alert: " + ex.toString(), req, resp);
106             return;
107          }
108       }
109       else if (action.trim().equals("Remove Monitor"))
110       {
111          try
112          {
113             log.debug("removing monitor: " + monitorObjectName.toString());
114             Object JavaDoc[] args = {"monitors", monitorName, "-service.xml"};
115             String JavaDoc[] signature = {"java.lang.String", "java.lang.String", "java.lang.String"};
116             mbeanServer.invoke(new ObjectName JavaDoc("jboss.admin:service=DeploymentFileRepository"), "remove", args, signature);
117             req.getRequestDispatcher("/ServerInfo.jsp").forward(req, resp);
118          }
119          catch (Exception JavaDoc ex)
120          {
121             error("Failed to Remove Monitor: " + ex.toString(), req, resp);
122          }
123          return;
124       }
125       try
126       {
127          monitorObjectName = new ObjectName JavaDoc(req.getParameter("monitorObjectName"));
128          oname = (ObjectName JavaDoc) mbeanServer.getAttribute(monitorObjectName, "ObservedObject");
129          attribute = (String JavaDoc) mbeanServer.getAttribute(monitorObjectName, "ObservedAttribute");
130       }
131       catch (Exception JavaDoc ex)
132       {
133          error("Malformed Monitor ObjectName: " + req.getParameter("monitorObjectName"), req, resp);
134          return;
135       }
136       String JavaDoc threshold = req.getParameter("threshold").trim();
137       log.debug(threshold);
138       boolean enabled = req.getParameter("enabled") != null;
139       log.debug("Enabled: " + enabled);
140       boolean persisted = req.getParameter("persisted") != null;
141       log.debug("Persisted: " + persisted);
142       String JavaDoc period = req.getParameter("period").trim();
143       log.debug(period);
144       long timePeriod = 0;
145       try
146       {
147          timePeriod = Long.parseLong(period);
148       }
149       catch (NumberFormatException JavaDoc e)
150       {
151          error("Illegal format for watch period.", req, resp);
152          return;
153       }
154       String JavaDoc compare = req.getParameter("compare").trim();
155       log.debug(compare);
156       String JavaDoc[] alerts = req.getParameterValues("alerts");
157       if (alerts == null)
158       {
159          error("you must select at least one alert listener", req, resp);
160          return;
161       }
162       int compareTo = 0;
163       if ("gt".equals(compare))
164          compareTo = -1;
165       else if ("lt".equals(compare))
166          compareTo = 1;
167       else if ("eq".equals(compare)) compareTo = 0;
168
169
170       try
171       {
172          mbeanServer.setAttribute(monitorObjectName, new Attribute JavaDoc("Threshold", threshold));
173          mbeanServer.setAttribute(monitorObjectName, new Attribute JavaDoc("Enabled", new Boolean JavaDoc(enabled)));
174          mbeanServer.setAttribute(monitorObjectName, new Attribute JavaDoc("Period", new Long JavaDoc(timePeriod)));
175          mbeanServer.setAttribute(monitorObjectName, new Attribute JavaDoc("CompareTo", new Integer JavaDoc(compareTo)));
176
177          ArrayList JavaDoc list = new ArrayList JavaDoc();
178          for (int i = 0; i < alerts.length; i++)
179          {
180             list.add(new ObjectName JavaDoc(alerts[i]));
181          }
182          mbeanServer.setAttribute(monitorObjectName, new Attribute JavaDoc("AlertListeners", list));
183       }
184       catch (Exception JavaDoc ex)
185       {
186          error("Failed to update mbean monitor: " + ex.toString(), req, resp);
187          return;
188       }
189       if (persisted)
190       {
191
192          try
193          {
194             Object JavaDoc[] args = {"monitors", monitorName, "-service.xml"};
195             String JavaDoc[] signature = {"java.lang.String", "java.lang.String", "java.lang.String"};
196             Object JavaDoc rtn = mbeanServer.invoke(new ObjectName JavaDoc("jboss.admin:service=DeploymentFileRepository"), "isStored", args, signature);
197             if (!((Boolean JavaDoc) rtn).booleanValue())
198             {
199                error("Monitor with this name doesn't exist in repository", req, resp);
200                return;
201             }
202          }
203          catch (Exception JavaDoc ex)
204          {
205             error("Failed to determine if monitor with that name already exists: " + ex.toString(), req, resp);
206             return;
207          }
208          StringBuffer JavaDoc xml = new StringBuffer JavaDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
209          xml.append("<server>\n");
210
211          xml.append("<mbean code=\"org.jboss.monitor.ThresholdMonitor\"\n");
212          xml.append(" name=\"jboss.monitor:service=" + monitorName.replace(' ', '_') + "\">\n");
213          xml.append(" <attribute name=\"MonitorName\">" + monitorName + "</attribute>\n");
214          try
215          {
216             if (mbeanServer.isInstanceOf(oname, "org.jboss.system.ServiceMBean"))
217             {
218                xml.append(" <depends optional-attribute-name=\"ObservedObject\">" + oname + "</depends>\n");
219             }
220             else
221             {
222                xml.append(" <attribute name=\"ObservedObject\">" + oname + "</attribute>\n");
223             }
224          }
225          catch (Exception JavaDoc ex)
226          {
227             error("failed creating service: " + ex.toString(), req, resp);
228             return;
229          }
230          xml.append(" <attribute name=\"ObservedAttribute\">" + attribute + "</attribute>\n");
231          xml.append(" <depends-list optional-attribute-name=\"AlertListeners\">\n");
232          for (int i = 0; i < alerts.length; i++)
233          {
234             xml.append(" <depends-list-element>");
235             xml.append(alerts[i].trim());
236             xml.append(" </depends-list-element>\n");
237          }
238          xml.append(" </depends-list>\n");
239          xml.append(" <attribute name=\"Threshold\">" + threshold + "</attribute>\n");
240          xml.append(" <attribute name=\"Period\">" + timePeriod + "</attribute>\n");
241          xml.append(" <attribute name=\"CompareTo\">" + compareTo + "</attribute>\n");
242          xml.append(" <attribute name=\"Enabled\">" + enabled + "</attribute>\n");
243          xml.append("</mbean>\n</server>");
244
245          try
246          {
247             Object JavaDoc[] args = {"monitors", monitorName, "-service.xml", xml.toString(), Boolean.TRUE};
248             String JavaDoc[] signature = {"java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "boolean"};
249             mbeanServer.invoke(new ObjectName JavaDoc("jboss.admin:service=DeploymentFileRepository"), "store", args, signature);
250          }
251          catch (Exception JavaDoc ex)
252          {
253             error("Failed to create persisted file: " + ex.toString(), req, resp);
254             return;
255          }
256       }
257       req.setAttribute("error", "Update complete!");
258       req.getRequestDispatcher("/manageThresholdMonitor.jsp").forward(req, resp);
259
260    }
261
262 }
263
Popular Tags