1 7 8 package com.sun.jmx.mbeanserver; 9 10 import static com.sun.jmx.mbeanserver.Util.*; 11 import java.util.Map ; 12 import java.lang.ref.WeakReference ; 13 import java.lang.reflect.InvocationHandler ; 14 import java.lang.reflect.Proxy ; 15 import java.security.AccessController ; 16 import javax.management.InstanceAlreadyExistsException ; 17 import javax.management.JMX ; 18 import javax.management.MBeanServerConnection ; 19 import javax.management.MBeanServerInvocationHandler ; 20 import javax.management.ObjectName ; 21 22 25 26 68 public class MXBeanLookup { 69 private MXBeanLookup(MBeanServerConnection mbsc) { 70 this.mbsc = mbsc; 71 } 72 73 static MXBeanLookup lookupFor(MBeanServerConnection mbsc) { 74 synchronized (mbscToLookup) { 75 WeakReference <MXBeanLookup> weakLookup = mbscToLookup.get(mbsc); 76 MXBeanLookup lookup = (weakLookup == null) ? null : weakLookup.get(); 77 if (lookup == null) { 78 lookup = new MXBeanLookup(mbsc); 79 mbscToLookup.put(mbsc, new WeakReference <MXBeanLookup>(lookup)); 80 } 81 return lookup; 82 } 83 } 84 85 synchronized <T> T objectNameToMXBean(ObjectName name, Class <T> type) { 86 WeakReference <Object > wr = objectNameToProxy.get(name); 87 if (wr != null) { 88 Object proxy = wr.get(); 89 if (type.isInstance(proxy)) 90 return type.cast(proxy); 91 } 92 InvocationHandler handler = 93 new MBeanServerInvocationHandler (mbsc, name); 94 T proxy = JMX.newMXBeanProxy(mbsc, name, type); 95 objectNameToProxy.put(name, new WeakReference <Object >(proxy)); 96 return proxy; 97 } 98 99 synchronized ObjectName mxbeanToObjectName(Object mxbean) { 100 if (mxbean instanceof Proxy ) { 101 InvocationHandler ih = Proxy.getInvocationHandler(mxbean); 102 if (ih instanceof MBeanServerInvocationHandler ) { 103 MBeanServerInvocationHandler mbsih = 104 (MBeanServerInvocationHandler ) ih; 105 if (mbsih.getMBeanServerConnection().equals(mbsc)) 106 return mbsih.getObjectName(); 107 } 108 return null; 109 } else 110 return mxbeanToObjectName.get(mxbean); 111 } 112 113 synchronized void addReference(ObjectName name, Object mxbean) 114 throws InstanceAlreadyExistsException { 115 ObjectName existing = mxbeanToObjectName.get(mxbean); 116 if (existing != null) { 117 String multiname = AccessController.doPrivileged( 118 new GetPropertyAction("jmx.mxbean.multiname")); 119 if (!"true".equalsIgnoreCase(multiname)) { 120 throw new InstanceAlreadyExistsException ( 121 "MXBean already registered with name " + existing); 122 } 123 } 124 mxbeanToObjectName.put(mxbean, name); 125 } 126 127 synchronized boolean removeReference(ObjectName name, Object mxbean) { 128 if (name.equals(mxbeanToObjectName.get(mxbean))) { 129 mxbeanToObjectName.remove(mxbean); 130 return true; 131 } else 132 return false; 133 136 } 137 138 private final MBeanServerConnection mbsc; 139 private final WeakIdentityHashMap<Object , ObjectName > 140 mxbeanToObjectName = WeakIdentityHashMap.make(); 141 private final Map <ObjectName , WeakReference <Object >> 142 objectNameToProxy = newMap(); 143 private static WeakIdentityHashMap<MBeanServerConnection , 144 WeakReference <MXBeanLookup>> 145 mbscToLookup = WeakIdentityHashMap.make(); 146 } 147 | Popular Tags |