1 22 package org.jboss.console.plugins.monitor; 23 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 27 import javax.management.MBeanServer ; 28 import javax.management.MalformedObjectNameException ; 29 import javax.management.ObjectName ; 30 import javax.servlet.ServletException ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import org.jboss.logging.Logger; 35 import org.jboss.monitor.StringThresholdMonitor; 36 import org.jboss.mx.util.MBeanServerLocator; 37 38 45 public class CreateStringThresholdMonitorServlet extends javax.servlet.http.HttpServlet 46 { 47 private static final Logger log = Logger.getLogger(CreateStringThresholdMonitorServlet.class); 48 49 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 50 throws ServletException , IOException 51 { 52 doit(req, resp); 53 } 54 55 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 56 throws ServletException , IOException 57 { 58 doit(req, resp); 59 } 60 61 protected void error(String msg, HttpServletRequest req, HttpServletResponse resp) 62 throws ServletException , IOException 63 { 64 req.setAttribute("error", "Error: " + msg); 65 req.getRequestDispatcher("/createStringThresholdMonitor.jsp").forward(req, resp); 67 return; 68 } 69 70 protected void doit(HttpServletRequest req, HttpServletResponse resp) 71 throws ServletException , IOException 72 { 73 74 String monitorName = req.getParameter("monitorName").trim(); 75 log.debug(monitorName); 76 String objectName = req.getParameter("objectName").trim(); 77 log.debug(objectName); 78 MBeanServer mbeanServer = MBeanServerLocator.locateJBoss(); 79 ObjectName oname = null; 80 try 81 { 82 oname = new ObjectName (objectName); 83 } 84 catch (MalformedObjectNameException e) 85 { 86 error("Malformed ObjectName ", req, resp); 87 return; 88 } 89 String attribute = req.getParameter("attribute").trim(); 90 log.debug(attribute); 91 Object val = null; 92 try 93 { 94 val = mbeanServer.getAttribute(oname, attribute); 95 } 96 catch (Exception e) 97 { 98 error("Unable to pull attribute value from MBean, does the attribute exist? ", req, resp); 99 return; 100 } 101 String 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 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 e) 115 { 116 error("Illegal format for watch period.", req, resp); 117 return; 118 } 119 boolean equality = req.getParameter("equality") != null; 120 String [] 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 [] args = {"monitors", monitorName, "-service.xml"}; 133 String [] signature = {"java.lang.String", "java.lang.String", "java.lang.String"}; 134 Object rtn = mbeanServer.invoke(new ObjectName ("jboss.admin:service=DeploymentFileRepository"), "isStored", args, signature); 135 if (((Boolean )rtn).booleanValue()) 136 { 137 error("Monitor with this name already exists", req, resp); 138 return; 139 } 140 } 141 catch (Exception ex) 142 { 143 error("Failed to determine if monitor with that name already exists: " + ex.toString(), req, resp); 144 return; 145 } 146 StringBuffer xml = new StringBuffer ("<?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 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 [] args = {"monitors", monitorName, "-service.xml", xml.toString(), Boolean.TRUE}; 186 String [] signature = {"java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "boolean"}; 187 mbeanServer.invoke(new ObjectName ("jboss.admin:service=DeploymentFileRepository"), "store", args, signature); 188 } 189 catch (Exception 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 (enabled).booleanValue()); 206 monitor.setPeriod(timePeriod); 207 monitor.setEqualityTriggersAlert(equality); 208 ArrayList list = new ArrayList (); 209 for (int i = 0; i < alerts.length; i++) 210 { 211 list.add(new ObjectName (alerts[i])); 212 } 213 monitor.setAlertListeners(list); 214 mbeanServer.registerMBean(monitor, new ObjectName ("jboss.monitor:name=" + monitorName)); 215 monitor.create(); 216 monitor.start(); 217 } 218 catch (Exception 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 |