jdk/src/share/classes/javax/management/JMX.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 687 874e25a9844a
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Static methods from the JMX API.  There are no instances of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class JMX {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /* Code within this package can prove that by providing this instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static final JMX proof = new JMX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private JMX() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * The name of the <a href="Descriptor.html#defaultValue">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * defaultValue}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static final String DEFAULT_VALUE_FIELD = "defaultValue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * The name of the <a href="Descriptor.html#immutableInfo">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * immutableInfo}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final String IMMUTABLE_INFO_FIELD = "immutableInfo";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * The name of the <a href="Descriptor.html#interfaceClassName">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * interfaceClassName}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final String INTERFACE_CLASS_NAME_FIELD = "interfaceClassName";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The name of the <a href="Descriptor.html#legalValues">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * legalValues}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final String LEGAL_VALUES_FIELD = "legalValues";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * The name of the <a href="Descriptor.html#maxValue">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * maxValue}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static final String MAX_VALUE_FIELD = "maxValue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * The name of the <a href="Descriptor.html#minValue">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * minValue}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final String MIN_VALUE_FIELD = "minValue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The name of the <a href="Descriptor.html#mxbean">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * mxbean}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static final String MXBEAN_FIELD = "mxbean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The name of the <a href="Descriptor.html#openType">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * openType}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static final String OPEN_TYPE_FIELD = "openType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * The name of the <a href="Descriptor.html#originalType">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * originalType}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static final String ORIGINAL_TYPE_FIELD = "originalType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <p>Make a proxy for a Standard MBean in a local or remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * MBean Server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <p>If you have an MBean Server {@code mbs} containing an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * with {@link ObjectName} {@code name}, and if the MBean's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * management interface is described by the Java interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * {@code MyMBean}, you can construct a proxy for the MBean like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * MyMBean proxy = JMX.newMBeanProxy(mbs, name, MyMBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <p>Suppose, for example, {@code MyMBean} looks like this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * public interface MyMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *     public String getSomeAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *     public void setSomeAttribute(String value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *     public void someOperation(String param1, int param2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <p>Then you can execute:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <li>{@code proxy.getSomeAttribute()} which will result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * call to {@code mbs.}{@link MBeanServerConnection#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * getAttribute}{@code (name, "SomeAttribute")}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <li>{@code proxy.setSomeAttribute("whatever")} which will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * in a call to {@code mbs.}{@link MBeanServerConnection#setAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * setAttribute}{@code (name, new Attribute("SomeAttribute", "whatever"))}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <li>{@code proxy.someOperation("param1", 2)} which will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * translated into a call to {@code mbs.}{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * MBeanServerConnection#invoke invoke}{@code (name, "someOperation", <etc>)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <p>The object returned by this method is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * {@link Proxy} whose {@code InvocationHandler} is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * {@link MBeanServerInvocationHandler}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <p>This method is equivalent to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * #newMBeanProxy(MBeanServerConnection, ObjectName, Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * boolean) newMBeanProxy(connection, objectName, interfaceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * false)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * {@code connection} to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param interfaceClass the management interface that the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * exports, which will also be implemented by the returned proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * interfaceClass} parameter is {@code MyMBean.class}, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * example, then the return type is {@code MyMBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public static <T> T newMBeanProxy(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                      ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                      Class<T> interfaceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return newMBeanProxy(connection, objectName, interfaceClass, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <p>Make a proxy for a Standard MBean in a local or remote MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Server that may also support the methods of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <p>This method behaves the same as {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * #newMBeanProxy(MBeanServerConnection, ObjectName, Class)}, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * additionally, if {@code notificationBroadcaster} is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * true}, then the MBean is assumed to be a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * NotificationBroadcaster} or {@link NotificationEmitter} and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * returned proxy will implement {@link NotificationEmitter} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * well as {@code interfaceClass}.  A call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * NotificationBroadcaster#addNotificationListener} on the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * will result in a call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * MBeanServerConnection#addNotificationListener(ObjectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * NotificationListener, NotificationFilter, Object)}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * likewise for the other methods of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * NotificationBroadcaster} and {@link NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * {@code connection} to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param interfaceClass the management interface that the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * exports, which will also be implemented by the returned proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param notificationBroadcaster make the returned proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * implement {@link NotificationEmitter} by forwarding its methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * via {@code connection}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * interfaceClass} parameter is {@code MyMBean.class}, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * example, then the return type is {@code MyMBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public static <T> T newMBeanProxy(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                      ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                      Class<T> interfaceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                      boolean notificationBroadcaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return MBeanServerInvocationHandler.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                interfaceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                notificationBroadcaster);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <p>Make a proxy for an MXBean in a local or remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * MBean Server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <p>If you have an MBean Server {@code mbs} containing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * MXBean with {@link ObjectName} {@code name}, and if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * MXBean's management interface is described by the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * interface {@code MyMXBean}, you can construct a proxy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * the MXBean like this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * MyMXBean proxy = JMX.newMXBeanProxy(mbs, name, MyMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <p>Suppose, for example, {@code MyMXBean} looks like this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * public interface MyMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *     public String getSimpleAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *     public void setSimpleAttribute(String value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *     public {@link java.lang.management.MemoryUsage} getMappedAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *     public void setMappedAttribute(MemoryUsage memoryUsage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *     public MemoryUsage someOperation(String param1, MemoryUsage param2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <p>Then:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <li><p>{@code proxy.getSimpleAttribute()} will result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * call to {@code mbs.}{@link MBeanServerConnection#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * getAttribute}{@code (name, "SimpleAttribute")}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <li><p>{@code proxy.setSimpleAttribute("whatever")} will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * in a call to {@code mbs.}{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * MBeanServerConnection#setAttribute setAttribute}<code>(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * new Attribute("SimpleAttribute", "whatever"))</code>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *     <p>Because {@code String} is a <em>simple type</em>, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *     sense of {@link javax.management.openmbean.SimpleType}, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *     is not changed in the context of an MXBean.  The MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *     proxy behaves the same as a Standard MBean proxy (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *     {@link #newMBeanProxy(MBeanServerConnection, ObjectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *     Class) newMBeanProxy}) for the attribute {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *     SimpleAttribute}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <li><p>{@code proxy.getMappedAttribute()} will result in a call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * to {@code mbs.getAttribute("MappedAttribute")}.  The MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * mapping rules mean that the actual type of the attribute {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * MappedAttribute} will be {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * javax.management.openmbean.CompositeData CompositeData} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * that is what the {@code mbs.getAttribute} call will return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * The proxy will then convert the {@code CompositeData} back into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * the expected type {@code MemoryUsage} using the MXBean mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * rules.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <li><p>Similarly, {@code proxy.setMappedAttribute(memoryUsage)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * will convert the {@code MemoryUsage} argument into a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * CompositeData} before calling {@code mbs.setAttribute}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <li><p>{@code proxy.someOperation("whatever", memoryUsage)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * will convert the {@code MemoryUsage} argument into a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * CompositeData} and call {@code mbs.invoke}.  The value returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * by {@code mbs.invoke} will be also be a {@code CompositeData},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * and the proxy will convert this into the expected type {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * MemoryUsage} using the MXBean mapping rules.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <p>The object returned by this method is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * {@link Proxy} whose {@code InvocationHandler} is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * {@link MBeanServerInvocationHandler}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <p>This method is equivalent to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * #newMXBeanProxy(MBeanServerConnection, ObjectName, Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * boolean) newMXBeanProxy(connection, objectName, interfaceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * false)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * {@code connection} to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param interfaceClass the MXBean interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * which will also be implemented by the returned proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * interfaceClass} parameter is {@code MyMXBean.class}, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * example, then the return type is {@code MyMXBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public static <T> T newMXBeanProxy(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                       ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                       Class<T> interfaceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return newMXBeanProxy(connection, objectName, interfaceClass, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p>Make a proxy for an MXBean in a local or remote MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Server that may also support the methods of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <p>This method behaves the same as {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * #newMXBeanProxy(MBeanServerConnection, ObjectName, Class)}, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * additionally, if {@code notificationBroadcaster} is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * true}, then the MXBean is assumed to be a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * NotificationBroadcaster} or {@link NotificationEmitter} and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * returned proxy will implement {@link NotificationEmitter} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * well as {@code interfaceClass}.  A call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * NotificationBroadcaster#addNotificationListener} on the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * will result in a call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * MBeanServerConnection#addNotificationListener(ObjectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * NotificationListener, NotificationFilter, Object)}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * likewise for the other methods of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * NotificationBroadcaster} and {@link NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * {@code connection} to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param interfaceClass the MXBean interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * which will also be implemented by the returned proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param notificationBroadcaster make the returned proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * implement {@link NotificationEmitter} by forwarding its methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * via {@code connection}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * interfaceClass} parameter is {@code MyMXBean.class}, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * example, then the return type is {@code MyMXBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public static <T> T newMXBeanProxy(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                       ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                       Class<T> interfaceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                       boolean notificationBroadcaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // Check interface for MXBean compliance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            Introspector.testComplianceMXBeanInterface(interfaceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            throw new IllegalArgumentException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        InvocationHandler handler = new MBeanServerInvocationHandler(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                connection, objectName, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        final Class[] interfaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (notificationBroadcaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            interfaces =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                new Class<?>[] {interfaceClass, NotificationEmitter.class};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            interfaces = new Class[] {interfaceClass};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        Object proxy = Proxy.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                interfaceClass.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                interfaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return interfaceClass.cast(proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * <p>Test whether an interface is an MXBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * An interface is an MXBean interface if it is annotated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * {@link MXBean &#64;MXBean} or {@code @MXBean(true)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * or if it does not have an {@code @MXBean} annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * and its name ends with "{@code MXBean}".</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param interfaceClass The candidate interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @return true if {@code interfaceClass} is an interface and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * meets the conditions described.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @throws NullPointerException if {@code interfaceClass} is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public static boolean isMXBeanInterface(Class<?> interfaceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (!interfaceClass.isInterface())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        MXBean a = interfaceClass.getAnnotation(MXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (a != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return a.value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return interfaceClass.getName().endsWith("MXBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // We don't bother excluding the case where the name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // exactly the string "MXBean" since that would mean there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        // was no package name, which is pretty unlikely in practice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}