1 22 package org.jboss.console.plugins.monitor; 23 24 import java.io.IOException ; 25 26 import javax.management.MBeanServer ; 27 import javax.management.MalformedObjectNameException ; 28 import javax.management.ObjectName ; 29 import javax.servlet.ServletException ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 33 import org.jboss.logging.Logger; 34 import org.jboss.monitor.SnapshotRecordingMonitor; 35 import org.jboss.mx.util.MBeanServerLocator; 36 37 44 public class CreateSnapshotServlet extends javax.servlet.http.HttpServlet 45 { 46 private static final Logger log = Logger.getLogger(CreateSnapshotServlet.class); 47 48 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 49 throws ServletException , IOException 50 { 51 doit(req, resp); 52 } 53 54 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 55 throws ServletException , IOException 56 { 57 doit(req, resp); 58 } 59 60 protected void error(String msg, HttpServletRequest req, HttpServletResponse resp) 61 throws ServletException , IOException 62 { 63 req.setAttribute("error", "Error: " + msg); 64 req.getRequestDispatcher("/createSnapshot.jsp").forward(req, resp); 66 return; 67 } 68 69 protected void doit(HttpServletRequest req, HttpServletResponse resp) 70 throws ServletException , IOException 71 { 72 73 String monitorName = req.getParameter("monitorName").trim(); 74 log.debug(monitorName); 75 String objectName = req.getParameter("objectName").trim(); 76 log.debug(objectName); 77 MBeanServer mbeanServer = MBeanServerLocator.locateJBoss(); 78 ObjectName oname = null; 79 try 80 { 81 oname = new ObjectName (objectName); 82 } 83 catch (MalformedObjectNameException e) 84 { 85 error("Malformed ObjectName ", req, resp); 86 return; 87 } 88 String attribute = req.getParameter("attribute").trim(); 89 log.debug(attribute); 90 Object val = null; 91 try 92 { 93 val = mbeanServer.getAttribute(oname, attribute); 94 } 95 catch (Exception e) 96 { 97 error("Unable to pull attribute value from MBean, does the attribute exist? ", req, resp); 98 return; 99 } 100 String period = req.getParameter("period").trim(); 101 log.debug(period); 102 long timePeriod = 0; 103 try 104 { 105 timePeriod = Long.parseLong(period); 106 } 107 catch (NumberFormatException e) 108 { 109 error("Illegal format for watch period.", req, resp); 110 return; 111 } 112 try 113 { 114 SnapshotRecordingMonitor monitor = new SnapshotRecordingMonitor(); 115 116 monitor.setMonitorName(monitorName); 117 monitor.setObservedObject(oname); 118 monitor.setObservedAttribute(attribute); 119 monitor.setPeriod(timePeriod); 120 ObjectName sname = new ObjectName ("jboss.snapshot:name=" + monitorName); 121 mbeanServer.registerMBean(monitor, sname); 122 resp.sendRedirect("/web-console/manageSnapshot.jsp?monitorObjectName=" + java.net.URLEncoder.encode(sname.toString())); 123 } 124 catch (Exception ex) 125 { 126 error("Failed to create non-persisted monitor: " + ex.toString(), req, resp); 127 } 128 129 130 } 131 132 } 133 | Popular Tags |