KickJava   Java API By Example, From Geeks To Geeks.

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


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 ManageStringThresholdMonitorServlet extends javax.servlet.http.HttpServlet JavaDoc
45 {
46    private static final Logger log = Logger.getLogger(ManageStringThresholdMonitorServlet.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("/manageStringThresholdMonitor.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       boolean equality = req.getParameter("equality") != null;
142       log.debug("Persisted: " + persisted);
143       String JavaDoc period = req.getParameter("period").trim();
144       log.debug(period);
145       long timePeriod = 0;
146       try
147       {
148          timePeriod = Long.parseLong(period);
149       }
150       catch (NumberFormatException JavaDoc e)
151       {
152          error("Illegal format for watch period.", req, resp);
153          return;
154       }
155       String JavaDoc[] alerts = req.getParameterValues("alerts");
156       if (alerts == null)
157       {
158          error("you must select at least one alert listener", req, resp);
159          return;
160       }
161       try
162       {
163          mbeanServer.setAttribute(monitorObjectName, new Attribute JavaDoc("Threshold", threshold));
164          mbeanServer.setAttribute(monitorObjectName, new Attribute JavaDoc("Enabled", new Boolean JavaDoc(enabled)));
165          mbeanServer.setAttribute(monitorObjectName, new Attribute JavaDoc("Period", new Long JavaDoc(timePeriod)));
166          mbeanServer.setAttribute(monitorObjectName, new Attribute JavaDoc("EqualityTriggersAlert", new Boolean JavaDoc(equality)));
167
168          ArrayList JavaDoc list = new ArrayList JavaDoc();
169          for (int i = 0; i < alerts.length; i++)
170          {
171             list.add(new ObjectName JavaDoc(alerts[i]));
172          }
173          mbeanServer.setAttribute(monitorObjectName, new Attribute JavaDoc("AlertListeners", list));
174       }
175       catch (Exception JavaDoc ex)
176       {
177          error("Failed to update mbean monitor: " + ex.toString(), req, resp);
178          return;
179       }
180       if (persisted)
181       {
182
183          try
184          {
185             Object JavaDoc[] args = {"monitors", monitorName, "-service.xml"};
186             String JavaDoc[] signature = {"java.lang.String", "java.lang.String", "java.lang.String"};
187             Object JavaDoc rtn = mbeanServer.invoke(new ObjectName JavaDoc("jboss.admin:service=DeploymentFileRepository"), "isStored", args, signature);
188             if (!((Boolean JavaDoc) rtn).booleanValue())
189             {
190                error("Monitor with this name doesn't exist in repository", req, resp);
191                return;
192             }
193          }
194          catch (Exception JavaDoc ex)
195          {
196             error("Failed to determine if monitor with that name already exists: " + ex.toString(), req, resp);
197             return;
198          }
199          StringBuffer JavaDoc xml = new StringBuffer JavaDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
200          xml.append("<server>\n");
201
202          xml.append("<mbean code=\"org.jboss.monitor.StringThresholdMonitor\"\n");
203          xml.append(" name=\"jboss.monitor:service=" + monitorName.replace(' ', '_') + "\">\n");
204          xml.append(" <attribute name=\"MonitorName\">" + monitorName + "</attribute>\n");
205          try
206          {
207             if (mbeanServer.isInstanceOf(oname, "org.jboss.system.ServiceMBean"))
208             {
209                xml.append(" <depends optional-attribute-name=\"ObservedObject\">" + oname + "</depends>\n");
210             }
211             else
212             {
213                xml.append(" <attribute name=\"ObservedObject\">" + oname + "</attribute>\n");
214             }
215          }
216          catch (Exception JavaDoc ex)
217          {
218             error("failed creating service: " + ex.toString(), req, resp);
219             return;
220          }
221          xml.append(" <attribute name=\"ObservedAttribute\">" + attribute + "</attribute>\n");
222          xml.append(" <depends-list optional-attribute-name=\"AlertListeners\">\n");
223          for (int i = 0; i < alerts.length; i++)
224          {
225             xml.append(" <depends-list-element>");
226             xml.append(alerts[i].trim());
227             xml.append(" </depends-list-element>\n");
228          }
229          xml.append(" </depends-list>\n");
230          xml.append(" <attribute name=\"Threshold\">" + threshold + "</attribute>\n");
231          xml.append(" <attribute name=\"Period\">" + timePeriod + "</attribute>\n");
232          xml.append(" <attribute name=\"EqualityTriggersAlert\">" + equality + "</attribute>\n");
233          xml.append(" <attribute name=\"Enabled\">" + enabled + "</attribute>\n");
234          xml.append("</mbean>\n</server>");
235
236          try
237          {
238             Object JavaDoc[] args = {"monitors", monitorName, "-service.xml", xml.toString(), Boolean.TRUE};
239             String JavaDoc[] signature = {"java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "boolean"};
240             mbeanServer.invoke(new ObjectName JavaDoc("jboss.admin:service=DeploymentFileRepository"), "store", args, signature);
241          }
242          catch (Exception JavaDoc ex)
243          {
244             error("Failed to create persisted file: " + ex.toString(), req, resp);
245             return;
246          }
247       }
248       req.setAttribute("error", "Update complete!");
249       req.getRequestDispatcher("/manageThresholdMonitor.jsp").forward(req, resp);
250
251    }
252
253 }
254
Popular Tags