src/java.management/share/classes/javax/management/NotificationBroadcasterSupport.java
author dholmes
Mon, 18 Nov 2019 23:41:06 -0500
changeset 59132 189f47d990b5
parent 47216 71c04702a3d5
permissions -rw-r--r--
8215355: Object monitor deadlock with no threads holding the monitor (using jemalloc 5.1) Reviewed-by: rehn, stuefe, dcubed, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18291
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
18291
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
    30
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.concurrent.CopyOnWriteArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.concurrent.Executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.jmx.remote.util.ClassLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>Provides an implementation of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * javax.management.NotificationEmitter NotificationEmitter}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * interface.  This can be used as the super class of an MBean that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * sends notifications.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>By default, the notification dispatch model is synchronous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * That is, when a thread calls sendNotification, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>NotificationListener.handleNotification</code> method of each listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * is called within that thread. You can override this default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * by overriding <code>handleNotification</code> in a subclass, or by passing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Executor to the constructor.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>If the method call of a filter or listener throws an {@link Exception},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * then that exception does not prevent other listeners from being invoked.  However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * if the method call of a filter or of {@code Executor.execute} or of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * {@code handleNotification} (when no {@code Excecutor} is specified) throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link Error}, then that {@code Error} is propagated to the caller of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * {@link #sendNotification sendNotification}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>Remote listeners added using the JMX Remote API (see JMXConnector) are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * usually called synchronously.  That is, when sendNotification returns, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * not guaranteed that any remote listeners have yet received the notification.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1710
diff changeset
    62
public class NotificationBroadcasterSupport implements NotificationEmitter {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Constructs a NotificationBroadcasterSupport where each listener is invoked by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * thread sending the notification. This constructor is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * {@link NotificationBroadcasterSupport#NotificationBroadcasterSupport(Executor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * MBeanNotificationInfo[] info) NotificationBroadcasterSupport(null, null)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public NotificationBroadcasterSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this(null, (MBeanNotificationInfo[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Constructs a NotificationBroadcasterSupport where each listener is invoked using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * the given {@link java.util.concurrent.Executor}. When {@link #sendNotification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * sendNotification} is called, a listener is selected if it was added with a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * {@link NotificationFilter}, or if {@link NotificationFilter#isNotificationEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * isNotificationEnabled} returns true for the notification being sent. The call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <code>NotificationFilter.isNotificationEnabled</code> takes place in the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * that called <code>sendNotification</code>. Then, for each selected listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * {@link Executor#execute executor.execute} is called with a command
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * that calls the <code>handleNotification</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * This constructor is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * {@link NotificationBroadcasterSupport#NotificationBroadcasterSupport(Executor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * MBeanNotificationInfo[] info) NotificationBroadcasterSupport(executor, null)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param executor an executor used by the method <code>sendNotification</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * send each notification. If it is null, the thread calling <code>sendNotification</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * will invoke the <code>handleNotification</code> method itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public NotificationBroadcasterSupport(Executor executor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this(executor, (MBeanNotificationInfo[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <p>Constructs a NotificationBroadcasterSupport with information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * about the notifications that may be sent.  Each listener is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * invoked by the thread sending the notification.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * constructor is equivalent to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * NotificationBroadcasterSupport#NotificationBroadcasterSupport(Executor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * MBeanNotificationInfo[] info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * NotificationBroadcasterSupport(null, info)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p>If the <code>info</code> array is not empty, then it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * cloned by the constructor as if by {@code info.clone()}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * each call to {@link #getNotificationInfo()} returns a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * clone.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param info an array indicating, for each notification this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * MBean may send, the name of the Java class of the notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * and the notification type.  Can be null, which is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public NotificationBroadcasterSupport(MBeanNotificationInfo... info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this(null, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <p>Constructs a NotificationBroadcasterSupport with information about the notifications that may be sent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * and where each listener is invoked using the given {@link java.util.concurrent.Executor}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <p>When {@link #sendNotification sendNotification} is called, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * listener is selected if it was added with a null {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * NotificationFilter}, or if {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * NotificationFilter#isNotificationEnabled isNotificationEnabled}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * returns true for the notification being sent. The call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <code>NotificationFilter.isNotificationEnabled</code> takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * place in the thread that called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <code>sendNotification</code>. Then, for each selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * listener, {@link Executor#execute executor.execute} is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * with a command that calls the <code>handleNotification</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p>If the <code>info</code> array is not empty, then it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * cloned by the constructor as if by {@code info.clone()}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * each call to {@link #getNotificationInfo()} returns a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * clone.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param executor an executor used by the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <code>sendNotification</code> to send each notification. If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * is null, the thread calling <code>sendNotification</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * invoke the <code>handleNotification</code> method itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param info an array indicating, for each notification this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * MBean may send, the name of the Java class of the notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * and the notification type.  Can be null, which is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public NotificationBroadcasterSupport(Executor executor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                          MBeanNotificationInfo... info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.executor = (executor != null) ? executor : defaultExecutor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        notifInfo = info == null ? NO_NOTIFICATION_INFO : info.clone();
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
     * Adds a listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param listener The listener to receive notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param filter The filter object. If filter is null, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * filtering will be performed before handling notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param handback An opaque object to be sent back to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * listener when a notification is emitted. This object cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * used by the Notification broadcaster object. It should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * resent unchanged with the notification to the listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @exception IllegalArgumentException thrown if the listener is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @see #removeNotificationListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public void addNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                        NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                        Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new IllegalArgumentException ("Listener can't be null") ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        listenerList.add(new ListenerInfo(listener, filter, handback));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public void removeNotificationListener(NotificationListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        ListenerInfo wildcard = new WildcardListenerInfo(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        boolean removed =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            listenerList.removeAll(Collections.singleton(wildcard));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (!removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new ListenerNotFoundException("Listener not registered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public void removeNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                           NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                           Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        ListenerInfo li = new ListenerInfo(listener, filter, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        boolean removed = listenerList.remove(li);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (!removed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new ListenerNotFoundException("Listener not registered " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                                "(with this filter and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                                "handback)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            // or perhaps not registered at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public MBeanNotificationInfo[] getNotificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (notifInfo.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            return notifInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return notifInfo.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Sends a notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * If an {@code Executor} was specified in the constructor, it will be given one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * task per selected listener to deliver the notification to that listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param notification The notification to send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void sendNotification(Notification notification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (notification == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        boolean enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        for (ListenerInfo li : listenerList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                enabled = li.filter == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    li.filter.isNotificationEnabled(notification);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    logger.debug("sendNotification", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if (enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                executor.execute(new SendNotifJob(notification, li));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <p>This method is called by {@link #sendNotification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * sendNotification} for each listener in order to send the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * notification to that listener.  It can be overridden in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * subclasses to change the behavior of notification delivery,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * for instance to deliver the notification in a separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * thread.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <p>The default implementation of this method is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * listener.handleNotification(notif, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param listener the listener to which the notification is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param notif the notification being delivered to the listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param handback the handback object that was supplied when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * listener was added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    protected void handleNotification(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                      Notification notif, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        listener.handleNotification(notif, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    // private stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private static class ListenerInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        NotificationListener listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        NotificationFilter filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        Object handback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        ListenerInfo(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                     NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                     Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            this.listener = listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            this.handback = handback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
18291
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
   292
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (!(o instanceof ListenerInfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            ListenerInfo li = (ListenerInfo) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (li instanceof WildcardListenerInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                return (li.listener == listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                return (li.listener == listener && li.filter == filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        && li.handback == handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
18291
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
   303
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
   304
        @Override
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
   305
        public int hashCode() {
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
   306
            return Objects.hashCode(listener);
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
   307
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private static class WildcardListenerInfo extends ListenerInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        WildcardListenerInfo(NotificationListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            super(listener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
18291
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
   315
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            assert (!(o instanceof WildcardListenerInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            return o.equals(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
18291
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
   320
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
   321
        @Override
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
   322
        public int hashCode() {
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
   323
            return super.hashCode();
18803e8ff87c 8016592: Clean-up Javac Overrides Warnings In javax/management/NotificationBroadcasterSupport.java
dxu
parents: 5506
diff changeset
   324
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private List<ListenerInfo> listenerList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        new CopyOnWriteArrayList<ListenerInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    // since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private final Executor executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private final MBeanNotificationInfo[] notifInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private final static Executor defaultExecutor = new Executor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            // DirectExecutor using caller thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            public void execute(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                r.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    private static final MBeanNotificationInfo[] NO_NOTIFICATION_INFO =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        new MBeanNotificationInfo[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private class SendNotifJob implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        public SendNotifJob(Notification notif, ListenerInfo listenerInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            this.notif = notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            this.listenerInfo = listenerInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                handleNotification(listenerInfo.listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                   notif, listenerInfo.handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    logger.debug("SendNotifJob-run", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        private final Notification notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        private final ListenerInfo listenerInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        new ClassLogger("javax.management", "NotificationBroadcasterSupport");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
}