jdk/src/share/classes/javax/management/NotificationBroadcasterSupport.java
author sjiang
Tue, 09 Dec 2008 20:50:45 +0100
changeset 1710 0bdf9295cab1
parent 1247 b4c26443dee5
child 4156 acaa49a2768a
permissions -rw-r--r--
6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear Reviewed-by: emcmanus
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 1999-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
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.concurrent.CopyOnWriteArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.concurrent.Executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.jmx.remote.util.ClassLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>Provides an implementation of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * javax.management.NotificationEmitter NotificationEmitter}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * interface.  This can be used as the super class of an MBean that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * sends notifications.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>By default, the notification dispatch model is synchronous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * That is, when a thread calls sendNotification, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>NotificationListener.handleNotification</code> method of each listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * is called within that thread. You can override this default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * by overriding <code>handleNotification</code> in a subclass, or by passing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Executor to the constructor.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>If the method call of a filter or listener throws an {@link Exception},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * then that exception does not prevent other listeners from being invoked.  However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * if the method call of a filter or of {@code Executor.execute} or of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * {@code handleNotification} (when no {@code Excecutor} is specified) throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * {@link Error}, then that {@code Error} is propagated to the caller of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link #sendNotification sendNotification}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>Remote listeners added using the JMX Remote API (see JMXConnector) are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * usually called synchronously.  That is, when sendNotification returns, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * not guaranteed that any remote listeners have yet received the notification.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
    61
public class NotificationBroadcasterSupport
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 2
diff changeset
    62
        implements NotificationEmitter, SendNotification {
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
    }
1710
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   252
    /**
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   253
     * Returns true if there are any listeners.
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   254
     *
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   255
     * @return true if there is at least one listener that has been added with
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   256
     * {@code addNotificationListener} and not subsequently removed with
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   257
     * {@code removeNotificationListener} or {@code removeAllNotificationListeners}.
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   258
     * @since 1.7
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   259
     */
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   260
    public boolean isListenedTo() {
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   261
        return listenerList.size() > 0;
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   262
    }
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   263
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   264
    /**
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   265
     * Removes all listeners.
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   266
     *
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   267
     * @since 1.7
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   268
     */
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   269
    public void removeAllNotificationListeners() {
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   270
        listenerList.clear();
0bdf9295cab1 6336980: NotificationBroadcasterSupport: to tell whether there are listeners and to do clear
sjiang
parents: 1247
diff changeset
   271
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <p>This method is called by {@link #sendNotification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * sendNotification} for each listener in order to send the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * notification to that listener.  It can be overridden in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * subclasses to change the behavior of notification delivery,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * for instance to deliver the notification in a separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * thread.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <p>The default implementation of this method is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * listener.handleNotification(notif, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param listener the listener to which the notification is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param notif the notification being delivered to the listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param handback the handback object that was supplied when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * listener was added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    protected void handleNotification(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                      Notification notif, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        listener.handleNotification(notif, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    // private stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private static class ListenerInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        NotificationListener listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        NotificationFilter filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        Object handback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        ListenerInfo(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                     NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                     Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            this.listener = listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            this.handback = handback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (!(o instanceof ListenerInfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            ListenerInfo li = (ListenerInfo) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (li instanceof WildcardListenerInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                return (li.listener == listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                return (li.listener == listener && li.filter == filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        && li.handback == handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private static class WildcardListenerInfo extends ListenerInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        WildcardListenerInfo(NotificationListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            super(listener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            assert (!(o instanceof WildcardListenerInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return o.equals(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private List<ListenerInfo> listenerList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        new CopyOnWriteArrayList<ListenerInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    // since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private final Executor executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    private final MBeanNotificationInfo[] notifInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private final static Executor defaultExecutor = new Executor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            // DirectExecutor using caller thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            public void execute(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                r.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private static final MBeanNotificationInfo[] NO_NOTIFICATION_INFO =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        new MBeanNotificationInfo[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private class SendNotifJob implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        public SendNotifJob(Notification notif, ListenerInfo listenerInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            this.notif = notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            this.listenerInfo = listenerInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                handleNotification(listenerInfo.listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                   notif, listenerInfo.handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    logger.debug("SendNotifJob-run", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        private final Notification notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        private final ListenerInfo listenerInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        new ClassLogger("javax.management", "NotificationBroadcasterSupport");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
}