jdk/src/share/classes/javax/management/StandardEmitterMBean.java
author emcmanus
Wed, 21 Oct 2009 17:33:18 +0200
changeset 4156 acaa49a2768a
parent 1636 eb801ce73ac9
child 5506 202f599c92aa
permissions -rw-r--r--
6851617: Remove JSR 255 (JMX API 2.0) from JDK 7 Summary: See http://weblogs.java.net/blog/2009/06/16/jsr-255-jmx-api-20-postponed Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1247
b4c26443dee5 6754988: Update copyright year
xdono
parents: 833
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  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
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
/**
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final NotificationEmitter emitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final MBeanNotificationInfo[] notificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p>Make an MBean whose management interface is specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * {@code mbeanInterface}, with the given implementation and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * where notifications are handled by the given {@code NotificationEmitter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * The resultant MBean implements the {@code NotificationEmitter} interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * by forwarding its methods to {@code emitter}.  It is legal and useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * for {@code implementation} and {@code emitter} to be the same object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <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
    79
     * NotificationBroadcasterSupport} then the MBean's {@link #sendNotification
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * 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
    81
     * NotificationBroadcasterSupport#sendNotification sendNotification}.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <p>The array returned by {@link #getNotificationInfo()} on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * new MBean is a copy of the array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * {@code emitter.}{@link NotificationBroadcaster#getNotificationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * getNotificationInfo()} at the time of construction.  If the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * returned by {@code emitter.getNotificationInfo()} later changes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * that will have no effect on this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * {@code getNotificationInfo()}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param implementation the implementation of the MBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param mbeanInterface a Standard MBean interface.
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
    93
     * @param emitter the object that will handle notifications.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @throws IllegalArgumentException if the {@code mbeanInterface}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *    does not follow JMX design patterns for Management Interfaces, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *    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
    98
     *    specified interface, or if {@code emitter} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public <T> StandardEmitterMBean(T implementation, Class<T> mbeanInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                    NotificationEmitter emitter) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   102
        super(implementation, mbeanInterface, false);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   103
        if (emitter == null)
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   104
            throw new IllegalArgumentException("Null emitter");
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   105
        this.emitter = emitter;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   106
        this.notificationInfo = emitter.getNotificationInfo();
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param implementation the implementation of the MBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param mbeanInterface a Standard MBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param isMXBean If true, the {@code mbeanInterface} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * 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
   137
     * @param emitter the object that will handle notifications.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @throws IllegalArgumentException if the {@code mbeanInterface}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *    does not follow JMX design patterns for Management Interfaces, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *    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
   142
     *    specified interface, or if {@code emitter} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public <T> StandardEmitterMBean(T implementation, Class<T> mbeanInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                    boolean isMXBean,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                    NotificationEmitter emitter) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   147
        super(implementation, mbeanInterface, isMXBean);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (emitter == null)
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   149
            throw new IllegalArgumentException("Null emitter");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.emitter = emitter;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   151
        this.notificationInfo = emitter.getNotificationInfo();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <p>Make an MBean whose management interface is specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * {@code mbeanInterface}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * where notifications are handled by the given {@code NotificationEmitter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * The resultant MBean implements the {@code NotificationEmitter} interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * by forwarding its methods to {@code emitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <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
   162
     * NotificationBroadcasterSupport} then the MBean's {@link #sendNotification
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * 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
   164
     * NotificationBroadcasterSupport#sendNotification sendNotification}.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p>The array returned by {@link #getNotificationInfo()} on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * new MBean is a copy of the array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * {@code emitter.}{@link NotificationBroadcaster#getNotificationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * getNotificationInfo()} at the time of construction.  If the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * returned by {@code emitter.getNotificationInfo()} later changes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * that will have no effect on this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * {@code getNotificationInfo()}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <p>This constructor must be called from a subclass that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * the given {@code mbeanInterface}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param mbeanInterface a StandardMBean interface.
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   178
     * @param emitter the object that will handle notifications.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @throws IllegalArgumentException if the {@code mbeanInterface}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *    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
   182
     *    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
   183
     *    if {@code emitter} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    protected StandardEmitterMBean(Class<?> mbeanInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                   NotificationEmitter emitter) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   187
        super(mbeanInterface, false);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   188
        if (emitter == null)
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   189
            throw new IllegalArgumentException("Null emitter");
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   190
        this.emitter = emitter;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   191
        this.notificationInfo = emitter.getNotificationInfo();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <p>Make an MBean whose management interface is specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * {@code mbeanInterface}, and where notifications are handled by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * the given {@code NotificationEmitter}.  This constructor can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * used to make either Standard MBeans or MXBeans.  The resultant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * MBean implements the {@code NotificationEmitter} interface by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * forwarding its methods to {@code emitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <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
   203
     * NotificationBroadcasterSupport} then the MBean's {@link #sendNotification
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * 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
   205
     * NotificationBroadcasterSupport#sendNotification sendNotification}.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p>The array returned by {@link #getNotificationInfo()} on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * new MBean is a copy of the array returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * {@code emitter.}{@link NotificationBroadcaster#getNotificationInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * getNotificationInfo()} at the time of construction.  If the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * returned by {@code emitter.getNotificationInfo()} later changes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * that will have no effect on this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * {@code getNotificationInfo()}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <p>This constructor must be called from a subclass that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * the given {@code mbeanInterface}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param mbeanInterface a StandardMBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param isMXBean If true, the {@code mbeanInterface} parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * 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
   221
     * @param emitter the object that will handle notifications.
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
   222
     *
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
   223
     * @throws IllegalArgumentException if the {@code mbeanInterface}
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
   224
     *    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
   225
     *    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
   226
     *    if {@code emitter} is null.
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
   227
     */
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   228
    protected StandardEmitterMBean(Class<?> mbeanInterface, boolean isMXBean,
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
   229
                                   NotificationEmitter emitter) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   230
        super(mbeanInterface, isMXBean);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (emitter == null)
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   232
            throw new IllegalArgumentException("Null emitter");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        this.emitter = emitter;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   234
        this.notificationInfo = emitter.getNotificationInfo();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public void removeNotificationListener(NotificationListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        emitter.removeNotificationListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public void removeNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                           NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                           Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        emitter.removeNotificationListener(listener, filter, handback);
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 addNotificationListener(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
        emitter.addNotificationListener(listener, filter, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public MBeanNotificationInfo[] getNotificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return notificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <p>Sends a notification.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   262
     * <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
   263
     * instance of {@code NotificationBroadcasterSupport} then this
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   264
     * method will call {@code emitter.}{@link
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   265
     * NotificationBroadcasterSupport#sendNotification
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * sendNotification}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param n the notification to send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @throws ClassCastException if the {@code emitter} parameter to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * constructor was not a {@code NotificationBroadcasterSupport}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public void sendNotification(Notification n) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   274
        if (emitter instanceof NotificationBroadcasterSupport)
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   275
            ((NotificationBroadcasterSupport) emitter).sendNotification(n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                "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
   279
                "instance of NotificationBroadcasterSupport: " +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1636
diff changeset
   280
                emitter.getClass().getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            throw new ClassCastException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <p>Get the MBeanNotificationInfo[] that will be used in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * MBeanInfo returned by this MBean.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>The default implementation of this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * {@link #getNotificationInfo()}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param info The default MBeanInfo derived by reflection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return the MBeanNotificationInfo[] for the new MBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
   295
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    MBeanNotificationInfo[] getNotifications(MBeanInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return getNotificationInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
}