Path: blob/master/src/java.management/share/classes/sun/management/NotificationEmitterSupport.java
41152 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management;2627import javax.management.ListenerNotFoundException;28import javax.management.MBeanNotificationInfo;29import javax.management.Notification;30import javax.management.NotificationEmitter;31import javax.management.NotificationFilter;32import javax.management.NotificationListener;3334import java.util.List;35import java.util.ArrayList;36import java.util.Collections;3738/**39* Abstract helper class for notification emitter support.40*/41public abstract class NotificationEmitterSupport implements NotificationEmitter {4243protected NotificationEmitterSupport() {44}4546private Object listenerLock = new Object();4748// Implementation of NotificationEmitter interface49// Cloned from JMX NotificationBroadcasterSupport class.50public void addNotificationListener(NotificationListener listener,51NotificationFilter filter,52Object handback) {5354if (listener == null) {55throw new IllegalArgumentException ("Listener can't be null") ;56}5758/* Adding a new listener takes O(n) time where n is the number59of existing listeners. If you have a very large number of60listeners performance could degrade. That's a fairly61surprising configuration, and it is hard to avoid this62behaviour while still retaining the property that the63listenerList is not synchronized while notifications are64being sent through it. If this becomes a problem, a65possible solution would be a multiple-readers single-writer66setup, so any number of sendNotification() calls could run67concurrently but they would exclude an68add/removeNotificationListener. A simpler but less69efficient solution would be to clone the listener list70every time a notification is sent. */71synchronized (listenerLock) {72List<ListenerInfo> newList = new ArrayList<>(listenerList.size() + 1);73newList.addAll(listenerList);74newList.add(new ListenerInfo(listener, filter, handback));75listenerList = newList;76}77}7879public void removeNotificationListener(NotificationListener listener)80throws ListenerNotFoundException {8182synchronized (listenerLock) {83List<ListenerInfo> newList = new ArrayList<>(listenerList);84/* We scan the list of listeners in reverse order because85in forward order we would have to repeat the loop with86the same index after a remove. */87for (int i=newList.size()-1; i>=0; i--) {88ListenerInfo li = newList.get(i);8990if (li.listener == listener)91newList.remove(i);92}93if (newList.size() == listenerList.size())94throw new ListenerNotFoundException("Listener not registered");95listenerList = newList;96}97}9899public void removeNotificationListener(NotificationListener listener,100NotificationFilter filter,101Object handback)102throws ListenerNotFoundException {103104boolean found = false;105106synchronized (listenerLock) {107List<ListenerInfo> newList = new ArrayList<>(listenerList);108final int size = newList.size();109for (int i = 0; i < size; i++) {110ListenerInfo li = newList.get(i);111112if (li.listener == listener) {113found = true;114if (li.filter == filter115&& li.handback == handback) {116newList.remove(i);117listenerList = newList;118return;119}120}121}122}123124if (found) {125/* We found this listener, but not with the given filter126* and handback. A more informative exception message may127* make debugging easier. */128throw new ListenerNotFoundException("Listener not registered " +129"with this filter and " +130"handback");131} else {132throw new ListenerNotFoundException("Listener not registered");133}134}135136public void sendNotification(Notification notification) {137138if (notification == null) {139return;140}141142List<ListenerInfo> currentList;143synchronized (listenerLock) {144currentList = listenerList;145}146147final int size = currentList.size();148for (int i = 0; i < size; i++) {149ListenerInfo li = currentList.get(i);150151if (li.filter == null152|| li.filter.isNotificationEnabled(notification)) {153try {154li.listener.handleNotification(notification, li.handback);155} catch (Exception e) {156e.printStackTrace();157throw new AssertionError("Error in invoking listener");158}159}160}161}162163public boolean hasListeners() {164synchronized (listenerLock) {165return !listenerList.isEmpty();166}167}168169private class ListenerInfo {170public NotificationListener listener;171NotificationFilter filter;172Object handback;173174public ListenerInfo(NotificationListener listener,175NotificationFilter filter,176Object handback) {177this.listener = listener;178this.filter = filter;179this.handback = handback;180}181}182183/**184* Current list of listeners, a List of ListenerInfo. The object185* referenced by this field is never modified. Instead, the field186* is set to a new object when a listener is added or removed,187* within a synchronized(this). In this way, there is no need to188* synchronize when traversing the list to send a notification to189* the listeners in it. That avoids potential deadlocks if the190* listeners end up depending on other threads that are themselves191* accessing this NotificationBroadcasterSupport.192*/193private List<ListenerInfo> listenerList = Collections.emptyList();194195abstract public MBeanNotificationInfo[] getNotificationInfo();196}197198199