jdk/src/share/classes/javax/management/NotificationBroadcasterSupport.java
author emcmanus
Wed, 21 Oct 2009 17:33:18 +0200
changeset 4156 acaa49a2768a
parent 1710 0bdf9295cab1
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
/*
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1710
diff changeset
     2
 * Copyright 1999-2007 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
 */
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1710
diff changeset
    61
public class NotificationBroadcasterSupport implements NotificationEmitter {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Constructs a NotificationBroadcasterSupport where each listener is invoked by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * thread sending the notification. This constructor is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * {@link NotificationBroadcasterSupport#NotificationBroadcasterSupport(Executor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * MBeanNotificationInfo[] info) NotificationBroadcasterSupport(null, null)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public NotificationBroadcasterSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this(null, (MBeanNotificationInfo[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Constructs a NotificationBroadcasterSupport where each listener is invoked using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * the given {@link java.util.concurrent.Executor}. When {@link #sendNotification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * sendNotification} is called, a listener is selected if it was added with a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * {@link NotificationFilter}, or if {@link NotificationFilter#isNotificationEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * isNotificationEnabled} returns true for the notification being sent. The call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <code>NotificationFilter.isNotificationEnabled</code> takes place in the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * that called <code>sendNotification</code>. Then, for each selected listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * {@link Executor#execute executor.execute} is called with a command
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * that calls the <code>handleNotification</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * This constructor is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * {@link NotificationBroadcasterSupport#NotificationBroadcasterSupport(Executor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * MBeanNotificationInfo[] info) NotificationBroadcasterSupport(executor, null)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param executor an executor used by the method <code>sendNotification</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * send each notification. If it is null, the thread calling <code>sendNotification</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * will invoke the <code>handleNotification</code> method itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public NotificationBroadcasterSupport(Executor executor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this(executor, (MBeanNotificationInfo[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <p>Constructs a NotificationBroadcasterSupport with information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * about the notifications that may be sent.  Each listener is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * invoked by the thread sending the notification.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * constructor is equivalent to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * NotificationBroadcasterSupport#NotificationBroadcasterSupport(Executor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * MBeanNotificationInfo[] info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * NotificationBroadcasterSupport(null, info)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <p>If the <code>info</code> array is not empty, then it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * cloned by the constructor as if by {@code info.clone()}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * each call to {@link #getNotificationInfo()} returns a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * clone.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param info an array indicating, for each notification this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * MBean may send, the name of the Java class of the notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * and the notification type.  Can be null, which is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public NotificationBroadcasterSupport(MBeanNotificationInfo... info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this(null, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <p>Constructs a NotificationBroadcasterSupport with information about the notifications that may be sent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * and where each listener is invoked using the given {@link java.util.concurrent.Executor}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <p>When {@link #sendNotification sendNotification} is called, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * listener is selected if it was added with a null {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * NotificationFilter}, or if {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * NotificationFilter#isNotificationEnabled isNotificationEnabled}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * returns true for the notification being sent. The call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <code>NotificationFilter.isNotificationEnabled</code> takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * place in the thread that called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <code>sendNotification</code>. Then, for each selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * listener, {@link Executor#execute executor.execute} is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * with a command that calls the <code>handleNotification</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <p>If the <code>info</code> array is not empty, then it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * cloned by the constructor as if by {@code info.clone()}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * each call to {@link #getNotificationInfo()} returns a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * clone.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param executor an executor used by the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <code>sendNotification</code> to send each notification. If it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * is null, the thread calling <code>sendNotification</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * invoke the <code>handleNotification</code> method itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param info an array indicating, for each notification this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * MBean may send, the name of the Java class of the notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * and the notification type.  Can be null, which is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public NotificationBroadcasterSupport(Executor executor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                          MBeanNotificationInfo... info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.executor = (executor != null) ? executor : defaultExecutor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        notifInfo = info == null ? NO_NOTIFICATION_INFO : info.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Adds a listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param listener The listener to receive notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param filter The filter object. If filter is null, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * filtering will be performed before handling notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param handback An opaque object to be sent back to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * listener when a notification is emitted. This object cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * used by the Notification broadcaster object. It should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * resent unchanged with the notification to the listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @exception IllegalArgumentException thrown if the listener is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @see #removeNotificationListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void addNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                        NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                        Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            throw new IllegalArgumentException ("Listener can't be null") ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        listenerList.add(new ListenerInfo(listener, filter, handback));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public void removeNotificationListener(NotificationListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        ListenerInfo wildcard = new WildcardListenerInfo(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        boolean removed =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            listenerList.removeAll(Collections.singleton(wildcard));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (!removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new ListenerNotFoundException("Listener not registered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public void removeNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                           NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                           Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        ListenerInfo li = new ListenerInfo(listener, filter, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        boolean removed = listenerList.remove(li);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (!removed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            throw new ListenerNotFoundException("Listener not registered " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                                "(with this filter and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                                "handback)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // or perhaps not registered at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public MBeanNotificationInfo[] getNotificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (notifInfo.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return notifInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return notifInfo.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
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
     * Sends a notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * If an {@code Executor} was specified in the constructor, it will be given one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * task per selected listener to deliver the notification to that listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param notification The notification to send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public void sendNotification(Notification notification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (notification == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        boolean enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        for (ListenerInfo li : listenerList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                enabled = li.filter == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    li.filter.isNotificationEnabled(notification);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    logger.debug("sendNotification", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                executor.execute(new SendNotifJob(notification, li));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
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
     * <p>This method is called by {@link #sendNotification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * sendNotification} for each listener in order to send the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * notification to that listener.  It can be overridden in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * subclasses to change the behavior of notification delivery,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * for instance to deliver the notification in a separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * thread.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <p>The default implementation of this method is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * listener.handleNotification(notif, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param listener the listener to which the notification is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param notif the notification being delivered to the listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param handback the handback object that was supplied when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * listener was added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    protected void handleNotification(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                      Notification notif, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        listener.handleNotification(notif, handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // private stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private static class ListenerInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        NotificationListener listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        NotificationFilter filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        Object handback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        ListenerInfo(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                     NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                     Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            this.listener = listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            this.handback = handback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (!(o instanceof ListenerInfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            ListenerInfo li = (ListenerInfo) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (li instanceof WildcardListenerInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                return (li.listener == listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                return (li.listener == listener && li.filter == filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        && li.handback == handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    private static class WildcardListenerInfo extends ListenerInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        WildcardListenerInfo(NotificationListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            super(listener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            assert (!(o instanceof WildcardListenerInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return o.equals(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private List<ListenerInfo> listenerList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        new CopyOnWriteArrayList<ListenerInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    // since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private final Executor executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private final MBeanNotificationInfo[] notifInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private final static Executor defaultExecutor = new Executor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // DirectExecutor using caller thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            public void execute(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                r.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private static final MBeanNotificationInfo[] NO_NOTIFICATION_INFO =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        new MBeanNotificationInfo[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private class SendNotifJob implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        public SendNotifJob(Notification notif, ListenerInfo listenerInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            this.notif = notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            this.listenerInfo = listenerInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                handleNotification(listenerInfo.listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                   notif, listenerInfo.handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                if (logger.debugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    logger.debug("SendNotifJob-run", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        private final Notification notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        private final ListenerInfo listenerInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        new ClassLogger("javax.management", "NotificationBroadcasterSupport");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
}