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:
026: import javax.management.MBeanServer;
027: import javax.management.MalformedObjectNameException;
028: import javax.management.ObjectName;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import org.jboss.logging.Logger;
034: import org.jboss.monitor.SnapshotRecordingMonitor;
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 CreateSnapshotServlet extends
045: javax.servlet.http.HttpServlet {
046: static final long serialVersionUID = -6005190747212975396L;
047: private static final Logger log = Logger
048: .getLogger(CreateSnapshotServlet.class);
049:
050: protected void doGet(HttpServletRequest req,
051: HttpServletResponse resp) throws ServletException,
052: IOException {
053: doit(req, resp);
054: }
055:
056: protected void doPost(HttpServletRequest req,
057: HttpServletResponse resp) throws ServletException,
058: IOException {
059: doit(req, resp);
060: }
061:
062: protected void error(String msg, HttpServletRequest req,
063: HttpServletResponse resp) throws ServletException,
064: IOException {
065: req.setAttribute("error", "Error: " + msg);
066: //this.getServletContext().getRequestDispatcher("/createThresholdMonitor.jsp").forward(req, resp);
067: req.getRequestDispatcher("/createSnapshot.jsp").forward(req,
068: resp);
069: return;
070: }
071:
072: protected void doit(HttpServletRequest req, HttpServletResponse resp)
073: throws ServletException, IOException {
074:
075: String monitorName = req.getParameter("monitorName").trim();
076: log.debug(monitorName);
077: String objectName = req.getParameter("objectName").trim();
078: log.debug(objectName);
079: MBeanServer mbeanServer = MBeanServerLocator.locateJBoss();
080: ObjectName oname = null;
081: try {
082: oname = new ObjectName(objectName);
083: } catch (MalformedObjectNameException e) {
084: error("Malformed ObjectName ", req, resp);
085: return;
086: }
087: String attribute = req.getParameter("attribute").trim();
088: log.debug(attribute);
089: Object val = null;
090: try {
091: val = mbeanServer.getAttribute(oname, attribute);
092: } catch (Exception e) {
093: error(
094: "Unable to pull attribute value from MBean, does the attribute exist? ",
095: req, resp);
096: return;
097: }
098: String period = req.getParameter("period").trim();
099: log.debug(period);
100: long timePeriod = 0;
101: try {
102: timePeriod = Long.parseLong(period);
103: } catch (NumberFormatException e) {
104: error("Illegal format for watch period.", req, resp);
105: return;
106: }
107: try {
108: SnapshotRecordingMonitor monitor = new SnapshotRecordingMonitor();
109:
110: monitor.setMonitorName(monitorName);
111: monitor.setObservedObject(oname);
112: monitor.setObservedAttribute(attribute);
113: monitor.setPeriod(timePeriod);
114: ObjectName sname = new ObjectName("jboss.snapshot:name="
115: + monitorName);
116: mbeanServer.registerMBean(monitor, sname);
117: resp
118: .sendRedirect("/web-console/manageSnapshot.jsp?monitorObjectName="
119: + java.net.URLEncoder.encode(sname
120: .toString()));
121: } catch (Exception ex) {
122: error("Failed to create non-persisted monitor: "
123: + ex.toString(), req, resp);
124: }
125:
126: }
127:
128: }
|