jdk/src/share/classes/javax/management/JMX.java
author jbachorik
Thu, 28 Mar 2013 09:39:26 +0100
changeset 18213 45c8ed869a1b
parent 5506 202f599c92aa
child 18805 b359f8adc8ad
permissions -rw-r--r--
8008982: Adjust JMX for underlying interface changes Reviewed-by: mchung, dholmes, dfuchs, skoivu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     2
 * Copyright (c) 2005, 2006, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jmx.mbeanserver.Introspector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.InvocationHandler;
18213
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
    30
import java.lang.reflect.Modifier;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Proxy;
18213
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
    32
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Static methods from the JMX API.  There are no instances of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class JMX {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /* Code within this package can prove that by providing this instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static final JMX proof = new JMX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private JMX() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * The name of the <a href="Descriptor.html#defaultValue">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * defaultValue}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static final String DEFAULT_VALUE_FIELD = "defaultValue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
1699
3611e5fd6da5 6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents: 1636
diff changeset
    53
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The name of the <a href="Descriptor.html#immutableInfo">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * immutableInfo}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final String IMMUTABLE_INFO_FIELD = "immutableInfo";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The name of the <a href="Descriptor.html#interfaceClassName">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * interfaceClassName}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final String INTERFACE_CLASS_NAME_FIELD = "interfaceClassName";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * The name of the <a href="Descriptor.html#legalValues">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * legalValues}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static final String LEGAL_VALUES_FIELD = "legalValues";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * The name of the <a href="Descriptor.html#maxValue">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * maxValue}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static final String MAX_VALUE_FIELD = "maxValue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * The name of the <a href="Descriptor.html#minValue">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * minValue}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static final String MIN_VALUE_FIELD = "minValue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * The name of the <a href="Descriptor.html#mxbean">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * mxbean}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static final String MXBEAN_FIELD = "mxbean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The name of the <a href="Descriptor.html#openType">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * openType}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public static final String OPEN_TYPE_FIELD = "openType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * The name of the <a href="Descriptor.html#originalType">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * originalType}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static final String ORIGINAL_TYPE_FIELD = "originalType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <p>Make a proxy for a Standard MBean in a local or remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * MBean Server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <p>If you have an MBean Server {@code mbs} containing an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * with {@link ObjectName} {@code name}, and if the MBean's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * management interface is described by the Java interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * {@code MyMBean}, you can construct a proxy for the MBean like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * MyMBean proxy = JMX.newMBeanProxy(mbs, name, MyMBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <p>Suppose, for example, {@code MyMBean} looks like this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * public interface MyMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *     public String getSomeAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *     public void setSomeAttribute(String value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *     public void someOperation(String param1, int param2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p>Then you can execute:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <li>{@code proxy.getSomeAttribute()} which will result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * call to {@code mbs.}{@link MBeanServerConnection#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * getAttribute}{@code (name, "SomeAttribute")}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <li>{@code proxy.setSomeAttribute("whatever")} which will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * in a call to {@code mbs.}{@link MBeanServerConnection#setAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * setAttribute}{@code (name, new Attribute("SomeAttribute", "whatever"))}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <li>{@code proxy.someOperation("param1", 2)} which will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * translated into a call to {@code mbs.}{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * MBeanServerConnection#invoke invoke}{@code (name, "someOperation", <etc>)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <p>The object returned by this method is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * {@link Proxy} whose {@code InvocationHandler} is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * {@link MBeanServerInvocationHandler}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <p>This method is equivalent to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * #newMBeanProxy(MBeanServerConnection, ObjectName, Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * boolean) newMBeanProxy(connection, objectName, interfaceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * false)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * {@code connection} to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param interfaceClass the management interface that the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * exports, which will also be implemented by the returned proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * interfaceClass} parameter is {@code MyMBean.class}, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * example, then the return type is {@code MyMBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public static <T> T newMBeanProxy(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                      ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                      Class<T> interfaceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return newMBeanProxy(connection, objectName, interfaceClass, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p>Make a proxy for a Standard MBean in a local or remote MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Server that may also support the methods of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p>This method behaves the same as {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * #newMBeanProxy(MBeanServerConnection, ObjectName, Class)}, but
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   177
     * additionally, if {@code notificationEmitter} is {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * true}, then the MBean is assumed to be a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * NotificationBroadcaster} or {@link NotificationEmitter} and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * returned proxy will implement {@link NotificationEmitter} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * well as {@code interfaceClass}.  A call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * NotificationBroadcaster#addNotificationListener} on the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * will result in a call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * MBeanServerConnection#addNotificationListener(ObjectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * NotificationListener, NotificationFilter, Object)}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * likewise for the other methods of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * NotificationBroadcaster} and {@link NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * {@code connection} to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param interfaceClass the management interface that the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * exports, which will also be implemented by the returned proxy.
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   194
     * @param notificationEmitter make the returned proxy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * implement {@link NotificationEmitter} by forwarding its methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * via {@code connection}.
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   197
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * interfaceClass} parameter is {@code MyMBean.class}, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * example, then the return type is {@code MyMBean}.
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   201
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public static <T> T newMBeanProxy(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                      ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                      Class<T> interfaceClass,
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   207
                                      boolean notificationEmitter) {
18213
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   208
        return createProxy(connection, objectName, interfaceClass, notificationEmitter, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <p>Make a proxy for an MXBean in a local or remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * MBean Server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <p>If you have an MBean Server {@code mbs} containing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * MXBean with {@link ObjectName} {@code name}, and if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * MXBean's management interface is described by the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * interface {@code MyMXBean}, you can construct a proxy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * the MXBean like this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * MyMXBean proxy = JMX.newMXBeanProxy(mbs, name, MyMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <p>Suppose, for example, {@code MyMXBean} looks like this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * public interface MyMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *     public String getSimpleAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *     public void setSimpleAttribute(String value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *     public {@link java.lang.management.MemoryUsage} getMappedAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *     public void setMappedAttribute(MemoryUsage memoryUsage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *     public MemoryUsage someOperation(String param1, MemoryUsage param2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <p>Then:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <li><p>{@code proxy.getSimpleAttribute()} will result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * call to {@code mbs.}{@link MBeanServerConnection#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * getAttribute}{@code (name, "SimpleAttribute")}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <li><p>{@code proxy.setSimpleAttribute("whatever")} will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * in a call to {@code mbs.}{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * MBeanServerConnection#setAttribute setAttribute}<code>(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * new Attribute("SimpleAttribute", "whatever"))</code>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *     <p>Because {@code String} is a <em>simple type</em>, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *     sense of {@link javax.management.openmbean.SimpleType}, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *     is not changed in the context of an MXBean.  The MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *     proxy behaves the same as a Standard MBean proxy (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *     {@link #newMBeanProxy(MBeanServerConnection, ObjectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *     Class) newMBeanProxy}) for the attribute {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *     SimpleAttribute}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <li><p>{@code proxy.getMappedAttribute()} will result in a call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * to {@code mbs.getAttribute("MappedAttribute")}.  The MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * mapping rules mean that the actual type of the attribute {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * MappedAttribute} will be {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * javax.management.openmbean.CompositeData CompositeData} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * that is what the {@code mbs.getAttribute} call will return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * The proxy will then convert the {@code CompositeData} back into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * the expected type {@code MemoryUsage} using the MXBean mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * rules.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <li><p>Similarly, {@code proxy.setMappedAttribute(memoryUsage)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * will convert the {@code MemoryUsage} argument into a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * CompositeData} before calling {@code mbs.setAttribute}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <li><p>{@code proxy.someOperation("whatever", memoryUsage)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * will convert the {@code MemoryUsage} argument into a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * CompositeData} and call {@code mbs.invoke}.  The value returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * by {@code mbs.invoke} will be also be a {@code CompositeData},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * and the proxy will convert this into the expected type {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * MemoryUsage} using the MXBean mapping rules.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   281
     * <p>The object returned by this method is a
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   282
     * {@link Proxy} whose {@code InvocationHandler} is an
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   283
     * {@link MBeanServerInvocationHandler}.</p>
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   284
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <p>This method is equivalent to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * #newMXBeanProxy(MBeanServerConnection, ObjectName, Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * boolean) newMXBeanProxy(connection, objectName, interfaceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * false)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * {@code connection} to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param interfaceClass the MXBean interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * which will also be implemented by the returned proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * interfaceClass} parameter is {@code MyMXBean.class}, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * example, then the return type is {@code MyMXBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public static <T> T newMXBeanProxy(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                       ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                       Class<T> interfaceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return newMXBeanProxy(connection, objectName, interfaceClass, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <p>Make a proxy for an MXBean in a local or remote MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Server that may also support the methods of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <p>This method behaves the same as {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * #newMXBeanProxy(MBeanServerConnection, ObjectName, Class)}, but
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   315
     * additionally, if {@code notificationEmitter} is {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * true}, then the MXBean is assumed to be a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * NotificationBroadcaster} or {@link NotificationEmitter} and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * returned proxy will implement {@link NotificationEmitter} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * well as {@code interfaceClass}.  A call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * NotificationBroadcaster#addNotificationListener} on the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * will result in a call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * MBeanServerConnection#addNotificationListener(ObjectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * NotificationListener, NotificationFilter, Object)}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * likewise for the other methods of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * NotificationBroadcaster} and {@link NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * {@code connection} to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @param interfaceClass the MXBean interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * which will also be implemented by the returned proxy.
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   332
     * @param notificationEmitter make the returned proxy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * implement {@link NotificationEmitter} by forwarding its methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * via {@code connection}.
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   335
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * interfaceClass} parameter is {@code MyMXBean.class}, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * example, then the return type is {@code MyMXBean}.
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1700
diff changeset
   339
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public static <T> T newMXBeanProxy(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                       ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                       Class<T> interfaceClass,
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   345
                                       boolean notificationEmitter) {
18213
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   346
        return createProxy(connection, objectName, interfaceClass, notificationEmitter, true);
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
     * <p>Test whether an interface is an MXBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * An interface is an MXBean interface if it is annotated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * {@link MXBean &#64;MXBean} or {@code @MXBean(true)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * or if it does not have an {@code @MXBean} annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * and its name ends with "{@code MXBean}".</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param interfaceClass The candidate interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return true if {@code interfaceClass} is an interface and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * meets the conditions described.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @throws NullPointerException if {@code interfaceClass} is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public static boolean isMXBeanInterface(Class<?> interfaceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (!interfaceClass.isInterface())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        MXBean a = interfaceClass.getAnnotation(MXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (a != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            return a.value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return interfaceClass.getName().endsWith("MXBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        // We don't bother excluding the case where the name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        // exactly the string "MXBean" since that would mean there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // was no package name, which is pretty unlikely in practice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
18213
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   374
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   375
    /**
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   376
     * Centralised M(X)Bean proxy creation code
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   377
     * @param connection {@linkplain MBeanServerConnection} to use
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   378
     * @param objectName M(X)Bean object name
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   379
     * @param interfaceClass M(X)Bean interface class
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   380
     * @param notificationEmitter Is a notification emitter?
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   381
     * @param isMXBean Is an MXBean?
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   382
     * @return Returns an M(X)Bean proxy generated for the provided interface class
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   383
     * @throws SecurityException
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   384
     * @throws IllegalArgumentException
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   385
     */
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   386
    private static <T> T createProxy(MBeanServerConnection connection,
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   387
                                     ObjectName objectName,
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   388
                                     Class<T> interfaceClass,
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   389
                                     boolean notificationEmitter,
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   390
                                     boolean isMXBean) {
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   391
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   392
        if (System.getSecurityManager() != null) {
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   393
            checkProxyInterface(interfaceClass);
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   394
        }
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   395
        try {
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   396
            if (isMXBean) {
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   397
                // Check interface for MXBean compliance
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   398
                Introspector.testComplianceMXBeanInterface(interfaceClass);
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   399
            } else {
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   400
                // Check interface for MBean compliance
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   401
                Introspector.testComplianceMBeanInterface(interfaceClass);
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   402
            }
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   403
        } catch (NotCompliantMBeanException e) {
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   404
            throw new IllegalArgumentException(e);
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   405
        }
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   406
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   407
        InvocationHandler handler = new MBeanServerInvocationHandler(
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   408
                connection, objectName, isMXBean);
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   409
        final Class<?>[] interfaces;
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   410
        if (notificationEmitter) {
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   411
            interfaces =
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   412
                new Class<?>[] {interfaceClass, NotificationEmitter.class};
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   413
        } else
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   414
            interfaces = new Class<?>[] {interfaceClass};
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   415
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   416
        Object proxy = Proxy.newProxyInstance(
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   417
                interfaceClass.getClassLoader(),
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   418
                interfaces,
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   419
                handler);
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   420
        return interfaceClass.cast(proxy);
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   421
    }
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   422
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   423
    /**
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   424
     * Checks for the M(X)Bean proxy interface being public and not restricted
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   425
     * @param interfaceClass MBean proxy interface
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   426
     * @throws SecurityException when the proxy interface comes from a restricted
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   427
     *                           package or is not public
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   428
     */
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   429
    private static void checkProxyInterface(Class<?> interfaceClass) {
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   430
        if (!Modifier.isPublic(interfaceClass.getModifiers())) {
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   431
            throw new SecurityException("mbean proxy interface non-public");
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   432
        }
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   433
        ReflectUtil.checkPackageAccess(interfaceClass);
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   434
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
}