jdk/src/java.management/share/classes/javax/management/StandardEmitterMBean.java
author darcy
Thu, 16 Apr 2015 09:51:29 -0700
changeset 29927 9cc3e111a1d8
parent 25859 3317bb8137f4
permissions -rw-r--r--
8077923: Add missing doclint in javax.management Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29927
9cc3e111a1d8 8077923: Add missing doclint in javax.management
darcy
parents: 25859
diff changeset
     2
 * Copyright (c) 2005, 2015, 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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * <p>An MBean whose management interface is determined by reflection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * on a Java interface, and that emits notifications.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <p>The following example shows how to use the public constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * {@link #StandardEmitterMBean(Object, Class, NotificationEmitter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * StandardEmitterMBean(implementation, mbeanInterface, emitter)} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * create an MBean emitting notifications with any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * implementation class name <i>Impl</i>, with a management
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * interface defined (as for current Standard MBeans) by any interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <i>Intf</i>, and with any implementation of the interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * {@link NotificationEmitter}. The example uses the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * {@link NotificationBroadcasterSupport} as an implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of the interface {@link NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *     <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *     MBeanServer mbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *     ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *     final String[] types = new String[] {"sun.disc.space","sun.disc.alarm"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *     final MBeanNotificationInfo info = new MBeanNotificationInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *                                          types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *                                          Notification.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *                                          "Notification about disc info.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *     final NotificationEmitter emitter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *                    new NotificationBroadcasterSupport(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     final Intf impl = new Impl(...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *     final Object mbean = new StandardEmitterMBean(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *                                     impl, Intf.class, emitter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *     mbs.registerMBean(mbean, objectName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see StandardMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class StandardEmitterMBean extends StandardMBean
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
    65
        implements NotificationEmitter {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
18188
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
    67
    private static final MBeanNotificationInfo[] NO_NOTIFICATION_INFO =
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
    68
        new MBeanNotificationInfo[0];
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
    69
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private final NotificationEmitter emitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private final MBeanNotificationInfo[] notificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <p>Make an MBean whose management interface is specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * {@code mbeanInterface}, with the given implementation and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * where notifications are handled by the given {@code NotificationEmitter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * The resultant MBean implements the {@code NotificationEmitter} interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * by forwarding its methods to {@code emitter}.  It is legal and useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * for {@code implementation} and {@code emitter} to be the same object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <p>If {@code emitter} is an instance of {@code
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
    82
     * NotificationBroadcasterSupport} then the MBean's {@link #sendNotification
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * sendNotification} method will call {@code emitter.}{@link
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
    84
     * NotificationBroadcasterSupport#sendNotification sendNotification}.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <p>The array returned by {@link #getNotificationInfo()} on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * new MBean is a copy of the array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * {@code emitter.}{@link NotificationBroadcaster#getNotificationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * getNotificationInfo()} at the time of construction.  If the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * returned by {@code emitter.getNotificationInfo()} later changes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * that will have no effect on this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * {@code getNotificationInfo()}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
29927
9cc3e111a1d8 8077923: Add missing doclint in javax.management
darcy
parents: 25859
diff changeset
    94
     * @param <T> the implementation type of the MBean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param implementation the implementation of the MBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param mbeanInterface a Standard MBean interface.
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
    97
     * @param emitter the object that will handle notifications.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @throws IllegalArgumentException if the {@code mbeanInterface}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *    does not follow JMX design patterns for Management Interfaces, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *    if the given {@code implementation} does not implement the
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   102
     *    specified interface, or if {@code emitter} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public <T> StandardEmitterMBean(T implementation, Class<T> mbeanInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                    NotificationEmitter emitter) {
18188
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   106
        this(implementation, mbeanInterface, false, emitter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p>Make an MBean whose management interface is specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * {@code mbeanInterface}, with the given implementation and where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * notifications are handled by the given {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * NotificationEmitter}.  This constructor can be used to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * either Standard MBeans or MXBeans.  The resultant MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * implements the {@code NotificationEmitter} interface by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * forwarding its methods to {@code emitter}.  It is legal and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * useful for {@code implementation} and {@code emitter} to be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * same object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <p>If {@code emitter} is an instance of {@code
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   121
     * NotificationBroadcasterSupport} then the MBean's {@link #sendNotification
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * sendNotification} method will call {@code emitter.}{@link
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   123
     * NotificationBroadcasterSupport#sendNotification sendNotification}.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p>The array returned by {@link #getNotificationInfo()} on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * new MBean is a copy of the array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * {@code emitter.}{@link NotificationBroadcaster#getNotificationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * getNotificationInfo()} at the time of construction.  If the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * returned by {@code emitter.getNotificationInfo()} later changes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * that will have no effect on this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * {@code getNotificationInfo()}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
29927
9cc3e111a1d8 8077923: Add missing doclint in javax.management
darcy
parents: 25859
diff changeset
   133
     * @param <T> the implementation type of the MBean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param implementation the implementation of the MBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param mbeanInterface a Standard MBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param isMXBean If true, the {@code mbeanInterface} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * names an MXBean interface and the resultant MBean is an MXBean.
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   138
     * @param emitter the object that will handle notifications.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws IllegalArgumentException if the {@code mbeanInterface}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *    does not follow JMX design patterns for Management Interfaces, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *    if the given {@code implementation} does not implement the
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   143
     *    specified interface, or if {@code emitter} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public <T> StandardEmitterMBean(T implementation, Class<T> mbeanInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                    boolean isMXBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                    NotificationEmitter emitter) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   148
        super(implementation, mbeanInterface, isMXBean);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (emitter == null)
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   150
            throw new IllegalArgumentException("Null emitter");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this.emitter = emitter;
18188
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   152
        MBeanNotificationInfo[] infos = emitter.getNotificationInfo();
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   153
        if (infos == null || infos.length == 0) {
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   154
            this.notificationInfo = NO_NOTIFICATION_INFO;
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   155
        } else {
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   156
            this.notificationInfo = infos.clone();
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   157
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p>Make an MBean whose management interface is specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * {@code mbeanInterface}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * where notifications are handled by the given {@code NotificationEmitter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * The resultant MBean implements the {@code NotificationEmitter} interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * by forwarding its methods to {@code emitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p>If {@code emitter} is an instance of {@code
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   168
     * NotificationBroadcasterSupport} then the MBean's {@link #sendNotification
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * sendNotification} method will call {@code emitter.}{@link
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   170
     * NotificationBroadcasterSupport#sendNotification sendNotification}.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <p>The array returned by {@link #getNotificationInfo()} on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * new MBean is a copy of the array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * {@code emitter.}{@link NotificationBroadcaster#getNotificationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * getNotificationInfo()} at the time of construction.  If the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * returned by {@code emitter.getNotificationInfo()} later changes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * that will have no effect on this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * {@code getNotificationInfo()}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <p>This constructor must be called from a subclass that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * the given {@code mbeanInterface}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param mbeanInterface a StandardMBean interface.
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   184
     * @param emitter the object that will handle notifications.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @throws IllegalArgumentException if the {@code mbeanInterface}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *    does not follow JMX design patterns for Management Interfaces, or
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   188
     *    if {@code this} does not implement the specified interface, or
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   189
     *    if {@code emitter} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    protected StandardEmitterMBean(Class<?> mbeanInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                   NotificationEmitter emitter) {
18188
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   193
        this(mbeanInterface, false, emitter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <p>Make an MBean whose management interface is specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * {@code mbeanInterface}, and where notifications are handled by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * the given {@code NotificationEmitter}.  This constructor can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * used to make either Standard MBeans or MXBeans.  The resultant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * MBean implements the {@code NotificationEmitter} interface by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * forwarding its methods to {@code emitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <p>If {@code emitter} is an instance of {@code
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   205
     * NotificationBroadcasterSupport} then the MBean's {@link #sendNotification
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * sendNotification} method will call {@code emitter.}{@link
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   207
     * NotificationBroadcasterSupport#sendNotification sendNotification}.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <p>The array returned by {@link #getNotificationInfo()} on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * new MBean is a copy of the array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * {@code emitter.}{@link NotificationBroadcaster#getNotificationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * getNotificationInfo()} at the time of construction.  If the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * returned by {@code emitter.getNotificationInfo()} later changes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * that will have no effect on this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * {@code getNotificationInfo()}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <p>This constructor must be called from a subclass that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * the given {@code mbeanInterface}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param mbeanInterface a StandardMBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param isMXBean If true, the {@code mbeanInterface} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * names an MXBean interface and the resultant MBean is an MXBean.
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   223
     * @param emitter the object that will handle notifications.
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
   224
     *
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
   225
     * @throws IllegalArgumentException if the {@code mbeanInterface}
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
   226
     *    does not follow JMX design patterns for Management Interfaces, or
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   227
     *    if {@code this} does not implement the specified interface, or
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   228
     *    if {@code emitter} is null.
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
   229
     */
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   230
    protected StandardEmitterMBean(Class<?> mbeanInterface, boolean isMXBean,
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
   231
                                   NotificationEmitter emitter) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   232
        super(mbeanInterface, isMXBean);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (emitter == null)
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   234
            throw new IllegalArgumentException("Null emitter");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        this.emitter = emitter;
18188
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   236
        MBeanNotificationInfo[] infos = emitter.getNotificationInfo();
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   237
        if (infos == null || infos.length == 0) {
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   238
            this.notificationInfo = NO_NOTIFICATION_INFO;
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   239
        } else {
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   240
            this.notificationInfo = infos.clone();
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   241
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public void removeNotificationListener(NotificationListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        emitter.removeNotificationListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public void removeNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                                           NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                           Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        emitter.removeNotificationListener(listener, filter, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public void addNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                        NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                        Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        emitter.addNotificationListener(listener, filter, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public MBeanNotificationInfo[] getNotificationInfo() {
18195
7ea73f748935 8009996: tests javax/management/mxbean/MiscTest.java and javax/management/mxbean/StandardMBeanOverrideTest.java fail
jbachorik
parents: 18188
diff changeset
   263
        // this getter might get called from the super constructor
7ea73f748935 8009996: tests javax/management/mxbean/MiscTest.java and javax/management/mxbean/StandardMBeanOverrideTest.java fail
jbachorik
parents: 18188
diff changeset
   264
        // when the notificationInfo has not been properly set yet
7ea73f748935 8009996: tests javax/management/mxbean/MiscTest.java and javax/management/mxbean/StandardMBeanOverrideTest.java fail
jbachorik
parents: 18188
diff changeset
   265
        if (notificationInfo == null) {
7ea73f748935 8009996: tests javax/management/mxbean/MiscTest.java and javax/management/mxbean/StandardMBeanOverrideTest.java fail
jbachorik
parents: 18188
diff changeset
   266
            return NO_NOTIFICATION_INFO;
7ea73f748935 8009996: tests javax/management/mxbean/MiscTest.java and javax/management/mxbean/StandardMBeanOverrideTest.java fail
jbachorik
parents: 18188
diff changeset
   267
        }
18188
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   268
        if (notificationInfo.length == 0) {
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   269
            return notificationInfo;
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   270
        } else {
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   271
            return notificationInfo.clone();
48e73ecb2461 8009038: Improve JMX notification support
jbachorik
parents: 5506
diff changeset
   272
        }
2
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>Sends a notification.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   278
     * <p>If the {@code emitter} parameter to the constructor was an
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   279
     * instance of {@code NotificationBroadcasterSupport} then this
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   280
     * method will call {@code emitter.}{@link
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   281
     * NotificationBroadcasterSupport#sendNotification
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * sendNotification}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param n the notification to send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @throws ClassCastException if the {@code emitter} parameter to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * constructor was not a {@code NotificationBroadcasterSupport}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public void sendNotification(Notification n) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   290
        if (emitter instanceof NotificationBroadcasterSupport)
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   291
            ((NotificationBroadcasterSupport) emitter).sendNotification(n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                "Cannot sendNotification when emitter is not an " +
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   295
                "instance of NotificationBroadcasterSupport: " +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   296
                emitter.getClass().getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            throw new ClassCastException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p>Get the MBeanNotificationInfo[] that will be used in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * MBeanInfo returned by this MBean.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <p>The default implementation of this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * {@link #getNotificationInfo()}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param info The default MBeanInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @return the MBeanNotificationInfo[] for the new MBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
   311
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    MBeanNotificationInfo[] getNotifications(MBeanInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return getNotificationInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
}