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