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