1 22 package org.jboss.mx.server; 23 24 import java.lang.reflect.Method ; 25 import java.util.HashMap ; 26 27 import javax.management.MBeanAttributeInfo ; 28 import javax.management.MBeanOperationInfo ; 29 import javax.management.MBeanParameterInfo ; 30 import javax.management.Descriptor ; 31 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 32 import org.jboss.mx.modelmbean.ModelMBeanConstants; 33 34 44 public class MethodMapper 45 { 46 48 private HashMap map = null; 49 50 52 55 public static String getterSignature(MBeanAttributeInfo info) 56 { 57 if (null == info) 58 { 59 throw new IllegalArgumentException ("MBeanAttributeInfo cannot be null"); 60 } 61 62 String sig = null; 63 if( info instanceof ModelMBeanAttributeInfo ) 64 { 65 ModelMBeanAttributeInfo minfo = (ModelMBeanAttributeInfo ) info; 66 Descriptor desc = minfo.getDescriptor(); 67 String methodName = (String ) desc.getFieldValue(ModelMBeanConstants.GET_METHOD); 68 if( methodName != null ) 69 sig = methodSignature(info.getType(), methodName, null); 70 } 71 if( sig == null ) 72 { 73 String prefix = (info.isIs()) ? "is" : "get"; 74 sig = methodSignature(info.getType(), prefix + info.getName(), null); 75 } 76 return sig; 77 } 78 79 82 public static String setterSignature(MBeanAttributeInfo info) 83 { 84 if (null == info) 85 { 86 throw new IllegalArgumentException ("MBeanAttributeInfo cannot be null"); 87 } 88 89 String sig = null; 90 if( info instanceof ModelMBeanAttributeInfo ) 91 { 92 ModelMBeanAttributeInfo minfo = (ModelMBeanAttributeInfo ) info; 93 Descriptor desc = minfo.getDescriptor(); 94 String methodName = (String ) desc.getFieldValue(ModelMBeanConstants.SET_METHOD); 95 String [] typeSig = {info.getType()}; 96 if( methodName != null ) 97 sig = methodSignature(Void.TYPE.getName(), methodName, typeSig); 98 } 99 if( sig == null ) 100 { 101 String [] typeSig = {info.getType()}; 102 sig = methodSignature(Void.TYPE.getName(), "set" + info.getName(), typeSig); 103 } 104 return sig; 105 } 106 107 110 public static String operationSignature(MBeanOperationInfo info) 111 { 112 if (null == info) 113 { 114 throw new IllegalArgumentException ("MBeanOperationInfo cannot be null"); 115 } 116 117 MBeanParameterInfo [] params = info.getSignature(); 118 String [] signature = new String [params.length]; 119 for (int i = 0; i < signature.length; i++) 120 { 121 signature[i] = params[i].getType(); 122 } 123 return methodSignature(info.getReturnType(), info.getName(), signature); 124 } 125 126 129 public static String methodSignature(Method method) 130 { 131 if (null == method) 132 { 133 throw new IllegalArgumentException ("Method cannot be null"); 134 } 135 136 Class [] paramtypes = method.getParameterTypes(); 137 String [] signature = new String [paramtypes.length]; 138 for (int i = 0; i < signature.length; i++) 139 { 140 signature[i] = paramtypes[i].getName(); 141 } 142 return methodSignature(method.getReturnType().getName(), method.getName(), signature); 143 } 144 145 148 public static String methodSignature(String returnType, String name, String [] signature) 149 { 150 if (null == returnType) 151 { 152 throw new IllegalArgumentException ("returnType cannot be null"); 153 } 154 if (null == name) 155 { 156 throw new IllegalArgumentException ("method name cannot be null"); 157 } 158 159 StringBuffer buf = new StringBuffer (returnType).append(';').append(name); 160 if (null == signature) 161 { 162 return buf.toString(); 163 } 164 165 for (int i = 0; i < signature.length; i++) 166 { 167 buf.append(';').append(signature[i]); } 169 170 return buf.toString(); 171 } 172 173 179 public static Method lookupOperation(MBeanOperationInfo info, Object mbean) 180 { 181 Class mbeanClass = mbean.getClass(); 182 Method m = null; 183 try 184 { 185 ClassLoader loader = mbeanClass.getClassLoader(); 186 MBeanParameterInfo [] params = info.getSignature(); 187 Class [] signature = new Class [params.length]; 188 for (int i = 0; i < signature.length; i++) 189 { 190 Class type = loader.loadClass(params[i].getType()); 191 signature[i] = type; 192 } 193 m = mbeanClass.getMethod(info.getName(), signature); 194 if (Object .class.equals(m.getDeclaringClass())) 196 m = null; 197 } 198 catch(Exception e) 199 { 200 } 201 202 return m; 203 } 204 205 207 210 public MethodMapper(Class resourceClass) 211 { 212 map = createMap(resourceClass); 213 } 214 215 216 218 221 public Method lookupOperation(MBeanOperationInfo info) 222 { 223 if (null == info) 224 { 225 throw new IllegalArgumentException ("MBeanOperationInfo cannot be null"); 226 } 227 228 return (Method ) map.get(operationSignature(info)); 229 } 230 231 234 public Method lookupGetter(MBeanAttributeInfo info) 235 { 236 if (null == info) 237 { 238 throw new IllegalArgumentException ("MBeanAttributeInfo cannot be null"); 239 } 240 241 return (Method ) map.get(getterSignature(info)); 242 } 243 244 247 public Method lookupSetter(MBeanAttributeInfo info) 248 { 249 if (null == info) 250 { 251 throw new IllegalArgumentException ("MBeanAttributeInfo cannot be null"); 252 } 253 254 return (Method ) map.get(setterSignature(info)); 255 } 256 257 260 public Method lookupMethod(String returnType, String name, String [] signature) 261 { 262 if (null == returnType) 263 { 264 throw new IllegalArgumentException ("returnType cannot be null"); 265 } 266 267 if (null == name) 268 { 269 throw new IllegalArgumentException ("method name cannot be null"); 270 } 271 272 return (Method ) map.get(methodSignature(returnType, name, signature)); 273 } 274 275 public String toString() 276 { 277 return map.toString(); 278 } 279 280 282 285 protected HashMap createMap(Class resourceClass) 286 { 287 HashMap cmap = new HashMap (); 288 if (resourceClass != null) 289 { 290 Method [] methods = resourceClass.getMethods(); 291 for (int i = 0; i < methods.length; i++) 292 { 293 Method method = methods[i]; 294 cmap.put(methodSignature(method), method); 295 } 296 } 297 return cmap; 298 } 299 300 } 301 | Popular Tags |