1 23 24 176 177 package com.sun.enterprise.admin.meta; 178 179 import java.io.InputStream ; 180 import java.util.Hashtable ; 181 import java.util.TreeMap ; 182 import java.util.Iterator ; 183 import java.util.Set ; 184 import java.util.ArrayList ; 185 186 import java.util.logging.Logger ; 188 import java.util.logging.Level ; 189 import com.sun.logging.LogDomains; 190 191 import javax.management.MBeanServer ; 192 import javax.management.DynamicMBean ; 193 import javax.management.ObjectName ; 194 import javax.management.Descriptor ; 195 import javax.management.MBeanServerFactory ; 196 import javax.management.MalformedObjectNameException ; 197 198 import com.sun.org.apache.commons.modeler.ManagedBean; 199 import com.sun.org.apache.commons.modeler.Registry; 200 import com.sun.org.apache.commons.modeler.AttributeInfo; 201 202 import com.sun.enterprise.admin.meta.naming.MBeanNamingDescriptor; 203 import com.sun.enterprise.admin.meta.naming.MBeanNamingInfo; 204 import com.sun.enterprise.admin.meta.naming.MBeanNamingException; 205 import com.sun.enterprise.admin.meta.naming.MBeansNaming; 206 207 import com.sun.enterprise.admin.BaseAdminMBean; 208 209 import com.sun.enterprise.config.ConfigContext; 210 import com.sun.enterprise.config.ConfigException; 211 import com.sun.enterprise.config.ConfigBean; 212 import com.sun.enterprise.config.ConfigBeansFactory; 213 import com.sun.enterprise.config.serverbeans.ServerTags; 214 import com.sun.enterprise.config.serverbeans.ServerXPathHelper; 215 import com.sun.enterprise.config.ConfigContextEvent; 216 217 220 public class MBeanRegistry { 222 private final static String MSG_FINDREGISTRYENTRY_FAILED = "mbean.config.findregistryentry_failed"; 223 224 static protected Logger _sLogger = LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 226 227 private MBeanRegistryEntry[] entries = null; 228 229 public String toString() 230 { 231 String str = "MBeanRegistry("+entries.length+" entries):\n"; 232 for(int i=0; i<entries.length; i++) 233 { 234 str = str + entries[i].toString(); 235 } 236 return str; 237 } 238 239 public String toFormatString() 240 { 241 String str = "MBeanRegistry("+entries.length+" entries):\n"; 242 for(int i=0; i<entries.length; i++) 243 { 244 str = str + entries[i].toFormatString(); 245 } 246 return str; 247 } 248 249 private class MyRegistry extends Registry 251 { 252 public MyRegistry() 253 { 254 super(); 255 } 256 257 } 258 259 260 private class MyManagedBean extends ManagedBean 262 { 263 public MyManagedBean(ManagedBean mb) 264 { 265 super(); 266 AttributeInfo attrs[] = mb.getAttributes(); 267 if(attrs.length>0 && "modelerType".equals(attrs[0].getName())) 268 { 269 attributes = new AttributeInfo[attrs.length-1]; 270 for(int i=1; i<attrs.length; i++) 271 attributes[i-1]=attrs[i]; 272 } 273 else 274 attributes = attrs; 275 className = mb.getClassName(); 276 constructors = mb.getConstructors(); 277 description = mb.getDescription(); 278 domain = mb.getDomain(); 279 group = mb.getGroup(); 280 name = mb.getName(); 281 fields = mb.getFields(); 282 notifications = mb.getNotifications(); 283 operations = mb.getOperations(); 284 type = mb.getType(); 285 } 286 287 } 288 289 290 public void loadMBeanRegistry(InputStream stream) throws MBeanMetaException,MBeanNamingException,ClassNotFoundException 292 { 293 MyRegistry registry = new MyRegistry(); 294 try { 295 registry.loadDescriptors("MbeansDescriptorsDOMSource", stream, null); 296 } catch (Exception e) 297 { 298 throw new MBeanMetaException(e.getMessage()); 299 } 300 String [] names = registry.findManagedBeans(); 302 entries = new MBeanRegistryEntry[names.length]; for(int i=0; i<names.length; i++) 304 { 305 entries[i] = new MBeanRegistryEntry(new MyManagedBean(registry.findManagedBean(names[i]))); 306 } 307 309 } 310 311 public MBeanRegistryEntry findMBeanRegistryEntry(ObjectName objectName) 313 { 314 try 315 { 316 Hashtable ht = objectName.getKeyPropertyList(); 317 ht.put(":",objectName.getDomain()); 319 for(int i=0; i<entries.length; i++) 320 { 321 if(entries[i]!=null && entries[i].isObjectNamePatternMatch(ht)) 322 return entries[i]; 323 } 324 } 325 catch (Exception e) 326 { 327 _sLogger.log(Level.FINE, MSG_FINDREGISTRYENTRY_FAILED, e); 328 } 329 return null; 330 } 331 public MBeanRegistryEntry findMBeanRegistryEntryByType(String type) 333 { 334 for(int i=0; i<entries.length; i++) 335 { 336 if(entries[i]!=null && entries[i].getName().equals(type)) 337 return entries[i]; 338 } 339 return null; 340 } 341 342 public MBeanRegistryEntry findMBeanRegistryEntryByXPath(String xpath) 344 { 345 if(xpath!=null) 346 while(xpath.length()>1 && xpath.endsWith("/")) 347 xpath = xpath.substring(0, xpath.length()-1); 348 Object [] tokens = MBeanNamingDescriptor.getXPathTokens(xpath); 349 350 for(int i=0; i<entries.length; i++) 351 { 352 if(entries[i].getNamingDescriptor().isXpathTokensMatch(tokens)) 353 return entries[i]; 354 } 355 return null; 356 } 357 358 public MBeanRegistryEntry findMBeanRegistryEntryByXPathPattern(String patternPrefix) 360 { 361 patternPrefix = MBeanNamingDescriptor.XPATH_TO_MASK(patternPrefix); 363 int len = patternPrefix.length(); 364 String pattern; 365 for(int i=0; i<entries.length; i++) 367 { 368 pattern = entries[i].namingDescriptor.getXPathPattern(); 369 if( pattern!= null && 370 pattern.startsWith(patternPrefix) && 371 (pattern.length()==len || pattern.indexOf('/', len)<0 ) ) 372 return entries[i]; 373 } 374 return null; 375 } 376 377 public ObjectName getMbeanObjectName(String type, String [] location) 379 { 380 return getMbeanObjectName(findMBeanRegistryEntryByType(type), type, location); 381 } 382 383 private ObjectName getMbeanObjectName(MBeanRegistryEntry entry, String type, String [] location) 385 { 386 387 if(entry!=null) 388 { 389 try { 390 MBeanNamingInfo namingInfo = new MBeanNamingInfo(entry.getNamingDescriptor(), type, location); 391 if(namingInfo!=null) 392 return namingInfo.getObjectName(); 393 } catch (Exception e) 394 { 395 e.printStackTrace(); 396 } 397 } 398 return null; 399 } 400 401 public BaseAdminMBean instantiateConfigMBean( ObjectName objectName, Object managedResource) throws Exception 403 { 404 return instantiateConfigMBean( objectName, managedResource, null); 405 } 406 407 public BaseAdminMBean instantiateConfigMBean( ObjectName objectName, Object managedResource, ConfigContext ctx) throws Exception 409 { 410 MBeanRegistryEntry entry = findMBeanRegistryEntry(objectName); 411 if(entry!=null) 412 { 413 MBeanNamingDescriptor descr = entry.getNamingDescriptor(); 414 return entry.instantiateMBean(objectName, managedResource, ctx); 415 } 416 return null; 417 } 418 419 public BaseAdminMBean instantiateMBean( String type, String [] location) throws Exception 421 { 422 return instantiateMBean( type, location, null, null); 423 } 424 public BaseAdminMBean instantiateMBean( String type, String [] location, Object managedResource, ConfigContext ctx) throws Exception 426 { 427 return instantiateMBean( type, location, managedResource, ctx, false); 428 } 429 430 public MBeanNamingInfo getMBeanNamingInfo(ObjectName objectName) throws Exception 432 { 433 MBeanRegistryEntry entry = this.findMBeanRegistryEntry(objectName); 434 return new MBeanNamingInfo(entry.getNamingDescriptor(), objectName); 435 } 436 public void notifyRegisterMBean(ObjectName objectName, ConfigContext ctx) 441 { 442 try { 443 MBeanNamingInfo namingInfo = getMBeanNamingInfo(objectName); 444 ArrayList arr = getValidDottedNames(namingInfo); 445 if(arr!=null) 446 for(int i=0; i<arr.size(); i++) addDottedName((String )arr.get(i), namingInfo.getObjectName()); 448 } catch (Exception e) 450 {} 451 } 452 453 public void notifyUnregisterMBean(ObjectName objectName, ConfigContext ctx) 455 { 456 try { 457 MBeanNamingInfo namingInfo = getMBeanNamingInfo(objectName); 458 ConfigBean bean = null; 459 try 460 { 461 bean = (ConfigBean)ConfigBeansFactory.getConfigBeanByXPath(ctx, namingInfo.getXPath()); 462 } 463 catch (Exception e) 464 {} 465 if(bean==null) 466 removeDottedName(objectName); 467 } catch (Exception e) 468 {} 469 } 470 471 public void generateAndRegisterAllDottedNames(ConfigContext ctx, String domainName) throws MBeanMetaException 473 { 474 ConfigBean bean = null; 475 try 476 { 477 bean = (ConfigBean)ConfigBeansFactory.getConfigBeanByXPath(ctx, "/domain"); 478 registerDottedName(bean, domainName); 479 } 480 catch (Exception e) 481 { 482 throw new MBeanMetaException("DottedNamesRegistration: Could not get '/domain' ConfigMBean"); 483 } 484 } 485 486 506 public void instantiateAndRegisterAllConfigMBeans( 507 ConfigContext ctx, 508 String domainName) 509 throws MBeanMetaException 510 { 511 ConfigBean bean = null; 512 try 513 { 514 bean = (ConfigBean)ConfigBeansFactory.getConfigBeanByXPath(ctx, 515 ServerXPathHelper.XPATH_DOMAIN); 516 } 517 catch (ConfigException e) 518 { 519 _sLogger.log(Level.FINEST, 520 "Exception during Instantiating All Config MBeans in MBeanRegistry", e); 521 throw new MBeanMetaException 522 ("ConfigException in getting Domain bean from config context", e); 523 } 524 instantiateAndRegisterConfigMBeans(bean, domainName); 525 _sLogger.log(Level.FINEST, 526 "Instantiated All Config MBeans in MBeanRegistry Successfully"); 527 } 528 529 530 private String getFirstValidDottedName(MBeanNamingInfo namingInfo) 532 { 533 ArrayList arr = getValidDottedNames(namingInfo); 534 if(arr!=null && arr.size()>0) 535 return (String )arr.get(0); 536 return null; 537 } 538 539 private ArrayList getValidDottedNames(MBeanNamingInfo namingInfo) 541 { 542 ArrayList arr = new ArrayList (); 543 try { 544 String [] dottedNames = namingInfo.getDottedNames(); 545 for(int i=0; i<dottedNames.length; i++) 546 { 547 if( dottedNames[i].length()>0 && 548 !dottedNames[i].startsWith("@")) 549 arr.add(dottedNames[i]); 550 } 551 } catch (Exception e) 552 {} 553 return arr; 554 } 555 private void registerDottedName(ConfigBean bean, String domainName) 559 { 560 if(bean==null) 561 return; 562 String xpath = bean.getAbsoluteXPath(""); 563 try 564 { 565 MBeanNamingInfo namingInfo = getNamingInfoForConfigBean(bean, domainName); 566 ObjectName objectName = namingInfo.getObjectName(); 567 ArrayList arr = getValidDottedNames(namingInfo); 568 if(arr!=null) 569 for(int i=0; i<arr.size(); i++) { 571 addDottedName((String )arr.get(i), objectName); 573 } 574 } 575 catch(Exception e) 576 { 577 if(xpath!=null && xpath.indexOf("/"+ServerTags.ELEMENT_PROPERTY+"[")==0) 578 { 579 _sLogger.log(Level.FINE,"---- Exception for xpath="+xpath, e); 580 } 581 } 582 try 583 { 584 ConfigBean[] beans = bean.getAllChildBeans(); 585 for(int i=0; i<beans.length; i++) 586 { 587 registerDottedName(beans[i], domainName); 588 } 589 } 590 catch(Exception e) 591 { 592 } 593 } 594 595 597 606 private void instantiateAndRegisterConfigMBeans(ConfigBean bean, String domainName) 607 { 608 if(bean == null) 609 return; 610 String xpath = bean.getAbsoluteXPath(""); 611 ConfigContext ctx = bean.getConfigContext(); 612 try 613 { 614 ObjectName objectName = getObjectNameForConfigBean(bean, domainName); 615 if(objectName!=null) 616 { 617 MBeanServer mServer = (MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 618 if (!mServer.isRegistered(objectName)) 619 { 620 BaseAdminMBean mbean = instantiateConfigMBean(objectName, null, ctx) ; 622 registerMBean(mbean, objectName); 623 } 624 } 625 } 626 catch(Exception e) 627 { 628 if(xpath!=null && xpath.indexOf("/"+ServerTags.ELEMENT_PROPERTY+"[")==0) 629 { 630 _sLogger.log(Level.FINEST,"---- Exception for xpath="+xpath, e); 631 } 632 } 633 try 634 { 635 ConfigBean[] beans = bean.getAllChildBeans(); 636 for(int i=0; i<beans.length; i++) 637 { 638 if(beans[i]!=null && beans[i].getConfigContext()==null) 639 { beans[i].setConfigContext(ctx); 641 } 642 643 try { 644 instantiateAndRegisterConfigMBeans(beans[i], domainName); 645 } catch(Exception e1) { 646 _sLogger.log(Level.FINEST, 648 "Exception in instantiateAndRegisterConfigMBeans:", e1); 649 } 650 } 651 } 652 catch(Exception e) 653 { 654 _sLogger.log(Level.FINEST, 655 "Exception in instantiateAndRegisterConfigMBeans:", e); 656 } 657 } 658 659 670 private void registerMBean(BaseAdminMBean mbean, ObjectName name) 671 throws Exception { 672 MBeanServer mServer = (MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 673 if (!mServer.isRegistered(name)) { 674 mServer.registerMBean(mbean, name); 675 } 676 } 677 private static final ObjectName REGISTRATION_MBEAN_OBJECTNAME = getDottedRegMBeanName(); 679 private static final String REGISTRATION_OPERATION_NAME = "add"; 680 private static final String UNREGISTRATION_OPERATION_NAME = "remove"; 681 private static ObjectName getDottedRegMBeanName() 683 { 684 try { 685 return new ObjectName ("com.sun.appserv:name=dotted-name-registry,type=dotted-name-support"); } 687 catch (Exception e) 688 { 689 e.printStackTrace(); 690 } 691 return null; 692 } 693 private void addDottedName(String dottedName, ObjectName objectName) throws Exception 695 { 696 _sLogger.log(Level.FINE, "******regDottedName******* "+dottedName +" ObjectName="+objectName); 697 MBeanServer mbeanServer = (MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 698 mbeanServer.invoke(REGISTRATION_MBEAN_OBJECTNAME, REGISTRATION_OPERATION_NAME, 699 new Object []{dottedName, objectName}, 700 new String []{dottedName.getClass().getName(), objectName.getClass().getName()}); 701 } 702 private void removeDottedName(ObjectName objectName) throws Exception 704 { 705 _sLogger.log(Level.FINE, "******unregDottedName******* for ObjectName="+objectName); 706 MBeanServer mbeanServer = (MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 707 mbeanServer.invoke(REGISTRATION_MBEAN_OBJECTNAME, UNREGISTRATION_OPERATION_NAME, 708 new Object []{objectName}, 709 new String []{objectName.getClass().getName()}); 710 } 711 public BaseAdminMBean instantiateMBean( String type, String [] location, Object managedResource, ConfigContext ctx, boolean bRegister) throws Exception 713 { 714 MBeanRegistryEntry entry = findMBeanRegistryEntryByType(type); 715 BaseAdminMBean mbean = null; 716 if(entry!=null) 717 { 718 mbean = entry.instantiateMBean(type, location, managedResource, location[0], ctx); 719 if(bRegister && mbean!=null) 720 { 721 ObjectName objectName = getMbeanObjectName(entry, type, location); 722 if(objectName!=null) 723 { 724 registerMBean(mbean, objectName); 725 } 726 } 727 } 728 return mbean; 729 } 730 756 757 public static final int SORT_BY_NAME = 0; 758 public static final int SORT_BY_XPATH = 1; 759 public void sortRegistryEntries(int sortType ) 760 { 761 TreeMap map = new TreeMap (); 763 String emptyKey = " "; 764 String key; 765 for(int i=0; i<entries.length; i++) 766 { 767 if(sortType==SORT_BY_XPATH) 768 key = entries[i].getNamingDescriptor().getXPathPattern(); 769 else 770 key = entries[i].getName(); 771 if(key==null || key.length()==0) 772 { 773 key = emptyKey; 774 emptyKey = emptyKey + " "; 775 } 776 map.put(key, entries[i]); 777 } 778 entries = (MBeanRegistryEntry[])map.values().toArray((Object [])(new MBeanRegistryEntry[0])); 779 } 780 781 791 public void adoptConfigBeanAdd(ConfigBean bean, String domainName) 793 { 794 _sLogger.log(Level.FINEST, "****** MBeanRegistry.adoptConfigBeanAddDelete->add element:"+bean); 795 instantiateAndRegisterConfigMBeans(bean, domainName); 796 registerDottedName(bean, domainName); return; 798 } 799 800 809 public void adoptConfigBeanDelete(ConfigBean bean, String domainName) 810 { 811 _sLogger.log(Level.FINEST, "****** MBeanRegistry.adoptConfigBeanAddDelete->delete element"+bean); 814 ArrayList arr = new ArrayList (); 815 collectChildrenObjectNames(bean, domainName, arr); 816 for(int i=0; i<arr.size(); i++) 817 { 818 ObjectName objectName = (ObjectName )arr.get(i); 819 if(objectName!=null) 820 { 821 try 823 { 824 removeDottedName(objectName); 825 } 826 catch (Throwable t) 827 { 828 _sLogger.fine("!!!!!!!!!!!!!! Can not unregister dotted name for MBean: "+objectName); 830 } 831 832 try 834 { 835 MBeanServer server = (MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 836 if(server.isRegistered(objectName)) 837 server.unregisterMBean(objectName); 838 } 842 catch (Throwable t) 843 { 844 _sLogger.fine("!!!!!!!!!!!!!! Can not unregister MBean: "+objectName); 846 } 847 } 848 } 849 } 850 851 860 private void collectChildrenObjectNames(ConfigBean bean, String domainName, ArrayList arr) 861 { 862 try 863 { 864 ConfigBean[] beans = bean.getAllChildBeans(); 865 for(int i=0; i<beans.length; i++) 866 { 867 collectChildrenObjectNames(beans[i], domainName, arr); 868 } 869 } 870 catch(Exception e) 871 { 872 } 874 try 875 { 876 ObjectName name = getObjectNameForConfigBean(bean, domainName); 877 if(name!=null) 878 { 879 arr.add(name); 880 _sLogger.log(Level.FINEST, "******collectChildrenObjectNames.add-> "); 881 } 882 } 883 catch(MalformedObjectNameException e) 884 { 885 String xpath = bean.getAbsoluteXPath(""); 886 _sLogger.log(Level.FINE, "Object name malformed for bean: "+ xpath, e); 887 } 888 } 889 890 895 private MBeanNamingInfo getNamingInfoForConfigBean(ConfigBean bean, String domainName) 896 { 897 if(bean == null) 898 return null; 899 try 900 { 901 String xpath = bean.getAbsoluteXPath(""); 902 MBeanRegistryEntry entry = findMBeanRegistryEntryByXPath(xpath); 903 MBeanNamingDescriptor descr = entry.getNamingDescriptor(); 904 String [] parms = descr.extractParmListFromXPath(xpath); 905 if(parms!=null && parms.length>0 && parms[0]==null) 906 parms[0] = domainName; 907 return new MBeanNamingInfo(descr, descr.getType(), parms); 908 } 909 catch (Exception e) 910 { 911 return null; 913 } 914 } 915 920 public ObjectName getObjectNameForConfigBean(ConfigBean bean, String domainName) throws MalformedObjectNameException 921 { 922 MBeanNamingInfo namingInfo = getNamingInfoForConfigBean(bean, domainName); 923 if(namingInfo!=null) 924 return namingInfo.getObjectName(); 925 return null; 926 } 927 931 public String getConfigElementPrintName(String xpath, boolean bIncludingKey, boolean bReplaceRefByParentElem) 932 { 933 xpath = MBeanMetaHelper.cutAttributeTokenFromXPath(xpath.trim()); 934 MBeanRegistryEntry entry = findMBeanRegistryEntryByXPath(xpath); 935 String printName; 936 String elemName = MBeanMetaHelper.extractLastElemNameFromXPath(xpath); 937 if(bReplaceRefByParentElem && elemName.endsWith("-ref")) 938 { 939 xpath = MBeanMetaHelper.cutLastElementFromXPath(xpath); 940 elemName = MBeanMetaHelper.extractLastElemNameFromXPath(xpath); 941 } 942 if(entry==null || 943 (printName = entry.getElementPrintName())==null) 944 { 945 printName = elemName.replace('-', ' '); 947 } 948 String key = MBeanMetaHelper.getMultipleElementKeyValue(xpath); 949 if(key!=null && bIncludingKey) 950 printName = printName + " '" + key + "'"; 951 return printName; 952 } 953 } 954 | Popular Tags |