1 12 13 package org.ejtools.jmx.browser.mbean; 14 15 16 17 import java.io.File ; 18 19 import java.io.InputStream ; 20 21 import java.util.Calendar ; 22 23 import java.util.Iterator ; 24 25 import java.util.TreeMap ; 26 27 28 29 import javax.management.MBeanNotificationInfo ; 30 31 import javax.management.Notification ; 32 33 import javax.xml.parsers.DocumentBuilder ; 34 35 import javax.xml.parsers.DocumentBuilderFactory ; 36 37 import org.w3c.dom.Element ; 38 39 import org.w3c.dom.NodeList ; 40 41 42 43 import org.jboss.deployment.DeploymentException; 44 45 import org.jboss.deployment.DeploymentInfo; 46 47 import org.jboss.deployment.SubDeployerSupport; 48 49 import org.xml.sax.InputSource ; 50 51 52 53 72 73 public class CustomViewDeployer extends SubDeployerSupport implements CustomViewDeployerMBean 74 75 { 76 77 78 79 protected TreeMap views = new TreeMap (); 80 81 82 83 private MBeanNotificationInfo [] info = null; 84 85 86 87 private long sequence = 0; 88 89 90 91 public final static String EXTENSION = ".jmxml"; 92 93 94 95 public final static String OBJECT_NAME = "user:service=CustomViewDeployer"; 96 97 98 99 100 101 110 111 public CustomViewDeployer() { } 112 113 114 115 116 117 130 131 public boolean accepts(DeploymentInfo di) 132 133 { 134 135 String urlStr = di.url.toString(); 136 137 return urlStr.endsWith(EXTENSION); 138 139 } 140 141 142 143 144 145 158 159 public void create(DeploymentInfo di) 160 161 throws DeploymentException 162 163 { 164 165 try 166 167 { 168 169 NodeList nl = null; 170 171 log.debug("Deploying Custom View, create step: url " + di.url); 172 173 174 175 String url = di.url.toString(); 176 177 Element top = (Element ) (di.document.getElementsByTagName("customview")).item(0); 178 179 180 181 View custview = new View(); 182 183 custview.setName(url); 184 185 custview.setDisplayName(top.getAttribute("displayName")); 186 187 log.debug("Create view " + custview.getName()); 188 189 190 191 Element attributes = (Element ) (di.document.getElementsByTagName("attributes")).item(0); 192 193 Element operations = (Element ) (di.document.getElementsByTagName("operations")).item(0); 194 195 196 197 nl = attributes.getElementsByTagName("query"); 198 199 log.debug("About to create " + nl.getLength() + " attribute queries"); 200 201 202 203 for (int i = 0; i < nl.getLength(); i++) 204 205 { 206 207 Element node = (Element ) nl.item(i); 208 209 210 211 String exp = node.getAttribute("exp"); 212 213 String name = node.getAttribute("name"); 214 215 216 217 custview.addAttributeLine(exp, name); 218 219 log.debug("Query expression " + exp + " which has name " + name); 220 221 } 222 223 224 225 nl = operations.getElementsByTagName("query"); 226 227 log.debug("About to create " + nl.getLength() + " operation queries"); 228 229 230 231 for (int i = 0; i < nl.getLength(); i++) 232 233 { 234 235 Element node = (Element ) nl.item(i); 236 237 238 239 String exp = node.getAttribute("exp"); 240 241 String name = node.getAttribute("name"); 242 243 244 245 custview.addOperationLine(exp, name); 246 247 log.debug("Query expression " + exp + " which has operation " + name); 248 249 } 250 251 252 253 views.put(url, custview); 254 255 log.debug("View added"); 256 257 258 259 Notification notification = new Notification ("VIEW_ADDED", this, sequence++, Calendar.getInstance().getTime().getTime(), "Custom view " + url); 260 261 this.sendNotification(notification); 262 263 } 264 265 catch (Exception e) 266 267 { 268 269 destroy(di); 270 271 throw new DeploymentException("create operation failed for package " + di.url, e); 272 273 } 274 275 } 276 277 278 279 280 281 292 293 public void destroy(DeploymentInfo di) 294 295 { 296 297 log.debug("Deploying Custom View, destroy step: url " + di.url); 298 299 300 301 try 302 303 { 304 305 String url = di.url.toString(); 306 307 log.debug("Destroy view " + url); 308 309 310 311 views.remove(url); 312 313 log.debug("View removed"); 314 315 316 317 Notification notification = new Notification ("VIEW_REMOVED", this, sequence++, Calendar.getInstance().getTime().getTime(), "Custom view " + url); 318 319 this.sendNotification(notification); 320 321 } 322 323 catch (Exception e) 324 325 { 326 327 } 328 329 } 330 331 332 333 334 335 344 345 public MBeanNotificationInfo [] getNotificationInfo() 346 347 { 348 349 if (info == null) 350 351 { 352 353 info = new MBeanNotificationInfo []{ 354 355 new MBeanNotificationInfo (new String []{"VIEW_ADDED"}, "javax.management.Notification", "Notification that a view has been added"), 356 357 new MBeanNotificationInfo (new String []{"VIEW_REMOVED"}, "javax.management.Notification", "Notification that a view has been removed") 358 359 }; 360 361 } 362 363 return info; 364 365 } 366 367 368 369 370 371 384 385 public View getView(int index) 386 387 { 388 389 Iterator iterator = views.values().iterator(); 390 391 View view = null; 392 393 int i = 0; 394 395 396 397 while (iterator.hasNext()) 398 399 { 400 401 view = (View) iterator.next(); 402 403 if (index == i) 404 405 { 406 407 break; 408 409 } 410 411 i++; 412 413 } 414 415 416 417 return view; 418 419 } 420 421 422 423 424 425 436 437 public View[] getViews() 438 439 { 440 441 return (View[]) views.values().toArray(new View[0]); 442 443 } 444 445 446 447 448 449 462 463 public void init(DeploymentInfo di) 464 465 throws DeploymentException 466 467 { 468 469 try 470 471 { 472 473 475 if (di.url.getProtocol().equals("file")) 476 477 { 478 479 File file = new File (di.url.getFile()); 480 481 482 483 485 if (!file.isDirectory()) 486 487 { 488 489 di.watch = di.url; 490 491 } 492 493 } 494 495 else 496 497 { 498 499 501 di.watch = di.url; 502 503 } 504 505 506 507 509 parseDocument(di); 510 511 } 512 513 catch (Exception e) 514 515 { 516 517 throw new DeploymentException(e); 518 519 } 520 521 522 523 525 processNestedDeployments(di); 526 527 } 528 529 530 531 532 533 544 545 protected void parseDocument(DeploymentInfo di) 546 547 throws Exception 548 549 { 550 551 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 552 553 InputStream stream = null; 554 555 556 557 559 if (di.isXML) 560 561 { 562 563 stream = di.localUrl.openStream(); 564 565 } 566 567 569 else 570 571 { 572 573 throw new DeploymentException("Can only handle *" + EXTENSION); 574 575 } 576 577 578 579 581 if (stream == null) 582 583 { 584 585 throw new DeploymentException("Failed to find valid *" + EXTENSION); 586 587 } 588 589 590 591 InputSource is = new InputSource (stream); 592 593 di.document = parser.parse(is); 594 595 } 596 597 } 598 599 | Popular Tags |