jdk/src/share/classes/com/sun/jmx/mbeanserver/Introspector.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 34 2d042367885f
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
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 java.lang.annotation.Annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.AnnotatedElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.UndeclaredThrowableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.DescriptorKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.DynamicMBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.ImmutableDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.MBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import com.sun.jmx.mbeanserver.Util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * This class contains the methods for performing all the tests needed to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * that a class represents a JMX compliant MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class Introspector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *  PRIVATE CONSTRUCTORS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // private constructor defined to "hide" the default public constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Introspector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *  PUBLIC METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Tell whether a MBean of the given class is a Dynamic MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * This method does nothing more than returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * javax.management.DynamicMBean.class.isAssignableFrom(c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * This method does not check for any JMX MBean compliance:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <ul><li>If <code>true</code> is returned, then instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *     <code>c</code> are DynamicMBean.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *     <li>If <code>false</code> is returned, then no further
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *     assumption can be made on instances of <code>c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *     In particular, instances of <code>c</code> may, or may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *     be JMX standard MBeans.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param c The class of the MBean under examination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @return <code>true</code> if instances of <code>c</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *         Dynamic MBeans, <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static final boolean isDynamic(final Class c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // Check if the MBean implements the DynamicMBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return javax.management.DynamicMBean.class.isAssignableFrom(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Basic method for testing that a MBean of a given class can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * instantiated by the MBean server.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * This method checks that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <ul><li>The given class is a concrete class.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *     <li>The given class exposes at least one public constructor.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * If these conditions are not met, throws a NotCompliantMBeanException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param c The class of the MBean we want to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @exception NotCompliantMBeanException if the MBean class makes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *            impossible to instantiate the MBean from within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *            MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static void testCreation(Class c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // Check if the class is a concrete class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        final int mods = c.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (Modifier.isAbstract(mods) || Modifier.isInterface(mods)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new NotCompliantMBeanException("MBean class must be concrete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // Check if the MBean has a public constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        final Constructor[] consList = c.getConstructors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (consList.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new NotCompliantMBeanException("MBean class must have public constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public static void checkCompliance(Class mbeanClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // Is DynamicMBean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (DynamicMBean.class.isAssignableFrom(mbeanClass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // Is Standard MBean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        final Exception mbeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            getStandardMBeanInterface(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            mbeanException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // Is MXBean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        final Exception mxbeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            getMXBeanInterface(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            mxbeanException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            "MBean class " + mbeanClass.getName() + " does not implement " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            "DynamicMBean, neither follows the Standard MBean conventions (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            mbeanException.toString() + ") nor the MXBean conventions (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            mxbeanException.toString() + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public static DynamicMBean makeDynamicMBean(Object mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (mbean instanceof DynamicMBean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            return (DynamicMBean) mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        final Class mbeanClass = mbean.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        Class<?> c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            c = getStandardMBeanInterface(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // Ignore exception - we need to check whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // mbean is an MXBean first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (c != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return new StandardMBeanSupport(mbean, Util.<Class<Object>>cast(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            c = getMXBeanInterface(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // Ignore exception - we cannot decide whether mbean was supposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // to be an MBean or an MXBean. We will call checkCompliance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            // to generate the appropriate exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (c != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return new MXBeanSupport(mbean, Util.<Class<Object>>cast(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        checkCompliance(mbeanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        throw new NotCompliantMBeanException("Not compliant"); // not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Basic method for testing if a given class is a JMX compliant MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param baseClass The class to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return <code>null</code> if the MBean is a DynamicMBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *         the computed {@link javax.management.MBeanInfo} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @exception NotCompliantMBeanException The specified class is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *            JMX compliant MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public static MBeanInfo testCompliance(Class baseClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // ------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // Check if the MBean implements the MBean or the Dynamic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        // MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (isDynamic(baseClass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return testCompliance(baseClass, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public static void testComplianceMXBeanInterface(Class interfaceClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        MXBeanIntrospector.getInstance().getAnalyzer(interfaceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Basic method for testing if a given class is a JMX compliant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Standard MBean.  This method is only called by the legacy code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * in com.sun.management.jmx.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param mbeanInterface the MBean interface that the class implements,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * or null if the interface must be determined by introspection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @return the computed {@link javax.management.MBeanInfo}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @exception NotCompliantMBeanException The specified class is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *            JMX compliant Standard MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public static synchronized MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            testCompliance(final Class<?> baseClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                           Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (mbeanInterface == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            mbeanInterface = getStandardMBeanInterface(baseClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        MBeanIntrospector<?> introspector = StandardMBeanIntrospector.getInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return getClassMBeanInfo(introspector, baseClass, mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private static <M> MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            getClassMBeanInfo(MBeanIntrospector<M> introspector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                              Class<?> baseClass, Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        PerInterface<M> perInterface = introspector.getPerInterface(mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return introspector.getClassMBeanInfo(baseClass, perInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Get the MBean interface implemented by a JMX Standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * MBean class. This method is only called by the legacy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * code in "com.sun.management.jmx".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @return The MBean interface implemented by the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *         Return <code>null</code> if the MBean is a DynamicMBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *         or if no MBean interface is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public static Class getMBeanInterface(Class baseClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // Check if the given class implements the MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // or the Dynamic MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (isDynamic(baseClass)) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return getStandardMBeanInterface(baseClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Get the MBean interface implemented by a JMX Standard MBean class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @return The MBean interface implemented by the Standard MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @throws NotCompliantMBeanException The specified class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * not a JMX compliant Standard MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public static Class getStandardMBeanInterface(Class baseClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        Class current = baseClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        Class mbeanInterface = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        while (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            mbeanInterface =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                findMBeanInterface(current, current.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (mbeanInterface != null) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            current = current.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (mbeanInterface != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return mbeanInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                "Class " + baseClass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                " is not a JMX compliant Standard MBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Get the MXBean interface implemented by a JMX MXBean class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param baseClass The class to be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @return The MXBean interface implemented by the MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @throws NotCompliantMBeanException The specified class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * not a JMX compliant MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public static Class getMXBeanInterface(Class baseClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return MXBeanSupport.findMXBeanInterface(baseClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            throw throwException(baseClass,e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *  PRIVATE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Try to find the MBean interface corresponding to the class aName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * - i.e. <i>aName</i>MBean, from within aClass and its superclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private static Class findMBeanInterface(Class aClass, String aName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        Class current = aClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        while (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            final Class[] interfaces = current.getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            final int len = interfaces.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            for (int i=0;i<len;i++)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                final Class inter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    implementsMBean(interfaces[i], aName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if (inter != null) return inter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            current = current.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public static Descriptor descriptorForElement(final AnnotatedElement elmt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (elmt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        final Annotation[] annots = elmt.getAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return descriptorForAnnotations(annots);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public static Descriptor descriptorForAnnotations(Annotation[] annots) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (annots.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        Map<String, Object> descriptorMap = new HashMap<String, Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        for (Annotation a : annots) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            Class<? extends Annotation> c = a.annotationType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            Method[] elements = c.getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            for (Method element : elements) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                DescriptorKey key = element.getAnnotation(DescriptorKey.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    String name = key.value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        value = element.invoke(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        // we don't expect this - except for possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                        // security exceptions?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        // RuntimeExceptions shouldn't be "UndeclaredThrowable".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                        // anyway...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        // we don't expect this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        throw new UndeclaredThrowableException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    value = annotationToField(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    Object oldValue = descriptorMap.put(name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    if (oldValue != null && !equals(oldValue, value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                            "Inconsistent values for descriptor field " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                            " from annotations: " + value + " :: " + oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (descriptorMap.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return new ImmutableDescriptor(descriptorMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Throws a NotCompliantMBeanException or a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param notCompliant the class which was under examination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param cause the raeson why NotCompliantMBeanException should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *        be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @return nothing - this method always throw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *         The return type makes it possible to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *         <pre> throw throwException(clazz,cause); </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @throws SecurityException - if cause is a SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @throws NotCompliantMBeanException otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    static NotCompliantMBeanException throwException(Class<?> notCompliant,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            Throwable cause)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            throws NotCompliantMBeanException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (cause instanceof SecurityException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            throw (SecurityException) cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (cause instanceof NotCompliantMBeanException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            throw (NotCompliantMBeanException)cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        final String classname =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                (notCompliant==null)?"null class":notCompliant.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        final String reason =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                (cause==null)?"Not compliant":cause.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        final NotCompliantMBeanException res =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                new NotCompliantMBeanException(classname+": "+reason);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        res.initCause(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        throw res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    // Convert a value from an annotation element to a descriptor field value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    // E.g. with @interface Foo {class value()} an annotation @Foo(String.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    // will produce a Descriptor field value "java.lang.String"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    private static Object annotationToField(Object x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // An annotation element cannot have a null value but never mind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (x == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (x instanceof Number || x instanceof String ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                x instanceof Character || x instanceof Boolean ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                x instanceof String[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        // Remaining possibilities: array of primitive (e.g. int[]),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        // enum, class, array of enum or class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        Class<?> c = x.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (c.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if (c.getComponentType().isPrimitive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            Object[] xx = (Object[]) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            String[] ss = new String[xx.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            for (int i = 0; i < xx.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                ss[i] = (String) annotationToField(xx[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            return ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (x instanceof Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            return ((Class<?>) x).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (x instanceof Enum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            return ((Enum) x).name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        // The only other possibility is that the value is another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // annotation, or that the language has evolved since this code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // was written.  We don't allow for either of those currently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        throw new IllegalArgumentException("Illegal type for annotation " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                "element: " + x.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    // This must be consistent with the check for duplicate field values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    // ImmutableDescriptor.union.  But we don't expect to be called very
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    // often so this inefficient check should be enough.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    private static boolean equals(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return Arrays.deepEquals(new Object[] {x}, new Object[] {y});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Returns the XXMBean interface or null if no such interface exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param c The interface to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param clName The name of the class implementing this interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    private static Class implementsMBean(Class c, String clName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        String clMBeanName = clName + "MBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (c.getName().equals(clMBeanName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        Class[] interfaces = c.getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        for (int i = 0;i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (interfaces[i].getName().equals(clMBeanName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                return interfaces[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
}