jdk/src/java.management/share/classes/sun/management/NotificationEmitterSupport.java
author sjiang
Fri, 17 Apr 2015 09:40:02 +0200
changeset 30355 e37c7eba132f
parent 25859 3317bb8137f4
permissions -rw-r--r--
8042901: Allow com.sun.management to be in a different module to java.lang.management Reviewed-by: mchung, dfuchs, erikj, jbachorik
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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 sun.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.management.ListenerNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.management.MBeanNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.management.NotificationEmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.management.NotificationFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Abstract helper class for notification emitter support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 25859
diff changeset
    42
public abstract class NotificationEmitterSupport implements NotificationEmitter {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    protected NotificationEmitterSupport() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private Object listenerLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // Implementation of NotificationEmitter interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // Cloned from JMX NotificationBroadcasterSupport class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public void addNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                        NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                                        Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            throw new IllegalArgumentException ("Listener can't be null") ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        /* Adding a new listener takes O(n) time where n is the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
           of existing listeners.  If you have a very large number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
           listeners performance could degrade.  That's a fairly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
           surprising configuration, and it is hard to avoid this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
           behaviour while still retaining the property that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
           listenerList is not synchronized while notifications are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
           being sent through it.  If this becomes a problem, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
           possible solution would be a multiple-readers single-writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
           setup, so any number of sendNotification() calls could run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
           concurrently but they would exclude an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
           add/removeNotificationListener.  A simpler but less
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
           efficient solution would be to clone the listener list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
           every time a notification is sent.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        synchronized (listenerLock) {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
    73
            List<ListenerInfo> newList = new ArrayList<>(listenerList.size() + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            newList.addAll(listenerList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            newList.add(new ListenerInfo(listener, filter, handback));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            listenerList = newList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public void removeNotificationListener(NotificationListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        synchronized (listenerLock) {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
    84
            List<ListenerInfo> newList = new ArrayList<>(listenerList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            /* We scan the list of listeners in reverse order because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
               in forward order we would have to repeat the loop with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
               the same index after a remove.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            for (int i=newList.size()-1; i>=0; i--) {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
    89
                ListenerInfo li = newList.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (li.listener == listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    newList.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (newList.size() == listenerList.size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                throw new ListenerNotFoundException("Listener not registered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            listenerList = newList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public void removeNotificationListener(NotificationListener listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                           NotificationFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                           Object handback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throws ListenerNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        synchronized (listenerLock) {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   108
            List<ListenerInfo> newList = new ArrayList<>(listenerList);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            final int size = newList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            for (int i = 0; i < size; i++) {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   111
                ListenerInfo li =  newList.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                if (li.listener == listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    if (li.filter == filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        && li.handback == handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        newList.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        listenerList = newList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            /* We found this listener, but not with the given filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
             * and handback.  A more informative exception message may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
             * make debugging easier.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new ListenerNotFoundException("Listener not registered " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                                "with this filter and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                                "handback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new ListenerNotFoundException("Listener not registered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 25859
diff changeset
   137
    public void sendNotification(Notification notification) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (notification == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        List<ListenerInfo> currentList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        synchronized (listenerLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            currentList = listenerList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        final int size = currentList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        for (int i = 0; i < size; i++) {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   150
            ListenerInfo li =  currentList.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (li.filter == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                || li.filter.isNotificationEnabled(notification)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    li.listener.handleNotification(notification, li.handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    e.printStackTrace();
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   158
                    throw new AssertionError("Error in invoking listener");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 25859
diff changeset
   164
    public boolean hasListeners() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        synchronized (listenerLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return !listenerList.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private class ListenerInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        public NotificationListener listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        NotificationFilter filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        Object handback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        public ListenerInfo(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
            this.listener = listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            this.handback = handback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Current list of listeners, a List of ListenerInfo.  The object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * referenced by this field is never modified.  Instead, the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * is set to a new object when a listener is added or removed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * within a synchronized(this).  In this way, there is no need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * synchronize when traversing the list to send a notification to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * the listeners in it.  That avoids potential deadlocks if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * listeners end up depending on other threads that are themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * accessing this NotificationBroadcasterSupport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private List<ListenerInfo> listenerList = Collections.emptyList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    abstract public MBeanNotificationInfo[] getNotificationInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
}