In "Creating an MBean (Method 2)", we rely totally on the MBean server to create and access an MBean. The code example in that section demonstrates how to get attributes and invoke operations through the MBean server. Here we will concentrate on the usage of MBean descriptor classes when accessing MBeans representing resources.
We will rely on the StandardAgent and DynamicAgent classes presented in Chapter 1, Standard MBeans and Chapter 2, Dynamic MBeans. As mentioned in "Comparison with the SimpleStandard Example", the two are nearly identical. We examine the MBean descriptor method that is common to both: the same code works for any registered MBean, whether standard or dynamic.
private MBeanServer server = null; // assigned by MBeanServerFactory private void printMBeanInfo(ObjectName name) { println("Getting the management information for " + name.toString() ); MBeanInfo info = null; try { info = server.getMBeanInfo(name); } catch (Exception e) { e.printStackTrace(); return; } println("\nCLASSNAME: \t"+ info.getClassName()); println("\nDESCRIPTION: \t"+ info.getDescription()); println("\nATTRIBUTES"); MBeanAttributeInfo[] attrInfo = info.getAttributes(); if (attrInfo.length>0) { for(int i=0; i<attrInfo.length; i++) { println(" ** NAME: \t"+ attrInfo[i].getName()); println(" DESCR: \t"+ attrInfo[i].getDescription()); println(" TYPE: \t"+ attrInfo[i].getType() + "\tREAD: "+ attrInfo[i].isReadable() + "\tWRITE: "+ attrInfo[i].isWritable()); } } else println(" ** No attributes **"); println("\nCONSTRUCTORS"); MBeanConstructorInfo[] constrInfo = info.getConstructors(); // Note: the class necessarily has at least one constructor for(int i=0; i<constrInfo.length; i++) { println(" ** NAME: \t"+ constrInfo[i].getName()); println(" DESCR: \t"+ constrInfo[i].getDescription()); println(" PARAM: \t"+ constrInfo[i].getSignature().length + " parameter(s)"); } println("\nOPERATIONS"); MBeanOperationInfo[] opInfo = info.getOperations(); if (opInfo.length>0) { for(int i=0; i<constrInfo.length; i++) { println(" ** NAME: \t"+ opInfo[i].getName()); println(" DESCR: \t"+ opInfo[i].getDescription()); println(" PARAM: \t"+ opInfo[i].getSignature().length + " parameter(s)"); } } else println(" ** No operations ** "); println("\nNOTIFICATIONS"); MBeanNotificationInfo[] notifInfo = info.getNotifications(); if (notifInfo.length>0) { for(int i=0; i<constrInfo.length; i++) { println(" ** NAME: \t"+ notifInfo[i].getName()); println(" DESCR: \t"+ notifInfo[i].getDescription()); } } else println(" ** No notifications **"); } |
The getMBeanInfo method of the MBean server gets the descriptor of an MBean's management interface and hides the MBean's implementation. This method returns an MBeanInfo object which contains the MBean's description. We can also get the lists of attributes, operations, constructors, and notifications to display their descriptions. Recall that the dynamic MBean provides its own meaningful descriptions and that those of the standard MBean are default strings provided by the introspection mechanism of the MBean server.