1 31 package org.objectweb.proactive.core.event; 32 33 import org.apache.log4j.Logger; 34 35 36 64 public abstract class AbstractEventProducer implements java.io.Serializable { 65 protected static Logger logger = Logger.getLogger(AbstractEventProducer.class.getName()); 69 70 71 protected boolean shouldSerializeListeners; 72 73 76 protected transient ListenerList eventListeners; 77 78 82 85 public AbstractEventProducer() { 86 this(false); 87 } 88 89 94 public AbstractEventProducer(boolean shouldSerializeListeners) { 95 this.shouldSerializeListeners = shouldSerializeListeners; 96 if (shouldSerializeListeners) { 97 eventListeners = new PlainListenerList(); 98 } else { 99 eventListeners = new WeakReferenceListenerList(); 100 } 101 } 102 103 110 114 protected boolean hasListeners() { 115 return !eventListeners.isEmpty(); 116 } 117 118 122 protected void addListener(ProActiveListener listener) { 123 synchronized (eventListeners) { 124 if (!eventListeners.contains(listener)) { 125 eventListeners.add(listener); 126 } 127 } 128 } 129 130 134 protected void removeListener(ProActiveListener listener) { 135 synchronized (eventListeners) { 136 eventListeners.remove(listener); 137 } 138 } 139 140 145 protected void notifyAllListeners(ProActiveEvent event) { 146 synchronized (eventListeners) { 147 java.util.Iterator iterator = eventListeners.iterator(); 148 while (iterator.hasNext()) { 149 notifyOneListener((ProActiveListener) iterator.next(), event); 150 } 151 } 152 } 153 154 159 protected abstract void notifyOneListener(ProActiveListener listener, 160 ProActiveEvent event); 161 162 166 169 private void writeObject(java.io.ObjectOutputStream s) 170 throws java.io.IOException { 171 s.writeBoolean(shouldSerializeListeners); 172 if (shouldSerializeListeners) { 173 s.writeObject(eventListeners); 174 } 175 } 176 177 182 private void readObject(java.io.ObjectInputStream s) 183 throws java.io.IOException , ClassNotFoundException { 184 shouldSerializeListeners = s.readBoolean(); 185 if (shouldSerializeListeners) { 186 eventListeners = (ListenerList) s.readObject(); 187 } else { 188 eventListeners = new WeakReferenceListenerList(); 189 } 190 } 191 192 196 207 protected interface ListenerList { 208 209 213 public boolean isEmpty(); 214 215 219 public int size(); 220 221 225 public boolean contains(ProActiveListener listener); 226 227 232 public boolean add(ProActiveListener listener); 233 234 239 public boolean remove(ProActiveListener listener); 240 241 245 public java.util.Iterator iterator(); 246 } 247 248 258 private class PlainListenerList implements java.io.Serializable , 259 ListenerList { 260 protected java.util.ArrayList list; 261 262 public PlainListenerList() { 263 list = new java.util.ArrayList (); 264 } 265 266 public boolean isEmpty() { 267 return list.isEmpty(); 268 } 269 270 public int size() { 271 return list.size(); 272 } 273 274 public boolean contains(ProActiveListener listener) { 275 return list.contains(listener); 276 } 277 278 public boolean add(ProActiveListener listener) { 279 return list.add(listener); 280 } 281 282 public boolean remove(ProActiveListener listener) { 283 return list.remove(listener); 284 } 285 286 public java.util.Iterator iterator() { 287 return list.iterator(); 288 } 289 } 290 291 303 private class WeakReferenceListenerList extends PlainListenerList { 304 public WeakReferenceListenerList() { 305 } 306 307 public boolean contains(ProActiveListener listener) { 308 java.util.Iterator iterator = iterator(); 309 while (iterator.hasNext()) { 310 if (iterator.next() == listener) { 311 return true; 312 } 313 } 314 return false; 315 } 316 317 public boolean add(ProActiveListener listener) { 318 return list.add(new java.lang.ref.WeakReference (listener)); 319 } 320 321 public boolean remove(ProActiveListener listener) { 322 java.util.Iterator iterator = iterator(); 323 while (iterator.hasNext()) { 324 if (iterator.next() == listener) { 325 iterator.remove(); 326 return true; 327 } 328 } 329 return false; 330 } 331 332 public java.util.Iterator iterator() { 333 return new WeakReferenceIterator(list.iterator()); 334 } 335 } 336 337 350 private class WeakReferenceIterator implements java.util.Iterator { 351 private java.util.Iterator iterator; 352 private Object nextObject; 353 354 public WeakReferenceIterator(java.util.Iterator iterator) { 355 this.iterator = iterator; 356 nextObject = getNextObject(); 357 } 358 359 public boolean hasNext() { 360 return nextObject != null; 361 } 362 363 public Object next() { 364 Object result = nextObject; 365 nextObject = getNextObject(); 366 return result; 367 } 368 369 public void remove() { 370 iterator.remove(); 371 } 372 373 private Object getNextObject() { 374 while (iterator.hasNext()) { 375 java.lang.ref.WeakReference ref = (java.lang.ref.WeakReference ) iterator.next(); 376 Object target = ref.get(); 377 if (target == null) { 378 iterator.remove(); 381 } else { 382 return target; 383 } 384 } 385 return null; 386 } 387 } 388 } 390 | Popular Tags |