src/java.management/share/classes/javax/management/StandardMBean.java
author fweimer
Sat, 28 Sep 2019 11:04:01 +0200
changeset 58433 a7c95e2f8814
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231600: Avoid GCC 8 strncpy error in jdk.jdwp.agent Reviewed-by: cjplummer, amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
     2
 * Copyright (c) 2002, 2017, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    23
 * questions.
2
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 javax.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
    28
import static com.sun.jmx.defaults.JmxProperties.MISC_LOGGER;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jmx.mbeanserver.DescriptorCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jmx.mbeanserver.Introspector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jmx.mbeanserver.MBeanSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.jmx.mbeanserver.MXBeanSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.jmx.mbeanserver.StandardMBeanSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.jmx.mbeanserver.Util;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
    35
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.WeakHashMap;
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
    41
import java.lang.System.Logger.Level;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.openmbean.OpenMBeanAttributeInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.openmbean.OpenMBeanConstructorInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.openmbean.OpenMBeanConstructorInfoSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.openmbean.OpenMBeanOperationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.openmbean.OpenMBeanOperationInfoSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.openmbean.OpenMBeanParameterInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.openmbean.OpenMBeanParameterInfoSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>An MBean whose management interface is determined by reflection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * on a Java interface.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>This class brings more flexibility to the notion of Management
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Interface in the use of Standard MBeans.  Straightforward use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * the patterns for Standard MBeans described in the JMX Specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * means that there is a fixed relationship between the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * class of an MBean and its management interface (i.e., if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * implementation class is Thing, the management interface must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * ThingMBean).  This class makes it possible to keep the convenience
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * of specifying the management interface with a Java interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * without requiring that there be any naming relationship between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * implementation and interface classes.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>By making a DynamicMBean out of an MBean, this class makes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * it possible to select any interface implemented by the MBean as its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * management interface, provided that it complies with JMX patterns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * (i.e., attributes defined by getter/setter etc...).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p> This class also provides hooks that make it possible to supply
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * custom descriptions and names for the {@link MBeanInfo} returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * the DynamicMBean interface.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>Using this class, an MBean can be created with any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * implementation class name <i>Impl</i> and with a management
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * interface defined (as for current Standard MBeans) by any interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <i>Intf</i>, in one of two general ways:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <li>Using the public constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *     {@link #StandardMBean(java.lang.Object, java.lang.Class, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     StandardMBean(impl,interface)}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *     <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     MBeanServer mbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *     ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *     Impl impl = new Impl(...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *     StandardMBean mbean = new StandardMBean(impl, Intf.class, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *     mbs.registerMBean(mbean, objectName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *     </pre></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <li>Subclassing StandardMBean:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *     <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *     public class Impl extends StandardMBean implements Intf {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *        public Impl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *          super(Intf.class, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *       // implement methods of Intf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *     }
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
 *     MBeanServer mbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *     ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *     Impl impl = new Impl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *     mbs.registerMBean(impl, objectName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *     </pre></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <p>In either case, the class <i>Impl</i> must implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * interface <i>Intf</i>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <p>Standard MBeans based on the naming relationship between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * implementation and interface classes are of course still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * available.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <p>This class may also be used to construct MXBeans.  The usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * is exactly the same as for Standard MBeans except that in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * examples above, the {@code false} parameter to the constructor or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * {@code super(...)} invocation is instead {@code true}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 */
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   126
public class StandardMBean implements DynamicMBean, MBeanRegistration {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private final static DescriptorCache descriptors =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        DescriptorCache.getInstance(JMX.proof);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * The DynamicMBean that wraps the MXBean or Standard MBean implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private volatile MBeanSupport<?> mbean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * The cached MBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private volatile MBeanInfo cachedMBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Make a DynamicMBean out of <var>implementation</var>, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * specified <var>mbeanInterface</var> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param implementation The implementation of this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *        If <code>null</code>, and null implementation is allowed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *        then the implementation is assumed to be <var>this</var>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param mbeanInterface The Management Interface exported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *        MBean's implementation. If <code>null</code>, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *        object will use standard JMX design pattern to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *        the management interface associated with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *        implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param nullImplementationAllowed <code>true</code> if a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *        implementation is allowed. If null implementation is allowed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *        and a null implementation is passed, then the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *        is assumed to be <var>this</var>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception IllegalArgumentException if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *    <var>implementation</var> is null, and null is not allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private <T> void construct(T implementation, Class<T> mbeanInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                               boolean nullImplementationAllowed,
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   161
                               boolean isMXBean)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                               throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (implementation == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            // Have to use (T)this rather than mbeanInterface.cast(this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // because mbeanInterface might be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (nullImplementationAllowed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                implementation = Util.<T>cast(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            else throw new IllegalArgumentException("implementation is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   170
        if (isMXBean) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   171
            if (mbeanInterface == null) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   172
                mbeanInterface = Util.cast(Introspector.getMXBeanInterface(
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   173
                        implementation.getClass()));
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   174
            }
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   175
            this.mbean = new MXBeanSupport(implementation, mbeanInterface);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } else {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   177
            if (mbeanInterface == null) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   178
                mbeanInterface = Util.cast(Introspector.getStandardMBeanInterface(
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   179
                        implementation.getClass()));
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   180
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            this.mbean =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    new StandardMBeanSupport(implementation, mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <p>Make a DynamicMBean out of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <var>implementation</var>, using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <var>mbeanInterface</var> class.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param implementation The implementation of this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param mbeanInterface The Management Interface exported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *        MBean's implementation. If <code>null</code>, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *        object will use standard JMX design pattern to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *        the management interface associated with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *        implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param <T> Allows the compiler to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * that {@code implementation} does indeed implement the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * described by {@code mbeanInterface}.  The compiler can only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * check this if {@code mbeanInterface} is a class literal such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * as {@code MyMBean.class}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @exception IllegalArgumentException if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *    <var>implementation</var> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @exception NotCompliantMBeanException if the <var>mbeanInterface</var>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *    does not follow JMX design patterns for Management Interfaces, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *    if the given <var>implementation</var> does not implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *    specified interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public <T> StandardMBean(T implementation, Class<T> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        throws NotCompliantMBeanException {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   212
        construct(implementation, mbeanInterface, false, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <p>Make a DynamicMBean out of <var>this</var>, using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <var>mbeanInterface</var> class.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 526
diff changeset
   219
     * <p>Calls {@link #StandardMBean(java.lang.Object, java.lang.Class)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *       this(this,mbeanInterface)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * This constructor is reserved to subclasses.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param mbeanInterface The Management Interface exported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *        MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @exception NotCompliantMBeanException if the <var>mbeanInterface</var>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *    does not follow JMX design patterns for Management Interfaces, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *    if <var>this</var> does not implement the specified interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    protected StandardMBean(Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        throws NotCompliantMBeanException {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   232
        construct(null, mbeanInterface, true, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <p>Make a DynamicMBean out of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <var>implementation</var>, using the specified
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 526
diff changeset
   238
     * <var>mbeanInterface</var> class, and choosing whether the
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 526
diff changeset
   239
     * resultant MBean is an MXBean.  This constructor can be used
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * to make either Standard MBeans or MXBeans.  Unlike the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * constructor {@link #StandardMBean(Object, Class)}, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * does not throw NotCompliantMBeanException.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param implementation The implementation of this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param mbeanInterface The Management Interface exported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *        MBean's implementation. If <code>null</code>, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *        object will use standard JMX design pattern to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *        the management interface associated with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *        implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param isMXBean If true, the {@code mbeanInterface} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * names an MXBean interface and the resultant MBean is an MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param <T> Allows the compiler to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * that {@code implementation} does indeed implement the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * described by {@code mbeanInterface}.  The compiler can only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * check this if {@code mbeanInterface} is a class literal such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * as {@code MyMBean.class}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @exception IllegalArgumentException if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *    <var>implementation</var> is null, or if the <var>mbeanInterface</var>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *    does not follow JMX design patterns for Management Interfaces, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *    if the given <var>implementation</var> does not implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *    specified interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public <T> StandardMBean(T implementation, Class<T> mbeanInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                             boolean isMXBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        try {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   269
            construct(implementation, mbeanInterface, false, isMXBean);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            throw new IllegalArgumentException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <p>Make a DynamicMBean out of <var>this</var>, using the specified
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 526
diff changeset
   277
     * <var>mbeanInterface</var> class, and choosing whether the resulting
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 526
diff changeset
   278
     * MBean is an MXBean.  This constructor can be used
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * to make either Standard MBeans or MXBeans.  Unlike the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * constructor {@link #StandardMBean(Object, Class)}, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * does not throw NotCompliantMBeanException.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 526
diff changeset
   283
     * <p>Calls {@link #StandardMBean(java.lang.Object, java.lang.Class, boolean)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *       this(this, mbeanInterface, isMXBean)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * This constructor is reserved to subclasses.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param mbeanInterface The Management Interface exported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *        MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param isMXBean If true, the {@code mbeanInterface} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * names an MXBean interface and the resultant MBean is an MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @exception IllegalArgumentException if the <var>mbeanInterface</var>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *    does not follow JMX design patterns for Management Interfaces, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *    if <var>this</var> does not implement the specified interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    protected StandardMBean(Class<?> mbeanInterface, boolean isMXBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        try {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   300
            construct(null, mbeanInterface, true, isMXBean);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw new IllegalArgumentException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <p>Replace the implementation object wrapped in this object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param implementation The new implementation of this Standard MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * (or MXBean). The <code>implementation</code> object must implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * the Standard MBean (or MXBean) interface that was supplied when this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <code>StandardMBean</code> was constructed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @exception IllegalArgumentException if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <var>implementation</var> is null.
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   316
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @exception NotCompliantMBeanException if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <var>implementation</var> does not implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Standard MBean (or MXBean) interface that was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * supplied at construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @see #getImplementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public void setImplementation(Object implementation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (implementation == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            throw new IllegalArgumentException("implementation is null");
1701
f5cedaa459bf 6450834: RFE: allow StandardMBean to call MBeanRegistration methods on its wrapped resource
jfdenise
parents: 1636
diff changeset
   329
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   330
        if (isMXBean()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            this.mbean = new MXBeanSupport(implementation,
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   332
                    Util.<Class<Object>>cast(getMBeanInterface()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   334
            this.mbean = new StandardMBeanSupport(implementation,
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   335
                    Util.<Class<Object>>cast(getMBeanInterface()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Get the implementation of this Standard MBean (or MXBean).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @return The implementation of this Standard MBean (or MXBean).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @see #setImplementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public Object getImplementation() {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   346
        return mbean.getResource();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Get the Management Interface of this Standard MBean (or MXBean).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @return The management interface of this Standard MBean (or MXBean).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public final Class<?> getMBeanInterface() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return mbean.getMBeanInterface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Get the class of the implementation of this Standard MBean (or MXBean).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @return The class of the implementation of this Standard MBean (or MXBean).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public Class<?> getImplementationClass() {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   362
        return mbean.getResource().getClass();
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 526
diff changeset
   363
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 526
diff changeset
   364
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    // From the DynamicMBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public Object getAttribute(String attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        throws AttributeNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
               MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
               ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return mbean.getAttribute(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    // From the DynamicMBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public void setAttribute(Attribute attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        throws AttributeNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
               InvalidAttributeValueException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
               MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
               ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        mbean.setAttribute(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    // From the DynamicMBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public AttributeList getAttributes(String[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return mbean.getAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    // From the DynamicMBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public AttributeList setAttributes(AttributeList attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return mbean.setAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    // From the DynamicMBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public Object invoke(String actionName, Object params[], String signature[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            throws MBeanException, ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        return mbean.invoke(actionName, params, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Get the {@link MBeanInfo} for this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * This method implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * {@link javax.management.DynamicMBean#getMBeanInfo()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *   DynamicMBean.getMBeanInfo()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * This method first calls {@link #getCachedMBeanInfo()} in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * retrieve the cached MBeanInfo for this MBean, if any. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * MBeanInfo returned by {@link #getCachedMBeanInfo()} is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * then it is returned.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Otherwise, this method builds a default MBeanInfo for this MBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * using the Management Interface specified for this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * While building the MBeanInfo, this method calls the customization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * hooks that make it possible for subclasses to supply their custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * descriptions, parameter names, etc...<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Finally, it calls {@link #cacheMBeanInfo(javax.management.MBeanInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * cacheMBeanInfo()} in order to cache the new MBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @return The cached MBeanInfo for that MBean, if not null, or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *         newly built MBeanInfo if none was cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public MBeanInfo getMBeanInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            final MBeanInfo cached = getCachedMBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (cached != null) return cached;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        } catch (RuntimeException x) {
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   435
            if (MISC_LOGGER.isLoggable(Level.DEBUG)) {
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   436
                MISC_LOGGER.log(Level.DEBUG,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        "Failed to get cached MBeanInfo", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   441
        if (MISC_LOGGER.isLoggable(Level.TRACE)) {
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   442
            MISC_LOGGER.log(Level.TRACE,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    "Building MBeanInfo for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    getImplementationClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 834
diff changeset
   447
        MBeanSupport<?> msupport = mbean;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        final MBeanInfo bi = msupport.getMBeanInfo();
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   449
        final Object impl = msupport.getResource();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        final boolean immutableInfo = immutableInfo(this.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        final String                  cname = getClassName(bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        final String                  text  = getDescription(bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        final MBeanConstructorInfo[]  ctors = getConstructors(bi,impl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        final MBeanAttributeInfo[]    attrs = getAttributes(bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        final MBeanOperationInfo[]    ops   = getOperations(bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        final MBeanNotificationInfo[] ntfs  = getNotifications(bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        final Descriptor              desc  = getDescriptor(bi, immutableInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        final MBeanInfo nmbi = new MBeanInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                cname, text, attrs, ctors, ops, ntfs, desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            cacheMBeanInfo(nmbi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        } catch (RuntimeException x) {
43235
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   466
            if (MISC_LOGGER.isLoggable(Level.DEBUG)) {
da1786d695b6 8172971: java.management could use System.Logger
dfuchs
parents: 25859
diff changeset
   467
                MISC_LOGGER.log(Level.DEBUG,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        "Failed to cache MBeanInfo", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        return nmbi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Get the className that will be used in the MBeanInfo returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * Subclasses may redefine this method in order to supply their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * custom class name.  The default implementation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * {@link MBeanInfo#getClassName() info.getClassName()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @param info The default MBeanInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @return the class name for the new MBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    protected String getClassName(MBeanInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (info == null) return getImplementationClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        return info.getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Get the description that will be used in the MBeanInfo returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Subclasses may redefine this method in order to supply their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * custom MBean description.  The default implementation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * {@link MBeanInfo#getDescription() info.getDescription()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @param info The default MBeanInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @return the description for the new MBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    protected String getDescription(MBeanInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (info == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        return info.getDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * <p>Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Get the description that will be used in the MBeanFeatureInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * returned by this MBean.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * <p>Subclasses may redefine this method in order to supply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * their custom description.  The default implementation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * {@link MBeanFeatureInfo#getDescription()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * info.getDescription()}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * <p>This method is called by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *      {@link #getDescription(MBeanAttributeInfo)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *      {@link #getDescription(MBeanOperationInfo)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *      {@link #getDescription(MBeanConstructorInfo)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @param info The default MBeanFeatureInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @return the description for the given MBeanFeatureInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    protected String getDescription(MBeanFeatureInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if (info == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return info.getDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * Get the description that will be used in the MBeanAttributeInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * returned by this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * <p>Subclasses may redefine this method in order to supply their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * custom description.  The default implementation returns {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * #getDescription(MBeanFeatureInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * getDescription((MBeanFeatureInfo) info)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param info The default MBeanAttributeInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @return the description for the given MBeanAttributeInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    protected String getDescription(MBeanAttributeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        return getDescription((MBeanFeatureInfo)info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Get the description that will be used in the MBeanConstructorInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * returned by this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Subclasses may redefine this method in order to supply their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * custom description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * The default implementation returns {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * #getDescription(MBeanFeatureInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * getDescription((MBeanFeatureInfo) info)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @param info The default MBeanConstructorInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @return the description for the given MBeanConstructorInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    protected String getDescription(MBeanConstructorInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        return getDescription((MBeanFeatureInfo)info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Get the description that will be used for the  <var>sequence</var>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * MBeanParameterInfo of the MBeanConstructorInfo returned by this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Subclasses may redefine this method in order to supply their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * custom description.  The default implementation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * {@link MBeanParameterInfo#getDescription() param.getDescription()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @param ctor  The default MBeanConstructorInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @param param The default MBeanParameterInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @param sequence The sequence number of the parameter considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *        ("0" for the first parameter, "1" for the second parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *        etc...).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @return the description for the given MBeanParameterInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    protected String getDescription(MBeanConstructorInfo ctor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                    MBeanParameterInfo   param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                                    int sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        if (param == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        return param.getDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * Get the name that will be used for the <var>sequence</var>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * MBeanParameterInfo of the MBeanConstructorInfo returned by this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Subclasses may redefine this method in order to supply their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * custom parameter name.  The default implementation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * {@link MBeanParameterInfo#getName() param.getName()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @param ctor  The default MBeanConstructorInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @param param The default MBeanParameterInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @param sequence The sequence number of the parameter considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *        ("0" for the first parameter, "1" for the second parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *        etc...).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @return the name for the given MBeanParameterInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    protected String getParameterName(MBeanConstructorInfo ctor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                                      MBeanParameterInfo param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                                      int sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (param == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return param.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Get the description that will be used in the MBeanOperationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * returned by this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Subclasses may redefine this method in order to supply their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * custom description.  The default implementation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * {@link #getDescription(MBeanFeatureInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * getDescription((MBeanFeatureInfo) info)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @param info The default MBeanOperationInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @return the description for the given MBeanOperationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    protected String getDescription(MBeanOperationInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        return getDescription((MBeanFeatureInfo)info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Get the <var>impact</var> flag of the operation that will be used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * the MBeanOperationInfo returned by this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * Subclasses may redefine this method in order to supply their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * custom impact flag.  The default implementation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * {@link MBeanOperationInfo#getImpact() info.getImpact()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param info The default MBeanOperationInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @return the impact flag for the given MBeanOperationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    protected int getImpact(MBeanOperationInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (info == null) return MBeanOperationInfo.UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        return info.getImpact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * Get the name that will be used for the <var>sequence</var>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * MBeanParameterInfo of the MBeanOperationInfo returned by this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * Subclasses may redefine this method in order to supply their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * custom parameter name.  The default implementation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * {@link MBeanParameterInfo#getName() param.getName()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @param op    The default MBeanOperationInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @param param The default MBeanParameterInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @param sequence The sequence number of the parameter considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *        ("0" for the first parameter, "1" for the second parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *        etc...).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @return the name to use for the given MBeanParameterInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    protected String getParameterName(MBeanOperationInfo op,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                      MBeanParameterInfo param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                                      int sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        if (param == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        return param.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * Get the description that will be used for the  <var>sequence</var>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * MBeanParameterInfo of the MBeanOperationInfo returned by this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * Subclasses may redefine this method in order to supply their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * custom description.  The default implementation returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * {@link MBeanParameterInfo#getDescription() param.getDescription()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @param op    The default MBeanOperationInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @param param The default MBeanParameterInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @param sequence The sequence number of the parameter considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *        ("0" for the first parameter, "1" for the second parameter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *        etc...).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @return the description for the given MBeanParameterInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    protected String getDescription(MBeanOperationInfo op,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                                    MBeanParameterInfo param,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                                    int sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        if (param == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return param.getDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * Get the MBeanConstructorInfo[] that will be used in the MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * returned by this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * By default, this method returns <code>null</code> if the wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * implementation is not <var>this</var>. Indeed, if the wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * implementation is not this object itself, it will not be possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * to recreate a wrapped implementation by calling the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * constructors through <code>MBeanServer.createMBean(...)</code>.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * Otherwise, if the wrapped implementation is <var>this</var>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * <var>ctors</var> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * Subclasses may redefine this method in order to modify this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * behavior, if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * @param ctors The default MBeanConstructorInfo[] derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @param impl  The wrapped implementation. If <code>null</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *        passed, the wrapped implementation is ignored and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *        <var>ctors</var> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @return the MBeanConstructorInfo[] for the new MBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    protected MBeanConstructorInfo[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        getConstructors(MBeanConstructorInfo[] ctors, Object impl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            if (ctors == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            if (impl != null && impl != this) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            return ctors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * Get the MBeanNotificationInfo[] that will be used in the MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * returned by this MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * Subclasses may redefine this method in order to supply their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * custom notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @param info The default MBeanInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @return the MBeanNotificationInfo[] for the new MBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    MBeanNotificationInfo[] getNotifications(MBeanInfo info) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   726
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * <p>Get the Descriptor that will be used in the MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * returned by this MBean.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * <p>Subclasses may redefine this method in order to supply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * their custom descriptor.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * <p>The default implementation of this method returns a Descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * that contains at least the field {@code interfaceClassName}, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * value {@link #getMBeanInterface()}.getName(). It may also contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * the field {@code immutableInfo}, with a value that is the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * {@code "true"} if the implementation can determine that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * {@code MBeanInfo} returned by {@link #getMBeanInfo()} will always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * be the same. It may contain other fields: fields defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * JMX specification must have appropriate values, and other fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * must follow the conventions for non-standard field names.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @param info The default MBeanInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @return the Descriptor for the new MBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    Descriptor getDescriptor(MBeanInfo info, boolean immutableInfo) {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   750
        ImmutableDescriptor desc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        if (info == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            info.getDescriptor() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            info.getDescriptor().getFieldNames().length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            final String interfaceClassNameS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                "interfaceClassName=" + getMBeanInterface().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            final String immutableInfoS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                "immutableInfo=" + immutableInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            desc = new ImmutableDescriptor(interfaceClassNameS, immutableInfoS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            desc = descriptors.get(desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            Descriptor d = info.getDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            Map<String,Object> fields = new HashMap<String,Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            for (String fieldName : d.getFieldNames()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                if (fieldName.equals("immutableInfo")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    // Replace immutableInfo as the underlying MBean/MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    // could already implement NotificationBroadcaster and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    // return immutableInfo=true in its MBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    fields.put(fieldName, Boolean.toString(immutableInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    fields.put(fieldName, d.getFieldValue(fieldName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            desc = new ImmutableDescriptor(fields);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        return desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * Return the MBeanInfo cached for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * <p>Subclasses may redefine this method in order to implement their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * own caching policy.  The default implementation stores one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * {@link MBeanInfo} object per instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @return The cached MBeanInfo, or null if no MBeanInfo is cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * @see #cacheMBeanInfo(MBeanInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    protected MBeanInfo getCachedMBeanInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        return cachedMBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * Customization hook:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * cache the MBeanInfo built for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * <p>Subclasses may redefine this method in order to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * their own caching policy.  The default implementation stores
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * <code>info</code> in this instance.  A subclass can define
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * other policies, such as not saving <code>info</code> (so it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * reconstructed every time {@link #getMBeanInfo()} is called) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * sharing a unique {@link MBeanInfo} object when several
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * <code>StandardMBean</code> instances have equal {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * MBeanInfo} values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @param info the new <code>MBeanInfo</code> to cache.  Any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * previously cached value is discarded.  This parameter may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * null, in which case there is no new cached value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    protected void cacheMBeanInfo(MBeanInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        cachedMBeanInfo = info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   815
    private boolean isMXBean() {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   816
        return mbean.isMXBean();
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   817
    }
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
   818
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    private static <T> boolean identicalArrays(T[] a, T[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (a == b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        if (a == null || b == null || a.length != b.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        for (int i = 0; i < a.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            if (a[i] != b[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    private static <T> boolean equal(T a, T b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        if (a == b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        if (a == null || b == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        return a.equals(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    private static MBeanParameterInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            customize(MBeanParameterInfo pi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                      String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                      String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        if (equal(name, pi.getName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                equal(description, pi.getDescription()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            return pi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        else if (pi instanceof OpenMBeanParameterInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            OpenMBeanParameterInfo opi = (OpenMBeanParameterInfo) pi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            return new OpenMBeanParameterInfoSupport(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                                                     description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                                                     opi.getOpenType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                                                     pi.getDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            return new MBeanParameterInfo(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                                          pi.getType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                                          description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                                          pi.getDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    private static MBeanConstructorInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            customize(MBeanConstructorInfo ci,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                      String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                      MBeanParameterInfo[] signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        if (equal(description, ci.getDescription()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                identicalArrays(signature, ci.getSignature()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            return ci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        if (ci instanceof OpenMBeanConstructorInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            OpenMBeanParameterInfo[] oparams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                paramsToOpenParams(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            return new OpenMBeanConstructorInfoSupport(ci.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                                                       description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                                                       oparams,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                                                       ci.getDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            return new MBeanConstructorInfo(ci.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                                            description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                                            signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                                            ci.getDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    private static MBeanOperationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            customize(MBeanOperationInfo oi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                      String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                      MBeanParameterInfo[] signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                      int impact) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        if (equal(description, oi.getDescription()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                identicalArrays(signature, oi.getSignature()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                impact == oi.getImpact())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            return oi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        if (oi instanceof OpenMBeanOperationInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            OpenMBeanOperationInfo ooi = (OpenMBeanOperationInfo) oi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            OpenMBeanParameterInfo[] oparams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                paramsToOpenParams(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            return new OpenMBeanOperationInfoSupport(oi.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                                                     description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                                                     oparams,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                                                     ooi.getReturnOpenType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                                                     impact,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                                                     oi.getDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            return new MBeanOperationInfo(oi.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                                          description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                                          signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                                          oi.getReturnType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                                          impact,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                                          oi.getDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    private static MBeanAttributeInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            customize(MBeanAttributeInfo ai,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                      String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (equal(description, ai.getDescription()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            return ai;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        if (ai instanceof OpenMBeanAttributeInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            OpenMBeanAttributeInfo oai = (OpenMBeanAttributeInfo) ai;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            return new OpenMBeanAttributeInfoSupport(ai.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                                                     description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                                                     oai.getOpenType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                                                     ai.isReadable(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                                                     ai.isWritable(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                                                     ai.isIs(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                                                     ai.getDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            return new MBeanAttributeInfo(ai.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                                          ai.getType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                                          description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                                          ai.isReadable(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                                          ai.isWritable(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                                          ai.isIs(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                                          ai.getDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    private static OpenMBeanParameterInfo[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            paramsToOpenParams(MBeanParameterInfo[] params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        if (params instanceof OpenMBeanParameterInfo[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            return (OpenMBeanParameterInfo[]) params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        OpenMBeanParameterInfo[] oparams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            new OpenMBeanParameterInfoSupport[params.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        System.arraycopy(params, 0, oparams, 0, params.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        return oparams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    // Build the custom MBeanConstructorInfo[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    private MBeanConstructorInfo[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            getConstructors(MBeanInfo info, Object impl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        final MBeanConstructorInfo[] ctors =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            getConstructors(info.getConstructors(), impl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        if (ctors == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        final int ctorlen = ctors.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        final MBeanConstructorInfo[] nctors = new MBeanConstructorInfo[ctorlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        for (int i=0; i<ctorlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            final MBeanConstructorInfo c = ctors[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            final MBeanParameterInfo[] params = c.getSignature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            final MBeanParameterInfo[] nps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                final int plen = params.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                nps = new MBeanParameterInfo[plen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                for (int ii=0;ii<plen;ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                    MBeanParameterInfo p = params[ii];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                    nps[ii] = customize(p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                                        getParameterName(c,p,ii),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                                        getDescription(c,p,ii));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                nps = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            nctors[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                customize(c, getDescription(c), nps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        return nctors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    // Build the custom MBeanOperationInfo[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    private MBeanOperationInfo[] getOperations(MBeanInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        final MBeanOperationInfo[] ops = info.getOperations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        if (ops == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        final int oplen = ops.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        final MBeanOperationInfo[] nops = new MBeanOperationInfo[oplen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        for (int i=0; i<oplen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            final MBeanOperationInfo o = ops[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            final MBeanParameterInfo[] params = o.getSignature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            final MBeanParameterInfo[] nps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            if (params != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                final int plen = params.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                nps = new MBeanParameterInfo[plen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                for (int ii=0;ii<plen;ii++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    MBeanParameterInfo p = params[ii];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                    nps[ii] = customize(p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                                        getParameterName(o,p,ii),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                                        getDescription(o,p,ii));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                nps = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            nops[i] = customize(o, getDescription(o), nps, getImpact(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        return nops;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    // Build the custom MBeanAttributeInfo[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    // ------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    private MBeanAttributeInfo[] getAttributes(MBeanInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        final MBeanAttributeInfo[] atts = info.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        if (atts == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            return null; // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        final MBeanAttributeInfo[] natts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        final int attlen = atts.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        natts = new MBeanAttributeInfo[attlen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        for (int i=0; i<attlen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            final MBeanAttributeInfo a = atts[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            natts[i] = customize(a, getDescription(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        return natts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * <p>Allows the MBean to perform any operations it needs before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * being registered in the MBean server.  If the name of the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * is not specified, the MBean can provide a name for its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * registration.  If any exception is raised, the MBean will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * registered in the MBean server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * <p>The default implementation of this method returns the {@code name}
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
  1034
     * parameter.  It does nothing else for
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
  1035
     * Standard MBeans.  For MXBeans, it records the {@code MBeanServer}
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
  1036
     * and {@code ObjectName} parameters so they can be used to translate
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
  1037
     * inter-MXBean references.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * <p>It is good practice for a subclass that overrides this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * to call the overridden method via {@code super.preRegister(...)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * This is necessary if this object is an MXBean that is referenced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * by attributes or operations in other MXBeans.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @param server The MBean server in which the MBean will be registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @param name The object name of the MBean.  This name is null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * the name parameter to one of the <code>createMBean</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * <code>registerMBean</code> methods in the {@link MBeanServer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * interface is null.  In that case, this method must return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * non-null ObjectName for the new MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * @return The name under which the MBean is to be registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * This value must not be null.  If the <code>name</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * parameter is not null, it will usually but not necessarily be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * the returned value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * @throws IllegalArgumentException if this is an MXBean and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * {@code name} is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @throws InstanceAlreadyExistsException if this is an MXBean and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * it has already been registered under another name (in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * MBean Server or another).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * @throws Exception no other checked exceptions are thrown by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * this method but {@code Exception} is declared so that subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * can override the method and throw their own exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    public ObjectName preRegister(MBeanServer server, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        mbean.register(server, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * <p>Allows the MBean to perform any operations needed after having been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * registered in the MBean server or after the registration has failed.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
  1080
     * <p>The default implementation of this method does nothing for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * Standard MBeans.  For MXBeans, it undoes any work done by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * {@link #preRegister preRegister} if registration fails.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * <p>It is good practice for a subclass that overrides this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * to call the overridden method via {@code super.postRegister(...)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * This is necessary if this object is an MXBean that is referenced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * by attributes or operations in other MXBeans.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * @param registrationDone Indicates whether or not the MBean has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * been successfully registered in the MBean server. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * false means that the registration phase has failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    public void postRegister(Boolean registrationDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        if (!registrationDone)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            mbean.unregister();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * <p>Allows the MBean to perform any operations it needs before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * being unregistered by the MBean server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
  1104
     * <p>The default implementation of this method does nothing.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * <p>It is good practice for a subclass that overrides this method
1701
f5cedaa459bf 6450834: RFE: allow StandardMBean to call MBeanRegistration methods on its wrapped resource
jfdenise
parents: 1636
diff changeset
  1107
     * to call the overridden method via {@code super.preDeregister(...)}.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * @throws Exception no checked exceptions are throw by this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * but {@code Exception} is declared so that subclasses can override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * this method and throw their own exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    public void preDeregister() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * <p>Allows the MBean to perform any operations needed after having been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * unregistered in the MBean server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1702
diff changeset
  1122
     * <p>The default implementation of this method does nothing for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * Standard MBeans.  For MXBeans, it removes any information that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * was recorded by the {@link #preRegister preRegister} method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * <p>It is good practice for a subclass that overrides this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * to call the overridden method via {@code super.postRegister(...)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * This is necessary if this object is an MXBean that is referenced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * by attributes or operations in other MXBeans.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    public void postDeregister() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        mbean.unregister();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    // MBeanInfo immutability
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * Cached results of previous calls to immutableInfo. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * a WeakHashMap so that we don't prevent a class from being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * garbage collected just because we know whether its MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * is immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 834
diff changeset
  1147
    private static final Map<Class<?>, Boolean> mbeanInfoSafeMap =
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 834
diff changeset
  1148
        new WeakHashMap<Class<?>, Boolean>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * Return true if {@code subclass} is known to preserve the immutability
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * of the {@code MBeanInfo}. The {@code subclass} is considered to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * an immutable {@code MBeanInfo} if it does not override any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * getMBeanInfo, getCachedMBeanInfo, cacheMBeanInfo and getNotificationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    static boolean immutableInfo(Class<? extends StandardMBean> subclass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        if (subclass == StandardMBean.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            subclass == StandardEmitterMBean.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        synchronized (mbeanInfoSafeMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            Boolean safe = mbeanInfoSafeMap.get(subclass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            if (safe == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                    MBeanInfoSafeAction action =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                        new MBeanInfoSafeAction(subclass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                    safe = AccessController.doPrivileged(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                } catch (Exception e) { // e.g. SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                    /* We don't know, so we assume it isn't.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                    safe = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                mbeanInfoSafeMap.put(subclass, safe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            return safe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    static boolean overrides(Class<?> subclass, Class<?> superclass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                             String name, Class<?>... params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        for (Class<?> c = subclass; c != superclass; c = c.getSuperclass()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                c.getDeclaredMethod(name, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            } catch (NoSuchMethodException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                // OK: this class doesn't override it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    private static class MBeanInfoSafeAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            implements PrivilegedAction<Boolean> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 834
diff changeset
  1194
        private final Class<?> subclass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 834
diff changeset
  1196
        MBeanInfoSafeAction(Class<?> subclass) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            this.subclass = subclass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        public Boolean run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            // Check for "void cacheMBeanInfo(MBeanInfo)" method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            if (overrides(subclass, StandardMBean.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                          "cacheMBeanInfo", MBeanInfo.class))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            // Check for "MBeanInfo getCachedMBeanInfo()" method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            if (overrides(subclass, StandardMBean.class,
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 834
diff changeset
  1210
                          "getCachedMBeanInfo", (Class<?>[]) null))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            // Check for "MBeanInfo getMBeanInfo()" method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            if (overrides(subclass, StandardMBean.class,
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 834
diff changeset
  1216
                          "getMBeanInfo", (Class<?>[]) null))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            // Check for "MBeanNotificationInfo[] getNotificationInfo()"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            // method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            //
1636
eb801ce73ac9 6776225: JMX.isNotificationSource wrong when DynamicWrapperMBean + SendNotification injection
emcmanus
parents: 1510
diff changeset
  1222
            // This method is taken into account for the MBeanInfo
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            // immutability checks if and only if the given subclass is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            // StandardEmitterMBean itself or can be assigned to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            // StandardEmitterMBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            if (StandardEmitterMBean.class.isAssignableFrom(subclass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                if (overrides(subclass, StandardEmitterMBean.class,
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 834
diff changeset
  1229
                              "getNotificationInfo", (Class<?>[]) null))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
}