1 23 24 162 163 package com.sun.enterprise.admin.meta; 164 165 import javax.management.DynamicMBean ; 167 import javax.management.modelmbean.ModelMBeanInfo ; 168 import com.sun.org.apache.commons.modeler.ManagedBean; 169 import com.sun.org.apache.commons.modeler.FeatureInfo; 170 import com.sun.org.apache.commons.modeler.AttributeInfo; 171 import com.sun.org.apache.commons.modeler.OperationInfo; 172 import com.sun.org.apache.commons.modeler.ParameterInfo; 173 import com.sun.org.apache.commons.modeler.FieldInfo; 174 175 import java.util.Iterator ; 176 import java.util.List ; 177 import java.util.HashMap ; 178 import java.util.Hashtable ; 179 import java.util.HashSet ; 180 import java.util.Set ; 181 import java.util.StringTokenizer ; 182 183 import java.lang.reflect.Constructor ; 184 185 import com.sun.enterprise.admin.meta.naming.MBeanNamingDescriptor; 186 import com.sun.enterprise.admin.meta.naming.MBeansNaming; 187 import com.sun.enterprise.admin.meta.naming.MBeanNamingInfo; 188 import com.sun.enterprise.admin.meta.naming.MBeanNamingException; 189 190 import com.sun.enterprise.admin.BaseAdminMBean; 191 import com.sun.enterprise.admin.config.BaseConfigMBean; 192 193 import com.sun.enterprise.config.ConfigException; 194 import com.sun.enterprise.config.ConfigContext; 195 import com.sun.enterprise.config.ConfigBeansFactory; 196 197 import javax.management.Descriptor ; 198 import javax.management.ObjectName ; 199 import javax.management.MBeanException ; 200 import javax.management.InstanceNotFoundException ; 201 import javax.management.modelmbean.ModelMBeanInfo ; 202 203 204 207 public class MBeanRegistryEntry implements MBeanMetaConstants 208 { 209 MBeanNamingDescriptor namingDescriptor = null; 210 211 private ManagedBean managedBean = null; 213 private HashMap fields = null; 214 215 216 public MBeanRegistryEntry(ManagedBean mb) throws MBeanNamingException,ClassNotFoundException 217 { 218 managedBean = mb; 219 fields = convertListOfFieldsToHashMap(mb.getFields()); 220 221 String dynList = (String )fields.get(DYNAMICALLY_RECONFIGURABLE_LIST_FIELD_NAME); 223 if(dynList!=null) 224 { 225 fields.put(DYNAMICALLY_RECONFIGURABLE_LIST_FIELD_NAME, convertDelimListToSet(dynList)); 227 } 228 229 String clazz = managedBean.getClassName(); 231 if(clazz.startsWith("com.sun.org.apache")) 232 clazz = null; 233 String group = managedBean.getGroup(); 234 if("config".equals(group)) 235 { 236 if(clazz==null) 237 { 238 clazz = "com.sun.enterprise.admin.config.BaseConfigMBean"; 239 managedBean.setClassName(clazz); 240 } 241 else 242 { 243 if(clazz.indexOf('.')<0) 244 clazz = "com.sun.enterprise.admin.config.mbeans."+clazz; 245 } 246 MBeanMetaHelper.mergeWithConfigBean(managedBean, 247 MBeanMetaHelper.getConfigBeanClass((String )fields.get(XPATH_FIELD_NAME)), 248 EXPOSE_ALL); 249 MBeanMetaHelper.mergeWithDynamicMBean(managedBean, Class.forName(clazz)); 250 } 251 else if("runtime".equals(group)) 252 { 253 if(clazz==null) 254 { 255 clazz = "com.sun.enterprise.admin.runtime.BaseRuntimeMBean"; 256 managedBean.setClassName(clazz); 257 } 258 else 259 { 260 if(clazz.indexOf('.')<0) 261 clazz = "com.sun.enterprise.admin.runtime.mbeans"+clazz; 262 } 263 Class modelBeanClass = MBeanMetaHelper.getRuntimeModelBeanClass(mb.getName()); 264 if(modelBeanClass!=null) 265 MBeanMetaHelper.mergeWithRuntimeModelBean(managedBean, modelBeanClass); 266 Class configBeanClass = MBeanMetaHelper.getConfigBeanClass((String )fields.get(XPATH_FIELD_NAME)); 267 if(configBeanClass!=null) 268 { 269 int mode; 270 if(modelBeanClass!=null) 271 mode = EXPOSE_RUNTIME_WITH_MODEL; 272 else 273 mode = EXPOSE_RUNTIME_WITHOUT_MODEL; 274 275 MBeanMetaHelper.mergeWithConfigBean(managedBean, configBeanClass, mode); 276 } 277 MBeanMetaHelper.mergeWithDynamicMBean(managedBean, Class.forName(clazz)); 278 } 279 280 namingDescriptor = new MBeanNamingDescriptor( mb.getName(), 281 new Integer (MBeansNaming.MODE_CONFIG), 282 (String )fields.get(CLINAME_FIELD_NAME), 283 (String )fields.get(OBJECTNAME_FIELD_NAME), 284 MBeanNamingDescriptor.XPATH_TO_MASK((String )fields.get(XPATH_FIELD_NAME)), 285 (String )clazz); 287 296 } 297 298 public String toString() 299 { 300 String str = "\n--- MBeanRegistryEntry:\n "; 301 if(managedBean!=null) 302 { 303 str = str + managedBean.toString() + "\n " + 304 MBeanMetaHelper.descriptorToString(managedBean.getFields()); 305 } 306 AttributeInfo[] attrs = managedBean.getAttributes(); 307 str = str + "\n\n --- Attributes:"; 308 if(attrs!=null) 309 for(int i=0; i<attrs.length; i++) 310 { 311 if("modelerType".equals(attrs[i].getName())) 312 continue; 313 str = str + "\n " + attrs[i].toString(); 314 if(attrs[i].getFields().size()>0) 315 str = str + "\n " + MBeanMetaHelper.descriptorToString(attrs[i].getFields()); 316 } 317 318 OperationInfo[] opers = managedBean.getOperations(); 319 if(opers!=null && opers.length>0) 320 { 321 str = str + "\n\n --- Operations:"; 322 for(int i=0; i<opers.length; i++) 323 { 324 str = str + "\n " + opers[i].toString(); 325 if(opers[i].getFields().size()>0) 326 str = str + "\n " + MBeanMetaHelper.descriptorToString(opers[i].getFields()); 327 ParameterInfo[] params = opers[i].getSignature(); 328 if(params!=null && params.length>0) 329 { 330 str = str + "\n --- Parameters:"; 331 for(int j=0; j<params.length; j++) 332 { 333 str = str + "\n " + params[j].toString(); 334 if(params[j].getFields().size()>0) 335 str = str + "\n " + MBeanMetaHelper.descriptorToString(params[j].getFields()); 336 } 337 } 338 } 339 } 340 return str; 341 } 342 343 final static String filler=" "; 344 private String fillStr(String name, int minSize) 345 { 346 if(name.length()>=minSize) 347 return name; 348 return name+filler.substring(0, minSize-name.length()); 349 } 350 351 private String formatClassName(String className, int minSize) 352 { 353 if(className==null || className.length()==0) 354 className = "void"; 355 else 356 { 357 int iLast = className.lastIndexOf('.'); 358 boolean bArray = className.startsWith("["); 359 if(iLast>=0) 360 className = className.substring(iLast+1); 361 if(bArray) 362 className = className.substring(0, className.length()-1)+"[]"; 363 } 364 return fillStr(className, minSize); 365 } 366 367 public String toFormatString() 368 { 369 String str = "\n\n*************** mbean type:"+managedBean.getName()+" ***************"; 370 String wrk; 371 str += "\n--- ObjectName ="+ fields.get(OBJECTNAME_FIELD_NAME); 372 str += "\n--- XPath ="+ fields.get(XPATH_FIELD_NAME); 373 AttributeInfo[] attrs = managedBean.getAttributes(); 374 str = str + "\n--- Attributes:"; 375 if(attrs!=null) 376 for(int i=0; i<attrs.length; i++) 377 { 378 if("modelerType".equals(attrs[i].getName())) 379 continue; 380 str = str + "\n " + formatClassName(attrs[i].getType(), 12)+" "+attrs[i].getName()+","; 381 if(attrs[i].getDescription()!=null) 382 str = str + str + " // "+ attrs[i].getDescription(); 383 } 384 385 OperationInfo[] opers = managedBean.getOperations(); 386 if(opers!=null && opers.length>0) 387 { 388 str = str + "\n--- Operations:"; 389 for(int i=0; i<opers.length; i++) 390 { 391 wrk = formatClassName(opers[i].getReturnType(), 14) + " " + opers[i].getName() + "("; 392 str = str + "\n"+wrk; 393 wrk = fillStr("\n", wrk.length()+1); 395 ParameterInfo[] params = opers[i].getSignature(); 396 if(params!=null && params.length>0) 397 { 398 for(int j=0; j<params.length; j++) 399 { 400 if(j!=0) 401 str += wrk; 402 str = str + formatClassName(params[j].getType(), 0) + " " + params[j].getName(); 403 } 404 } 406 409 str = str + ");"; 410 if(opers[i].getDescription()!=null) 411 str = str + str + " // "+ opers[i].getDescription(); 412 } 413 } 414 return str; 415 } 416 417 static private HashMap convertListOfFieldsToHashMap(List fields) 419 { 420 Iterator iter = fields.iterator(); 421 HashMap fldsMap = new HashMap (); 422 while(iter.hasNext()) 423 { 424 FieldInfo field = (FieldInfo)iter.next(); 425 fldsMap.put(field.getName(), field.getValue()); 426 } 427 return fldsMap; 428 } 429 430 static private Set convertDelimListToSet(String list) 432 { 433 HashSet set = new HashSet (); 434 StringTokenizer tokenizer = new StringTokenizer (list, ", "); 435 while(tokenizer.hasMoreTokens()) 436 { 437 String token = tokenizer.nextToken(); 438 if(token.length()>0) set.add(token); 440 } 441 return set; 442 } 443 444 public String getName() 446 { 447 if(managedBean!=null) 448 return managedBean.getName(); 449 return null; 450 } 451 public MBeanNamingDescriptor getNamingDescriptor() 453 { 454 return namingDescriptor; 455 } 456 public boolean isObjectNamePatternMatch(Hashtable ht) 458 { 459 if(namingDescriptor!=null) 460 return namingDescriptor.isObjectNamePatternMatch(ht); 461 return false; 462 } 463 464 public boolean isObjectNamePatternMatch(ObjectName objectName) 466 { 467 if(namingDescriptor!=null) 468 return namingDescriptor.isObjectNamePatternMatch(objectName); 469 return false; 470 } 471 472 public String [] getAttributeNames() 474 { 475 AttributeInfo[] infos = managedBean.getAttributes(); 476 if(infos==null) 477 return (new String [0]); 478 String [] names = new String [infos.length]; 479 for(int i=0; i<infos.length; i++) 480 names[i] = infos[i].getName(); 481 return names; 482 } 483 public void mergeAttribute( String attrName, String attrType, 485 String attrGetMethod, String attrSetMethod, 486 boolean attrReadable, boolean attrWriteable) 487 { 488 AttributeInfo ai = findAttributeInfo(attrName); 489 if(ai==null) 490 { 491 String str[] = new String [2]; 492 ai = new AttributeInfo(); 493 ai.setName(attrName); 494 ai.setType(attrType); 495 managedBean.addAttribute(ai); 496 } 497 if(ai.isReadable() && !attrReadable) 498 ai.setReadable(false); 499 if(ai.isWriteable() && !attrWriteable) 500 ai.setWriteable(false); 501 if(attrGetMethod!=null) 502 ai.setGetMethod(attrGetMethod); 503 if(attrSetMethod!=null) 504 ai.setSetMethod(attrSetMethod); 505 } 506 507 508 private FeatureInfo findFeatureInfo(String attrName, FeatureInfo[] features) 510 { 511 if(attrName==null) 512 return null; 513 for(int i=0; i<features.length; i++) 514 { 515 if(attrName.equals(features[i].getName())) 516 return features[i]; 517 } 518 return null; 519 } 520 521 private AttributeInfo findAttributeInfo(String attrName) 523 { 524 return (AttributeInfo)findFeatureInfo(attrName, (FeatureInfo[]) managedBean.getAttributes()); 525 } 526 527 public String getElementChangeEventName() 532 { 533 return (String )this.fields.get(ELEMENTCHANGEEVENT_FIELD_NAME); 534 } 535 536 public Set getDynamicallyReconfigurableAttributes() 541 { 542 return (Set )this.fields.get(DYNAMICALLY_RECONFIGURABLE_LIST_FIELD_NAME); 543 } 544 545 private boolean checkItemsInReconfigurableSet(String name1, String name2) 547 { 548 Set set = getDynamicallyReconfigurableAttributes(); 549 if(set==null || set.size()<1) 550 return false; 551 if(set.contains("**") || 552 set.contains(name1) || 553 (name2!=null && set.contains(name2))) 554 return true; 555 return false; 556 } 557 public boolean isAttributeDynamicallyReconfigurable(String attr) 559 { 560 return checkItemsInReconfigurableSet(attr, "*"); 561 } 562 public boolean isPropertyDynamicallyReconfigurable(String attr) 564 { 565 return checkItemsInReconfigurableSet("property."+attr, "property.*"); 566 } 567 public boolean isElementCreationDynamicallyReconfigurable() 569 { 570 return checkItemsInReconfigurableSet("+", null); 571 } 572 public boolean isElementDeletionDynamicallyReconfigurable() 574 { 575 return checkItemsInReconfigurableSet("-", null); 576 } 577 public boolean isElementDynamicallyReconfigurable() 579 { 580 return checkItemsInReconfigurableSet("**", null); 581 } 582 583 public boolean isAttributeEmptyValueAllowed(String attrName) throws MBeanMetaException 589 { 590 AttributeInfo ai = findAttributeInfo(attrName); 591 if(ai==null) 592 throw new MBeanMetaException("Attribute info is not founmd for attribute "+attrName); 593 List fields = ai.getFields(); 595 if(fields!=null) 596 for(int i=0; i<fields.size(); i++) { 598 if(EMPTYVALUEALLOWED_FIELD_NAME.equals(((FieldInfo)fields.get(i)).getName())) 599 { 600 Object value = ((FieldInfo)fields.get(i)).getValue(); 602 if(value!=null && (value instanceof String ) && 603 ("true".equalsIgnoreCase((String )value)|| "yes".equalsIgnoreCase((String )value))) 604 return true; } 606 } 607 return false; } 609 610 public ModelMBeanInfo createMBeanInfo(MBeanNamingInfo namingInfo, String domainName) throws Exception 612 { 613 managedBean.setDescription(managedBean.getDescription()); 615 616 ModelMBeanInfo info = managedBean.createMBeanInfo(); 617 Descriptor descr = info.getMBeanDescriptor(); 618 if(domainName!=null) 619 descr.setField(DOMAIN_FIELD_NAME, domainName); 620 if(namingInfo!=null) 621 { 622 descr.setField(NMTYPE_FIELD_NAME, namingInfo.getType()); 623 descr.setField(NMLOCATION_FIELD_NAME, namingInfo.getLocationParams()); 624 } 625 info.setMBeanDescriptor(descr); 626 return info; 627 } 628 629 public BaseAdminMBean instantiateMBean(ObjectName objectName, Object managedResource, ConfigContext ctx) 631 throws Exception 632 { 633 String domainName = objectName.getDomain(); 634 return instantiateMBean(new MBeanNamingInfo(namingDescriptor, objectName), managedResource, domainName, ctx); 635 } 636 637 public BaseAdminMBean instantiateMBean(String type, String [] location, Object managedResource, String domainName, ConfigContext ctx) 639 throws Exception 640 { 641 return instantiateMBean(new MBeanNamingInfo(namingDescriptor, type, location), managedResource, domainName, ctx); 642 } 643 644 private BaseAdminMBean instantiateMBean(MBeanNamingInfo namingInfo, Object managedResource, String domainName, ConfigContext ctx) 646 throws Exception 647 { 648 if(managedBean==null) 649 return null; 650 ModelMBeanInfo mbi = createMBeanInfo(namingInfo, domainName); 651 String clazz = managedBean.getClassName(); 653 String group = managedBean.getGroup(); 654 655 Constructor constructor = Class.forName(clazz).getConstructor(); 656 BaseAdminMBean mbean = (BaseAdminMBean)constructor.newInstance(); 657 mbean.setModelMBeanInfo(mbi); 658 659 String xpath = namingInfo.getXPath(); 661 if("config".equals(group)) 662 { 663 if(xpath!=null && xpath.length()>0) 664 { 665 Object configBean = (Object )ConfigBeansFactory.getConfigBeanByXPath(ctx, xpath); 666 if(configBean!=null) 667 mbean.setManagedResource(configBean, CONFIG_BEAN_REF); 668 else 669 throw new MBeanMetaException("instantiateMBean(): config mbean does not exist for xpath - " + xpath ); 670 } 671 return mbean; 672 } 673 if( "runtime".equals(group)) 674 { 675 if(xpath!=null && xpath.length()>0) 677 { 678 Object configBean = (Object )ConfigBeansFactory.getConfigBeanByXPath(ctx, xpath); 679 if(configBean!=null) 680 mbean.setManagedResource(configBean, CONFIG_BEAN_REF); 681 } 682 683 684 String beanClassName = (String )fields.get(JSR77BEAN_FIELD_NAME); 687 if(beanClassName==null || beanClassName.length()==0) 688 beanClassName = "com.sun.enterprise.management.model."+namingInfo.getType()+"Mdl"; 689 690 if(managedResource==null) 691 { 692 Class cl = Class.forName(beanClassName); 693 if(cl!=null) 694 { 695 String [] location = namingInfo.getLocationParams(); 696 if(location!=null) 697 { 698 Constructor ctr = cl.getConstructor(new Class []{location.getClass()}); 699 if(ctr!=null) 700 managedResource = ctr.newInstance(new Object []{location}); 701 702 } 703 } 704 } 705 if(managedResource!=null) 706 mbean.setManagedResource(managedResource, JSR77_MODEL_BEAN_REF); 707 return mbean; 708 709 } 710 return null; 711 } 712 public String getElementPrintName() 713 { 714 return (String )fields.get(PRINTNAME_FIELD_NAME); 715 } 716 } 717 718 | Popular Tags |