1 40 41 package com.sun.jmx.examples.scandir; 42 43 import java.lang.reflect.InvocationHandler ; 44 import java.lang.reflect.Proxy ; 45 import java.util.logging.Logger ; 46 import javax.management.JMX ; 47 import javax.management.MBeanServerConnection ; 48 import javax.management.MBeanServerInvocationHandler ; 49 import javax.management.NotificationEmitter ; 50 import javax.management.ObjectName ; 51 52 57 public class TestUtils { 58 59 62 private static final Logger LOG = 63 Logger.getLogger(TestUtils.class.getName()); 64 65 66 private TestUtils() { 67 } 68 69 73 public static ObjectName getObjectName(Object proxy) { 74 if (!(proxy instanceof Proxy )) 75 throw new IllegalArgumentException ("not a "+Proxy .class.getName()); 76 final Proxy p = (Proxy ) proxy; 77 final InvocationHandler handler = 78 Proxy.getInvocationHandler(proxy); 79 if (handler instanceof MBeanServerInvocationHandler ) 80 return ((MBeanServerInvocationHandler )handler).getObjectName(); 81 throw new IllegalArgumentException ("not a JMX Proxy"); 82 } 83 84 89 public static <T> T makeNotificationEmitter(T proxy, 90 Class <T> mbeanInterface) { 91 if (proxy instanceof NotificationEmitter ) 92 return proxy; 93 if (proxy == null) return null; 94 if (!(proxy instanceof Proxy )) 95 throw new IllegalArgumentException ("not a "+Proxy .class.getName()); 96 final Proxy p = (Proxy ) proxy; 97 final InvocationHandler handler = 98 Proxy.getInvocationHandler(proxy); 99 if (!(handler instanceof MBeanServerInvocationHandler )) 100 throw new IllegalArgumentException ("not a JMX Proxy"); 101 final MBeanServerInvocationHandler h = 102 (MBeanServerInvocationHandler )handler; 103 final ObjectName name = h.getObjectName(); 104 final MBeanServerConnection mbs = h.getMBeanServerConnection(); 105 final boolean isMXBean = h.isMXBean(); 106 final T newProxy; 107 if (isMXBean) 108 newProxy = JMX.newMXBeanProxy(mbs,name,mbeanInterface,true); 109 else 110 newProxy = JMX.newMBeanProxy(mbs,name,mbeanInterface,true); 111 return newProxy; 112 } 113 114 } 115 | Popular Tags |