1 package org.objectweb.celtix.bus.management.jmx.export.runtime; 2 3 import java.lang.reflect.Method ; 4 5 import javax.management.Descriptor ; 6 import javax.management.modelmbean.ModelMBeanInfo ; 7 8 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedAttribute; 9 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedNotification; 10 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedNotifications; 11 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedOperation; 12 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedOperationParameter; 13 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedOperationParameters; 14 import org.objectweb.celtix.bus.management.jmx.export.annotation.ManagedResource; 15 16 17 public class ModelMBeanAssembler { 18 private ModelMBeanInfoSupporter supporter = new ModelMBeanInfoSupporter(); 19 20 @SuppressWarnings ("unchecked") 21 public ManagedResource getManagedResource(Class clazz) { 22 return (ManagedResource)clazz.getAnnotation(ManagedResource.class); 23 } 24 25 public ManagedAttribute getManagedAttribute(Method method) { 26 return method.getAnnotation(ManagedAttribute.class); 27 } 28 29 public ManagedOperation getManagedOperation(Method method) { 30 return method.getAnnotation(ManagedOperation.class); 31 } 32 33 public ManagedOperationParameter[] getManagedOperationParameters(Method method) { 34 ManagedOperationParameters params = method.getAnnotation(ManagedOperationParameters.class); 35 ManagedOperationParameter[] result = null; 36 if (params == null) { 37 result = new ManagedOperationParameter[0]; 38 } else { 39 result = params.value(); 40 } 41 return result; 42 } 43 44 @SuppressWarnings ("unchecked") 45 public ManagedNotification[] getManagedNotifications(Class clazz) { 46 ManagedNotifications notificationsAnn = 47 (ManagedNotifications)clazz.getAnnotation(ManagedNotifications.class); 48 ManagedNotification[] result = null; 49 if (null == notificationsAnn) { 50 return new ManagedNotification[0]; 51 } 52 result = notificationsAnn.value(); 53 return result; 54 } 55 56 public String getAttributeName(String methodName) { 57 if (methodName.indexOf("set") == 0) { 58 return methodName.substring(3); 59 } 60 if (methodName.indexOf("get") == 0) { 61 return methodName.substring(3); 62 } 63 if (methodName.indexOf("is") == 0) { 64 return methodName.substring(2); 65 } 66 return null; 67 } 68 69 public static boolean checkMethod(Method [] methods, String methodName) { 70 boolean result = false; 71 for (int i = 0; i < methods.length; i++) { 72 if (methods[i].getName().compareTo(methodName) == 0) { 73 result = true; 74 break; 75 } 76 } 77 return result; 78 } 79 80 public static String getAttributeType(Method [] methods, String attributeName) { 81 String result = null; 82 String searchMethod = "get" + attributeName; 83 for (int i = 0; i < methods.length; i++) { 84 if (methods[i].getName().compareTo(searchMethod) == 0) { 85 result = methods[i].getReturnType().getName(); 86 break; 87 } 88 } 89 if (null == result) { 91 searchMethod = "is" + attributeName; 92 for (int i = 0; i < methods.length; i++) { 93 if (methods[i].getName().compareTo(searchMethod) == 0) { 94 result = methods[i].getReturnType().getName(); 95 break; 96 } 97 } 98 } 99 return result; 100 } 101 102 class ManagedAttributeInfo { 103 String fname; 104 String ftype; 105 String description; 106 boolean read; 107 boolean write; 108 boolean is; 109 }; 110 111 112 public ManagedAttributeInfo getAttributInfo(Method [] methods, 114 String attributName, 115 String attributType, 116 ManagedAttribute managedAttribute) { 117 ManagedAttributeInfo mai = new ManagedAttributeInfo(); 118 mai.fname = attributName; 119 mai.ftype = attributType; 120 mai.description = managedAttribute.description(); 121 mai.is = checkMethod(methods, "is" + attributName); 122 mai.write = checkMethod(methods, "set" + attributName); 123 124 if (mai.is) { 125 mai.read = true; 126 } else { 127 mai.read = checkMethod(methods, "get" + attributName); 128 } 129 130 return mai; 131 132 } 133 134 Method findMethodByName(Method methods[], String methodName) { 135 for (int i = 0; i < methods.length; i++) { 136 if (methods[i].getName().compareTo(methodName) == 0) { 137 return methods[i]; 138 } else { 139 continue; 140 } 141 142 } 143 return null; 144 145 } 146 147 void addAttributeOperation(Method method) { 148 Descriptor operationDescriptor = 149 supporter.buildAttributeOperationDescriptor(method.getName()); 150 151 Class <?>[] types = method.getParameterTypes(); 152 153 String [] paramTypes = new String [types.length]; 154 String [] paramNames = new String [types.length]; 155 String [] paramDescs = new String [types.length]; 156 157 for (int j = 0; j < types.length; j++) { 158 paramTypes[j] = types[j].getName(); 159 paramDescs[j] = ""; 160 paramNames[j] = types[j].getName(); 161 } 162 163 supporter.addModelMBeanMethod(method.getName(), 164 paramTypes, 165 paramNames, 166 paramDescs, 167 "", 168 method.getReturnType().getName(), 169 operationDescriptor); 170 } 171 172 public ModelMBeanInfo getModelMbeanInfo(Class clazz) { 173 supporter.clear(); 174 ManagedResource mr = getManagedResource(clazz); 175 if (mr == null) { 176 return null; 178 } 179 Descriptor mbeanDescriptor = supporter.buildMBeanDescriptor(mr); 181 182 ManagedNotification[] mns = getManagedNotifications(clazz); 184 for (int k = 0; k < mns.length; k++) { 185 supporter.addModelMBeanNotification(mns[k].notificationTypes(), 186 mns[k].name(), 187 mns[k].description(), null); 188 } 189 190 Method [] methods = clazz.getDeclaredMethods(); 191 192 for (int i = 0; i < methods.length; i++) { 193 ManagedAttribute ma = getManagedAttribute(methods[i]); 194 if (ma != null) { 196 String attributeName = getAttributeName(methods[i].getName()); 197 if (!supporter.checkAttribute(attributeName)) { 198 String attributeType = getAttributeType(methods, attributeName); 199 ManagedAttributeInfo mai = getAttributInfo(methods, 200 attributeName, 201 attributeType, 202 ma); 203 Descriptor attributeDescriptor = 204 supporter.buildAttributeDescriptor(ma, 205 attributeName, 206 mai.is, mai.read, mai.write); 207 208 supporter.addModelMBeanAttribute(mai.fname, 210 mai.ftype, 211 mai.read, 212 mai.write, 213 mai.is, 214 mai.description, 215 attributeDescriptor); 216 217 Method method; 218 if (mai.read) { 220 if (mai.is) { 221 method = findMethodByName(methods, "is" + attributeName); 222 } else { 223 method = findMethodByName(methods, "get" + attributeName); 224 } 225 addAttributeOperation(method); 226 } 227 if (mai.write) { 228 method = findMethodByName(methods, "set" + attributeName); 229 addAttributeOperation(method); 230 } 231 } 232 233 } else { 234 ManagedOperation mo = getManagedOperation(methods[i]); 236 237 if (mo != null) { 238 Class <?>[] types = methods[i].getParameterTypes(); 239 ManagedOperationParameter[] mop = getManagedOperationParameters(methods[i]); 240 String [] paramTypes = new String [types.length]; 241 String [] paramNames = new String [types.length]; 242 String [] paramDescs = new String [types.length]; 243 244 for (int j = 0; j < types.length; j++) { 245 paramTypes[j] = types[j].getName(); 246 if (j < mop.length) { 247 paramDescs[j] = mop[j].description(); 248 paramNames[j] = mop[j].name(); 249 } else { 250 paramDescs[j] = ""; 251 paramNames[j] = types[j].getName(); 252 } 253 } 254 Descriptor operationDescriptor = 255 supporter.buildOperationDescriptor(mo, methods[i].getName()); 256 supporter.addModelMBeanMethod(methods[i].getName(), 257 paramTypes, 258 paramNames, 259 paramDescs, 260 mo.description(), 261 methods[i].getReturnType().getName(), 262 operationDescriptor); 263 } 264 } 265 266 } 267 return supporter.buildModelMBeanInfo(mbeanDescriptor); 268 } 269 } 270 | Popular Tags |