1 22 package org.jboss.mx.metadata; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import javax.management.Descriptor ; 29 import javax.management.MBeanAttributeInfo ; 30 import javax.management.MBeanConstructorInfo ; 31 import javax.management.MBeanException ; 32 import javax.management.MBeanInfo ; 33 import javax.management.MBeanNotificationInfo ; 34 import javax.management.MBeanOperationInfo ; 35 import javax.management.MBeanParameterInfo ; 36 import javax.management.modelmbean.DescriptorSupport ; 37 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 38 import javax.management.modelmbean.ModelMBeanConstructorInfo ; 39 import javax.management.modelmbean.ModelMBeanInfo ; 40 import javax.management.modelmbean.ModelMBeanInfoSupport ; 41 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 42 import javax.management.modelmbean.ModelMBeanOperationInfo ; 43 import org.jboss.mx.modelmbean.ModelMBeanConstants; 44 import org.jboss.mx.server.MethodMapper; 45 46 53 public class MBeanInfoConversion 54 implements ModelMBeanConstants 55 { 56 61 public static ModelMBeanInfoSupport toModelMBeanInfo(MBeanInfo info) 62 { 63 return toModelMBeanInfo(info, true); 64 } 65 66 78 public static ModelMBeanInfoSupport toModelMBeanInfo(MBeanInfo info, boolean createAttributeOperationMapping) 79 { 80 if (info instanceof ModelMBeanInfoSupport ) 81 return (ModelMBeanInfoSupport )info; 82 83 if (info instanceof ModelMBeanInfo ) 84 return new ModelMBeanInfoSupport ((ModelMBeanInfo )info); 85 86 MBeanAttributeInfo [] attributes = info.getAttributes(); 88 ModelMBeanAttributeInfo [] mmbAttributes = new ModelMBeanAttributeInfo [attributes.length]; 89 List accessorOperations = new ArrayList (); 90 91 for (int i = 0; i < attributes.length; ++i) 92 { 93 ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo ( 95 attributes[i].getName(), 96 attributes[i].getType(), 97 attributes[i].getDescription(), 98 attributes[i].isReadable(), 99 attributes[i].isWritable(), 100 attributes[i].isIs() 101 ); 102 103 Descriptor d = attrInfo.getDescriptor(); 105 d.setField(CURRENCY_TIME_LIMIT, CACHE_NEVER); 106 attrInfo.setDescriptor(d); 107 108 mmbAttributes[i] = attrInfo; 109 110 if (createAttributeOperationMapping) 115 { 116 String getterOperationName = null; 117 String setterOperationName = null; 118 Descriptor getterDescriptor = null; 119 Descriptor setterDescriptor = null; 120 121 if (attributes[i].isReadable()) 123 { 124 if (attributes[i].isIs()) 125 getterOperationName = "is" + attributes[i].getName(); 126 else 127 getterOperationName = "get" + attributes[i].getName(); 128 129 getterDescriptor = new DescriptorSupport (); 131 getterDescriptor.setField(NAME, getterOperationName); 132 getterDescriptor.setField(DESCRIPTOR_TYPE, OPERATION_DESCRIPTOR); 133 getterDescriptor.setField(ROLE, ROLE_GETTER); 134 135 ModelMBeanOperationInfo opInfo = new ModelMBeanOperationInfo ( 137 getterOperationName, 138 "Read accessor operation for '" + attributes[i].getName() + "' attribute.", 139 new MBeanParameterInfo [0], attributes[i].getType(), MBeanOperationInfo.INFO, getterDescriptor 143 ); 144 145 Descriptor attrDescriptor = mmbAttributes[i].getDescriptor(); 148 attrDescriptor.setField(GET_METHOD, getterOperationName); 149 mmbAttributes[i].setDescriptor(attrDescriptor); 150 151 accessorOperations.add(opInfo); 152 } 153 154 if (attributes[i].isWritable()) 156 { 157 setterOperationName = "set" + attributes[i].getName(); 158 159 setterDescriptor = new DescriptorSupport (); 161 setterDescriptor.setField(NAME, setterOperationName); 162 setterDescriptor.setField(DESCRIPTOR_TYPE, OPERATION_DESCRIPTOR); 163 setterDescriptor.setField(ROLE, ROLE_SETTER); 164 165 ModelMBeanOperationInfo opInfo = new ModelMBeanOperationInfo ( 167 setterOperationName, 168 "Write accessor operation for '" + attributes[i].getName() + "' attribute.", 169 170 new MBeanParameterInfo [] { 171 new MBeanParameterInfo ("value", attributes[i].getType(), "Attribute's value.") 172 }, 173 174 Void.TYPE.getName(), 175 MBeanOperationInfo.ACTION, 176 setterDescriptor 177 ); 178 179 Descriptor attrDescriptor = mmbAttributes[i].getDescriptor(); 182 attrDescriptor.setField(SET_METHOD, setterOperationName); 183 mmbAttributes[i].setDescriptor(attrDescriptor); 184 185 accessorOperations.add(opInfo); 186 } 187 } 188 } 189 190 MBeanOperationInfo [] operations = info.getOperations(); 192 ModelMBeanOperationInfo [] mmbOperations = new ModelMBeanOperationInfo [operations.length + accessorOperations.size()]; 193 194 for (int i = 0; i < operations.length; ++i) 195 { 196 mmbOperations[i] = new ModelMBeanOperationInfo ( 197 operations[i].getName(), 198 operations[i].getDescription(), 199 operations[i].getSignature(), 200 operations[i].getReturnType(), 201 operations[i].getImpact() 202 ); 203 } 204 205 for (int i = operations.length; i < mmbOperations.length; ++i) 206 mmbOperations[i] = (ModelMBeanOperationInfo )accessorOperations.get(i - operations.length); 207 208 MBeanConstructorInfo [] constructors = info.getConstructors(); 210 ModelMBeanConstructorInfo [] mmbConstructors = new ModelMBeanConstructorInfo [constructors.length]; 211 212 for (int i = 0; i < constructors.length; ++i) 213 { 214 mmbConstructors[i] = new ModelMBeanConstructorInfo ( 215 constructors[i].getName(), 216 constructors[i].getDescription(), 217 constructors[i].getSignature() 218 ); 219 } 220 221 223 MBeanNotificationInfo [] notifications = info.getNotifications(); 228 ModelMBeanNotificationInfo [] mmbNotifications = new ModelMBeanNotificationInfo [notifications.length]; 229 230 for (int i = 0; i < notifications.length; ++i) 231 { 232 mmbNotifications[i] = new ModelMBeanNotificationInfo ( 233 notifications[i].getNotifTypes(), 234 notifications[i].getName(), 235 notifications[i].getDescription() 236 ); 237 } 238 239 return new ModelMBeanInfoSupport (info.getClassName(), info.getDescription(), 240 mmbAttributes, mmbConstructors, mmbOperations, mmbNotifications); 241 } 242 243 250 public static ModelMBeanInfoSupport stripAttributeOperations(ModelMBeanInfo info, boolean stripAllRoles) throws MBeanException 252 { 253 HashMap opsMap = new HashMap (); 254 ModelMBeanOperationInfo [] operations = (ModelMBeanOperationInfo []) info.getOperations(); 255 256 for (int i = 0; i < operations.length; i++) 257 { 258 opsMap.put(MethodMapper.operationSignature(operations[i]), operations[i]); 259 } 260 261 ModelMBeanAttributeInfo [] attributes = (ModelMBeanAttributeInfo []) info.getAttributes(); 262 263 for (int i = 0; i < attributes.length; i++) 264 { 265 if (attributes[i].isReadable() && (attributes[i].getDescriptor().getFieldValue("getMethod") != null)) 266 { 267 String key = MethodMapper.getterSignature(attributes[i]); 268 ModelMBeanOperationInfo opinfo = (ModelMBeanOperationInfo ) opsMap.get(key); 269 String role = (String ) opinfo.getDescriptor().getFieldValue("role"); 270 if ("getter".equals(role) || stripAllRoles) 271 { 272 opsMap.remove(key); 273 } 274 } 275 276 if (attributes[i].isWritable() && (attributes[i].getDescriptor().getFieldValue("setMethod") != null)) 277 { 278 String key = MethodMapper.setterSignature(attributes[i]); 279 ModelMBeanOperationInfo opinfo = (ModelMBeanOperationInfo ) opsMap.get(key); 280 281 String role = (String ) opinfo.getDescriptor().getFieldValue("role"); 282 if ("setter".equals(role) || stripAllRoles) 283 { 284 opsMap.remove(key); 285 } 286 } 287 } 288 289 operations = new ModelMBeanOperationInfo [opsMap.size()]; 290 int position = 0; 291 for (Iterator iterator = opsMap.values().iterator(); iterator.hasNext(); position++) 292 { 293 operations[position] = (ModelMBeanOperationInfo ) iterator.next(); 294 } 295 296 return new ModelMBeanInfoSupport ( 297 info.getClassName(), info.getDescription(), 298 (ModelMBeanAttributeInfo []) info.getAttributes(), 299 (ModelMBeanConstructorInfo []) info.getConstructors(), 300 operations, 301 (ModelMBeanNotificationInfo []) info.getNotifications(), 302 info.getMBeanDescriptor() 303 ); 304 } 305 } 306 | Popular Tags |