< prev index next >

jdk/src/java.management/share/classes/sun/management/NotificationEmitterSupport.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.management;
  27 
  28 import javax.management.ListenerNotFoundException;
  29 import javax.management.MBeanNotificationInfo;
  30 import javax.management.Notification;
  31 import javax.management.NotificationEmitter;
  32 import javax.management.NotificationFilter;
  33 import javax.management.NotificationListener;
  34 
  35 import java.util.List;
  36 import java.util.ArrayList;
  37 import java.util.ListIterator;
  38 import java.util.Collections;
  39 
  40 /**
  41  * Abstract helper class for notification emitter support.
  42  */
  43 abstract class NotificationEmitterSupport implements NotificationEmitter {
  44 
  45     protected NotificationEmitterSupport() {
  46     }
  47 
  48     private Object listenerLock = new Object();
  49 
  50     // Implementation of NotificationEmitter interface
  51     // Cloned from JMX NotificationBroadcasterSupport class.
  52     public void addNotificationListener(NotificationListener listener,
  53                                         NotificationFilter filter,
  54                                         Object handback) {
  55 
  56         if (listener == null) {
  57             throw new IllegalArgumentException ("Listener can't be null") ;
  58         }
  59 
  60         /* Adding a new listener takes O(n) time where n is the number
  61            of existing listeners.  If you have a very large number of
  62            listeners performance could degrade.  That's a fairly
  63            surprising configuration, and it is hard to avoid this


 118                         newList.remove(i);
 119                         listenerList = newList;
 120                         return;
 121                     }
 122                 }
 123             }
 124         }
 125 
 126         if (found) {
 127             /* We found this listener, but not with the given filter
 128              * and handback.  A more informative exception message may
 129              * make debugging easier.  */
 130             throw new ListenerNotFoundException("Listener not registered " +
 131                                                 "with this filter and " +
 132                                                 "handback");
 133         } else {
 134             throw new ListenerNotFoundException("Listener not registered");
 135         }
 136     }
 137 
 138     void sendNotification(Notification notification) {
 139 
 140         if (notification == null) {
 141             return;
 142         }
 143 
 144         List<ListenerInfo> currentList;
 145         synchronized (listenerLock) {
 146             currentList = listenerList;
 147         }
 148 
 149         final int size = currentList.size();
 150         for (int i = 0; i < size; i++) {
 151             ListenerInfo li =  currentList.get(i);
 152 
 153             if (li.filter == null
 154                 || li.filter.isNotificationEnabled(notification)) {
 155                 try {
 156                     li.listener.handleNotification(notification, li.handback);
 157                 } catch (Exception e) {
 158                     e.printStackTrace();
 159                     throw new AssertionError("Error in invoking listener");
 160                 }
 161             }
 162         }
 163     }
 164 
 165     boolean hasListeners() {
 166         synchronized (listenerLock) {
 167             return !listenerList.isEmpty();
 168         }
 169     }
 170 
 171     private class ListenerInfo {
 172         public NotificationListener listener;
 173         NotificationFilter filter;
 174         Object handback;
 175 
 176         public ListenerInfo(NotificationListener listener,
 177                             NotificationFilter filter,
 178                             Object handback) {
 179             this.listener = listener;
 180             this.filter = filter;
 181             this.handback = handback;
 182         }
 183     }
 184 
 185     /**
   1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.management;
  27 
  28 import javax.management.ListenerNotFoundException;
  29 import javax.management.MBeanNotificationInfo;
  30 import javax.management.Notification;
  31 import javax.management.NotificationEmitter;
  32 import javax.management.NotificationFilter;
  33 import javax.management.NotificationListener;
  34 
  35 import java.util.List;
  36 import java.util.ArrayList;

  37 import java.util.Collections;
  38 
  39 /**
  40  * Abstract helper class for notification emitter support.
  41  */
  42 public abstract class NotificationEmitterSupport implements NotificationEmitter {
  43 
  44     protected NotificationEmitterSupport() {
  45     }
  46 
  47     private Object listenerLock = new Object();
  48 
  49     // Implementation of NotificationEmitter interface
  50     // Cloned from JMX NotificationBroadcasterSupport class.
  51     public void addNotificationListener(NotificationListener listener,
  52                                         NotificationFilter filter,
  53                                         Object handback) {
  54 
  55         if (listener == null) {
  56             throw new IllegalArgumentException ("Listener can't be null") ;
  57         }
  58 
  59         /* Adding a new listener takes O(n) time where n is the number
  60            of existing listeners.  If you have a very large number of
  61            listeners performance could degrade.  That's a fairly
  62            surprising configuration, and it is hard to avoid this


 117                         newList.remove(i);
 118                         listenerList = newList;
 119                         return;
 120                     }
 121                 }
 122             }
 123         }
 124 
 125         if (found) {
 126             /* We found this listener, but not with the given filter
 127              * and handback.  A more informative exception message may
 128              * make debugging easier.  */
 129             throw new ListenerNotFoundException("Listener not registered " +
 130                                                 "with this filter and " +
 131                                                 "handback");
 132         } else {
 133             throw new ListenerNotFoundException("Listener not registered");
 134         }
 135     }
 136 
 137     public void sendNotification(Notification notification) {
 138 
 139         if (notification == null) {
 140             return;
 141         }
 142 
 143         List<ListenerInfo> currentList;
 144         synchronized (listenerLock) {
 145             currentList = listenerList;
 146         }
 147 
 148         final int size = currentList.size();
 149         for (int i = 0; i < size; i++) {
 150             ListenerInfo li =  currentList.get(i);
 151 
 152             if (li.filter == null
 153                 || li.filter.isNotificationEnabled(notification)) {
 154                 try {
 155                     li.listener.handleNotification(notification, li.handback);
 156                 } catch (Exception e) {
 157                     e.printStackTrace();
 158                     throw new AssertionError("Error in invoking listener");
 159                 }
 160             }
 161         }
 162     }
 163 
 164     public boolean hasListeners() {
 165         synchronized (listenerLock) {
 166             return !listenerList.isEmpty();
 167         }
 168     }
 169 
 170     private class ListenerInfo {
 171         public NotificationListener listener;
 172         NotificationFilter filter;
 173         Object handback;
 174 
 175         public ListenerInfo(NotificationListener listener,
 176                             NotificationFilter filter,
 177                             Object handback) {
 178             this.listener = listener;
 179             this.filter = filter;
 180             this.handback = handback;
 181         }
 182     }
 183 
 184     /**
< prev index next >