1 7 8 package com.sun.jmx.mbeanserver; 9 10 import static com.sun.jmx.mbeanserver.Util.*; 11 12 import java.util.Iterator ; 13 import java.util.Set ; 14 15 import javax.management.InstanceAlreadyExistsException ; 16 import javax.management.JMX ; 17 import javax.management.MBeanServer ; 18 import javax.management.NotCompliantMBeanException ; 19 import javax.management.ObjectName ; 20 21 26 public class MXBeanSupport extends MBeanSupport<ConvertingMethod> { 27 28 46 public <T> MXBeanSupport(T resource, Class <T> mxbeanInterface) 47 throws NotCompliantMBeanException { 48 super(resource, mxbeanInterface); 49 } 50 51 @Override 52 MBeanIntrospector<ConvertingMethod> getMBeanIntrospector() { 53 return MXBeanIntrospector.getInstance(); 54 } 55 56 @Override 57 Object getCookie() { 58 return mxbeanLookup; 59 } 60 61 static Class <?> findMXBeanInterface(Class <?> resourceClass) 62 throws IllegalArgumentException { 63 if (resourceClass == null) 64 throw new IllegalArgumentException ("Null resource class"); 65 final Set <Class > intfs = transitiveInterfaces(resourceClass); 66 final Set <Class > candidates = newSet(); 67 for (Class intf : intfs) { 68 if (JMX.isMXBeanInterface(intf)) 69 candidates.add(intf); 70 } 71 reduce: 72 while (candidates.size() > 1) { 73 for (Class intf : candidates) { 74 for (Iterator <Class > it = candidates.iterator(); it.hasNext(); 75 ) { 76 final Class intf2 = it.next(); 77 if (intf != intf2 && intf2.isAssignableFrom(intf)) { 78 it.remove(); 79 continue reduce; 80 } 81 } 82 } 83 final String msg = 84 "Class " + resourceClass.getName() + " implements more than " + 85 "one MXBean interface: " + candidates; 86 throw new IllegalArgumentException (msg); 87 } 88 if (candidates.iterator().hasNext()) { 89 return candidates.iterator().next(); 90 } else { 91 final String msg = 92 "Class " + resourceClass.getName() + 93 " is not a JMX compliant MXBean"; 94 throw new IllegalArgumentException (msg); 95 } 96 } 97 98 101 private static Set <Class > transitiveInterfaces(Class c) { 102 Set <Class > set = newSet(); 103 transitiveInterfaces(c, set); 104 return set; 105 } 106 private static void transitiveInterfaces(Class c, Set <Class > intfs) { 107 if (c == null) 108 return; 109 if (c.isInterface()) 110 intfs.add(c); 111 transitiveInterfaces(c.getSuperclass(), intfs); 112 for (Class sup : c.getInterfaces()) 113 transitiveInterfaces(sup, intfs); 114 } 115 116 134 @Override 135 public void register(MBeanServer server, ObjectName name) 136 throws InstanceAlreadyExistsException { 137 if (name == null) 138 throw new IllegalArgumentException ("Null object name"); 139 141 synchronized (lock) { 142 this.mxbeanLookup = MXBeanLookup.lookupFor(server); 143 this.mxbeanLookup.addReference(name, getResource()); 144 this.objectName = name; 145 } 146 } 147 148 @Override 149 public void unregister() { 150 synchronized (lock) { 151 if (mxbeanLookup.removeReference(objectName, getResource())) 152 objectName = null; 153 } 154 } 155 156 private Object lock = new Object (); private MXBeanLookup mxbeanLookup; 158 private ObjectName objectName; 159 } 160 | Popular Tags |