1 22 package org.jboss.console.plugins.monitor; 23 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 27 import javax.management.Attribute ; 28 import javax.management.MBeanServer ; 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.mx.util.MBeanServerLocator; 36 37 44 public class ManageStringThresholdMonitorServlet extends javax.servlet.http.HttpServlet 45 { 46 private static final Logger log = Logger.getLogger(ManageStringThresholdMonitorServlet.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("/manageThresholdMonitor.jsp").forward(req, resp); 66 return; 67 } 68 69 protected void doit(HttpServletRequest req, HttpServletResponse resp) 70 throws ServletException , IOException 71 { 72 String action = req.getParameter("action"); 73 if (action == null) 74 { 75 error("unknown action: ", req, resp); 76 return; 77 } 78 String monitorName = req.getParameter("monitorName").trim(); 79 MBeanServer mbeanServer = MBeanServerLocator.locateJBoss(); 80 ObjectName monitorObjectName, oname = null; 81 String attribute = null; 82 try 83 { 84 monitorObjectName = new ObjectName (req.getParameter("monitorObjectName")); 85 } 86 catch (Exception ex) 87 { 88 error("Malformed Monitor ObjectName: " + req.getParameter("monitorObjectName"), req, resp); 89 return; 90 } 91 if (action.trim().equals("Clear Alert")) 92 { 93 try 94 { 95 log.debug("Clearing Alert for monitor: " + monitorObjectName.toString()); 96 String [] signature = {}; 97 Object [] args = {}; 98 mbeanServer.invoke(monitorObjectName, "clearAlert", args, signature); 99 req.setAttribute("error", "Alert cleared"); 100 req.getRequestDispatcher("/manageStringThresholdMonitor.jsp").forward(req, resp); 101 return; 102 } 103 catch (Exception ex) 104 { 105 error("Failed to Clear Alert: " + ex.toString(), req, resp); 106 return; 107 } 108 } 109 else if (action.trim().equals("Remove Monitor")) 110 { 111 try 112 { 113 log.debug("removing monitor: " + monitorObjectName.toString()); 114 Object [] args = {"monitors", monitorName, "-service.xml"}; 115 String [] signature = {"java.lang.String", "java.lang.String", "java.lang.String"}; 116 mbeanServer.invoke(new ObjectName ("jboss.admin:service=DeploymentFileRepository"), "remove", args, signature); 117 req.getRequestDispatcher("/ServerInfo.jsp").forward(req, resp); 118 } 119 catch (Exception ex) 120 { 121 error("Failed to Remove Monitor: " + ex.toString(), req, resp); 122 } 123 return; 124 } 125 try 126 { 127 monitorObjectName = new ObjectName (req.getParameter("monitorObjectName")); 128 oname = (ObjectName ) mbeanServer.getAttribute(monitorObjectName, "ObservedObject"); 129 attribute = (String ) mbeanServer.getAttribute(monitorObjectName, "ObservedAttribute"); 130 } 131 catch (Exception ex) 132 { 133 error("Malformed Monitor ObjectName: " + req.getParameter("monitorObjectName"), req, resp); 134 return; 135 } 136 String threshold = req.getParameter("threshold").trim(); 137 log.debug(threshold); 138 boolean enabled = req.getParameter("enabled") != null; 139 log.debug("Enabled: " + enabled); 140 boolean persisted = req.getParameter("persisted") != null; 141 boolean equality = req.getParameter("equality") != null; 142 log.debug("Persisted: " + persisted); 143 String period = req.getParameter("period").trim(); 144 log.debug(period); 145 long timePeriod = 0; 146 try 147 { 148 timePeriod = Long.parseLong(period); 149 } 150 catch (NumberFormatException e) 151 { 152 error("Illegal format for watch period.", req, resp); 153 return; 154 } 155 String [] alerts = req.getParameterValues("alerts"); 156 if (alerts == null) 157 { 158 error("you must select at least one alert listener", req, resp); 159 return; 160 } 161 try 162 { 163 mbeanServer.setAttribute(monitorObjectName, new Attribute ("Threshold", threshold)); 164 mbeanServer.setAttribute(monitorObjectName, new Attribute ("Enabled", new Boolean (enabled))); 165 mbeanServer.setAttribute(monitorObjectName, new Attribute ("Period", new Long (timePeriod))); 166 mbeanServer.setAttribute(monitorObjectName, new Attribute ("EqualityTriggersAlert", new Boolean (equality))); 167 168 ArrayList list = new ArrayList (); 169 for (int i = 0; i < alerts.length; i++) 170 { 171 list.add(new ObjectName (alerts[i])); 172 } 173 mbeanServer.setAttribute(monitorObjectName, new Attribute ("AlertListeners", list)); 174 } 175 catch (Exception ex) 176 { 177 error("Failed to update mbean monitor: " + ex.toString(), req, resp); 178 return; 179 } 180 if (persisted) 181 { 182 183 try 184 { 185 Object [] args = {"monitors", monitorName, "-service.xml"}; 186 String [] signature = {"java.lang.String", "java.lang.String", "java.lang.String"}; 187 Object rtn = mbeanServer.invoke(new ObjectName ("jboss.admin:service=DeploymentFileRepository"), "isStored", args, signature); 188 if (!((Boolean ) rtn).booleanValue()) 189 { 190 error("Monitor with this name doesn't exist in repository", req, resp); 191 return; 192 } 193 } 194 catch (Exception ex) 195 { 196 error("Failed to determine if monitor with that name already exists: " + ex.toString(), req, resp); 197 return; 198 } 199 StringBuffer xml = new StringBuffer ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 200 xml.append("<server>\n"); 201 202 xml.append("<mbean code=\"org.jboss.monitor.StringThresholdMonitor\"\n"); 203 xml.append(" name=\"jboss.monitor:service=" + monitorName.replace(' ', '_') + "\">\n"); 204 xml.append(" <attribute name=\"MonitorName\">" + monitorName + "</attribute>\n"); 205 try 206 { 207 if (mbeanServer.isInstanceOf(oname, "org.jboss.system.ServiceMBean")) 208 { 209 xml.append(" <depends optional-attribute-name=\"ObservedObject\">" + oname + "</depends>\n"); 210 } 211 else 212 { 213 xml.append(" <attribute name=\"ObservedObject\">" + oname + "</attribute>\n"); 214 } 215 } 216 catch (Exception ex) 217 { 218 error("failed creating service: " + ex.toString(), req, resp); 219 return; 220 } 221 xml.append(" <attribute name=\"ObservedAttribute\">" + attribute + "</attribute>\n"); 222 xml.append(" <depends-list optional-attribute-name=\"AlertListeners\">\n"); 223 for (int i = 0; i < alerts.length; i++) 224 { 225 xml.append(" <depends-list-element>"); 226 xml.append(alerts[i].trim()); 227 xml.append(" </depends-list-element>\n"); 228 } 229 xml.append(" </depends-list>\n"); 230 xml.append(" <attribute name=\"Threshold\">" + threshold + "</attribute>\n"); 231 xml.append(" <attribute name=\"Period\">" + timePeriod + "</attribute>\n"); 232 xml.append(" <attribute name=\"EqualityTriggersAlert\">" + equality + "</attribute>\n"); 233 xml.append(" <attribute name=\"Enabled\">" + enabled + "</attribute>\n"); 234 xml.append("</mbean>\n</server>"); 235 236 try 237 { 238 Object [] args = {"monitors", monitorName, "-service.xml", xml.toString(), Boolean.TRUE}; 239 String [] signature = {"java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "boolean"}; 240 mbeanServer.invoke(new ObjectName ("jboss.admin:service=DeploymentFileRepository"), "store", args, signature); 241 } 242 catch (Exception ex) 243 { 244 error("Failed to create persisted file: " + ex.toString(), req, resp); 245 return; 246 } 247 } 248 req.setAttribute("error", "Update complete!"); 249 req.getRequestDispatcher("/manageThresholdMonitor.jsp").forward(req, resp); 250 251 } 252 253 } 254 | Popular Tags |