KickJava   Java API By Example, From Geeks To Geeks.

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


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.ThresholdMonitor;
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 CreateThresholdMonitorServlet extends javax.servlet.http.HttpServlet JavaDoc
46 {
47    private static final Logger log = Logger.getLogger(CreateThresholdMonitorServlet.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("/createThresholdMonitor.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       String JavaDoc compare = req.getParameter("compare").trim();
120       log.debug(compare);
121       String JavaDoc[] alerts = req.getParameterValues("alerts");
122       if (alerts == null)
123       {
124          error("you must select at least one alert listener", req, resp);
125          return;
126       }
127       int compareTo = 0;
128       if ("gt".equals(compare))
129          compareTo = -1;
130       else if ("lt".equals(compare))
131          compareTo = 1;
132       else if ("eq".equals(compare)) compareTo = 0;
133
134       if (persisted)
135       {
136
137          try
138          {
139             Object JavaDoc[] args = {"monitors", monitorName, "-service.xml"};
140             String JavaDoc[] signature = {"java.lang.String", "java.lang.String", "java.lang.String"};
141             Object JavaDoc rtn = mbeanServer.invoke(new ObjectName JavaDoc("jboss.admin:service=DeploymentFileRepository"), "isStored", args, signature);
142             if (((Boolean JavaDoc)rtn).booleanValue())
143             {
144                error("Monitor with this name already exists", req, resp);
145                return;
146             }
147          }
148          catch (Exception JavaDoc ex)
149          {
150             error("Failed to determine if monitor with that name already exists: " + ex.toString(), req, resp);
151             return;
152          }
153          StringBuffer JavaDoc xml = new StringBuffer JavaDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
154          xml.append("<server>\n");
155
156          xml.append("<mbean code=\"org.jboss.monitor.ThresholdMonitor\"\n");
157          xml.append(" name=\"jboss.monitor:service=" + monitorName.replace(' ', '_') + "\">\n");
158          xml.append(" <attribute name=\"MonitorName\">" + monitorName + "</attribute>\n");
159          try
160          {
161          if (mbeanServer.isInstanceOf(oname, "org.jboss.system.ServiceMBean"))
162          {
163             xml.append(" <depends optional-attribute-name=\"ObservedObject\">" + objectName + "</depends>\n");
164          }
165          else
166          {
167             xml.append(" <attribute name=\"ObservedObject\">" + objectName + "</attribute>\n");
168          }
169          }
170          catch (Exception JavaDoc ex)
171          {
172             error("failed creating service: " + ex.toString(), req, resp);
173             return;
174          }
175          xml.append(" <attribute name=\"ObservedAttribute\">" + attribute + "</attribute>\n");
176          xml.append(" <depends-list optional-attribute-name=\"AlertListeners\">\n");
177          for (int i = 0; i < alerts.length; i++)
178          {
179             xml.append(" <depends-list-element>");
180             xml.append(alerts[i].trim());
181             xml.append(" </depends-list-element>\n");
182          }
183          xml.append(" </depends-list>\n");
184          xml.append(" <attribute name=\"Threshold\">" + threshold + "</attribute>\n");
185          xml.append(" <attribute name=\"Period\">" + timePeriod + "</attribute>\n");
186          xml.append(" <attribute name=\"CompareTo\">" + compareTo + "</attribute>\n");
187          xml.append(" <attribute name=\"Enabled\">" + enabled + "</attribute>\n");
188          xml.append("</mbean>\n</server>");
189
190          try
191          {
192             Object JavaDoc[] args = {"monitors", monitorName, "-service.xml", xml.toString(), Boolean.TRUE};
193             String JavaDoc[] signature = {"java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "boolean"};
194             mbeanServer.invoke(new ObjectName JavaDoc("jboss.admin:service=DeploymentFileRepository"), "store", args, signature);
195          }
196          catch (Exception JavaDoc ex)
197          {
198             error("Failed to create persisted file: " + ex.toString(), req, resp);
199             return;
200          }
201       }
202       else
203       {
204          try
205          {
206             ThresholdMonitor monitor = new ThresholdMonitor();
207
208             monitor.setMonitorName(monitorName);
209             monitor.setObservedObject(oname);
210             monitor.setObservedAttribute(attribute);
211             monitor.setThreshold(threshold);
212             monitor.setEnabled(new Boolean JavaDoc(enabled).booleanValue());
213             monitor.setPeriod(timePeriod);
214             monitor.setCompareTo(compareTo);
215             ArrayList JavaDoc list = new ArrayList JavaDoc();
216             for (int i = 0; i < alerts.length; i++)
217             {
218                list.add(new ObjectName JavaDoc(alerts[i]));
219             }
220             monitor.setAlertListeners(list);
221             mbeanServer.registerMBean(monitor, new ObjectName JavaDoc("jboss.monitor:name=" + monitorName));
222             monitor.create();
223             monitor.start();
224          }
225          catch (Exception JavaDoc ex)
226          {
227             error("Failed to create non-persisted monitor: " + ex.toString(), req, resp);
228          }
229       }
230       req.getRequestDispatcher("/createThresholdMonitorSummary.jsp").forward(req, resp);
231
232    }
233
234 }
235
Popular Tags