1 7 8 package test.compliance.standard; 9 10 import javax.management.InstanceAlreadyExistsException; 11 import javax.management.InstanceNotFoundException; 12 import javax.management.IntrospectionException; 13 import javax.management.MBeanAttributeInfo; 14 import javax.management.MBeanConstructorInfo; 15 import javax.management.MBeanInfo; 16 import javax.management.MBeanOperationInfo; 17 import javax.management.MBeanParameterInfo; 18 import javax.management.MBeanRegistrationException; 19 import javax.management.MBeanServer; 20 import javax.management.MBeanServerFactory; 21 import javax.management.MalformedObjectNameException; 22 import javax.management.NotCompliantMBeanException; 23 import javax.management.ObjectInstance; 24 import javax.management.ObjectName; 25 import javax.management.ReflectionException; 26 27 import junit.framework.Assert; 28 29 public class InfoUtil 30 { 31 public static MBeanInfo getMBeanInfo(Object mbean, String name) 32 { 33 MBeanInfo info = null; 34 35 try 36 { 37 MBeanServer server = MBeanServerFactory.newMBeanServer(); 38 39 ObjectName objectName = new ObjectName(name); 40 ObjectInstance instance = server.registerMBean(mbean, objectName); 41 info = server.getMBeanInfo(objectName); 42 } 43 catch (MalformedObjectNameException e) 44 { 45 Assert.fail("got spurious MalformedObjectNameException"); 46 } 47 catch (InstanceAlreadyExistsException e) 48 { 49 Assert.fail("got spurious InstanceAlreadyExistsException"); 50 } 51 catch (MBeanRegistrationException e) 52 { 53 Assert.fail("got spurious MBeanRegistrationException"); 54 } 55 catch (NotCompliantMBeanException e) 56 { 57 Assert.fail("got spurious NotCompliantMBeanException"); 58 } 59 catch (InstanceNotFoundException e) 60 { 61 Assert.fail("got spurious InstanceNotFoundException"); 62 } 63 catch (IntrospectionException e) 64 { 65 Assert.fail("got spurious IntrospectionException"); 66 } 67 catch (ReflectionException e) 68 { 69 Assert.fail("got spurious ReflectionException"); 70 } 71 72 return info; 73 } 74 75 public static MBeanAttributeInfo findAttribute(MBeanAttributeInfo[] attributes, String name) 76 { 77 for (int i = 0; i < attributes.length; i++) 78 { 79 if (attributes[i].getName().equals(name)) 80 { 81 return attributes[i]; 82 } 83 } 84 return null; 85 } 86 87 public static void dumpConstructors(MBeanConstructorInfo[] constructors) 88 { 89 System.out.println(""); 90 System.out.println("Constructors:"); 91 for (int i = 0; i < constructors.length; i++) 92 { 93 StringBuffer dump = new StringBuffer(); 94 MBeanConstructorInfo constructor = constructors[i]; 95 dump.append("name=").append(constructor.getName()); 96 dump.append(",signature=").append(makeSignatureString(constructor.getSignature())); 97 98 System.out.println(dump); 99 } 100 } 101 102 public static void dumpAttributes(MBeanAttributeInfo[] attributes) 103 { 104 System.out.println(""); 105 System.out.println("Attributes:"); 106 for (int i = 0; i < attributes.length; i++) 107 { 108 StringBuffer dump = new StringBuffer(); 109 MBeanAttributeInfo attribute = attributes[i]; 110 dump.append("name=").append(attribute.getName()); 111 dump.append(",type=").append(attribute.getType()); 112 dump.append(",readable=").append(attribute.isReadable()); 113 dump.append(",writable=").append(attribute.isWritable()); 114 dump.append(",isIS=").append(attribute.isIs()); 115 System.out.println(dump); 116 } 117 } 118 119 public static void dumpOperations(MBeanOperationInfo[] operations) 120 { 121 System.out.println(""); 122 System.out.println("Operations:"); 123 for (int i = 0; i < operations.length; i++) 124 { 125 StringBuffer dump = new StringBuffer(); 126 MBeanOperationInfo operation = operations[i]; 127 dump.append("name=").append(operation.getName()); 128 dump.append(",impact=").append(decodeImpact(operation.getImpact())); 129 dump.append(",returnType=").append(operation.getReturnType()); 130 dump.append(",signature=").append(makeSignatureString(operation.getSignature())); 131 132 System.out.println(dump); 133 } 134 } 135 136 public static String makeSignatureString(MBeanParameterInfo[] info) 137 { 138 String[] sig = new String[info.length]; 139 for (int i = 0; i < info.length; i++) 140 { 141 sig[i] = info[i].getType(); 142 } 143 return makeSignatureString(sig); 144 } 145 146 public static String makeSignatureString(String[] sig) 147 { 148 StringBuffer buf = new StringBuffer("("); 149 for (int i = 0; i < sig.length; i++) 150 { 151 buf.append(sig[i]); 152 if (i != sig.length - 1) 153 { 154 buf.append(","); 155 } 156 } 157 buf.append(")"); 158 return buf.toString(); 159 } 160 161 public static String decodeImpact(int impact) 162 { 163 switch (impact) 164 { 165 case MBeanOperationInfo.ACTION: 166 return "ACTION"; 167 case MBeanOperationInfo.ACTION_INFO: 168 return "ACTION_INFO"; 169 case MBeanOperationInfo.INFO: 170 return "INFO"; 171 case MBeanOperationInfo.UNKNOWN: 172 return "UNKNOWN"; 173 } 174 throw new IllegalArgumentException("unknown impact value:" + impact); 175 } 176 } 177 | Popular Tags |