1 7 8 package javax.management; 9 10 import java.util.ArrayList ; 11 import java.util.Collections ; 12 import java.util.List ; 13 14 import com.sun.jmx.trace.Trace; 15 16 55 public class NotificationBroadcasterSupport implements NotificationEmitter { 56 57 70 public void addNotificationListener(NotificationListener listener, 71 NotificationFilter filter, 72 Object handback) { 73 74 if (listener == null) { 75 throw new IllegalArgumentException ("Listener can't be null") ; 76 } 77 78 91 synchronized (lock) { 92 List newList = new ArrayList (listenerList.size() + 1); 93 newList.addAll(listenerList); 94 newList.add(new ListenerInfo(listener, filter, handback)); 95 listenerList = newList; 96 } 97 } 98 99 public void removeNotificationListener(NotificationListener listener) 100 throws ListenerNotFoundException { 101 102 synchronized (lock) { 103 List newList = new ArrayList (listenerList); 104 107 for (int i=newList.size()-1; i>=0; i--) { 108 ListenerInfo li = (ListenerInfo)newList.get(i); 109 110 if (li.listener == listener) 111 newList.remove(i); 112 } 113 if (newList.size() == listenerList.size()) 114 throw new ListenerNotFoundException ("Listener not registered"); 115 listenerList = newList; 116 } 117 } 118 119 public void removeNotificationListener(NotificationListener listener, 120 NotificationFilter filter, 121 Object handback) 122 throws ListenerNotFoundException { 123 124 boolean found = false; 125 126 synchronized (lock) { 127 List newList = new ArrayList (listenerList); 128 final int size = newList.size(); 129 for (int i = 0; i < size; i++) { 130 ListenerInfo li = (ListenerInfo) newList.get(i); 131 132 if (li.listener == listener) { 133 found = true; 134 if (li.filter == filter 135 && li.handback == handback) { 136 newList.remove(i); 137 listenerList = newList; 138 return; 139 } 140 } 141 } 142 } 143 144 if (found) { 145 148 throw new ListenerNotFoundException ("Listener not registered " + 149 "with this filter and " + 150 "handback"); 151 } else { 152 throw new ListenerNotFoundException ("Listener not registered"); 153 } 154 } 155 156 public MBeanNotificationInfo [] getNotificationInfo() { 157 return new MBeanNotificationInfo [0]; 158 } 159 160 161 166 public void sendNotification(Notification notification) { 167 168 if (notification == null) { 169 return; 170 } 171 172 List currentList; 173 synchronized (lock) { 174 currentList = listenerList; 175 } 176 177 final int size = currentList.size(); 178 for (int i = 0; i < size; i++) { 179 ListenerInfo li = (ListenerInfo) currentList.get(i); 180 181 if (li.filter == null 182 || li.filter.isNotificationEnabled(notification)) { 183 try { 184 this.handleNotification(li.listener, notification, 185 li.handback); 186 } catch (Exception e) { 187 trace("sendNotification", 188 "exception from listener: " + e); 189 } 190 } 191 } 192 } 193 194 219 protected void handleNotification(NotificationListener listener, 220 Notification notif, Object handback) { 221 listener.handleNotification(notif, handback); 222 } 223 224 226 private static void trace(String method, String message) { 227 if (Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MISC)) { 228 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MISC, 229 NotificationBroadcasterSupport .class.getName(), 230 method, message); 231 } 232 } 233 234 private class ListenerInfo { 235 public NotificationListener listener; 236 NotificationFilter filter; 237 Object handback; 238 239 public ListenerInfo(NotificationListener listener, 240 NotificationFilter filter, 241 Object handback) { 242 this.listener = listener; 243 this.filter = filter; 244 this.handback = handback; 245 } 246 } 247 248 258 private List listenerList = Collections.EMPTY_LIST; 259 260 277 private final Object lock = new Object (); 278 } 279 | Popular Tags |