1 23 24 116 117 package com.sun.enterprise.admin.config; 118 119 import java.util.Enumeration ; 120 import java.util.Hashtable ; 121 import java.text.CharacterIterator ; 122 import java.text.StringCharacterIterator ; 123 124 125 import java.lang.reflect.Method ; 126 import java.lang.reflect.Modifier ; 127 128 import javax.management.*; 130 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 131 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 132 import javax.management.modelmbean.ModelMBeanConstructorInfo ; 133 import javax.management.modelmbean.ModelMBeanOperationInfo ; 134 import javax.management.modelmbean.ModelMBeanInfoSupport ; 135 import javax.management.modelmbean.ModelMBeanInfo ; 136 137 import com.sun.org.apache.commons.modeler.AttributeInfo; 138 139 import com.sun.enterprise.admin.MBeanHelper; 141 142 import com.sun.enterprise.admin.meta.MBeanRegistry; 143 import com.sun.enterprise.admin.meta.MBeanRegistryEntry; 144 import com.sun.enterprise.admin.meta.naming.MBeanNamingInfo; 146 import com.sun.enterprise.admin.meta.naming.MBeanNamingDescriptor; 147 import com.sun.enterprise.admin.meta.MBeanMetaHelper; 148 149 import com.sun.enterprise.config.ConfigException; 151 import com.sun.enterprise.config.ConfigBean; 152 import com.sun.enterprise.config.ConfigBeansFactory; 153 import com.sun.enterprise.config.ConfigContext; 154 import com.sun.enterprise.config.serverbeans.ElementProperty; 155 156 158 public class ConfigMBeanHelper extends MBeanHelper 159 { 160 private static final String XPATH_SEPARATOR = "/"; 161 public static final String PROPERTY_NAME_PREFIX = "property."; 162 public static final String PROPERTY_SPECIAL_EMPTY_VALUE = "()"; 163 164 private ConfigBean m_baseConfigBean; 165 private BaseConfigMBean m_mbean; 166 public ConfigMBeanHelper(BaseConfigMBean mbean, ConfigBean cb) 167 { 168 m_baseConfigBean = cb; 169 m_mbean = mbean; 170 } 174 175 180 181 182 183 public static Object converConfigBeansToObjectNames(MBeanRegistry registry, ModelMBeanInfo parentInfo, Object ret) throws Exception 185 { 186 if(ret!=null) 187 { 188 if(ret instanceof ConfigBean) 189 return (Object )getChildObjectName(registry, parentInfo, (ConfigBean)ret); 190 if(ret instanceof ConfigBean[]) 191 return (Object )getChildObjectNames(registry, parentInfo, (ConfigBean[])ret); 192 } 193 return ret; 194 } 195 196 public static Class getConfigBeanClass(String xPath) 198 { 199 String beanName = ConfigBeansFactory.getConfigBeanNameByXPath(xPath); 201 try 203 { 204 Class cl = Class.forName("com.sun.enterprise.config.serverbeans."+beanName); 205 return cl; 206 } 207 catch(Exception e) 208 { 209 return null; 210 } 211 } 212 213 public static AttributeList getDefaultAttributeValues(MBeanNamingDescriptor descr, String attrNames[]) throws Exception 215 { 216 if(attrNames==null || attrNames.length<1) 217 return null; 218 AttributeList attrs = new AttributeList(); 219 Class cl = getConfigBeanClass(descr.getXPathPattern()); 220 if(cl==null) 221 return null; 222 Method method = cl.getDeclaredMethod("getDefaultAttributeValue", new Class []{Class.forName("java.lang.String")}); 223 for(int i=0; i<attrNames.length; i++) 224 { 225 try { 226 Object value = (String )method.invoke(null, new Object []{MBeanMetaHelper.mapToConfigBeanAttributeName(attrNames[i])}); 227 if(value!=null) 228 attrs.add(new Attribute(attrNames[i], value)); 229 } catch(Exception e) {}; 230 } 231 return attrs; 232 } 233 234 public static ObjectName getOwnObjectName(MBeanRegistry registry, ModelMBeanInfo parentInfo) throws Exception 236 { 237 Descriptor descr = parentInfo.getMBeanDescriptor(); 238 String type = (String )descr.getFieldValue(NMTYPE_FIELD_NAME); 239 String [] location = (String [])descr.getFieldValue(NMLOCATION_FIELD_NAME); 240 MBeanRegistryEntry entry = registry.findMBeanRegistryEntryByType(type); 241 MBeanNamingDescriptor namingDescr = entry.getNamingDescriptor(); 242 return namingDescr.createObjectName(location); 243 } 244 public static ObjectName[] getChildObjectNames(MBeanRegistry registry, ModelMBeanInfo parentInfo, ConfigBean[] children) throws Exception 246 { 247 Descriptor descr = parentInfo.getMBeanDescriptor(); 248 return getConfigBeansObjectNames(registry, (String )descr.getFieldValue(DOMAIN_FIELD_NAME), children); 249 } 250 public static ObjectName getChildObjectName(MBeanRegistry registry, ModelMBeanInfo parentInfo, ConfigBean childBean) throws Exception 252 { 253 Descriptor descr = parentInfo.getMBeanDescriptor(); 254 return getConfigBeanObjectName(registry, (String )descr.getFieldValue(DOMAIN_FIELD_NAME), childBean); 255 } 256 public static ObjectName[] getConfigBeansObjectNames(MBeanRegistry registry, String domainName, ConfigBean[] beans) throws Exception 258 { 259 ObjectName[] objNames = new ObjectName[beans.length]; 260 for(int i=0; i<beans.length; i++) 261 objNames[i] = getConfigBeanObjectName(registry, domainName, beans[i]); 262 return objNames; 263 } 264 public static ObjectName getConfigBeanObjectName(MBeanRegistry registry, String domainName, ConfigBean childBean) throws Exception 266 { 267 String xpath = childBean.getAbsoluteXPath(""); 268 MBeanRegistryEntry entry = registry.findMBeanRegistryEntryByXPath(xpath); 269 MBeanNamingDescriptor namingDescr = entry.getNamingDescriptor(); 270 String [] location = namingDescr.extractParmListFromXPath(xpath); 271 location[0] = domainName; 272 return namingDescr.createObjectName((Object [])location); 273 } 274 public static String [] getChildNamesList(ConfigBean[] beans) throws Exception 276 { 277 String [] names = new String [beans.length]; 278 for(int i=0; i<beans.length; i++) 279 { 280 String xpath = beans[i].getAbsoluteXPath(""); 281 names[i] = MBeanMetaHelper.getMultipleElementKeyValue(xpath); 282 } 283 return names; 284 } 285 287 protected static void debug(String s) { 288 System.out.println(s); 290 } 291 protected static void info(String s) { 292 System.out.println(s); 294 } 295 protected static void error(String s) { 296 System.out.println(s); 298 } 299 300 307 public ConfigBean getChildElementByName(String methodName, String name) throws MBeanException,AttributeNotFoundException 308 { 309 310 Class cl = m_baseConfigBean.getClass(); 311 ConfigBean bean; 312 try 313 { 314 Method method = cl.getDeclaredMethod(methodName, new Class []{Class.forName("java.lang.String")}); 315 return (ConfigBean)method.invoke(m_baseConfigBean, new Object []{name}); 316 } 317 catch (Exception e) 318 { 319 String msg = ( "admin.server.core.mbean.config.getattribute.undefined_childelement_in_base_element"+ name ); 320 throw new MBeanException(new MBeanConfigException( msg )); 321 } 322 } 323 324 331 public Object getPropertyElementValue(String propertyName) throws MBeanException,AttributeNotFoundException 332 { 333 334 Class cl = m_baseConfigBean.getClass(); 335 ElementProperty prop; 336 try 337 { 338 Method method = cl.getDeclaredMethod("getElementPropertyByName", new Class []{Class.forName("java.lang.String")}); 339 prop = (ElementProperty)method.invoke(m_baseConfigBean, new Object []{propertyName}); 340 } 341 catch (Exception e) 342 { 343 String msg = ( "admin.server.core.mbean.config.getattribute.undefined_properties_in_base_element"+ propertyName ); 344 throw new MBeanException(new MBeanConfigException( msg )); 345 } 346 if(prop==null) { 347 String msg = ( "admin.server.core.mbean.config.getattribute_properties_not_found_in_base_element"+ propertyName ); 348 throw new MBeanException(new MBeanConfigException( msg )); 349 } 350 return prop.getValue(); 351 } 352 358 public void setPropertyElementValue(Attribute attr, boolean bAllowsEmptyValue) throws MBeanException,AttributeNotFoundException 359 { 360 String propertyName = attr.getName(); 361 String value = (String )attr.getValue(); 362 363 Class cl = m_baseConfigBean.getClass(); 364 ElementProperty prop; 365 try 366 { 367 Method method = cl.getDeclaredMethod("getElementPropertyByName", new Class []{Class.forName("java.lang.String")}); 368 prop = (ElementProperty)method.invoke(m_baseConfigBean, new Object []{propertyName}); 369 } 370 catch (Exception e) 371 { 372 String msg = ( "admin.server.core.mbean.config.setattribute_undefined_properties_in_base_element"+ propertyName ); 373 throw new MBeanException(new MBeanConfigException( msg )); 374 } 375 if(prop==null && value!=null && (bAllowsEmptyValue || !value.equals(""))) 376 { 377 prop = new ElementProperty(); 378 prop.setName(propertyName); 379 prop.setValue(value); 380 try 381 { 382 Method method = cl.getDeclaredMethod("addElementProperty", new Class []{prop.getClass()}); 383 method.invoke(m_baseConfigBean, new Object []{prop}); 384 } 385 catch (Exception e) 386 { 387 String msg = ( "admin.server.core.mbean.config.setproperty_invoke_error"+propertyName ); 388 throw new MBeanException(new MBeanConfigException( msg )); 389 } 390 } 391 else 392 { 393 if(value==null || (!bAllowsEmptyValue && value.equals(""))) 394 { 395 try 396 { 397 Method method = cl.getDeclaredMethod("removeElementProperty", new Class []{prop.getClass()}); 398 method.invoke(m_baseConfigBean, new Object []{prop}); 399 } 400 catch (Exception e) 401 { 402 String msg = ( "admin.server.core.mbean.config.setproperty_could_not_remove_propery"+ propertyName ); 403 throw new MBeanException(new MBeanConfigException( msg )); 404 } 405 } 406 else 407 prop.setValue(value); 408 } 409 410 419 } 420 432 public static String convertTagName(String name) 433 { 434 CharacterIterator ci; 435 StringBuffer n = new StringBuffer (); 436 boolean up = true; 437 boolean keepCase = false; 438 char c; 439 440 ci = new StringCharacterIterator (name); 441 c = ci.first(); 442 443 while (c != CharacterIterator.DONE) 445 { 446 if (Character.isLowerCase(c)) 447 { 448 keepCase = true; 449 break; 450 } 451 c = ci.next(); 452 } 453 454 c = ci.first(); 455 while (c != CharacterIterator.DONE) 456 { 457 if (c == '-' || c == '_') 458 up = true; 459 else 460 { 461 if (up) 462 c = Character.toUpperCase(c); 463 else 464 if (!keepCase) 465 c = Character.toLowerCase(c); 466 n.append(c); 467 up = false; 468 } 469 c = ci.next(); 470 } 471 return n.toString(); 472 } 474 } 475 | Popular Tags |