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