1 package org.objectweb.kilim.tools.jmx; 2 3 import java.lang.reflect.Constructor ; 4 import java.lang.reflect.InvocationTargetException ; 5 import java.lang.reflect.Method ; 6 import java.util.Hashtable ; 7 import java.util.Iterator ; 8 import java.util.LinkedList ; 9 import java.util.List ; 10 11 import javax.management.Attribute ; 12 import javax.management.AttributeList ; 13 import javax.management.AttributeNotFoundException ; 14 import javax.management.DynamicMBean ; 15 import javax.management.InstanceAlreadyExistsException ; 16 import javax.management.InstanceNotFoundException ; 17 import javax.management.InvalidAttributeValueException ; 18 import javax.management.JMRuntimeException ; 19 import javax.management.MBeanAttributeInfo ; 20 import javax.management.MBeanConstructorInfo ; 21 import javax.management.MBeanException ; 22 import javax.management.MBeanInfo ; 23 import javax.management.MBeanNotificationInfo ; 24 import javax.management.MBeanOperationInfo ; 25 import javax.management.MBeanParameterInfo ; 26 import javax.management.MBeanRegistration ; 27 import javax.management.MBeanRegistrationException ; 28 import javax.management.MBeanServer ; 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.NotCompliantMBeanException ; 31 import javax.management.ObjectInstance ; 32 import javax.management.ObjectName ; 33 import javax.management.ReflectionException ; 34 35 36 import org.objectweb.kilim.KilimException; 37 import org.objectweb.kilim.description.KILIM; 38 import org.objectweb.kilim.description.Property; 39 import org.objectweb.kilim.description.Slot; 40 import org.objectweb.kilim.description.TemplateElementImpl; 41 import org.objectweb.kilim.helpers.KilimHelper; 42 import org.objectweb.kilim.model.Component; 43 import org.objectweb.kilim.model.ComponentInterface; 44 import org.objectweb.kilim.model.ComponentProperty; 45 import org.objectweb.kilim.model.ComponentSlot; 46 import org.objectweb.kilim.model.RtCollectionPort; 47 import org.objectweb.kilim.model.RtComponentInterface; 48 import org.objectweb.kilim.model.RtComponentProperty; 49 import org.objectweb.kilim.model.RtComponentSlot; 50 import org.objectweb.kilim.model.RtSingleValuePort; 51 import org.omg.CORBA.INTF_REPOS ; 52 53 61 public class ComponentMBean implements DynamicMBean , MBeanRegistration { 62 63 private static final String JMX_META_DATA_NAME = "JMX"; 64 65 private static MBeanServer mbeanserver; 66 67 private MBeanInfo dMBeanInfo = null; 68 private Component component; 69 private String root_component_name; 70 private String root_domain_name; 71 72 public ComponentMBean(String template_name) { 73 super(); 74 try { 75 component = KilimHelper.newComponent(template_name , this.getClass()); 76 } catch (Exception e) { 77 e.printStackTrace(); 78 throw new JMRuntimeException (e.getMessage()); 79 } 80 buildDynamicMBeanInfo(); 81 } 82 83 public ComponentMBean(Component component) { 84 super(); 85 this.component = component; 86 buildDynamicMBeanInfo(); 87 } 88 89 private ComponentMBean(Component component , String root_component_name , String root_domain_name) { 90 this(component); 91 this.root_component_name = root_component_name; 92 this.root_domain_name = root_domain_name; 93 } 94 95 public static void setDefaultMBeanServer(MBeanServer mbeanserver) { 96 ComponentMBean.mbeanserver = mbeanserver; 97 } 98 99 102 public Object getAttribute(String name) 103 throws AttributeNotFoundException , MBeanException , ReflectionException { 104 105 if (name.equals("InterfacesNames")) { return getPortsNames(); } 106 if (name.equals("SlotsNames")) { return getSlotsNames(); } 107 108 ComponentInterface rslt = null; 109 110 try { 111 rslt = component.getInterface(denormalize(name)); 112 } catch (KilimException e) { 113 throw new MBeanException (e); 114 } 115 116 if (rslt == null) { throw new AttributeNotFoundException (); } 117 118 Object value = null; 119 try { 120 value = rslt.getValue(); 121 } catch (KilimException e) { 122 throw new MBeanException (e); 123 } 124 125 return value; 126 } 127 128 private String cached_ports_names; 129 130 private Object getPortsNames() { 131 132 if (cached_ports_names != null) { return cached_ports_names; } 133 134 StringBuffer prtsnms_bffr = new StringBuffer (); 135 boolean first = true; 136 137 for (Iterator it = component.getInterfaces() ; it.hasNext() ;) { 138 ComponentInterface intf = (ComponentInterface) it.next(); 139 if (intf.isSingleValuePort()) { 140 if (first) { first = false; } else { prtsnms_bffr.append(" / "); } 141 RtSingleValuePort port = (RtSingleValuePort) intf; 142 prtsnms_bffr.append(port.getLocalName()); 143 } 144 } 145 146 cached_ports_names = prtsnms_bffr.toString(); 147 148 return cached_ports_names; 149 150 } 151 152 private String cached_slots_names; 153 154 private Object getSlotsNames() { 155 156 if (cached_slots_names != null) { return cached_slots_names; } 157 158 StringBuffer slots_nms_bffr = new StringBuffer (); 159 boolean first = true; 160 161 for (Iterator it = component.getSlots() ; it.hasNext() ;) { 162 RtComponentSlot slt = (RtComponentSlot) it.next(); 163 if (first) { first = false; } else { slots_nms_bffr.append(" / "); } 164 slots_nms_bffr.append(slt.getLocalName()); 165 } 166 167 cached_slots_names = slots_nms_bffr.toString(); 168 169 return cached_slots_names; 170 171 } 172 173 176 public AttributeList getAttributes(String [] names) { 177 178 AttributeList rslt = new AttributeList (); 179 180 for (int i = 0 ; i < names.length ; i++) { 181 182 try { 183 rslt.add(getAttribute(names[i])); 184 } catch (Exception e) { 185 e.printStackTrace(); 186 continue; 187 } 188 189 } 190 return rslt; 191 } 192 193 196 public MBeanInfo getMBeanInfo() { 197 return dMBeanInfo; 198 } 199 200 203 public Object invoke(String method_name, Object [] params, String [] signature) 204 throws MBeanException , ReflectionException { 205 206 Class [] clsss = new Class [signature.length]; 207 for (int i = 0 ; i < signature.length ; i++) { 208 try { 209 clsss[i] = this.getClass().getClassLoader().loadClass(signature[i]); 210 } catch (ClassNotFoundException e) { 211 throw new ReflectionException (e); 212 } 213 } 214 215 Method method = null; 216 try { 217 method = this.getClass().getMethod(method_name , clsss); 218 } catch (NoSuchMethodException e) { 219 throw new ReflectionException (e); 220 } 221 222 Object result = null; 223 try { 224 result = method.invoke(this , params); 225 } catch (InvocationTargetException e){ 226 throw new ReflectionException (e); 227 } catch (IllegalAccessException e){ 228 throw new ReflectionException (e); 229 } 230 231 return result; 232 233 } 234 235 238 public void setAttribute(Attribute att) 239 throws 240 AttributeNotFoundException , 241 InvalidAttributeValueException , 242 MBeanException , 243 ReflectionException { 244 245 String nm = denormalize(att.getName()); 246 Object vl = att.getValue(); 247 248 ComponentInterface intf = null; 249 try { 250 intf = component.getInterface(nm); 251 } catch (KilimException e) { 252 throw new MBeanException (e); 253 } 254 255 if (intf == null) { throw new AttributeNotFoundException (nm);} 256 257 if (!(intf instanceof RtComponentProperty)) { 258 throw new AttributeNotFoundException (nm); 259 } 260 261 RtComponentProperty prpt = (RtComponentProperty) intf; 262 263 try { 264 prpt.setValue(vl); 265 } catch (KilimException e) { 266 throw new InvalidAttributeValueException (vl.toString() + "[" + vl.getClass().toString() + "]"); 267 } 268 269 271 272 } 273 274 277 public AttributeList setAttributes(AttributeList attlist) { 278 279 AttributeList rslt = new AttributeList (); 280 281 for (Iterator it = attlist.iterator() ; it.hasNext() ; ) { 282 Attribute crrnt_att = (Attribute ) it.next(); 283 284 try { 285 setAttribute(crrnt_att); 286 } catch (Exception e) { 287 e.printStackTrace(); 288 continue; 289 } 290 291 rslt.add(crrnt_att); 292 293 } 294 295 return rslt; 296 297 } 298 299 301 public String getValueOnInterface(String name) { 302 303 ComponentInterface intrfc = null; 304 try { 305 306 intrfc = component.getInterface(name); 307 308 if (intrfc != null) { 309 return intrfc.getValue().toString(); 310 } 311 312 } catch (KilimException e) { 313 e.printStackTrace(); 314 } 315 316 return null; 317 } 318 319 public boolean rebindInterface(String name) { 320 321 RtSingleValuePort intrfc = null; 322 323 try { 324 325 ComponentInterface retrieved = component.getInterface(name); 326 327 if (!(retrieved instanceof RtSingleValuePort)) { 328 return false; 329 } 330 331 intrfc = (RtSingleValuePort) retrieved; 332 333 if (intrfc != null) { 334 intrfc.update(); 335 return true; 336 } 337 338 } catch (KilimException e) { 339 e.printStackTrace(); 340 } 341 342 return false; 343 } 344 345 public Component getComponent() { 346 return component; 347 } 348 349 public boolean fork() { 350 351 Component forked_component = null; 352 try { 353 forked_component = component.fork(); 354 } catch (KilimException e) { 355 e.printStackTrace(); 356 return false; 357 } 358 359 360 ComponentMBean forked_component_mbean = new ComponentMBean(forked_component); 361 362 try { 363 mbeanserver.registerMBean(forked_component_mbean , null); 364 } catch (MBeanRegistrationException e) { 365 e.printStackTrace(); 366 return false; 367 } catch (InstanceAlreadyExistsException e) { 368 e.printStackTrace(); 369 return false; 370 } catch (NotCompliantMBeanException e) { 371 e.printStackTrace(); 372 return false; 373 } 374 375 return true; 376 } 377 378 public boolean plugComponent(String slot_nm , ObjectName obj_nm) { 379 return plugAndUnplugComponent(true , slot_nm , obj_nm); 380 } 381 382 public boolean unplugComponent(String slot_nm , ObjectName obj_nm) { 383 return plugAndUnplugComponent(false , slot_nm , obj_nm); 384 } 385 386 public boolean plugAndUnplugComponent(boolean is_plug , String slot_nm , ObjectName obj_nm) { 387 388 System.out.println("Plugging \"" + obj_nm + "\" in slot \"" + slot_nm + "\"."); 389 390 Component plug; 391 try { 392 plug = (Component) mbeanserver.invoke(obj_nm , "getComponent" , new Object [0] , new String [0]); 393 } catch (InstanceNotFoundException e) { 394 e.printStackTrace(); 395 return false; 396 } catch (MBeanException e) { 397 e.printStackTrace(); 398 return false; 399 } catch (ReflectionException e) { 400 e.printStackTrace(); 401 return false; 402 } 403 404 ComponentSlot slt = null; 405 try { 406 slt = component.getSlot(slot_nm); 407 } catch (KilimException e) { 408 e.printStackTrace(); 409 return false; 410 } 411 412 try { 413 if (is_plug) { 414 slt.plug(plug); 415 } else { 416 slt.unplug(plug); 417 } 418 } catch (KilimException e) { 419 e.printStackTrace(); 420 return false; 421 } 422 423 return true; 424 } 425 426 431 432 441 private void buildDynamicMBeanInfo() { 442 443 444 LinkedList prprts = new LinkedList (); 445 446 Iterator edtbl_prprts = getEditableProperties(); 447 Iterator mntrd_intrfcs = getMonitoredInterfaces(); 448 449 LinkedList attributes_list = new LinkedList (); 450 451 attributes_list.add(new MBeanAttributeInfo ("InterfacesNames" , 452 "java.lang.String", 453 "Ports Names", 454 true, 455 false, 456 false) 457 ); 458 459 attributes_list.add(new MBeanAttributeInfo ("SlotsNames" , 460 "java.lang.String", 461 "Slots Names", 462 true, 463 false, 464 false) 465 ); 466 467 if (mntrd_intrfcs != null) { 468 for (; mntrd_intrfcs.hasNext() ;) { 469 ComponentInterface crrnt = (ComponentInterface) mntrd_intrfcs.next(); 470 Class clss = String .class; 471 try { 472 clss = crrnt.getValue().getClass(); 473 } catch (KilimException exc) {} 474 attributes_list.add(new MBeanAttributeInfo (normalize(crrnt.getLocalName()) , 475 clss.getName(), 476 crrnt.getQualifiedName(), 477 true, 478 false, 479 false) 480 ); 481 } 482 } 483 484 if (edtbl_prprts != null) { 485 for (; edtbl_prprts.hasNext() ;) { 486 ComponentProperty crrnt = (ComponentProperty) edtbl_prprts.next(); 487 Property property = (Property) crrnt.getElementDescription(); 488 Class type = property.getType(); 489 attributes_list.add(new MBeanAttributeInfo (normalize(crrnt.getLocalName()) , 490 type.getName(), 491 crrnt.getQualifiedName(), 492 true, 493 true, 494 false) 495 ); 496 } 497 } 498 499 501 MBeanAttributeInfo [] dAttributes = new MBeanAttributeInfo [attributes_list.size()]; 502 attributes_list.toArray(dAttributes); 503 504 Constructor [] constructors = this.getClass().getConstructors(); 505 506 MBeanConstructorInfo [] dConstructors = new MBeanConstructorInfo [1]; 507 dConstructors[0] = new MBeanConstructorInfo ("ComponentMBean(): Constructs a Component", 508 constructors[0]); 509 510 511 MBeanOperationInfo [] dOperations = new MBeanOperationInfo [6]; 512 513 MBeanParameterInfo [] parameters_0 = new MBeanParameterInfo [1]; 514 parameters_0[0] = new MBeanParameterInfo ("Interface" , "java.lang.String" , "The name of the Interface to get the Value from."); 515 dOperations[0] = new MBeanOperationInfo ("getValueOnInterface" , "Get the value of a given Interface, bind it if necessary." , parameters_0 , "java.lang.String" , MBeanOperationInfo.ACTION); 516 517 dOperations[1] = new MBeanOperationInfo ("rebindInterface" , "Updates the state of a given Interface's Value." , parameters_0 , "java.lang.String" , MBeanOperationInfo.ACTION); 518 519 Method meth = null; 520 try { 521 meth = this.getClass().getMethod("getComponent" , null); 522 } catch (NoSuchMethodException e) { 523 e.printStackTrace(); 524 } 525 dOperations[2] = new MBeanOperationInfo ("Get Component" ,meth); 526 527 meth = null; 528 try { 529 meth = this.getClass().getMethod("fork" , null); 530 } catch (NoSuchMethodException e) { 531 e.printStackTrace(); 532 } 533 dOperations[3] = new MBeanOperationInfo ("Fork Component" ,meth); 534 535 MBeanParameterInfo [] parameters = new MBeanParameterInfo [2]; 536 parameters[0] = new MBeanParameterInfo ("Slot" , "java.lang.String" , "The Slot where the Component will be Plugged."); 537 parameters[1] = new MBeanParameterInfo ("Component" , "javax.management.ObjectName" , "The Name of the Component to Plug."); 538 dOperations[4] = new MBeanOperationInfo ("plugComponent" , "Plug Component into given Slot" , parameters , "java.lang.Boolean" , MBeanOperationInfo.ACTION); 539 540 541 parameters[0] = new MBeanParameterInfo ("Slot" , "java.lang.String" , "The Slot from which the Component will be Unplugged."); 542 parameters[1] = new MBeanParameterInfo ("Component" , "javax.management.ObjectName" , "The Name of the Component to Unplug."); 543 dOperations[5] = new MBeanOperationInfo ("unplugComponent" , "Unplug Component from given Slot" , parameters , "java.lang.Boolean" , MBeanOperationInfo.ACTION); 544 545 546 dMBeanInfo = new MBeanInfo (this.getClass().getName(), 547 "JMX Supervision of Kilim Components", 548 dAttributes, 549 dConstructors, 550 dOperations, 551 new MBeanNotificationInfo [0]); 552 } 553 554 private static final String normalize(String nm) { 555 return nm.replaceAll(" ","__"); 556 } 557 558 private static final String denormalize(String nm) { 559 return nm.replaceAll("__"," "); 560 } 561 562 private Component getJMXDataContainer() { 563 return component.getSubComponent(JMX_META_DATA_NAME); 564 } 565 566 private Iterator getMetaData(String well_known_name) { 567 568 Component jmx_cntnr = getJMXDataContainer(); 569 if (jmx_cntnr == null) { return null; } 570 571 ComponentInterface intrfc = null; 572 try { 573 intrfc = jmx_cntnr.getInterface(well_known_name); 574 } catch (KilimException e) { 575 e.printStackTrace(); 576 } 577 578 if (intrfc == null) { return null; } 579 if (!(intrfc instanceof RtCollectionPort)) { return null; } 580 581 Iterator rslt = ((RtCollectionPort) intrfc).getBoundProviders(); 582 583 return rslt; 584 } 585 586 private Iterator getMonitoredInterfaces() { return getMetaData("visible"); } 587 private Iterator getEditableProperties() { return getMetaData("editable"); } 588 589 private Iterator getMonitoredSubComponents() { 590 591 Iterator cmpnnts = getMetaData("monitored sub-components"); 592 if (cmpnnts == null) return null; 593 594 LinkedList result = new LinkedList (); 595 for (; cmpnnts.hasNext() ;) { 596 Object cmpnnt = null; 597 try { 598 ComponentInterface intrfc = (ComponentInterface) cmpnnts.next(); 599 cmpnnt = intrfc.getValue(); 600 if (cmpnnt.equals("All")) { 601 Iterator sb_cmpnnts = component.getSubComponents(); 602 return sb_cmpnnts; 603 } 604 } catch (KilimException e) { e.printStackTrace(); } 605 result.add(cmpnnt); 606 } 607 608 return result.iterator(); 609 } 610 611 613 616 public void postDeregister() { 617 } 618 619 622 public void postRegister(Boolean arg0) { 623 624 Iterator it = getMonitoredSubComponents(); 625 if (it == null) { return; } 626 627 for (; it.hasNext() ;) { 628 try { 629 Component nxt = (Component) it.next(); 630 if (nxt.getLocalName().equals(JMX_META_DATA_NAME)) { continue; } 631 ComponentMBean mbn = new ComponentMBean(nxt , root_component_name , root_domain_name); 632 mbeanserver.registerMBean(mbn , null); 633 } catch (NotCompliantMBeanException e) { 634 e.printStackTrace(); 635 continue; 636 } catch (MBeanRegistrationException e) { 637 e.printStackTrace(); 638 continue; 639 } catch (InstanceAlreadyExistsException e) { 640 e.printStackTrace(); 641 continue; 642 } 643 } 644 } 645 646 649 public void preDeregister() throws Exception { 650 } 651 652 655 public ObjectName preRegister(MBeanServer srvr, ObjectName obj_nm) 656 throws Exception { 657 658 ObjectName trying_with_nm = null; 659 660 if (root_component_name == null) { 661 662 664 666 if (obj_nm == null) { 667 obj_nm = new ObjectName ("KilimComponent:name=FORKED"); 668 } 669 670 trying_with_nm = obj_nm; 671 672 ObjectInstance obj_instnc = null; 673 try { 674 obj_instnc = srvr.getObjectInstance(trying_with_nm); 675 } catch (InstanceNotFoundException e) {} 676 677 while (obj_instnc != null) { 678 Hashtable nms = trying_with_nm.getKeyPropertyList(); 679 String new_nm = (String ) nms.get("name"); 680 new_nm = new_nm + "_c"; 681 nms.put("name" , new_nm); 682 trying_with_nm = new ObjectName (trying_with_nm.getDomain() , nms); 683 684 try { 685 obj_instnc = srvr.getObjectInstance(trying_with_nm); 686 } catch (InstanceNotFoundException e) { 687 obj_instnc = null; 688 } 689 } 690 691 root_component_name = trying_with_nm.getKeyProperty("name"); 692 root_domain_name = trying_with_nm.getDomain(); 693 694 695 return trying_with_nm; 696 } 697 698 700 String nm = root_domain_name + ":name=" + root_component_name + "/" + component.getQualifiedName(); 701 nm = nm.replaceAll(" " , "_"); 702 trying_with_nm = new ObjectName (nm); 703 704 return trying_with_nm; 705 706 } 707 708 } 709 | Popular Tags |