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.ThresholdMonitor; 36 import org.jboss.mx.util.MBeanServerLocator; 37 38 45 public class CreateThresholdMonitorServlet extends javax.servlet.http.HttpServlet 46 { 47 private static final Logger log = Logger.getLogger(CreateThresholdMonitorServlet.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("/createThresholdMonitor.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 String compare = req.getParameter("compare").trim(); 120 log.debug(compare); 121 String [] 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 [] args = {"monitors", monitorName, "-service.xml"}; 140 String [] signature = {"java.lang.String", "java.lang.String", "java.lang.String"}; 141 Object rtn = mbeanServer.invoke(new ObjectName ("jboss.admin:service=DeploymentFileRepository"), "isStored", args, signature); 142 if (((Boolean )rtn).booleanValue()) 143 { 144 error("Monitor with this name already exists", req, resp); 145 return; 146 } 147 } 148 catch (Exception ex) 149 { 150 error("Failed to determine if monitor with that name already exists: " + ex.toString(), req, resp); 151 return; 152 } 153 StringBuffer xml = new StringBuffer ("<?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 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 [] args = {"monitors", monitorName, "-service.xml", xml.toString(), Boolean.TRUE}; 193 String [] signature = {"java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "boolean"}; 194 mbeanServer.invoke(new ObjectName ("jboss.admin:service=DeploymentFileRepository"), "store", args, signature); 195 } 196 catch (Exception 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 (enabled).booleanValue()); 213 monitor.setPeriod(timePeriod); 214 monitor.setCompareTo(compareTo); 215 ArrayList list = new ArrayList (); 216 for (int i = 0; i < alerts.length; i++) 217 { 218 list.add(new ObjectName (alerts[i])); 219 } 220 monitor.setAlertListeners(list); 221 mbeanServer.registerMBean(monitor, new ObjectName ("jboss.monitor:name=" + monitorName)); 222 monitor.create(); 223 monitor.start(); 224 } 225 catch (Exception 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 |