1 23 24 32 package com.sun.enterprise.admin.monitor; 33 34 import java.util.ArrayList ; 35 import java.util.Iterator ; 36 import java.util.HashMap ; 37 import java.util.Hashtable ; 38 import java.util.Map ; 39 import java.util.Set ; 40 import java.util.Vector ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 import javax.management.Attribute ; 45 import javax.management.AttributeList ; 46 import javax.management.AttributeNotFoundException ; 47 import javax.management.DynamicMBean ; 48 import javax.management.InstanceAlreadyExistsException ; 49 import javax.management.InstanceNotFoundException ; 50 import javax.management.InvalidAttributeValueException ; 51 import javax.management.MBeanAttributeInfo ; 52 import javax.management.MBeanConstructorInfo ; 53 import javax.management.MBeanOperationInfo ; 54 import javax.management.MBeanNotificationInfo ; 55 import javax.management.MBeanRegistrationException ; 56 import javax.management.MBeanException ; 57 import javax.management.MBeanInfo ; 58 import javax.management.MBeanServer ; 59 import javax.management.MalformedObjectNameException ; 60 import javax.management.NotCompliantMBeanException ; 61 import javax.management.ObjectName ; 62 import javax.management.ReflectionException ; 63 64 import com.sun.enterprise.admin.common.ObjectNames; 65 import com.sun.enterprise.admin.common.constant.AdminConstants; 66 import com.sun.enterprise.admin.monitor.types.MonitoredAttributeType; 67 import com.sun.enterprise.admin.common.MBeanServerFactory; 68 import com.sun.enterprise.admin.server.core.jmx.MBeanServerImpl; 69 70 import com.sun.enterprise.util.i18n.StringManager; 72 73 97 public abstract class BaseMonitorMBean implements DynamicMBean , IMonitorable { 98 99 102 static Logger logger = Logger.getLogger(AdminConstants.kLoggerName); 103 104 107 static final HashMap objectNameMap = new HashMap (); 108 109 112 private String [] attrNames = null; 113 114 117 private ObjectName objectName; 118 119 122 private String nodeName; 123 124 127 private String nodeType; 128 129 132 protected Vector childList = new Vector (); 133 134 139 protected void setObjectName(ObjectName objName) { 140 objectName = objName; 141 } 142 143 146 public ObjectName getObjectName() { 147 return objectName; 148 } 149 150 157 public String getNodeName() { 158 return nodeName; 159 } 160 161 168 protected void setNodeName(String name) { 169 nodeName = name; 170 } 171 172 177 public String getNodeType() { 178 return nodeType; 179 } 180 181 188 protected void setNodeType(String type) { 189 nodeType = type; 190 } 191 192 210 public synchronized ObjectName addChild(String name, MonitoredObjectType type, 211 BaseMonitorMBean mBean) 212 throws InstanceAlreadyExistsException , MBeanRegistrationException { 213 if (name == null || type == null || mBean == null) { 214 String str1 = (name == null) ? "Child Name (name); " : ""; 215 String str2 = (type == null) ? "Child Type (type); " : ""; 216 String str3 = (mBean == null) ? "MBean (mBean); " : ""; 217 218 String msg = localStrings.getString( "admin.monitor.null_arguments", str1, str2, str3 ); 219 throw new IllegalArgumentException ( msg ); 220 } 221 if (objectName == null || nodeName == null || nodeType == null) { 222 String msg = localStrings.getString( "admin.monitor.monitoring_mbean_not_added_to_mbeantree" ); 223 throw new IllegalStateException ( msg ); 224 } 225 boolean exists = false; 226 int size = childList.size(); 227 for (int i = 0; i < size; i++) { 228 BaseMonitorMBean m = (BaseMonitorMBean)childList.elementAt(i); 229 if (type.isSingleton()) { 230 if (m.nodeType.equals(type.getTypeName())) { 231 exists = true; 232 break; 233 } 234 } else { 235 if (m.nodeType.equals(type.getTypeName()) 236 && m.nodeName.equals(name)) { 237 exists = true; 238 break; 239 } 240 } 241 } 242 if (exists) { 243 String msg = localStrings.getString( "admin.monitor.mbean_already_exists", type, name ); 244 throw new InstanceAlreadyExistsException ( msg ); 245 } 246 if (type.isSingleton()) { 247 name = type.getTypeName(); 248 } 249 childList.add(mBean); 250 mBean.setNodeName(name); 251 mBean.setNodeType(type.getTypeName()); 252 ObjectName childObjName = getChildObjectName(objectName, type, name); 253 mBean.setObjectName(childObjName); 254 MBeanServer mbs = getMBeanServer(); 255 264 objectNameMap.put(childObjName, mBean); 265 if (type.isMonitoringEnabled()) { 266 mBean.startMonitoring(); 267 } 268 return childObjName; 269 } 270 271 274 private static ObjectName getChildObjectName(ObjectName parent, 275 MonitoredObjectType childType, String childName) 276 throws MBeanRegistrationException { 277 Hashtable props = parent.getKeyPropertyList(); 278 Hashtable newProps = (Hashtable )props.clone(); 279 String type = (String )props.get(MON_OBJTYPE); 280 String name = (String )props.get(MON_OBJNAME); 281 newProps.put(type, name); 282 newProps.put(MON_OBJTYPE, childType.getTypeName()); 283 newProps.put(MON_OBJNAME, childName); 284 ObjectName newName = null; 285 try { 286 newName = new ObjectName (parent.getDomain(), newProps); 287 } catch (MalformedObjectNameException mone) { 288 throw new MBeanRegistrationException (mone, mone.getMessage()); 289 } 290 return newName; 291 } 292 293 298 public ArrayList getChildList() { 299 ArrayList list = new ArrayList (); 300 list.addAll(childList); 301 return list; 302 } 303 304 312 public ArrayList getChildList(MonitoredObjectType type) { 313 if (type == null) { 314 String msg = localStrings.getString( "admin.monitor.null_argument_mbean_type" ); 315 throw new IllegalArgumentException ( msg ); 316 } 317 ArrayList list = new ArrayList (); 318 Iterator iter = childList.iterator(); 319 while (iter.hasNext()) { 320 BaseMonitorMBean mBean = (BaseMonitorMBean)iter.next(); 321 if (mBean.nodeType.equals(type.getTypeName())) { 322 list.add(mBean); 323 } 324 } 325 return list; 326 } 327 328 335 public ArrayList getChildList(String name) { 336 ArrayList list = new ArrayList (); 337 Iterator iter = childList.iterator(); 338 while (iter.hasNext()) { 339 BaseMonitorMBean mBean = (BaseMonitorMBean)iter.next(); 340 if (mBean.nodeName.equals(name)) { 341 list.add(mBean); 342 } 343 } 344 return list; 345 } 346 347 357 public BaseMonitorMBean getChild(MonitoredObjectType type, String name) 358 throws InstanceNotFoundException { 359 BaseMonitorMBean found = getChildOrNull(type, name); 360 if (found == null) { 361 String msg = localStrings.getString( "admin.monitor.mbean_not_found", type, name ); 362 throw new InstanceNotFoundException ( msg ); 363 } 364 return found; 365 } 366 367 374 BaseMonitorMBean getChildOrNull(MonitoredObjectType type, String name) { 375 if (type == null) { 376 String msg = localStrings.getString( "admin.monitor.monitored_object_type_null" ); 377 throw new IllegalArgumentException ( msg ); 378 } 379 BaseMonitorMBean found = null; 380 Iterator iter = childList.iterator(); 381 while (iter.hasNext()) { 382 BaseMonitorMBean mBean = (BaseMonitorMBean)iter.next(); 383 if (type.isSingleton()) { 384 if (mBean.nodeType.equals(type.getTypeName())) { 385 found = mBean; 386 } 387 } else { 388 if (mBean.nodeType.equals(type.getTypeName()) 389 && mBean.nodeName.equals(name)) { 390 found = mBean; 391 } 392 } 393 } 394 return found; 395 } 396 397 403 BaseMonitorMBean getFirstChildByName(String name) 404 throws InstanceNotFoundException { 405 ArrayList list = getChildList(name); 406 if (list.isEmpty()) { 407 String msg = localStrings.getString( "admin.monitor.child_mbean_not_available", name, objectName ); 408 throw new InstanceNotFoundException ( msg ); 409 } 410 return ((BaseMonitorMBean)list.get(0)); 411 } 412 413 426 public synchronized void removeChild(MonitoredObjectType type, String name) 427 throws InstanceNotFoundException , MBeanRegistrationException { 428 BaseMonitorMBean mBean = getChild(type, name); 429 removeChild(mBean); 430 } 431 432 443 synchronized void removeChild(BaseMonitorMBean child) 444 throws InstanceNotFoundException , MBeanRegistrationException { 445 removeAllChild(child); 446 childList.remove(child); 447 objectNameMap.remove(child.objectName); 448 MBeanServer mbs = getMBeanServer(); 449 } 452 453 462 private static void removeAllChild(BaseMonitorMBean mbean) 463 throws InstanceNotFoundException , MBeanRegistrationException { 464 Iterator iter = mbean.childList.iterator(); 465 while (iter.hasNext()) { 466 BaseMonitorMBean child = (BaseMonitorMBean)iter.next(); 467 removeAllChild(child); 468 objectNameMap.remove(child.objectName); 469 MBeanServer mbs = getMBeanServer(); 470 } 473 mbean.childList.removeAllElements(); 474 } 475 476 482 private static MBeanServer getMBeanServer() { 483 return MBeanServerFactory.getMBeanServer(); 484 } 485 486 492 public abstract Object getAttribute(String attribute) 493 throws AttributeNotFoundException ; 494 495 500 public abstract AttributeList getAttributes(String [] attributes); 501 502 510 public abstract MBeanInfo getMBeanInfo(); 511 512 515 public Object invoke(String str, Object [] obj, String [] str2) 516 throws MBeanException , ReflectionException { 517 throw new UnsupportedOperationException ( 518 getLocalString(UNSUPPORTED_ERRCODE, UNSUPPORTED_ERRMSG)); 519 } 520 521 524 public final void setAttribute(Attribute attribute) 525 throws AttributeNotFoundException , InvalidAttributeValueException , 526 MBeanException , ReflectionException { 527 throw new UnsupportedOperationException ( 528 getLocalString(UNSUPPORTED_ERRCODE, UNSUPPORTED_ERRMSG)); 529 } 530 531 534 public final AttributeList setAttributes(AttributeList attributeList) { 535 throw new UnsupportedOperationException ( 536 getLocalString(UNSUPPORTED_ERRCODE, UNSUPPORTED_ERRMSG)); 537 } 538 539 542 public abstract MonitoredAttributeType getAttributeType(String attrName); 543 544 550 public void startMonitoring() { 551 } 552 553 557 public void stopMonitoring() { 558 } 559 560 568 public abstract Map getMonitoringMetaData(); 569 570 578 public final Object getMonitoredAttributeValue(String monitorAttributeName) { 579 Object result = null; 580 try { 581 result = getAttribute(monitorAttributeName); 582 } catch (AttributeNotFoundException anfe) { 583 } 584 return result; 585 } 586 587 597 public final Map getMonitoredAttributeValues(Set monitorAttributeNameSet) { 598 return null; 599 } 600 601 616 protected static MBeanInfo createMBeanInfo(Map attrNameTypeMap) { 617 return (createMBeanInfo(attrNameTypeMap, new MBeanOperationInfo [0])); 618 } 619 620 635 protected static MBeanInfo createMBeanInfo(Map attrNameTypeMap, 636 MBeanOperationInfo [] operationInfoArray) { 637 String className = GenericMonitorMBean.class.getName(); 638 String description = "Generic Monitoring MBean"; 639 attrNameTypeMap = convertKeysToCamelCase(attrNameTypeMap); 640 Set keys = attrNameTypeMap.keySet(); 641 Iterator names = keys.iterator(); 642 MBeanAttributeInfo [] attrList = new MBeanAttributeInfo [keys.size()]; 643 int i = 0; 644 while (names.hasNext()) { 645 String name = (String )names.next(); 646 String type = ((MonitoredAttributeType)attrNameTypeMap.get(name)).getJavaTypeName(); 647 String desc = "Monitored attribute " + name; 648 attrList[i] = new MBeanAttributeInfo (name, type, desc, true, false, false); 649 i++; 650 } 651 MBeanConstructorInfo [] constructorList = new MBeanConstructorInfo [0]; 652 MBeanNotificationInfo [] notificationList = new MBeanNotificationInfo [0]; 654 return new MBeanInfo (className, description, attrList, constructorList, 655 operationInfoArray, notificationList); 656 } 657 658 665 public String [] getAllAttributeNames() { 666 return getAllAttributeNamesFromMBeanInfo(); 667 } 668 669 679 private String [] getAllAttributeNamesFromMBeanInfo() { 680 if (attrNames == null) { 681 MBeanInfo info = this.getMBeanInfo(); 682 if (info == null) { 683 String msg = localStrings.getString( "admin.monitor.null_mbean_info", this.getClass() ); 684 throw new IllegalStateException ( msg ); 685 } 686 MBeanAttributeInfo [] attrInfoList = info.getAttributes(); 687 if (attrInfoList == null) { 688 String msg = localStrings.getString( "admin.monitor.null_attribute_info_in_mbeaninfo", this.getClass() ); 689 throw new IllegalStateException ( msg ); 690 } 691 int size = attrInfoList.length; 692 attrNames = new String [size]; 693 for (int i = 0; i < size; i++) { 694 attrNames[i] = attrInfoList[i].getName(); 695 } 696 } 697 return attrNames; 698 } 699 700 715 protected static Map createAttrNameTypeMap(Object [][] attrNameTypeArray) { 716 HashMap map = new HashMap (); 717 for (int i = attrNameTypeArray.length; i > 0; i--) { 718 map.put(attrNameTypeArray[i-1][0], attrNameTypeArray[i-1][1]); 719 } 720 return map; 721 } 722 723 private static String getLocalString(String key, String dflt) { 724 return dflt; 725 } 726 727 734 private static Map convertKeysToCamelCase(Map attrNameTypeMap) { 735 Iterator keys = attrNameTypeMap.keySet().iterator(); 736 Iterator values = attrNameTypeMap.values().iterator(); 737 Map newMap = new HashMap (); 738 while(keys.hasNext()) { 739 String key = (String )keys.next(); 740 String camelCaseKey = toCamelCase(key); 741 newMap.put(camelCaseKey, values.next()); 742 } 743 return newMap; 744 } 745 746 759 private static String toCamelCase(String illegalIdentifier) { 760 final StringBuffer from = new StringBuffer (illegalIdentifier); 761 final StringBuffer to = new StringBuffer (); 762 final int length = from.length(); 763 boolean illegalFound = false; 764 for (int i = 0 ; i < length ; i++) { 765 char currentChar = from.charAt(i); 766 768 if (i == 0 && !Character.isJavaIdentifierStart(currentChar) || 769 i > 0 && !Character.isJavaIdentifierPart(currentChar)) { 770 illegalFound = true; 771 continue; 772 } 773 if (illegalFound) { 774 to.append(Character.toUpperCase(currentChar)); 775 illegalFound = false; 776 } 777 else { 778 to.append(currentChar); 779 } 780 } 781 return (to.toString()); 782 } 783 784 private static StringManager localStrings = 786 StringManager.getManager( BaseMonitorMBean.class ); 787 788 protected static final String UNSUPPORTED_ERRCODE = localStrings.getString( "admin.monitor.unsupported_mbean_monitor" ); 789 protected static final String UNSUPPORTED_ERRMSG = localStrings.getString( "admin.monitor.unsupported_action_on_monitoring_mbean" ); 790 791 private static final String MON_OBJTYPE = ObjectNames.kMonitoringClassName; 792 private static final String MON_OBJNAME = ObjectNames.kNameKeyName; 793 794 private static final String NON_COMPLIANT_MBEAN = "monitor.non_compliant_mbean"; 795 private static final String MBS_INIT_ERROR = "monitor.mbs_init_error"; 796 797 } 798 | Popular Tags |