1 23 24 290 291 package com.sun.enterprise.admin.config; 292 293 import java.text.CharacterIterator ; 294 import java.text.StringCharacterIterator ; 295 import java.lang.reflect.Method ; 296 import java.lang.reflect.InvocationTargetException ; 297 import java.util.Iterator ; 298 import java.util.ArrayList ; 299 import java.util.Properties ; 300 import java.util.Enumeration ; 301 302 import java.util.logging.Level ; 303 import java.util.logging.Logger ; 304 import com.sun.logging.LogDomains; 305 306 import com.sun.enterprise.util.i18n.StringManager; 307 308 import javax.management.*; 310 import javax.management.modelmbean.ModelMBeanOperationInfo ; 311 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 312 import javax.management.Descriptor ; 313 import javax.management.modelmbean.ModelMBeanInfo ; 314 315 import com.sun.enterprise.config.ConfigBean; 317 import com.sun.enterprise.config.ConfigBeansFactory; 318 import com.sun.enterprise.config.ConfigContext; 319 import com.sun.enterprise.config.serverbeans.ServerXPathHelper; 320 import com.sun.enterprise.config.serverbeans.ElementProperty; 321 import com.sun.enterprise.config.serverbeans.SystemProperty; 322 323 import com.sun.enterprise.admin.meta.naming.MBeanNamingDescriptor; 326 import com.sun.enterprise.admin.meta.naming.MBeanNamingInfo; 327 import com.sun.enterprise.admin.meta.MBeanMetaConstants; 328 import com.sun.enterprise.admin.meta.MBeanRegistry; 329 import com.sun.enterprise.admin.meta.MBeanMetaHelper; 330 import com.sun.enterprise.admin.meta.MBeanRegistryEntry; 331 import com.sun.enterprise.admin.meta.MBeanMetaException; 332 333 import com.sun.enterprise.admin.MBeanHelper; 334 import com.sun.enterprise.admin.BaseAdminMBean; 335 import com.sun.enterprise.admin.AdminValidationException; 336 337 338 340 public class ManagedConfigBean { 341 public final String PROPERTY_NAME_PREFIX = "property."; public final String SYSTEM_PROPERTY_NAME_PREFIX = "system-property."; 344 public static final Logger _sLogger = LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 345 private final static String MSG_BASE_GET_ATTRIBUTE = "mbean.config.base_get_attribute"; 346 private final static String MSG_BASE_SET_ATTRIBUTE = "mbean.config.base_set_attribute"; 347 private final static String MSG_BASE_GOT_ATTRIBUTE = "mbean.config.base_got_attribute"; 348 private final static String MSG_BASE_GET_PROPERTY = "mbean.config.base_get_property"; 349 private final static String MSG_BASE_GET_PROPERTIES = "mbean.config.base_get_properties"; 350 private final static String MSG_BASE_SET_PROPERTY = "mbean.config.base_set_property"; 351 private final static String MSG_BASE_GET_DEF_ATTR_VALUE = "mbean.config.get_def_attr_value"; 352 private final static String MSG_GET_CONFBEANBYXPATH = "mbean.config.get_confbeanbyxpath"; 353 private final static String MSG_BASE_GET_CUSTOM_PROPERTIESLIST = "mbean.config.base_get_custom_property"; 354 355 private ConfigBean m_baseConfigBean; 356 private MBeanInfo m_mbeanInfo; 357 private MBeanRegistry m_registry; 358 private ConfigContext m_configContext; 360 private static StringManager localStrings = StringManager.getManager( BaseAdminMBean.class ); 362 363 364 public ManagedConfigBean(DynamicMBean mbean, ConfigBean cb, MBeanRegistry registry) 365 { 366 this(mbean.getMBeanInfo(), cb, registry); 367 } 368 369 public ManagedConfigBean(MBeanInfo mbeanInfo, ConfigBean cb, MBeanRegistry registry) 370 { 371 m_baseConfigBean = cb; 372 m_mbeanInfo = mbeanInfo; 373 m_configContext = cb.getConfigContext(); 374 m_registry = registry; 375 } 376 377 public ManagedConfigBean(ConfigBean cb, 378 MBeanRegistry registry, String domainName) throws Exception 379 { 380 this(cb, null, registry, domainName); 381 } 382 383 public ManagedConfigBean(ConfigBean cb, String xpath, 384 MBeanRegistry registry, String domainName) throws Exception 385 { 386 if(xpath==null) 387 xpath = cb.getAbsoluteXPath(""); 388 MBeanRegistryEntry entry = registry.findMBeanRegistryEntryByXPath(xpath); 389 MBeanNamingDescriptor descr = entry.getNamingDescriptor(); 390 String [] parms = descr.extractParmListFromXPath(xpath); 391 MBeanNamingInfo namingInfo = new MBeanNamingInfo(descr, descr.getType(), parms); 392 393 m_mbeanInfo = (MBeanInfo )entry.createMBeanInfo(namingInfo, domainName); 394 m_baseConfigBean = cb; 395 m_configContext = cb.getConfigContext(); 396 m_registry = registry; 397 } 398 402 public ConfigContext getConfigContext() { 403 return m_configContext; 404 } 405 407 public MBeanNamingInfo getConfigMBeanNamingInfo() { 408 return null; } 410 411 412 416 public ConfigBean getBaseConfigBean() { 417 return m_baseConfigBean; 418 } 419 420 421 426 public ConfigBean getConfigBean(String externalName) 427 { 428 return m_baseConfigBean; 430 441 } 442 443 444 451 public Object getAttribute(ModelMBeanAttributeInfo attrInfo, String externalName) throws MBeanException,AttributeNotFoundException { 452 _sLogger.log(Level.FINEST, MSG_BASE_GET_ATTRIBUTE, externalName); 453 454 MBeanAttributeInfo ai = getAttrInfo(externalName); 455 boolean isProperty = externalName.startsWith(PROPERTY_NAME_PREFIX); 456 boolean isSystemProperty = externalName.startsWith(SYSTEM_PROPERTY_NAME_PREFIX); 457 boolean isArray = false; 458 459 if(externalName.equals("modelerType")) 460 return null; 461 if(ai==null && !isProperty && !isSystemProperty) { 462 String msg = localStrings.getString( "admin.server.core.mbean.config.attribute_not_defined", externalName ); 463 throw new AttributeNotFoundException( msg ); 464 } 465 if(ai!=null && !ai.isReadable()) { 466 String msg = localStrings.getString( "admin.server.core.mbean.config.attribute_not_readable", externalName ); 467 throw new AttributeNotFoundException( msg ); 468 } 469 470 471 472 if(isProperty) 474 { 475 return getPropertyValue(externalName.substring(PROPERTY_NAME_PREFIX.length())); 477 } 478 else if(isSystemProperty) 479 { 480 return getSystemPropertyValue(externalName.substring(SYSTEM_PROPERTY_NAME_PREFIX.length())); 482 } 483 else 484 { 485 AttrDescriptor descr = getDescriptor(externalName); 486 ConfigBean bean = getConfigBean(externalName); 487 Object value = null; 488 if(descr.isElement()) 489 { 490 wrapAndThrowMBeanException(null, "getattribute_not_implemented_for_xml", externalName); 493 } 494 else 495 { 496 if(ai.getType().startsWith("[")) { 498 Class cl = bean.getClass(); 499 try 500 { 501 Method method = cl.getMethod("getValues", new Class []{Class.forName("java.lang.String")}); 502 value = method.invoke(bean, new Object []{ConfigBean.camelize(descr.getAttributeName())}); 503 } 504 catch (Exception e) 505 { 506 String msg = localStrings.getString( "admin.server.core.mbean.config.getattribute_invocation_error", externalName ); 507 throw MBeanHelper.extractAndWrapTargetException(e, msg); 508 } 509 } 510 else 511 { 512 value = bean.getAttributeValue(descr.getAttributeName()); 513 } 514 515 516 517 } 519 _sLogger.log(Level.FINEST, MSG_BASE_GOT_ATTRIBUTE, new Object [] 520 {externalName,value}); 521 return value; 522 } 523 529 } 530 531 533 public MBeanAttributeInfo getAttributeInfo(String attrName) 534 { 535 attrName = MBeanMetaHelper.mapToMBeanAttributeName(attrName); 536 return (ModelMBeanAttributeInfo ) 537 MBeanHelper.findMatchingAttributeInfo(m_mbeanInfo, attrName); 538 } 539 540 547 public ConfigBean getChildElementByName(String methodName, String name) throws MBeanException,AttributeNotFoundException 548 { 549 550 ConfigBean baseBean = getBaseConfigBean(); 551 Class cl = baseBean.getClass(); 552 ConfigBean bean; 553 try 554 { 555 Method method = cl.getDeclaredMethod(methodName, new Class []{Class.forName("java.lang.String")}); 556 return (ConfigBean)method.invoke(baseBean, new Object []{name}); 557 } 558 catch (Exception e) 559 { 560 wrapAndThrowMBeanException(e, "getattribute.undefined_childelement_in_base_element", name ); 561 return null; } 563 } 564 565 567 public AttributeList getProperties() throws MBeanException,AttributeNotFoundException { 568 _sLogger.log(Level.FINEST, MSG_BASE_GET_PROPERTIES); 569 570 ConfigBean baseBean = getBaseConfigBean(); 571 Class cl = baseBean.getClass(); 572 ElementProperty[] props = null; 573 try { 574 Method method = cl.getDeclaredMethod("getElementProperty"); 575 props = (ElementProperty[])method.invoke(baseBean); 576 } 577 catch (Exception e) { 578 wrapAndThrowMBeanException(e, "getattribute.undefined_properties_in_base_element" ); 579 } 580 if(props==null) { 581 return new AttributeList(); 582 } 583 AttributeList list = new AttributeList(); 584 for(int i=0; i<props.length; i++) 585 { 586 list.add(new Attribute(props[i].getName(), props[i].getValue())); 587 } 588 return list; 589 } 590 591 593 public AttributeList getSystemProperties() throws MBeanException,AttributeNotFoundException { 594 _sLogger.log(Level.FINEST, MSG_BASE_GET_PROPERTIES); 595 596 ConfigBean baseBean = getBaseConfigBean(); 597 Class cl = baseBean.getClass(); 598 SystemProperty[] props = null; 599 try { 600 Method method = cl.getDeclaredMethod("getSystemProperty"); 601 props = (SystemProperty[])method.invoke(baseBean); 602 } 603 catch (Exception e) { 604 wrapAndThrowMBeanException(e, "getattribute.undefined_properties_in_base_element" ); 605 } 606 if(props==null) { 607 return new AttributeList(); 608 } 609 AttributeList list = new AttributeList(); 610 for(int i=0; i<props.length; i++) 611 { 612 list.add(new Attribute(props[i].getName(), props[i].getValue())); 613 } 614 return list; 615 } 616 617 public Object getDefaulCustomProperties(String propertyName) throws MBeanException,AttributeNotFoundException { 618 _sLogger.log(Level.FINEST, MSG_BASE_GET_CUSTOM_PROPERTIESLIST, propertyName); 619 return null; } 621 622 629 public Object getPropertyValue(String propertyName) throws MBeanException,AttributeNotFoundException { 630 _sLogger.log(Level.FINEST, MSG_BASE_GET_PROPERTY, propertyName); 631 632 ConfigBean baseBean = getBaseConfigBean(); 633 Class cl = baseBean.getClass(); 634 ElementProperty prop = null; 635 try { 636 Method method = cl.getDeclaredMethod("getElementPropertyByName", new Class []{Class.forName("java.lang.String")}); 637 prop = (ElementProperty)method.invoke(baseBean, new Object []{propertyName}); 638 } 639 catch (Exception e) { 640 wrapAndThrowMBeanException(e, "getattribute.undefined_properties_in_base_element"); 641 } 642 if(prop==null) { 643 wrapAndThrowMBeanException(null, "getattribute_properties_not_found_in_base_element", propertyName ); 644 } 645 return prop.getValue(); 646 } 647 654 public Object getSystemPropertyValue(String propertyName) throws MBeanException,AttributeNotFoundException { 655 _sLogger.log(Level.FINEST, MSG_BASE_GET_PROPERTY, propertyName); 656 657 ConfigBean baseBean = getBaseConfigBean(); 658 Class cl = baseBean.getClass(); 659 SystemProperty prop = null; 660 try { 661 Method method = cl.getDeclaredMethod("getSystemPropertyByName", new Class []{Class.forName("java.lang.String")}); 662 prop = (SystemProperty)method.invoke(baseBean, new Object []{propertyName}); 663 } 664 catch (Exception e) { 665 wrapAndThrowMBeanException(e, "getattribute.undefined_properties_in_base_element"); 666 } 667 if(prop==null) { 668 wrapAndThrowMBeanException(null, "getattribute_properties_not_found_in_base_element", propertyName ); 669 } 670 return prop.getValue(); 671 } 672 673 679 public void setProperty(Attribute attr) throws MBeanException,AttributeNotFoundException { 680 setElementProperty(attr, false); 681 } 682 688 public void setSystemProperty(Attribute attr) throws MBeanException,AttributeNotFoundException { 689 setSystemProperty(attr, false); 690 } 691 692 private void setElementProperty(Attribute attr, boolean bAllowsEmptyValue) throws MBeanException,AttributeNotFoundException { 694 String propertyName = attr.getName(); 695 String value = (String )attr.getValue(); 696 _sLogger.log(Level.FINEST, MSG_BASE_SET_PROPERTY, new Object []{propertyName, value}); 697 698 ConfigBean baseBean = getBaseConfigBean(); 699 700 Class cl = baseBean.getClass(); 701 ElementProperty prop = null; 702 try { 703 Method method = cl.getDeclaredMethod("getElementPropertyByName", new Class []{Class.forName("java.lang.String")}); 704 prop = (ElementProperty)method.invoke(baseBean, new Object []{propertyName}); 705 } 706 catch (Exception e) { 707 wrapAndThrowMBeanException(e, "setattribute_undefined_properties_in_base_element", propertyName ); 708 } 709 if(prop==null && value!=null && (bAllowsEmptyValue || !value.equals(""))) { 710 prop = new ElementProperty(); 711 prop.setName(propertyName); 712 if(ConfigMBeanHelper.PROPERTY_SPECIAL_EMPTY_VALUE.equals(value)) 713 prop.setValue(""); 714 else 715 prop.setValue(value); 716 try { 717 Method method = cl.getDeclaredMethod("addElementProperty", new Class []{prop.getClass()}); 718 method.invoke(baseBean, new Object []{prop}); 719 } 720 catch (Exception e) { 721 wrapAndThrowMBeanException(e, "setproperty_invoke_error", propertyName ); 722 } 723 } 724 else { 725 if(value==null || (!bAllowsEmptyValue && value.equals(""))) { 726 try { 727 Method method = cl.getDeclaredMethod("removeElementProperty", new Class []{prop.getClass()}); 728 method.invoke(baseBean, new Object []{prop}); 729 } 730 catch (Exception e) { 731 wrapAndThrowMBeanException(e, "setproperty_could_not_remove_propery", propertyName ); 732 } 733 } 734 else 735 { 736 if(ConfigMBeanHelper.PROPERTY_SPECIAL_EMPTY_VALUE.equals(value)) 737 prop.setValue(""); 738 else 739 prop.setValue(value); 740 } 741 } 742 } 743 744 private void setSystemProperty(Attribute attr, boolean bAllowsEmptyValue) throws MBeanException,AttributeNotFoundException { 745 String propertyName = attr.getName(); 746 String value = (String )attr.getValue(); 747 _sLogger.log(Level.FINEST, MSG_BASE_SET_PROPERTY, new Object []{propertyName, value}); 748 749 ConfigBean baseBean = getBaseConfigBean(); 750 751 Class cl = baseBean.getClass(); 752 SystemProperty prop=null; 753 try { 754 Method method = cl.getDeclaredMethod("getSystemPropertyByName", new Class []{Class.forName("java.lang.String")}); 755 prop = (SystemProperty)method.invoke(baseBean, new Object []{propertyName}); 756 } 757 catch (Exception e) { 758 wrapAndThrowMBeanException(e, "setattribute_undefined_properties_in_base_element", propertyName ); 759 } 760 if(prop==null && value!=null && (bAllowsEmptyValue || !value.equals(""))) { 761 prop = new SystemProperty(); 762 prop.setName(propertyName); 763 if(ConfigMBeanHelper.PROPERTY_SPECIAL_EMPTY_VALUE.equals(value)) 764 prop.setValue(""); 765 else 766 prop.setValue(value); 767 try { 768 Method method = cl.getDeclaredMethod("addSystemProperty", new Class []{prop.getClass()}); 769 method.invoke(baseBean, new Object []{prop}); 770 } 771 catch (Exception e) { 772 wrapAndThrowMBeanException(e, "setproperty_invoke_error", propertyName ); 773 } 774 } 775 else { 776 if(value==null || (!bAllowsEmptyValue && value.equals(""))) { 777 try { 778 Method method = cl.getDeclaredMethod("removeSystemProperty", new Class []{prop.getClass()}); 779 method.invoke(baseBean, new Object []{prop}); 780 } 781 catch (Exception e) { 782 wrapAndThrowMBeanException(e, "setproperty_could_not_remove_propery", propertyName ); 783 } 784 } 785 else 786 { 787 if(ConfigMBeanHelper.PROPERTY_SPECIAL_EMPTY_VALUE.equals(value)) 788 prop.setValue(""); 789 else 790 prop.setValue(value); 791 } 792 } 793 801 } 802 803 804 811 public String getDefaultAttributeValue(String externalName) throws Exception { 813 _sLogger.log(Level.FINEST, MSG_BASE_GET_DEF_ATTR_VALUE, externalName); 814 MBeanAttributeInfo ai = getAttrInfo(externalName); 815 if(ai==null) { 816 String msg = localStrings.getString( "admin.server.core.mbean.config.getdefaultattribute_undefined_attribute", externalName ); 817 throw new AttributeNotFoundException( msg ); 818 } 819 { 821 AttrDescriptor descr = getDescriptor(externalName); 822 String value = descr.getDefaultValue(); 823 return value; 824 } 825 831 } 832 833 838 private static boolean isEmptyValueAllowed(MBeanAttributeInfo ai) { 839 if(ai!=null && (ai instanceof ModelMBeanAttributeInfo )) 840 { 841 try { 842 Descriptor descr = ((ModelMBeanAttributeInfo )ai).getDescriptor(); 843 String strAllowed = (String )descr.getFieldValue(MBeanMetaConstants.EMPTYVALUEALLOWED_FIELD_NAME); 844 if(Boolean.valueOf(strAllowed).booleanValue()) 845 return true; 846 } catch(Exception e) 847 { 848 } 849 } 850 return false; 851 } 852 858 public void setAttribute(ModelMBeanAttributeInfo attrInfo, Attribute attr) throws MBeanException,AttributeNotFoundException { 859 String externalName = attr.getName(); 860 Object value = attr.getValue(); 861 _sLogger.log(Level.FINEST, MSG_BASE_SET_ATTRIBUTE, new Object []{externalName, value}); 862 MBeanAttributeInfo ai = getAttrInfo(externalName); 863 boolean isProperty = externalName.startsWith(PROPERTY_NAME_PREFIX); 864 boolean isSystemProperty = externalName.startsWith(SYSTEM_PROPERTY_NAME_PREFIX); 865 if(ai==null && !isProperty && !isSystemProperty) { 866 String msg = localStrings.getString( "admin.server.core.mbean.config.setattribute_undefined_attribute", externalName ); 867 throw new AttributeNotFoundException( msg ); 868 } 869 if(ai!=null && !ai.isWritable()) { 870 wrapAndThrowMBeanException(null, "setattribute_attribute_not_writable", externalName ); 871 } 872 873 if(isProperty) 875 { 876 boolean bAllowsEmptyValue = isEmptyValueAllowed(ai); 877 setElementProperty(new Attribute(externalName.substring(PROPERTY_NAME_PREFIX.length()), value), bAllowsEmptyValue); 879 return; 880 } 881 else if(isSystemProperty) 882 { 883 boolean bAllowsEmptyValue = isEmptyValueAllowed(ai); 884 setSystemProperty(new Attribute(externalName.substring(SYSTEM_PROPERTY_NAME_PREFIX.length()), value), bAllowsEmptyValue); 886 return; 887 } 888 else 889 { 894 AttrDescriptor descr = getDescriptor(externalName); 895 ConfigBean bean = getConfigBean(externalName); 896 if(descr.isElement()) 897 { 898 wrapAndThrowMBeanException(null, "not_supported_attribute_type", externalName ); 901 902 } 905 else 906 { 907 if(ai.getType().startsWith("[")) { 909 Class cl = bean.getClass(); 910 try 911 { 912 Method method = cl.getMethod("setValue", new Class []{Class.forName("java.lang.String"),(new Object [0]).getClass()}); 913 method.invoke(bean, new Object []{ConfigBean.camelize(descr.getAttributeName()),value}); 914 } 915 catch (Exception e) 916 { 917 String msg = localStrings.getString( "admin.server.core.mbean.config.setattribute_invocation_error", externalName ); 918 throw MBeanHelper.extractAndWrapTargetException(e, msg); 919 } 920 } 921 else 922 { 923 if(value==null || (value.equals("") && !isEmptyValueAllowed(ai)) ) 925 bean.setAttributeValue(descr.getAttributeName(), null); 926 else 927 bean.setAttributeValue(descr.getAttributeName(), value.toString()); 928 } 930 } 931 } 932 933 } 944 945 946 private void addArrayToList(ArrayList list, Object [] objects) 948 { 949 for(int i=0; i<objects.length; i++) { 950 list.add(objects[i]); 951 } 952 } 953 954 959 protected ArrayList getSimpleAttributeNames(String [] attributeNames) { 960 ArrayList simpleNames = new ArrayList (); 961 for(int i=0; i<attributeNames.length; i++) { 962 if(attributeNames[i].length()==0) { 963 addArrayToList(simpleNames, getAllAttributeNames()); 964 } 965 else if(attributeNames[i].equals(PROPERTY_NAME_PREFIX)) { 966 addArrayToList(simpleNames, getAllPropertyNames(true)); 967 } 968 else if(attributeNames[i].equals(SYSTEM_PROPERTY_NAME_PREFIX)) { 969 addArrayToList(simpleNames, getAllSystemPropertyNames(true)); 970 } 971 else { 972 simpleNames.add(attributeNames[i]); 973 } 974 975 } 976 return simpleNames; 977 } 978 979 984 public AttributeList getAttributes(String [] attributeNames) { 985 ArrayList names = getSimpleAttributeNames(attributeNames); 986 AttributeList attrs = new AttributeList(); 987 for(int i=0; i<names.size(); i++) { 988 try { 989 String name = MBeanMetaHelper.mapToMBeanAttributeName((String )names.get(i)); 990 Object value = getAttribute(null, name); attrs.add(new Attribute(name, value)); 992 } catch (MBeanException ce) { 993 } catch (AttributeNotFoundException ce) { 995 } catch (NullPointerException npe) { 998 } 1000 } 1001 return attrs; 1002 } 1003 1004 1009 public AttributeList setAttributes(AttributeList attrList) { 1010 AttributeList attrs = new AttributeList(); 1011 Iterator it = attrList.iterator(); 1012 while (it.hasNext()) { 1013 try { 1014 Attribute attribute = (Attribute) it.next(); 1015 attribute = new Attribute(MBeanMetaHelper.mapToMBeanAttributeName(attribute.getName()), attribute.getValue()); 1016 setAttribute(null, attribute); attrs.add(attribute); 1018 } 1019 catch (MBeanException mbe) { 1020 } 1021 catch (AttributeNotFoundException anfe) { 1022 } 1023 catch (NullPointerException npe) { 1024 } 1025 } 1026 return attrs; 1027 } 1028 1032 public Object invoke(String methodName, Object [] methodParams, 1033 String [] methodSignature) throws MBeanException, ReflectionException { 1034 return null; 1035 } 1036 1037 1038 private AttrDescriptor getDescriptor(String externalName) { 1040 try { 1041 return new AttrDescriptor(externalName); } catch (MBeanConfigException mbce) 1043 { 1044 return null; 1045 } 1046 } 1048 1049 private MBeanAttributeInfo getAttrInfo(String attrName) { 1051 if(m_mbeanInfo==null ) 1052 return null; 1053 MBeanAttributeInfo [] ai = m_mbeanInfo.getAttributes(); 1054 if(ai!=null) 1055 for(int i=0; i<ai.length; i++) { 1056 String name = ai[i].getName(); 1057 if(attrName.equals(ai[i].getName())) 1058 return ai[i]; 1059 } 1060 return null; } 1062 1063 private String [] getAllAttributeNames() { 1065 MBeanInfo mbi = m_mbeanInfo; 1066 if(mbi==null ) 1067 return null; 1068 MBeanAttributeInfo [] ai = mbi.getAttributes(); 1069 if(ai==null || ai.length==0) 1070 return null; 1071 ArrayList list = new ArrayList (); 1072 if(ai!=null) 1073 for(int i=0; i<ai.length; i++) 1074 { 1075 String name = ai[i].getName(); 1076 if(!name.equals("modelerType")) list.add(name); 1078 } 1079 return (String [])list.toArray(new String [list.size()]); 1080 } 1081 1082 private String [] getAllPropertyNames(boolean bAddPropertyPrefix) { 1084 1085 ConfigBean baseBean = getBaseConfigBean(); 1086 Class cl = baseBean.getClass(); 1087 ElementProperty[] props; 1088 try { 1089 Method method = cl.getDeclaredMethod("getElementProperty"); 1090 props = (ElementProperty[])method.invoke(baseBean); 1091 String [] names = new String [props.length]; 1092 for(int i=0; i<props.length; i++) { 1093 if(bAddPropertyPrefix) 1094 names[i] = PROPERTY_NAME_PREFIX+props[i].getName(); 1095 else 1096 names[i] = props[i].getName(); 1097 } 1098 return names; 1099 } 1100 catch (java.lang.NoSuchMethodException nsme) { 1101 return null; 1102 } 1103 catch (java.lang.IllegalAccessException iae) { 1104 return null; 1105 } 1106 catch (java.lang.reflect.InvocationTargetException ite) { 1107 return null; 1108 } 1109 } 1110 1111 private String [] getAllSystemPropertyNames(boolean bAddPropertyPrefix) { 1113 1114 ConfigBean baseBean = getBaseConfigBean(); 1115 Class cl = baseBean.getClass(); 1116 SystemProperty[] props; 1117 try { 1118 Method method = cl.getDeclaredMethod("getSystemProperty"); 1119 props = (SystemProperty[])method.invoke(baseBean); 1120 String [] names = new String [props.length]; 1121 for(int i=0; i<props.length; i++) { 1122 if(bAddPropertyPrefix) 1123 names[i] = SYSTEM_PROPERTY_NAME_PREFIX+props[i].getName(); 1124 else 1125 names[i] = props[i].getName(); 1126 } 1127 return names; 1128 } 1129 catch (java.lang.NoSuchMethodException nsme) { 1130 return null; 1131 } 1132 catch (java.lang.IllegalAccessException iae) { 1133 return null; 1134 } 1135 catch (java.lang.reflect.InvocationTargetException ite) { 1136 return null; 1137 } 1138 } 1139 1140 1163 public ConfigBean createChildByType(String childName, AttributeList childValues, Properties props, boolean bOnlyOneChildPossible) throws Exception 1165 { 1166 ConfigBean bean = createChildByType(childName, childValues, props); 1168 if(bean==null) 1169 return null; 1170 1171 addChildBean(childName, bean, bOnlyOneChildPossible); 1173 return bean; 1174 } 1175 1176 public ConfigBean createChildByType(String childName, AttributeList childValues, Properties props) throws Exception 1178 { 1179 if(m_baseConfigBean==null) 1180 return null; 1181 String xpath = MBeanHelper.getXPathPattern( (ModelMBeanInfo )m_mbeanInfo) + 1182 ServerXPathHelper.XPATH_SEPARATOR + childName; 1183 Class cl = ConfigMBeanHelper.getConfigBeanClass(xpath); 1184 ConfigBean bean = (ConfigBean)cl.newInstance(); 1185 MBeanRegistryEntry regEntry = m_registry.findMBeanRegistryEntryByXPathPattern(xpath); 1186 if(childValues!=null) 1187 for(int i=0; i<childValues.size(); i++) 1188 { 1189 1190 Attribute attr = (Attribute)childValues.get(i); 1191 if( (attr.getValue()==null || attr.getValue().equals(""))) 1192 { 1193 boolean bEmptyValueAllowed = false; 1195 try { 1196 bEmptyValueAllowed = regEntry.isAttributeEmptyValueAllowed(attr.getName()); 1198 } catch (MBeanMetaException mme) 1199 { 1200 } 1202 if(!bEmptyValueAllowed) 1203 continue; } 1205 if(attr.getValue()==null) 1206 bean.setAttributeValue(MBeanMetaHelper.mapToConfigBeanAttributeName(attr.getName()), (String )attr.getValue()); 1207 else 1208 bean.setAttributeValue(MBeanMetaHelper.mapToConfigBeanAttributeName(attr.getName()), ""+attr.getValue()); 1209 } 1210 if(props!=null && props.size()>0) 1211 { 1212 final Enumeration e = props.propertyNames(); 1213 ArrayList propsList = new ArrayList (); 1214 while (e.hasMoreElements()) 1215 { 1216 ElementProperty ep = new ElementProperty(); 1217 String key = (String )e.nextElement(); 1218 ep.setName(key); 1219 ep.setValue(props.getProperty(key)); 1220 propsList.add(ep); 1221 } 1222 if(propsList.size()>0) 1223 { 1224 ElementProperty eps[] = (ElementProperty[])propsList.toArray( 1225 new ElementProperty[propsList.size()]); 1226 bean.setValue("ElementProperty", eps); 1227 } 1228 } 1229 return bean; 1230 } 1231 1232 public void addChildBean(String childName, ConfigBean bean, boolean bOnlyOneChildPossible) throws Exception 1234 { 1235 Class clBase = m_baseConfigBean.getClass(); 1236 Method method; 1237 if(bOnlyOneChildPossible) 1238 method = clBase.getDeclaredMethod("set"+ConfigMBeanHelper.convertTagName(childName), new Class []{bean.getClass()}); 1239 else 1240 method = clBase.getDeclaredMethod("add"+ConfigMBeanHelper.convertTagName(childName), new Class []{bean.getClass()}); 1241 method.invoke(m_baseConfigBean, new Object []{bean}); 1242 } 1243 1244 void removeChild(String opName, MBeanOperationInfo opInfo, Object params[], String childTag) throws Exception 1246 { 1247 1248 MBeanOperationInfo getInfo = new MBeanOperationInfo ("get"+opName.substring("remove".length()), 1249 "", 1250 opInfo.getSignature(), 1251 "java.lang.Object", 1252 MBeanOperationInfo.INFO); 1253 Object ret = MBeanHelper.invokeOperationInBean(getInfo, this.getBaseConfigBean(), params); 1254 if(ret==null) 1255 { 1256 String parentName = ""; 1257 parentName = this.getBaseConfigBean().getClass().getName(); 1258 int last = parentName.lastIndexOf('.'); 1259 if(last>=0 && last<(parentName.length()-1)) 1260 parentName = parentName.substring(last+1); 1261 String elemName = ""; 1262 if(params!=null && params.length==1 && params[0] instanceof String ) 1263 elemName = "\""+(String )params[0]+"\""; 1264 wrapAndThrowMBeanException(null, "element_not_found", new Object []{parentName, childTag, elemName}); 1265 } 1266 if(ret==MBeanHelper.INVOKE_ERROR_SIGNAL_OBJECT) 1267 { 1268 String elemName = ""; 1269 if(params.length==1 && params[0] instanceof String ) 1270 elemName = (String )params[0]; 1271 wrapAndThrowMBeanException(null, "remove_get_invocation", 1272 new Object []{opName, elemName} ); 1273 } 1274 ArrayList arr = new ArrayList (); 1275 collectChildrenObjectNames((ConfigBean)ret, arr); 1276 m_baseConfigBean.removeChild((ConfigBean)ret); 1278 MBeanServer server = null; 1280 try { 1281 server = (MBeanServer)(MBeanServerFactory.findMBeanServer(null)).get(0); 1282 } catch(Exception e) {}; 1284 for(int i=0; i<arr.size(); i++) 1285 { 1286 ObjectName objectName = (ObjectName)arr.get(i); 1287 try 1288 { 1289 if(server.isRegistered(objectName)) 1290 server.unregisterMBean(objectName); 1291 else 1292 m_registry.notifyUnregisterMBean(objectName, getConfigContext()); 1294 } 1295 catch (Throwable t) 1296 { 1297 _sLogger.fine("!!!!!!!!!!!!!! Can not unregister MBean: "+objectName); 1298 } 1299 } 1300 } 1301 1302 void collectChildrenObjectNames(ConfigBean bean, ArrayList arr) 1304 { 1305 try 1306 { 1307 ConfigBean[] beans = bean.getAllChildBeans(); 1308 for(int i=0; i<beans.length; i++) 1309 { 1310 collectChildrenObjectNames(beans[i], arr); 1311 } 1312 } 1313 catch(Exception e) 1314 { 1315 } 1316 try 1317 { 1318 ObjectName name = (ObjectName)ConfigMBeanHelper.converConfigBeansToObjectNames(m_registry, (ModelMBeanInfo )m_mbeanInfo, (Object )bean); 1319 if(name!=null) 1320 arr.add(name); 1321 } 1322 catch(Exception e) 1323 { 1324 } 1325 } 1326 1327 public void deleteSelf() throws Exception 1329 { 1330 if(m_baseConfigBean==null) 1331 return; 1332 String xpath = null; 1333 xpath = m_baseConfigBean.getAbsoluteXPath(null); 1335 xpath = ServerXPathHelper.getParentXPath(xpath); 1336 ConfigContext ctx = getConfigContext(); ConfigBean parent = ConfigBeansFactory.getConfigBeanByXPath(ctx, xpath); 1338 parent.removeChild(m_baseConfigBean); 1339 } 1345 1346 private class AttrDescriptor { 1348 public String m_attributeName; 1349 public boolean m_bAllowsEmptyValue = false; 1350 1351 public AttrDescriptor(String description) throws MBeanConfigException { 1353 try { 1354 m_attributeName = MBeanMetaHelper.mapToConfigBeanAttributeName(description); 1355 1356 1373 } 1374 catch (Throwable e) { 1375 String msg = localStrings.getString( "admin.server.core.mbean.config.attrdescriptor_constructor_exception", description, e.getMessage() ); 1376 throw new MBeanConfigException( msg ); 1377 } 1378 } 1379 1380 public String getAttributeName() { 1382 return m_attributeName; 1383 } 1384 1385 public boolean isEmptyValueAllowed() { 1387 return m_bAllowsEmptyValue; 1388 } 1389 1390 public String getDefaultValue() throws Exception { 1392 if(m_baseConfigBean==null || isProperty() || isSystemProperty() || isElement()) 1393 return null; 1394 Class clBase = m_baseConfigBean.getClass(); 1396 Method method = clBase.getDeclaredMethod("getDefaultAttributeValue", new Class []{Class.forName("java.lang.String")}); 1397 return (String )method.invoke(m_baseConfigBean, new Object []{m_attributeName}); 1398 } 1399 public boolean isProperty() { 1401 if( m_attributeName!=null) 1402 return m_attributeName.startsWith(PROPERTY_NAME_PREFIX); 1403 return false; 1404 } 1405 public boolean isSystemProperty() { 1407 if( m_attributeName!=null) 1408 return m_attributeName.startsWith(SYSTEM_PROPERTY_NAME_PREFIX); 1409 return false; 1410 } 1411 public boolean isElement() { 1413 return (m_attributeName==null); 1414 } 1415 } 1416 1417 public Object invokeOperation(ModelMBeanOperationInfo opInfo, Object params[], String signature[]) throws Exception 1418 { 1419 Descriptor descr = opInfo.getDescriptor(); 1420 String opName = opInfo.getName(); 1421 String child = (String )descr.getFieldValue(MBeanMetaConstants.CHILD_FIELD_NAME); 1422 String multi = (String )descr.getFieldValue(MBeanMetaConstants.MULTI_FIELD_NAME); 1423 boolean bOnlyOne = true; 1424 if(multi!=null && multi.length()>0 && 1425 multi.charAt(0)=='t') 1426 bOnlyOne = false; 1427 if(child!=null && opName.startsWith("create")) 1428 { 1429 ConfigBean bean = createChildByType(child, (AttributeList)params[0], null, bOnlyOne); 1430 ObjectName objectName = ConfigMBeanHelper.getChildObjectName(m_registry, (ModelMBeanInfo )m_mbeanInfo, bean); 1431 m_registry.notifyRegisterMBean(objectName, getConfigContext()); 1432 return objectName; 1433 } 1434 1435 if(child!=null && opName.startsWith("remove")) 1436 { 1437 removeChild(opName, opInfo, params, child); 1438 return null; 1439 } 1440 1441 1442 1443 Object ret = MBeanHelper.invokeOperationInBean(opInfo, this, params); 1445 if(ret!=MBeanHelper.INVOKE_ERROR_SIGNAL_OBJECT) 1446 { 1447 return ret; 1448 } 1449 1450 boolean bNamesListOp = false; 1451 String modifiedOpName = opName; 1452 if(!bOnlyOne && child!=null && 1453 opName.startsWith("get") && 1454 opName.endsWith(MBeanMetaConstants.GET_LISTNAMES_OP_SUFFIX)) 1455 { 1456 bNamesListOp = true; 1457 modifiedOpName = opName.substring(0, opName.length()- 1458 MBeanMetaConstants.GET_LISTNAMES_OP_SUFFIX.length()); 1459 } 1460 1461 ret = MBeanHelper.invokeOperationInBean(modifiedOpName, opInfo, 1462 this.getBaseConfigBean(), params); 1463 1464 if(ret!=MBeanHelper.INVOKE_ERROR_SIGNAL_OBJECT) 1465 { 1466 if(ret==null && "javax.management.ObjectName".equals(opInfo.getReturnType())) 1467 { 1468 String parentName = ""; 1469 parentName = this.getBaseConfigBean().getClass().getName(); 1470 int last = parentName.lastIndexOf('.'); 1471 if(last>=0 && last<(parentName.length()-1)) 1472 parentName = parentName.substring(last+1); 1473 String elemName = ""; 1474 if(params!=null && params.length==1 && params[0] instanceof String ) 1475 elemName = (String )params[0]; 1476 String msg; 1477 if(child!=null) 1478 msg = localStrings.getString( "admin.server.core.mbean.config.element_not_found", new Object []{parentName, child, elemName}); 1479 else 1480 msg = localStrings.getString( "admin.server.core.mbean.config.oper_element_not_found", new Object []{parentName, modifiedOpName, elemName}); 1481 throw new MBeanConfigInstanceNotFoundException(msg); 1482 } 1483 1484 if(ret!=null) 1485 { 1486 if(bNamesListOp && (ret instanceof ConfigBean[])) 1487 { 1488 return (Object )ConfigMBeanHelper.getChildNamesList( 1489 (ConfigBean[])ret); 1490 } 1491 else 1492 { 1493 ret = ConfigMBeanHelper.converConfigBeansToObjectNames(m_registry, 1494 (ModelMBeanInfo )m_mbeanInfo, ret); 1495 if(ret instanceof ConfigBean) 1496 return (Object )ConfigMBeanHelper.getChildObjectName(m_registry, 1497 (ModelMBeanInfo )m_mbeanInfo, (ConfigBean)ret); 1498 if(ret instanceof ConfigBean[]) 1499 return (Object )ConfigMBeanHelper.getChildObjectNames(m_registry, 1500 (ModelMBeanInfo )m_mbeanInfo, (ConfigBean[])ret); 1501 } 1502 } 1503 return ret; 1504 } 1505 return MBeanHelper.INVOKE_ERROR_SIGNAL_OBJECT; 1506 } 1507 1508 private void uncatchValidationException(Exception e) 1509 { 1510 if(e instanceof AdminValidationException) 1511 throw (AdminValidationException)e; 1512 1513 if(e instanceof InvocationTargetException ) 1514 { 1515 Throwable t = ((InvocationTargetException )e).getTargetException(); 1516 if(t instanceof AdminValidationException) 1517 throw (AdminValidationException)t; 1518 } 1519 } 1520 1521 private void wrapAndThrowMBeanException(Exception e, 1522 String strSuffix) throws MBeanException 1523 { 1524 wrapAndThrowMBeanException(e, strSuffix, null); 1525 } 1526 private void wrapAndThrowMBeanException(Exception e, 1527 String strSuffix, Object val) throws MBeanException 1528 { 1529 if(e!=null) 1530 uncatchValidationException(e); 1531 String msg; 1532 if(val instanceof Object []) 1533 msg = localStrings.getString( "admin.server.core.mbean.config."+strSuffix, (Object [])val ); 1534 else 1535 msg = localStrings.getString( "admin.server.core.mbean.config."+strSuffix, val ); 1536 throw new MBeanException(new MBeanConfigException( msg )); 1537 } 1538 1539} 1540 1541 | Popular Tags |