1 22 package org.jboss.mx.notification; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.NoSuchElementException ; 28 29 import javax.management.JMException ; 30 import javax.management.ListenerNotFoundException ; 31 import javax.management.NotificationFilter ; 32 import javax.management.NotificationListener ; 33 34 57 public class ListenerRegistry 58 { 59 61 64 private HashMap listeners = new HashMap (); 65 66 69 private ListenerRegistrationFactory factory; 70 71 72 74 78 public ListenerRegistry() 79 { 80 this(null); 81 } 82 83 90 public ListenerRegistry(ListenerRegistrationFactory factory) 91 { 92 if (factory == null) 93 this.factory = new DefaultListenerRegistrationFactory(); 94 else 95 this.factory = factory; 96 } 97 98 100 109 public void add(NotificationListener listener, NotificationFilter filter, Object handback) 110 throws JMException 111 { 112 if (listener == null) 113 throw new IllegalArgumentException ("Null listener"); 114 115 synchronized(listeners) 116 { 117 HashMap newListeners = (HashMap ) listeners.clone(); 118 119 ArrayList registrations = (ArrayList ) newListeners.get(listener); 120 if (registrations == null) 121 { 122 registrations = new ArrayList (); 123 newListeners.put(listener, registrations); 124 } 125 else 126 { 127 registrations = (ArrayList ) registrations.clone(); 128 newListeners.put(listener, registrations); 129 } 130 131 registrations.add(factory.create(listener, filter, handback)); 132 133 listeners = newListeners; 134 } 135 } 136 137 143 public void remove(NotificationListener listener) 144 throws ListenerNotFoundException 145 { 146 ArrayList registrations = null; 147 synchronized(listeners) 148 { 149 if (listeners.containsKey(listener) == false) 150 throw new ListenerNotFoundException ("Listener not found " + listener); 151 152 HashMap newListeners = (HashMap ) listeners.clone(); 153 154 registrations = (ArrayList ) newListeners.remove(listener); 155 156 listeners = newListeners; 157 } 158 159 for (Iterator iterator = registrations.iterator(); iterator.hasNext();) 160 { 161 ListenerRegistration registration = (ListenerRegistration) iterator.next(); 162 registration.removed(); 163 } 164 } 165 166 174 public void remove(NotificationListener listener, NotificationFilter filter, Object handback) 175 throws ListenerNotFoundException 176 { 177 ListenerRegistration registration = null; 178 synchronized(listeners) 179 { 180 ArrayList registrations = (ArrayList ) listeners.get(listener); 181 if (registrations == null) 182 throw new ListenerNotFoundException ("No registristrations for listener not listener=" + listener + 183 " filter=" + filter + " handback=" + handback); 184 185 registration = new DefaultListenerRegistration(listener, filter, handback); 186 int index = registrations.indexOf(registration); 187 if (index == -1) 188 throw new ListenerNotFoundException ("Listener not found listener=" + listener + 189 " filter=" + filter + " handback=" + handback); 190 191 HashMap newListeners = (HashMap ) listeners.clone(); 192 193 registrations = (ArrayList ) registrations.clone(); 194 registration = (ListenerRegistration) registrations.remove(index); 195 if (registrations.isEmpty()) 196 newListeners.remove(listener); 197 else 198 newListeners.put(listener, registrations); 199 200 listeners = newListeners; 201 } 202 203 registration.removed(); 204 } 205 206 209 public void removeAll() 210 { 211 synchronized (listeners) 212 { 213 listeners.clear(); 214 } 215 } 216 217 225 public ListenerRegistrationIterator iterator() 226 { 227 return new ListenerRegistrationIterator(); 228 } 229 230 235 public boolean isEmpty() 236 { 237 return listeners.isEmpty(); 238 } 239 240 242 public class ListenerRegistrationIterator 243 implements Iterator 244 { 245 private Iterator listenerIterator; 246 private Iterator registrationIterator; 247 248 251 public ListenerRegistrationIterator() 252 { 253 listenerIterator = listeners.values().iterator(); 254 } 255 256 public boolean hasNext() 257 { 258 if (registrationIterator == null || registrationIterator.hasNext() == false) 259 { 260 do 261 { 262 if (listenerIterator.hasNext() == false) 263 return false; 264 265 registrationIterator = ((ArrayList ) listenerIterator.next()).iterator(); 266 } 267 while (registrationIterator.hasNext() == false); 268 } 269 return true; 270 } 271 272 public Object next() 273 { 274 if (hasNext() == false) 275 throw new NoSuchElementException ("Use hasNext before next"); 276 277 return registrationIterator.next(); 278 } 279 280 283 public ListenerRegistration nextRegistration() 284 { 285 return (ListenerRegistration) next(); 286 } 287 288 291 public void remove() 292 { 293 throw new UnsupportedOperationException ("remove is not supported"); 294 } 295 } 296 } 297 | Popular Tags |