KickJava   Java API By Example, From Geeks To Geeks.

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


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.MBeanServer JavaDoc;
28 import javax.management.MalformedObjectNameException 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.monitor.StringThresholdMonitor;
36 import org.jboss.mx.util.MBeanServerLocator;
37
38 /**
39  * Created by IntelliJ IDEA.
40  * User: wburke
41  * Date: Nov 25, 2003
42  * Time: 5:53:01 PM
43  * To change this template use Options | File Templates.
44  */

45 public class CreateStringThresholdMonitorServlet extends javax.servlet.http.HttpServlet JavaDoc
46 {
47    private static final Logger log = Logger.getLogger(CreateStringThresholdMonitorServlet.class);
48    
49    protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
50            throws ServletException JavaDoc, IOException JavaDoc
51    {
52       doit(req, resp);
53    }
54
55    protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
56            throws ServletException JavaDoc, IOException JavaDoc
57    {
58       doit(req, resp);
59    }
60
61    protected void error(String JavaDoc msg, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
62            throws ServletException JavaDoc, IOException JavaDoc
63    {
64       req.setAttribute("error", "Error: " + msg);
65       //this.getServletContext().getRequestDispatcher("/createThresholdMonitor.jsp").forward(req, resp);
66
req.getRequestDispatcher("/createStringThresholdMonitor.jsp").forward(req, resp);
67       return;
68    }
69
70    protected void doit(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp)
71            throws ServletException JavaDoc, IOException JavaDoc
72    {
73
74       String JavaDoc monitorName = req.getParameter("monitorName").trim();
75       log.debug(monitorName);
76       String JavaDoc objectName = req.getParameter("objectName").trim();
77       log.debug(objectName);
78       MBeanServer JavaDoc mbeanServer = MBeanServerLocator.locateJBoss();
79       ObjectName JavaDoc oname = null;
80       try
81       {
82          oname = new ObjectName JavaDoc(objectName);
83       }
84       catch (MalformedObjectNameException JavaDoc e)
85       {
86          error("Malformed ObjectName ", req, resp);
87          return;
88       }
89       String JavaDoc attribute = req.getParameter("attribute").trim();
90       log.debug(attribute);
91       Object JavaDoc val = null;
92       try
93       {
94          val = mbeanServer.getAttribute(oname, attribute);
95       }
96       catch (Exception JavaDoc e)
97       {
98          error("Unable to pull attribute value from MBean, does the attribute exist? ", req, resp);
99          return;
100       }
101       String JavaDoc threshold = req.getParameter("threshold").trim();
102       log.debug(threshold);
103       boolean enabled = req.getParameter("enabled") != null;
104       log.debug("Enabled: " + enabled);
105       boolean persisted = req.getParameter("persisted") != null;
106       log.debug("Persisted: " + persisted);
107       String JavaDoc period = req.getParameter("period").trim();
108       log.debug(period);
109       long timePeriod = 0;
110       try
111       {
112          timePeriod = Long.parseLong(period);
113       }
114       catch (NumberFormatException JavaDoc e)
115       {
116          error("Illegal format for watch period.", req, resp);
117          return;
118       }
119       boolean equality = req.getParameter("equality") != null;
120       String JavaDoc[] alerts = req.getParameterValues("alerts");
121       if (alerts == null)
122       {
123          error("you must select at least one alert listener", req, resp);
124          return;
125       }
126
127       if (persisted)
128       {
129
130          try
131          {
132             Object JavaDoc[] args = {"monitors", monitorName, "-service.xml"};
133             String JavaDoc[] signature = {"java.lang.String", "java.lang.String", "java.lang.String"};
134             Object JavaDoc rtn = mbeanServer.invoke(new ObjectName JavaDoc("jboss.admin:service=DeploymentFileRepository"), "isStored", args, signature);
135             if (((Boolean JavaDoc)rtn).booleanValue())
136             {
137                error("Monitor with this name already exists", req, resp);
138                return;
139             }
140          }
141          catch (Exception JavaDoc ex)
142          {
143             error("Failed to determine if monitor with that name already exists: " + ex.toString(), req, resp);
144             return;
145          }
146          StringBuffer JavaDoc xml = new StringBuffer JavaDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
147          xml.append("<server>\n");
148
149          xml.append("<mbean code=\"org.jboss.monitor.StringThresholdMonitor\"\n");
150          xml.append(" name=\"jboss.monitor:service=" + monitorName.replace(' ', '_') + "\">\n");
151          xml.append(" <attribute name=\"MonitorName\">" + monitorName + "</attribute>\n");
152          try
153          {
154          if (mbeanServer.isInstanceOf(oname, "org.jboss.system.ServiceMBean"))
155          {
156             xml.append(" <depends optional-attribute-name=\"ObservedObject\">" + objectName + "</depends>\n");
157          }
158          else
159          {
160             xml.append(" <attribute name=\"ObservedObject\">" + objectName + "</attribute>\n");
161          }
162          }
163          catch (Exception JavaDoc ex)
164          {
165             error("failed creating service: " + ex.toString(), req, resp);
166             return;
167          }
168          xml.append(" <attribute name=\"ObservedAttribute\">" + attribute + "</attribute>\n");
169          xml.append(" <depends-list optional-attribute-name=\"AlertListeners\">\n");
170          for (int i = 0; i < alerts.length; i++)
171          {
172             xml.append(" <depends-list-element>");
173             xml.append(alerts[i].trim());
174             xml.append(" </depends-list-element>\n");
175          }
176          xml.append(" </depends-list>\n");
177          xml.append(" <attribute name=\"Threshold\">" + threshold + "</attribute>\n");
178          xml.append(" <attribute name=\"Period\">" + timePeriod + "</attribute>\n");
179          xml.append(" <attribute name=\"EqualityTriggersAlert\">" + equality + "</attribute>\n");
180          xml.append(" <attribute name=\"Enabled\">" + enabled + "</attribute>\n");
181          xml.append("</mbean>\n</server>");
182
183          try
184          {
185             Object JavaDoc[] args = {"monitors", monitorName, "-service.xml", xml.toString(), Boolean.TRUE};
186             String JavaDoc[] signature = {"java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "boolean"};
187             mbeanServer.invoke(new ObjectName JavaDoc("jboss.admin:service=DeploymentFileRepository"), "store", args, signature);
188          }
189          catch (Exception JavaDoc ex)
190          {
191             error("Failed to create persisted file: " + ex.toString(), req, resp);
192             return;
193          }
194       }
195       else
196       {
197          try
198          {
199             StringThresholdMonitor monitor = new StringThresholdMonitor();
200
201             monitor.setMonitorName(monitorName);
202             monitor.setObservedObject(oname);
203             monitor.setObservedAttribute(attribute);
204             monitor.setThreshold(threshold);
205             monitor.setEnabled(new Boolean JavaDoc(enabled).booleanValue());
206             monitor.setPeriod(timePeriod);
207             monitor.setEqualityTriggersAlert(equality);
208             ArrayList JavaDoc list = new ArrayList JavaDoc();
209             for (int i = 0; i < alerts.length; i++)
210             {
211                list.add(new ObjectName JavaDoc(alerts[i]));
212             }
213             monitor.setAlertListeners(list);
214             mbeanServer.registerMBean(monitor, new ObjectName JavaDoc("jboss.monitor:name=" + monitorName));
215             monitor.create();
216             monitor.start();
217          }
218          catch (Exception JavaDoc ex)
219          {
220             error("Failed to create non-persisted monitor: " + ex.toString(), req, resp);
221          }
222       }
223       req.getRequestDispatcher("/createStringThresholdMonitorSummary.jsp").forward(req, resp);
224
225    }
226
227 }
228
Popular Tags