1 22 package org.jboss.mx.util; 23 24 import java.io.Externalizable ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectOutput ; 28 import java.lang.reflect.InvocationHandler ; 29 import java.lang.reflect.Method ; 30 import java.lang.reflect.Proxy ; 31 import java.security.AccessController ; 32 import java.security.PrivilegedAction ; 33 import java.util.HashMap ; 34 35 import javax.management.Attribute ; 36 import javax.management.MBeanAttributeInfo ; 37 import javax.management.MBeanInfo ; 38 import javax.management.MBeanServer ; 39 import javax.management.MBeanServerConnection ; 40 import javax.management.MalformedObjectNameException ; 41 import javax.management.ObjectName ; 42 43 54 public class MBeanProxyExt 55 implements InvocationHandler , MBeanProxyInstance, Externalizable 56 { 57 58 private static final long serialVersionUID = -2942844863242742655L; 59 60 63 public static MBeanServerConnection remote; 64 65 68 private MBeanServerConnection server; 69 70 73 private ObjectName name; 74 75 78 private transient final HashMap attributeMap = new HashMap (); 79 82 private transient boolean inited = false; 83 84 87 public MBeanProxyExt() 88 { 89 } 90 91 94 MBeanProxyExt(final ObjectName name, final MBeanServer server, boolean lazyInit) 95 { 96 this.name = name; 97 this.server = server; 98 if (lazyInit == false) 99 init(); 100 } 101 102 105 private static final Object EMPTY_ARGS[] = {}; 106 107 111 public Object invoke(final Object proxy, 112 final Method method, 113 final Object [] args) 114 throws Throwable 115 { 116 Class type = method.getDeclaringClass(); 118 if (type == MBeanProxyInstance.class || type == Object .class) 119 { 120 return method.invoke(this, args); 121 } 122 123 String methodName = method.getName(); 124 125 if (methodName.startsWith("get") && args == null) 127 { 128 if (inited == false) 129 init(); 130 131 String attrName = methodName.substring(3); 132 MBeanAttributeInfo info = (MBeanAttributeInfo ) attributeMap.get(attrName); 133 if (info != null) 134 { 135 String retType = method.getReturnType().getName(); 136 if (retType.equals(info.getType())) 137 { 138 try 139 { 140 return server.getAttribute(name, attrName); 141 } 142 catch (Exception e) 143 { 144 throw JMXExceptionDecoder.decode(e); 145 } 146 } 147 } 148 } 149 150 else if (methodName.startsWith("is") && args == null) 152 { 153 if (inited == false) 154 init(); 155 156 String attrName = methodName.substring(2); 157 MBeanAttributeInfo info = (MBeanAttributeInfo ) attributeMap.get(attrName); 158 if (info != null && info.isIs()) 159 { 160 Class retType = method.getReturnType(); 161 if (retType.equals(Boolean .class) || retType.equals(Boolean.TYPE)) 162 { 163 try 164 { 165 return server.getAttribute(name, attrName); 166 } 167 catch (Exception e) 168 { 169 throw JMXExceptionDecoder.decode(e); 170 } 171 } 172 } 173 } 174 175 else if (methodName.startsWith("set") && args != null && args.length == 1) 177 { 178 if (inited == false) 179 init(); 180 181 String attrName = methodName.substring(3); 182 MBeanAttributeInfo info = (MBeanAttributeInfo ) attributeMap.get(attrName); 183 if (info != null && method.getReturnType() == Void.TYPE) 184 { 185 try 186 { 187 server.setAttribute(name, new Attribute (attrName, args[0])); 188 return null; 189 } 190 catch (Exception e) 191 { 192 throw JMXExceptionDecoder.decode(e); 193 } 194 } 195 } 196 197 199 Class [] types = method.getParameterTypes(); 201 String [] sig = new String [types.length]; 202 for (int i = 0; i < types.length; i++) 203 { 204 sig[i] = types[i].getName(); 205 } 206 207 try 209 { 210 return server.invoke(name, methodName, args == null ? EMPTY_ARGS : args, sig); 211 } 212 catch (Exception e) 213 { 214 throw JMXExceptionDecoder.decode(e); 215 } 216 } 217 218 219 223 public final ObjectName getMBeanProxyObjectName() 224 { 225 return name; 226 } 227 228 public final MBeanServer getMBeanProxyMBeanServer() 229 { 230 if (server instanceof MBeanServer == false) 231 throw new IllegalStateException ("This operation is not available for an MBeanServerConnection"); 232 return (MBeanServer ) server; 233 } 234 235 public final MBeanServerConnection getMBeanProxyMBeanServerConnection() 236 { 237 return server; 238 } 239 240 244 248 public boolean equals(Object that) 249 { 250 if (that == null) return false; 251 if (that == this) return true; 252 253 if (that instanceof MBeanProxyInstance) 256 { 257 MBeanProxyInstance proxy = (MBeanProxyInstance) that; 258 259 if (name.equals(proxy.getMBeanProxyObjectName()) && 261 server.equals(proxy.getMBeanProxyMBeanServer())) 262 { 263 return true; 264 } 265 } 266 return false; 267 } 268 269 273 public int hashCode() 274 { 275 return name.hashCode() * 31 + server.hashCode(); 276 } 277 278 281 public String toString() 282 { 283 StringBuffer sbuf = new StringBuffer (128); 284 285 sbuf.append("MBeanProxyExt[").append(name.toString()).append(']'); 286 287 return sbuf.toString(); 288 } 289 290 294 303 public static Object create(final Class intf, final String name) 304 throws MalformedObjectNameException 305 { 306 return create(intf, new ObjectName (name)); 307 } 308 309 319 public static Object create(final Class intf, 320 final String name, 321 final MBeanServer server) 322 throws MalformedObjectNameException 323 { 324 return create(intf, new ObjectName (name), server); 325 } 326 327 333 public static Object create(final Class intf, final ObjectName name) 334 { 335 return create(intf, name, MBeanServerLocator.locateJBoss()); 336 } 337 338 345 public static Object create(final Class intf, 346 final ObjectName name, 347 final MBeanServer server) 348 { 349 return create(intf, name, server, false); 350 } 351 352 361 public static Object create(final Class intf, final ObjectName name, 362 final MBeanServer server, boolean lazyInit) 363 { 364 PrivilegedAction action = new PrivilegedAction () 366 { 367 public Object run() 368 { 369 ClassLoader cl = new ClassLoader (intf.getClassLoader()) 370 { 371 public Class loadClass(final String className) throws ClassNotFoundException 372 { 373 try 374 { 375 return super.loadClass(className); 376 } 377 catch (ClassNotFoundException e) 378 { 379 if (className.equals(MBeanProxyInstance.class.getName())) 381 { 382 return MBeanProxyInstance.class.getClassLoader().loadClass(className); 383 } 384 throw e; 386 } 387 } 388 }; 389 return cl; 390 } 391 }; 392 ClassLoader cl = (ClassLoader ) AccessController.doPrivileged(action); 393 Class [] ifaces = {MBeanProxyInstance.class, intf}; 394 InvocationHandler handler = new MBeanProxyExt(name, server, lazyInit); 395 return Proxy.newProxyInstance(cl, ifaces, handler); 396 } 397 398 401 private synchronized void init() 402 { 403 inited = true; 405 try 406 { 407 MBeanInfo info = server.getMBeanInfo(name); 408 MBeanAttributeInfo [] attributes = info.getAttributes(); 409 410 for (int i = 0; i < attributes.length; ++i) 411 attributeMap.put(attributes[i].getName(), attributes[i]); 412 } 413 catch (Exception e) 414 { 415 throw new RuntimeException ("Error creating MBeanProxy: " + name, e); 416 } 417 } 418 419 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 420 { 421 name = (ObjectName ) in.readObject(); 422 server = (MBeanServerConnection ) in.readObject(); 423 } 424 425 426 public void writeExternal(ObjectOutput out) throws IOException 427 { 428 out.writeObject(name); 429 if (remote != null) 430 out.writeObject(remote); 431 else 432 out.writeObject(server); } 434 } 435 | Popular Tags |