J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.jmx.mbeanserver; |
| 27 | |
| 28 | import static com.sun.jmx.mbeanserver.Util.*; |
| 29 | |
| 30 | import java.util.Iterator; |
| 31 | import java.util.Set; |
| 32 | |
| 33 | import javax.management.InstanceAlreadyExistsException; |
| 34 | import javax.management.JMX; |
| 35 | import javax.management.MBeanServer; |
| 36 | import javax.management.NotCompliantMBeanException; |
| 37 | import javax.management.ObjectName; |
| 38 | |
| 39 | /** |
| 40 | * Base class for MXBeans. |
| 41 | * |
| 42 | * @since 1.6 |
| 43 | */ |
| 44 | public class MXBeanSupport extends MBeanSupport<ConvertingMethod> { |
| 45 | |
| 46 | /** |
| 47 | <p>Construct an MXBean that wraps the given resource using the |
| 48 | given MXBean interface.</p> |
| 49 | |
| 50 | @param resource the underlying resource for the new MXBean. |
| 51 | |
| 52 | @param mxbeanInterface the interface to be used to determine |
| 53 | the MXBean's management interface. |
| 54 | |
| 55 | @param <T> a type parameter that allows the compiler to check |
| 56 | that {@code resource} implements {@code mxbeanInterface}, |
| 57 | provided that {@code mxbeanInterface} is a class constant like |
| 58 | {@code SomeMXBean.class}. |
| 59 | |
| 60 | @throws IllegalArgumentException if {@code resource} is null or |
| 61 | if it does not implement the class {@code mxbeanInterface} or if |
| 62 | that class is not a valid MXBean interface. |
| 63 | */ |
| 64 | public <T> MXBeanSupport(T resource, Class<T> mxbeanInterface) |
| 65 | throws NotCompliantMBeanException { |
| 66 | super(resource, mxbeanInterface); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | MBeanIntrospector<ConvertingMethod> getMBeanIntrospector() { |
| 71 | return MXBeanIntrospector.getInstance(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | Object getCookie() { |
| 76 | return mxbeanLookup; |
| 77 | } |
| 78 | |
| 79 | static Class<?> findMXBeanInterface(Class<?> resourceClass) |
| 80 | throws IllegalArgumentException { |
| 81 | if (resourceClass == null) |
| 82 | throw new IllegalArgumentException("Null resource class"); |
| 83 | final Set<Class<?>> intfs = transitiveInterfaces(resourceClass); |
| 84 | final Set<Class<?>> candidates = newSet(); |
| 85 | for (Class<?> intf : intfs) { |
| 86 | if (JMX.isMXBeanInterface(intf)) |
| 87 | candidates.add(intf); |
| 88 | } |
| 89 | reduce: |
| 90 | while (candidates.size() > 1) { |
| 91 | for (Class<?> intf : candidates) { |
| 92 | for (Iterator<Class<?>> it = candidates.iterator(); it.hasNext(); |
| 93 | ) { |
| 94 | final Class<?> intf2 = it.next(); |
| 95 | if (intf != intf2 && intf2.isAssignableFrom(intf)) { |
| 96 | it.remove(); |
| 97 | continue reduce; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | final String msg = |
| 102 | "Class " + resourceClass.getName() + " implements more than " + |
| 103 | "one MXBean interface: " + candidates; |
| 104 | throw new IllegalArgumentException(msg); |
| 105 | } |
| 106 | if (candidates.iterator().hasNext()) { |
| 107 | return candidates.iterator().next(); |
| 108 | } else { |
| 109 | final String msg = |
| 110 | "Class " + resourceClass.getName() + |
| 111 | " is not a JMX compliant MXBean"; |
| 112 | throw new IllegalArgumentException(msg); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /* Return all interfaces inherited by this class, directly or |
| 117 | * indirectly through the parent class and interfaces. |
| 118 | */ |
| 119 | private static Set<Class<?>> transitiveInterfaces(Class c) { |
| 120 | Set<Class<?>> set = newSet(); |
| 121 | transitiveInterfaces(c, set); |
| 122 | return set; |
| 123 | } |
| 124 | private static void transitiveInterfaces(Class<?> c, Set<Class<?>> intfs) { |
| 125 | if (c == null) |
| 126 | return; |
| 127 | if (c.isInterface()) |
| 128 | intfs.add(c); |
| 129 | transitiveInterfaces(c.getSuperclass(), intfs); |
| 130 | for (Class sup : c.getInterfaces()) |
| 131 | transitiveInterfaces(sup, intfs); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * The sequence of events for tracking inter-MXBean references is |
| 136 | * relatively complicated. We use the magical preRegister2 method |
| 137 | * which the MBeanServer knows about. The steps during registration |
| 138 | * are: |
| 139 | * (1) Call user preRegister, if any. If exception, abandon. |
| 140 | * (2) Call preRegister2 and hence this register method. If exception, |
| 141 | * call postRegister(false) and abandon. |
| 142 | * (3) Try to register the MBean. If exception, call registerFailed() |
| 143 | * which will call the unregister method. (Also call postRegister(false).) |
| 144 | * (4) If we get this far, we can call postRegister(true). |
| 145 | * |
| 146 | * When we are wrapped in an instance of javax.management.StandardMBean, |
| 147 | * things are simpler. That class calls this method from its preRegister, |
| 148 | * and propagates any exception. There is no user preRegister in this case. |
| 149 | * If this method succeeds but registration subsequently fails, |
| 150 | * StandardMBean calls unregister from its postRegister(false) method. |
| 151 | */ |
| 152 | @Override |
| 153 | public void register(MBeanServer server, ObjectName name) |
| 154 | throws InstanceAlreadyExistsException { |
| 155 | if (name == null) |
| 156 | throw new IllegalArgumentException("Null object name"); |
| 157 | // eventually we could have some logic to supply a default name |
| 158 | |
| 159 | synchronized (lock) { |
| 160 | if (this.objectName != null) { |
| 161 | final String msg = |
| 162 | "MXBean already registered with name " + this.objectName; |
| 163 | throw new InstanceAlreadyExistsException(msg); |
| 164 | } |
| 165 | this.mxbeanLookup = MXBeanLookup.lookupFor(server); |
| 166 | this.mxbeanLookup.addReference(name, getResource()); |
| 167 | this.objectName = name; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void unregister() { |
| 173 | synchronized (lock) { |
| 174 | if (mxbeanLookup.removeReference(objectName, getResource())) |
| 175 | objectName = null; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | private Object lock = new Object(); // for mxbeanLookup and objectName |
| 180 | private MXBeanLookup mxbeanLookup; |
| 181 | private ObjectName objectName; |
| 182 | } |