1 22 package org.xsocket; 23 24 import java.lang.reflect.Method ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import javax.management.Attribute ; 30 import javax.management.AttributeList ; 31 import javax.management.AttributeNotFoundException ; 32 import javax.management.DynamicMBean ; 33 import javax.management.InvalidAttributeValueException ; 34 import javax.management.MBeanAttributeInfo ; 35 import javax.management.MBeanException ; 36 import javax.management.MBeanInfo ; 37 import javax.management.ReflectionException ; 38 39 40 48 public final class IntrospectionBasedDynamicBean implements DynamicMBean { 49 50 private Object obj = null; 51 52 private final Map <String , Info > properties = new HashMap <String , Info >(); 53 54 59 public IntrospectionBasedDynamicBean(Object obj) { 60 this.obj = obj; 61 } 62 63 64 67 public Object getAttribute(String attribute) throws AttributeNotFoundException , MBeanException , ReflectionException { 68 String methodName = "get" + attribute; 69 try { 70 Method method = getMethod(obj.getClass(), methodName, new Class [0]); 71 method.setAccessible(true); 72 return method.invoke(obj, new Object [0]); 73 } catch (Exception e) { 74 e.printStackTrace(); 75 throw new ReflectionException (e); 76 } 77 } 78 79 @SuppressWarnings ("unchecked") 80 private Method getMethod(Class clazz, String methodname, Class [] params) { 81 do { 82 try { 83 Method method = clazz.getDeclaredMethod(methodname, params); 84 method.setAccessible(true); 85 return method; 86 } catch (Exception ignore) { } 87 88 for (Class interf : clazz.getInterfaces()) { 89 getMethod(interf, methodname, params); 90 } 91 92 clazz = clazz.getSuperclass(); 93 94 } while (clazz != null); 95 96 return null; 97 } 98 99 100 103 public AttributeList getAttributes(String [] attributes) { 104 AttributeList list = new AttributeList (); 105 for (String attribute : attributes) { 106 try { 107 list.add(new Attribute (attribute, getAttribute(attribute))); 108 } catch (Exception ignore) { } 109 } 110 return list; 111 } 112 113 114 117 public void setAttribute(Attribute attribute) throws AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException { 118 String methodName = "set" + attribute.getName(); 119 120 Info info = getInfo(attribute.getName()); 121 122 try { 123 Method method = getMethod(obj.getClass(), methodName, new Class [] { info.propertyType }); 124 method.setAccessible(true); 125 method.invoke(obj, new Object [] { attribute.getValue() }); 126 } catch (Exception e) { 127 throw new ReflectionException (e); 128 } 129 } 130 131 132 135 public AttributeList setAttributes(AttributeList attributes) { 136 AttributeList result = new AttributeList (); 137 138 Attribute [] attrs = (Attribute []) attributes.toArray(new Attribute [attributes.size()]); 139 for (Attribute attr : attrs) { 140 try { 141 setAttribute(attr); 142 result.add(new Attribute (attr.getName(), attr.getValue())); 143 } catch (Exception ignore) { } 144 } 145 return result; 146 } 147 148 149 150 153 public Object invoke(String actionName, Object [] params, String [] signature) throws MBeanException , ReflectionException { 154 return null; 156 } 157 158 159 162 public synchronized MBeanInfo getMBeanInfo() { 163 164 analyze(obj); 165 166 String [] attributes = properties.keySet().toArray(new String [properties.size()]); 167 MBeanAttributeInfo [] attrs = new MBeanAttributeInfo [attributes.length]; 168 for (int i = 0; i < attrs.length; i++) { 169 attrs[i] = properties.get(attributes[i]).asbMBeanAttributeInfo(); 170 } 171 172 173 return new MBeanInfo ( 174 obj.getClass().getName(), 175 "", 176 attrs, 177 null, null, 179 null); } 181 182 183 private void analyze(Object obj) { 184 Class clazz = obj.getClass(); 185 do { 186 analyzeType(clazz); 187 188 for (Class interf: clazz.getInterfaces()) { 189 analyzeType(interf); 190 } 191 192 clazz = clazz.getSuperclass(); 193 194 } while (clazz != null); 195 } 196 197 private void analyzeType(Class clazz) { 198 for (Method method : clazz.getDeclaredMethods()) { 199 String name = method.getName(); 200 201 if ((name.length() > 3) && name.startsWith("get")) { 202 if (method.getParameterTypes().length == 0) { 203 Class propertyType = method.getReturnType(); 204 205 if(isAcceptedPropertyType(propertyType)) { 206 Info info = getInfo(name.substring(3, name.length())); 207 info.isReadable = true; 208 info.propertyType = propertyType; 209 } 210 } 211 } 212 213 if ((name.length() > 3) && name.startsWith("set")) { 214 if (method.getParameterTypes().length == 1) { 215 Class propertyType = method.getParameterTypes()[0]; 216 217 if(isAcceptedPropertyType(propertyType)) { 218 Info info = getInfo(name.substring(3, name.length())); 219 info.isWriteable = true; 220 info.propertyType = propertyType; 221 } 222 } 223 } 224 } 225 } 226 227 228 private Info getInfo(String name) { 229 Info info = properties.get(name); 230 if (info == null) { 231 info = new Info (); 232 info.propertyName = name; 233 info.propertyDescription = "Property " + info.propertyName; 234 properties.put(name, info); 235 } 236 return info; 237 } 238 239 240 241 242 @SuppressWarnings ("unchecked") 243 private boolean isAcceptedPropertyType(Class clazz) { 244 if (clazz.isAssignableFrom(List .class)) { 245 return true; 246 } 247 248 String name = clazz.getName(); 249 return name.equals("int") 250 || name.equals("java.lang.Integer") 251 || name.equals("long") 252 || name.equals("java.lang.Long") 253 || name.equals("double") 254 || name.equals("java.lang.Double") 255 || name.equals("boolean") 256 || name.equals("java.lang.Boolean") 257 || name.equals("float") 258 || name.equals("java.lang.String") 259 || name.equals("java.lang.Float"); 260 } 261 262 263 private static class Info { 264 String propertyName = null; 265 Class propertyType = null; 266 String propertyDescription = null; 267 boolean isReadable = false; 268 boolean isWriteable = false; 269 boolean isIs = false; 270 271 272 MBeanAttributeInfo asbMBeanAttributeInfo() { 273 return new MBeanAttributeInfo (propertyName, propertyType.getName(), propertyDescription, isReadable, isWriteable, isIs); 274 } 275 } 276 } 277 | Popular Tags |