1 24 25 package org.objectweb.cjdbc.common.jmx.notifications; 26 27 import java.io.IOException ; 28 import java.io.StringReader ; 29 import java.io.StringWriter ; 30 import java.util.ArrayList ; 31 import java.util.Enumeration ; 32 import java.util.Hashtable ; 33 import java.util.Iterator ; 34 35 import org.dom4j.Document; 36 import org.dom4j.DocumentHelper; 37 import org.dom4j.Element; 38 import org.dom4j.io.OutputFormat; 39 import org.dom4j.io.SAXReader; 40 import org.dom4j.io.XMLWriter; 41 42 79 public class JmxNotification 80 { 81 84 private static final String ELT_jmxevent = "jmxevent"; 85 private static final String ELT_info = "info"; 86 private static final String ELT_source = "source"; 87 private static final String ELT_data = "data"; 88 private static final String ELT_priority = "priority"; 89 private static final String ELT_sequence = "sequence"; 90 private static final String ELT_type = "type"; 91 private static final String ELT_description = "description"; 92 private static final String ELT_time = "time"; 93 private static final String ELT_controller = "controller"; 94 private static final String ELT_mbean = "mbean"; 95 private static final String ELT_class = "class"; 96 private static final String ELT_server = "server"; 97 private static final String ELT_value = "value"; 98 99 private static final String ATT_ip = "ip"; 100 private static final String ATT_port = "port"; 101 private static final String ATT_name = "name"; 102 103 String priority; 104 String sequence; 105 String type; 106 String description; 107 String controllerName; 108 String mbeanClass; 109 String mbeanName; 110 String mbeanServerIP; 111 String mbeanServerPort; 112 String time; 113 Hashtable dataList; 114 115 130 public JmxNotification(String priority, String sequence, String type, 131 String description, String time, String controllerName, 132 String mbeanClass, String mbeanName, String mbeanServerIP, 133 String mbeanServerPort, Hashtable dataList) 134 { 135 this.priority = priority; 136 this.sequence = sequence; 137 this.type = type; 138 this.description = description; 139 this.controllerName = controllerName; 140 this.mbeanClass = mbeanClass; 141 this.mbeanName = mbeanName; 142 this.mbeanServerIP = mbeanServerIP; 143 this.mbeanServerPort = mbeanServerPort; 144 this.time = time; 145 this.dataList = dataList; 146 } 147 148 157 public static JmxNotification createNotificationFromXmlString(String xml) 158 throws Exception 159 { 160 StringReader sreader = new StringReader (xml); 161 SAXReader reader = new SAXReader(); 162 Document document = reader.read(sreader); 163 return createNotificationFromXml(document); 164 } 165 166 169 public Hashtable getDataList() 170 { 171 return dataList; 172 } 173 174 180 public String getDataValue(String key) 181 { 182 if (!dataList.containsKey(key)) 183 return null; 184 else 185 return (String ) ((ArrayList ) dataList.get(key)).get(0); 186 } 187 188 191 public String getControllerName() 192 { 193 return controllerName; 194 } 195 196 201 public String getControllerJmxName() 202 { 203 return getMbeanServerIP() + ":" + getMbeanServerPort(); 204 } 205 206 209 public void setControllerName(String controllerName) 210 { 211 this.controllerName = controllerName; 212 } 213 214 217 public String getDescription() 218 { 219 return description; 220 } 221 222 225 public void setDescription(String description) 226 { 227 this.description = description; 228 } 229 230 233 public String getMbeanClass() 234 { 235 return mbeanClass; 236 } 237 238 241 public void setMbeanClass(String mbeanClass) 242 { 243 this.mbeanClass = mbeanClass; 244 } 245 246 249 public String getMbeanName() 250 { 251 return mbeanName; 252 } 253 254 257 public void setMbeanName(String mbeanName) 258 { 259 this.mbeanName = mbeanName; 260 } 261 262 265 public String getMbeanServerIP() 266 { 267 return mbeanServerIP; 268 } 269 270 273 public void setMbeanServerIP(String mbeanServerIP) 274 { 275 this.mbeanServerIP = mbeanServerIP; 276 } 277 278 281 public String getMbeanServerPort() 282 { 283 return mbeanServerPort; 284 } 285 286 289 public void setMbeanServerPort(String mbeanServerPort) 290 { 291 this.mbeanServerPort = mbeanServerPort; 292 } 293 294 297 public String getPriority() 298 { 299 return priority; 300 } 301 302 305 public void setPriority(String priority) 306 { 307 this.priority = priority; 308 } 309 310 313 public String getSequence() 314 { 315 return sequence; 316 } 317 318 321 public void setSequence(String sequence) 322 { 323 this.sequence = sequence; 324 } 325 326 329 public String getType() 330 { 331 return type; 332 } 333 334 337 public void setType(String type) 338 { 339 this.type = type; 340 } 341 342 348 public static JmxNotification createNotificationFromXml(Document document) 349 { 350 String priority = document.selectSingleNode( 351 "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_priority).getText(); 352 String sequence = document.selectSingleNode( 353 "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_sequence).getText(); 354 String type = document.selectSingleNode( 355 "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_type).getText(); 356 String description = document.selectSingleNode( 357 "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_description).getText(); 358 String time = document.selectSingleNode( 359 "//" + ELT_jmxevent + "/" + ELT_info + "/" + ELT_time).getText(); 360 361 String controllerName = document.selectSingleNode( 362 "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_controller) 363 .getText(); 364 String mbeanclass = document.selectSingleNode( 365 "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_mbean + "/" 366 + ELT_class).getText(); 367 String mbeanname = document.selectSingleNode( 368 "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_mbean).valueOf( 369 "@" + ATT_name); 370 String serverip = document.selectSingleNode( 371 "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_mbean + "/" 372 + ELT_server).valueOf("@" + ATT_ip); 373 String serverport = document.selectSingleNode( 374 "//" + ELT_jmxevent + "/" + ELT_source + "/" + ELT_mbean + "/" 375 + ELT_server).valueOf("@" + ATT_port); 376 377 Element root = document.getRootElement(); 378 379 Hashtable dataList = new Hashtable (); 380 for (Iterator i = root.elementIterator(ELT_data); i.hasNext();) 381 { 382 Element data = (Element) i.next(); 383 ArrayList list = new ArrayList (); 384 for (Iterator j = data.elementIterator(ELT_value); j.hasNext();) 385 { 386 Element value = (Element) j.next(); 387 list.add(value.getTextTrim()); 388 } 389 dataList.put(data.valueOf("@" + ATT_name), list); 390 } 391 JmxNotification notif = new JmxNotification(priority, sequence, type, 392 description, time, controllerName, mbeanclass, mbeanname, serverip, 393 serverport, dataList); 394 return notif; 395 } 396 397 402 public Document toXmlDocument() 403 { 404 Document document = DocumentHelper.createDocument(); 405 Element root = document.addElement(ELT_jmxevent); 406 407 Element info = root.addElement(ELT_info); 409 info.addElement(ELT_priority).addText(priority); 410 info.addElement(ELT_sequence).addText(sequence); 411 info.addElement(ELT_type).addText(type); 412 info.addElement(ELT_description).addText(description); 413 info.addElement(ELT_time).addText(time); 414 415 Element source = root.addElement(ELT_source); 417 source.addElement(ELT_controller).addText(controllerName); 418 419 Element mbean = source.addElement(ELT_mbean).addAttribute(ATT_name, 421 mbeanName); 422 mbean.addElement(ELT_class).addText(mbeanClass); 423 mbean.addElement(ELT_server).addAttribute(ATT_ip, mbeanServerIP) 424 .addAttribute(ATT_port, mbeanServerPort); 425 426 Enumeration keys = dataList.keys(); 428 while (keys.hasMoreElements()) 429 { 430 String key = (String ) keys.nextElement(); 431 Element data = root.addElement(ELT_data).addAttribute(ATT_name, key); 432 433 Object entry = dataList.get(key); 434 if (entry instanceof ArrayList ) 435 { 436 ArrayList list = (ArrayList ) entry; 437 for (int i = 0; i < list.size(); i++) 438 data.addElement(ELT_value).addText((String ) list.get(i)); 439 } 440 else if (entry instanceof String []) 441 { 442 String [] list = (String []) entry; 443 for (int i = 0; i < list.length; i++) 444 data.addElement(ELT_value).addText(list[i]); 445 } 446 else if (entry instanceof String ) 447 { 448 data.addElement(ELT_value).addText((String ) entry); 449 } 450 } 451 return document; 452 } 453 454 457 public String toString() 458 { 459 StringWriter swriter = new StringWriter (); 460 OutputFormat format = OutputFormat.createCompactFormat(); 461 XMLWriter writer = new XMLWriter(swriter, format); 462 try 463 { 464 writer.write(toXmlDocument()); 465 } 466 catch (IOException e) 467 { 468 } 470 return swriter.getBuffer().toString(); 471 } 472 473 476 public String getTime() 477 { 478 return time; 479 } 480 481 484 public void setTime(String time) 485 { 486 this.time = time; 487 } 488 489 492 public void setDataList(Hashtable dataList) 493 { 494 this.dataList = dataList; 495 } 496 } | Popular Tags |