private void register( Adapter listener, AbstractEventFilter eventFilterTree, ListenerTypeEnum listenerType) { // Check preconditions for parameters if (listener == null) { throw new IllegalArgumentException("Event listener must not be null"); } if (eventFilterTree == null) { throw new IllegalArgumentException("Event filter must not be null"); } // Use WeakReference to avoid dangling registrations WeakReference<Adapter> listenerRef = new WeakReference<Adapter>(listener, adaptersNoLongerStronglyReferenced); // delegate registration to RegistrationManager // The event filter is cloned, because the calculation of the DNF will modify the filter tree registrationManager.register(eventFilterTree.clone(), listenerRef, listenerType); // instantiate and associate notifier AdapterCapsule notifier = null; if (listenerType.matches(listenersForNotifier)) { notifier = new AdapterCapsule(listenerRef, listenerType, this); } else if (listenerType.matches(listenersForDeferringNotifier)) { notifier = new DeferringNotifier(listenerRef, listenerType, this); } else { logger.warning("Unkown listenerType " + listenerType); } addNotifierForListener(notifier); }
void deregister(Reference<? extends Adapter> listenerRef) { Adapter adapter = listenerRef.get(); if (adapter == null) { // WeakHashMaps with adapter as key don't need to be taken care of anymore registrationManager.deregister(listenerRef); } else { deregister(adapter); } }
/** * This method notifies all interested listeners by invoking the fireEvent() method on their * associated Notifier. * * @param event the event that will be delivered to clients */ private void fireEvent(Notification event) { Collection<WeakReference<? extends Adapter>> listeners = registrationManager.getListenersFor(event); for (WeakReference<? extends Adapter> listenerRef : listeners) { AdapterCapsule notifier = getNotifierForListener(listenerRef, ListenerTypeEnum.postChange); if (notifier != null) { notifier.fireEvent(event); } } }
public String toString() { return registrationManager.toString(); }
public void deregister(Adapter listener) { // TODO what if a listener is being removed that has pending events?? -> EventDeferring registrationManager.deregister(listener); // remove Notifier(s) for listener removeListener(listener); }