001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.console.plugins.monitor;
023:
024: import java.io.IOException;
025: import java.util.ArrayList;
026:
027: import javax.management.Attribute;
028: import javax.management.MBeanServer;
029: import javax.management.ObjectName;
030: import javax.servlet.ServletException;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.jboss.logging.Logger;
035: import org.jboss.mx.util.MBeanServerLocator;
036:
037: /**
038: * Created by IntelliJ IDEA.
039: * User: wburke
040: * Date: Nov 25, 2003
041: * Time: 5:53:01 PM
042: * To change this template use Options | File Templates.
043: */
044: public class ManageThresholdMonitorServlet extends
045: javax.servlet.http.HttpServlet {
046: static final long serialVersionUID = -3119457291398933381L;
047:
048: private static final Logger log = Logger
049: .getLogger(ManageThresholdMonitorServlet.class);
050:
051: protected void doGet(HttpServletRequest req,
052: HttpServletResponse resp) throws ServletException,
053: IOException {
054: doit(req, resp);
055: }
056:
057: protected void doPost(HttpServletRequest req,
058: HttpServletResponse resp) throws ServletException,
059: IOException {
060: doit(req, resp);
061: }
062:
063: protected void error(String msg, HttpServletRequest req,
064: HttpServletResponse resp) throws ServletException,
065: IOException {
066: req.setAttribute("error", "Error: " + msg);
067: //this.getServletContext().getRequestDispatcher("/createThresholdMonitor.jsp").forward(req, resp);
068: req.getRequestDispatcher("/manageThresholdMonitor.jsp")
069: .forward(req, resp);
070: return;
071: }
072:
073: protected void doit(HttpServletRequest req, HttpServletResponse resp)
074: throws ServletException, IOException {
075: String action = req.getParameter("action");
076: if (action == null) {
077: error("unknown action: ", req, resp);
078: return;
079: }
080: String monitorName = req.getParameter("monitorName").trim();
081: MBeanServer mbeanServer = MBeanServerLocator.locateJBoss();
082: ObjectName monitorObjectName, oname = null;
083: String attribute = null;
084: try {
085: monitorObjectName = new ObjectName(req
086: .getParameter("monitorObjectName"));
087: } catch (Exception ex) {
088: error("Malformed Monitor ObjectName: "
089: + req.getParameter("monitorObjectName"), req, resp);
090: return;
091: }
092: if (action.trim().equals("Clear Alert")) {
093: try {
094: log.debug("Clearing Alert for monitor: "
095: + monitorObjectName.toString());
096: String[] signature = {};
097: Object[] args = {};
098: mbeanServer.invoke(monitorObjectName, "clearAlert",
099: args, signature);
100: req.setAttribute("error", "Alert cleared");
101: req.getRequestDispatcher("/manageThresholdMonitor.jsp")
102: .forward(req, resp);
103: return;
104: } catch (Exception ex) {
105: error("Failed to Clear Alert: " + ex.toString(), req,
106: resp);
107: return;
108: }
109: } else if (action.trim().equals("Remove Monitor")) {
110: try {
111: log.debug("removing monitor: "
112: + monitorObjectName.toString());
113: Object[] args = { "monitors", monitorName,
114: "-service.xml" };
115: String[] signature = { "java.lang.String",
116: "java.lang.String", "java.lang.String" };
117: mbeanServer
118: .invoke(
119: new ObjectName(
120: "jboss.admin:service=DeploymentFileRepository"),
121: "remove", args, signature);
122: req.getRequestDispatcher("/ServerInfo.jsp").forward(
123: req, resp);
124: } catch (Exception ex) {
125: error("Failed to Remove Monitor: " + ex.toString(),
126: req, resp);
127: }
128: return;
129: }
130: try {
131: monitorObjectName = new ObjectName(req
132: .getParameter("monitorObjectName"));
133: oname = (ObjectName) mbeanServer.getAttribute(
134: monitorObjectName, "ObservedObject");
135: attribute = (String) mbeanServer.getAttribute(
136: monitorObjectName, "ObservedAttribute");
137: } catch (Exception ex) {
138: error("Malformed Monitor ObjectName: "
139: + req.getParameter("monitorObjectName"), req, resp);
140: return;
141: }
142: String threshold = req.getParameter("threshold").trim();
143: log.debug(threshold);
144: boolean enabled = req.getParameter("enabled") != null;
145: log.debug("Enabled: " + enabled);
146: boolean persisted = req.getParameter("persisted") != null;
147: log.debug("Persisted: " + persisted);
148: String period = req.getParameter("period").trim();
149: log.debug(period);
150: long timePeriod = 0;
151: try {
152: timePeriod = Long.parseLong(period);
153: } catch (NumberFormatException e) {
154: error("Illegal format for watch period.", req, resp);
155: return;
156: }
157: String compare = req.getParameter("compare").trim();
158: log.debug(compare);
159: String[] alerts = req.getParameterValues("alerts");
160: if (alerts == null) {
161: error("you must select at least one alert listener", req,
162: resp);
163: return;
164: }
165: int compareTo = 0;
166: if ("gt".equals(compare))
167: compareTo = -1;
168: else if ("lt".equals(compare))
169: compareTo = 1;
170: else if ("eq".equals(compare))
171: compareTo = 0;
172:
173: try {
174: mbeanServer.setAttribute(monitorObjectName, new Attribute(
175: "Threshold", threshold));
176: mbeanServer.setAttribute(monitorObjectName, new Attribute(
177: "Enabled", new Boolean(enabled)));
178: mbeanServer.setAttribute(monitorObjectName, new Attribute(
179: "Period", new Long(timePeriod)));
180: mbeanServer.setAttribute(monitorObjectName, new Attribute(
181: "CompareTo", new Integer(compareTo)));
182:
183: ArrayList list = new ArrayList();
184: for (int i = 0; i < alerts.length; i++) {
185: list.add(new ObjectName(alerts[i]));
186: }
187: mbeanServer.setAttribute(monitorObjectName, new Attribute(
188: "AlertListeners", list));
189: } catch (Exception ex) {
190: error("Failed to update mbean monitor: " + ex.toString(),
191: req, resp);
192: return;
193: }
194: if (persisted) {
195:
196: try {
197: Object[] args = { "monitors", monitorName,
198: "-service.xml" };
199: String[] signature = { "java.lang.String",
200: "java.lang.String", "java.lang.String" };
201: Object rtn = mbeanServer
202: .invoke(
203: new ObjectName(
204: "jboss.admin:service=DeploymentFileRepository"),
205: "isStored", args, signature);
206: if (!((Boolean) rtn).booleanValue()) {
207: error(
208: "Monitor with this name doesn't exist in repository",
209: req, resp);
210: return;
211: }
212: } catch (Exception ex) {
213: error(
214: "Failed to determine if monitor with that name already exists: "
215: + ex.toString(), req, resp);
216: return;
217: }
218: StringBuffer xml = new StringBuffer(
219: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
220: xml.append("<server>\n");
221:
222: xml
223: .append("<mbean code=\"org.jboss.monitor.ThresholdMonitor\"\n");
224: xml.append(" name=\"jboss.monitor:service="
225: + monitorName.replace(' ', '_') + "\">\n");
226: xml.append(" <attribute name=\"MonitorName\">"
227: + monitorName + "</attribute>\n");
228: try {
229: if (mbeanServer.isInstanceOf(oname,
230: "org.jboss.system.ServiceMBean")) {
231: xml
232: .append(" <depends optional-attribute-name=\"ObservedObject\">"
233: + oname + "</depends>\n");
234: } else {
235: xml.append(" <attribute name=\"ObservedObject\">"
236: + oname + "</attribute>\n");
237: }
238: } catch (Exception ex) {
239: error("failed creating service: " + ex.toString(), req,
240: resp);
241: return;
242: }
243: xml.append(" <attribute name=\"ObservedAttribute\">"
244: + attribute + "</attribute>\n");
245: xml
246: .append(" <depends-list optional-attribute-name=\"AlertListeners\">\n");
247: for (int i = 0; i < alerts.length; i++) {
248: xml.append(" <depends-list-element>");
249: xml.append(alerts[i].trim());
250: xml.append(" </depends-list-element>\n");
251: }
252: xml.append(" </depends-list>\n");
253: xml.append(" <attribute name=\"Threshold\">" + threshold
254: + "</attribute>\n");
255: xml.append(" <attribute name=\"Period\">" + timePeriod
256: + "</attribute>\n");
257: xml.append(" <attribute name=\"CompareTo\">" + compareTo
258: + "</attribute>\n");
259: xml.append(" <attribute name=\"Enabled\">" + enabled
260: + "</attribute>\n");
261: xml.append("</mbean>\n</server>");
262:
263: try {
264: Object[] args = { "monitors", monitorName,
265: "-service.xml", xml.toString(), Boolean.TRUE };
266: String[] signature = { "java.lang.String",
267: "java.lang.String", "java.lang.String",
268: "java.lang.String", "boolean" };
269: mbeanServer
270: .invoke(
271: new ObjectName(
272: "jboss.admin:service=DeploymentFileRepository"),
273: "store", args, signature);
274: } catch (Exception ex) {
275: error("Failed to create persisted file: "
276: + ex.toString(), req, resp);
277: return;
278: }
279: }
280: req.setAttribute("error", "Update complete!");
281: req.getRequestDispatcher("/manageThresholdMonitor.jsp")
282: .forward(req, resp);
283:
284: }
285:
286: }
|