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 ManageThresholdMonitorServlet extends javax.servlet.http.HttpServlet 45 { 46 private static final Logger log = Logger.getLogger(ManageThresholdMonitorServlet.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("/manageThresholdMonitor.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 log.debug("Persisted: " + persisted); 142 String period = req.getParameter("period").trim(); 143 log.debug(period); 144 long timePeriod = 0; 145 try 146 { 147 timePeriod = Long.parseLong(period); 148 } 149 catch (NumberFormatException e) 150 { 151 error("Illegal format for watch period.", req, resp); 152 return; 153 } 154 String compare = req.getParameter("compare").trim(); 155 log.debug(compare); 156 String [] alerts = req.getParameterValues("alerts"); 157 if (alerts == null) 158 { 159 error("you must select at least one alert listener", req, resp); 160 return; 161 } 162 int compareTo = 0; 163 if ("gt".equals(compare)) 164 compareTo = -1; 165 else if ("lt".equals(compare)) 166 compareTo = 1; 167 else if ("eq".equals(compare)) compareTo = 0; 168 169 170 try 171 { 172 mbeanServer.setAttribute(monitorObjectName, new Attribute ("Threshold", threshold)); 173 mbeanServer.setAttribute(monitorObjectName, new Attribute ("Enabled", new Boolean (enabled))); 174 mbeanServer.setAttribute(monitorObjectName, new Attribute ("Period", new Long (timePeriod))); 175 mbeanServer.setAttribute(monitorObjectName, new Attribute ("CompareTo", new Integer (compareTo))); 176 177 ArrayList list = new ArrayList (); 178 for (int i = 0; i < alerts.length; i++) 179 { 180 list.add(new ObjectName (alerts[i])); 181 } 182 mbeanServer.setAttribute(monitorObjectName, new Attribute ("AlertListeners", list)); 183 } 184 catch (Exception ex) 185 { 186 error("Failed to update mbean monitor: " + ex.toString(), req, resp); 187 return; 188 } 189 if (persisted) 190 { 191 192 try 193 { 194 Object [] args = {"monitors", monitorName, "-service.xml"}; 195 String [] signature = {"java.lang.String", "java.lang.String", "java.lang.String"}; 196 Object rtn = mbeanServer.invoke(new ObjectName ("jboss.admin:service=DeploymentFileRepository"), "isStored", args, signature); 197 if (!((Boolean ) rtn).booleanValue()) 198 { 199 error("Monitor with this name doesn't exist in repository", req, resp); 200 return; 201 } 202 } 203 catch (Exception ex) 204 { 205 error("Failed to determine if monitor with that name already exists: " + ex.toString(), req, resp); 206 return; 207 } 208 StringBuffer xml = new StringBuffer ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 209 xml.append("<server>\n"); 210 211 xml.append("<mbean code=\"org.jboss.monitor.ThresholdMonitor\"\n"); 212 xml.append(" name=\"jboss.monitor:service=" + monitorName.replace(' ', '_') + "\">\n"); 213 xml.append(" <attribute name=\"MonitorName\">" + monitorName + "</attribute>\n"); 214 try 215 { 216 if (mbeanServer.isInstanceOf(oname, "org.jboss.system.ServiceMBean")) 217 { 218 xml.append(" <depends optional-attribute-name=\"ObservedObject\">" + oname + "</depends>\n"); 219 } 220 else 221 { 222 xml.append(" <attribute name=\"ObservedObject\">" + oname + "</attribute>\n"); 223 } 224 } 225 catch (Exception ex) 226 { 227 error("failed creating service: " + ex.toString(), req, resp); 228 return; 229 } 230 xml.append(" <attribute name=\"ObservedAttribute\">" + attribute + "</attribute>\n"); 231 xml.append(" <depends-list optional-attribute-name=\"AlertListeners\">\n"); 232 for (int i = 0; i < alerts.length; i++) 233 { 234 xml.append(" <depends-list-element>"); 235 xml.append(alerts[i].trim()); 236 xml.append(" </depends-list-element>\n"); 237 } 238 xml.append(" </depends-list>\n"); 239 xml.append(" <attribute name=\"Threshold\">" + threshold + "</attribute>\n"); 240 xml.append(" <attribute name=\"Period\">" + timePeriod + "</attribute>\n"); 241 xml.append(" <attribute name=\"CompareTo\">" + compareTo + "</attribute>\n"); 242 xml.append(" <attribute name=\"Enabled\">" + enabled + "</attribute>\n"); 243 xml.append("</mbean>\n</server>"); 244 245 try 246 { 247 Object [] args = {"monitors", monitorName, "-service.xml", xml.toString(), Boolean.TRUE}; 248 String [] signature = {"java.lang.String", "java.lang.String", "java.lang.String", "java.lang.String", "boolean"}; 249 mbeanServer.invoke(new ObjectName ("jboss.admin:service=DeploymentFileRepository"), "store", args, signature); 250 } 251 catch (Exception ex) 252 { 253 error("Failed to create persisted file: " + ex.toString(), req, resp); 254 return; 255 } 256 } 257 req.setAttribute("error", "Update complete!"); 258 req.getRequestDispatcher("/manageThresholdMonitor.jsp").forward(req, resp); 259 260 } 261 262 } 263 | Popular Tags |