1 22 package org.jboss.aspects.jmx; 23 24 import javax.management.Attribute ; 25 import javax.management.AttributeList ; 26 import javax.management.AttributeNotFoundException ; 27 import javax.management.DynamicMBean ; 28 import javax.management.InvalidAttributeValueException ; 29 import javax.management.MBeanAttributeInfo ; 30 import javax.management.MBeanException ; 31 import javax.management.MBeanInfo ; 32 import javax.management.MBeanOperationInfo ; 33 import javax.management.ReflectionException ; 34 35 import java.lang.reflect.InvocationTargetException ; 36 import java.lang.reflect.Method ; 37 import java.util.HashMap ; 38 import java.util.Iterator ; 39 40 50 public class JmxIntrospectingMixin implements DynamicMBean 51 { 52 public class OpsKey 53 { 54 private final String methodName; 55 private final String [] signature; 56 private final int hash; 57 58 public OpsKey(String methodName, String [] signature) 59 { 60 this.methodName = methodName; 61 this.signature = signature; 62 int tmp = methodName.hashCode(); 63 for (int i = 0; i < signature.length; i++) 64 { 65 tmp += signature[i].hashCode(); 66 } 67 hash = tmp; 68 } 69 70 public int hashCode() 71 { 72 return hash; 73 }; 74 public String getMethodName() 75 { 76 return methodName; 77 } 78 79 public String [] getSignature() 80 { 81 return signature; 82 } 83 84 public boolean equals(Object obj) 85 { 86 if (obj == this) return true; 87 OpsKey key = (OpsKey) obj; 88 if (key.hash != hash) return false; 89 if (!key.methodName.equals(methodName)) return false; 90 if (key.signature.length != signature.length) return false; 91 for (int i = 0; i < signature.length; i++) 92 { 93 if (!signature[i].equals(key.signature[i])) return false; 94 } 95 return true; 96 } 97 } 98 99 private final Object target; 100 private final HashMap ops = new HashMap (); 101 private final HashMap gets = new HashMap (); 102 private final HashMap sets = new HashMap (); 103 private MBeanInfo mbeanInfo; 104 105 106 public JmxIntrospectingMixin(Object target) 107 { 108 this.target = target; 109 } 110 111 public Object getAttribute(String attribute) throws AttributeNotFoundException , MBeanException , ReflectionException 112 { 113 Method get = (Method ) gets.get(attribute); 114 if (get == null) throw new AttributeNotFoundException (attribute); 115 try 116 { 117 return get.invoke(target, null); 118 } 119 catch (IllegalAccessException e) 120 { 121 throw new ReflectionException (e); 122 } 123 catch (InvocationTargetException e) 124 { 125 if (e.getTargetException() instanceof Exception ) throw new MBeanException ((Exception ) e.getTargetException()); 126 throw new MBeanException (new Exception (e.getTargetException().getMessage())); 127 } 128 } 129 130 public void setAttribute(Attribute attribute) throws AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException 131 { 132 Method set = (Method ) sets.get(attribute.getName()); 133 if (set == null) throw new AttributeNotFoundException (attribute.getName()); 134 try 135 { 136 Object [] args = {attribute.getValue()}; 137 set.invoke(target, args); 138 } 139 catch (IllegalAccessException e) 140 { 141 throw new ReflectionException (e); 142 } 143 catch (InvocationTargetException e) 144 { 145 if (e.getTargetException() instanceof Exception ) throw new MBeanException ((Exception ) e.getTargetException()); 146 throw new MBeanException (new Exception (e.getTargetException().getMessage())); 147 } 148 } 149 150 public AttributeList getAttributes(String [] attributes) 151 { 152 AttributeList list = new AttributeList (); 153 for (int i = 0; i < attributes.length; i++) 154 { 155 try 156 { 157 list.add(0, new Attribute (attributes[i], getAttribute(attributes[i]))); 158 } 159 catch (AttributeNotFoundException e) 160 { 161 throw new RuntimeException (e); 162 } 163 catch (MBeanException e) 164 { 165 throw new RuntimeException (e); 166 } 167 catch (ReflectionException e) 168 { 169 throw new RuntimeException (e); 170 } 171 } 172 return list; 173 } 174 175 public AttributeList setAttributes(AttributeList attributes) 176 { 177 for (int i = 0; i < attributes.size(); i++) 178 { 179 try 180 { 181 Attribute attr = (Attribute ) attributes.get(i); 182 setAttribute(attr); 183 } 184 catch (InvalidAttributeValueException inv) 185 { 186 throw new RuntimeException (inv); 187 } 188 catch (AttributeNotFoundException e) 189 { 190 throw new RuntimeException (e); 191 } 192 catch (MBeanException e) 193 { 194 throw new RuntimeException (e); 195 } 196 catch (ReflectionException e) 197 { 198 throw new RuntimeException (e); 199 } 200 } 201 return attributes; 202 } 203 204 public Object invoke(String actionName, Object params[], String signature[]) throws MBeanException , ReflectionException 205 { 206 OpsKey key = new OpsKey(actionName, signature); 207 Method m = (Method ) ops.get(key); 208 if (m == null) throw new NoSuchMethodError (actionName); 209 try 210 { 211 return m.invoke(target, params); 212 } 213 catch (IllegalAccessException e) 214 { 215 throw new ReflectionException (e); 216 } 217 catch (InvocationTargetException e) 218 { 219 Throwable cause = e.getTargetException(); 220 if (cause instanceof Exception ) throw new MBeanException ((Exception ) cause); 221 throw new MBeanException (new Exception (e.getMessage())); 222 } 223 } 224 225 public MBeanInfo getMBeanInfo() 226 { 227 if (mbeanInfo != null) 229 { 230 return mbeanInfo; 232 } 233 234 235 try 236 { 237 Method [] methods = target.getClass().getMethods(); 238 239 for (int i = 0; i < methods.length; i++) 241 { 242 if (methods[i].getName().equals("_getInstanceAdvisor")) continue; 243 if (methods[i].getName().equals("_setInstanceAdvisor")) continue; 244 if (methods[i].getName().equals("_getAdvisor")) continue; 245 if (methods[i].getName().equals("setAttribute")) continue; 246 if (methods[i].getName().equals("getMBeanInfo")) continue; 247 if (methods[i].getName().equals("getAttribute")) continue; 248 if (methods[i].getName().equals("setAttributes")) continue; 249 if (methods[i].getName().equals("getAttributes")) continue; 250 if (methods[i].getName().equals("invoke")) continue; 251 if (methods[i].getName().indexOf("$aop") != -1) continue; 252 if (methods[i].getDeclaringClass().equals(java.lang.Object .class)) continue; 253 if (methods[i].getName().startsWith("get") && methods[i].getParameterTypes().length == 0) 254 { 255 gets.put(methods[i].getName().substring(3), methods[i]); 257 } 258 else if (methods[i].getName().startsWith("is") && methods[i].getParameterTypes().length == 0 && 259 (methods[i].getReturnType().equals(Boolean .class) || methods[i].getReturnType().equals(boolean.class))) 260 { 261 gets.put(methods[i].getName().substring(2), methods[i]); 263 } 264 else if (methods[i].getName().startsWith("set") && methods[i].getParameterTypes().length == 1) 265 { 266 sets.put(methods[i].getName().substring(3), methods[i]); 268 } 269 else 270 { 271 String [] signature = new String [methods[i].getParameterTypes().length]; 273 for (int j = 0; j < methods[i].getParameterTypes().length; j++) 274 { 275 signature[j] = methods[i].getParameterTypes()[j].getName(); 276 } 277 ops.put(new OpsKey(methods[i].getName(), signature), methods[i]); 278 } 279 } 280 281 HashMap attributes = new HashMap (); 282 Iterator it = gets.keySet().iterator(); 283 while (it.hasNext()) 284 { 285 String attribute = (String ) it.next(); 286 Method m = (Method ) gets.get(attribute); 287 boolean isWritable = sets.containsKey(attribute); 289 boolean isIs = m.getName().startsWith("is"); 290 MBeanAttributeInfo info = new MBeanAttributeInfo (attribute, m.getReturnType().getName(), target.getClass().getName() + "." + attribute, true, isWritable, isIs); 291 attributes.put(attribute, info); 292 } 293 it = sets.keySet().iterator(); 294 while (it.hasNext()) 295 { 296 String attribute = (String ) it.next(); 297 if (gets.containsKey(attribute)) continue; 298 Method m = (Method ) sets.get(attribute); 300 MBeanAttributeInfo info = new MBeanAttributeInfo (attribute, m.getReturnType().getName(), target.getClass().getName() + "." + attribute, false, true, false); 301 attributes.put(attribute, info); 302 } 303 304 MBeanOperationInfo [] operations = new MBeanOperationInfo [ops.size()]; 305 it = ops.values().iterator(); 306 int i = 0; 307 while (it.hasNext()) 308 { 309 Method m = (Method ) it.next(); 310 operations[i++] = new MBeanOperationInfo (m.toString(), m); 311 } 312 MBeanAttributeInfo [] attrs = (MBeanAttributeInfo []) attributes.values().toArray(new MBeanAttributeInfo [attributes.size()]); 313 314 mbeanInfo = new MBeanInfo (target.getClass().getName(), target.getClass().getName(), 315 attrs, null, operations, null); 316 return mbeanInfo; 318 } 319 320 catch (Exception e) 321 { 322 e.printStackTrace(); 323 throw new RuntimeException (e); 324 } 325 } 326 } 327 | Popular Tags |