1 22 package org.jboss.kernel.plugins.event; 23 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.ListIterator ; 27 import java.util.Map ; 28 29 import org.jboss.kernel.spi.event.KernelEvent; 30 import org.jboss.kernel.spi.event.KernelEventEmitter; 31 import org.jboss.kernel.spi.event.KernelEventFilter; 32 import org.jboss.kernel.spi.event.KernelEventListener; 33 import org.jboss.logging.Logger; 34 import org.jboss.util.collection.CollectionsFactory; 35 36 42 public class AbstractEventEmitter implements KernelEventEmitter 43 { 44 45 private static final Logger log = Logger.getLogger(AbstractEventEmitter.class); 46 47 48 protected static final Object NULL = new Object (); 49 50 51 protected static final KernelEventFilter NULL_FILTER = new KernelEventFilter() 52 { 53 public boolean wantEvent(KernelEvent event, Object handback) 54 { 55 return false; 56 } 57 }; 58 59 60 protected Map <KernelEventFilter, Map <Object , List <KernelEventListener>>> eventListenerRegistry = CollectionsFactory.createConcurrentReaderMap(); 61 62 63 private long emitterSequence = 0; 64 65 70 public boolean hasListeners() 71 { 72 return eventListenerRegistry.isEmpty() == false; 73 } 74 75 78 public KernelEvent createEvent(String type, Object context) 79 { 80 return new AbstractEvent(this, type, nextEmitterSequence(), System.currentTimeMillis(), context); 81 } 82 83 public void registerListener(KernelEventListener listener, KernelEventFilter filter, Object handback) throws Throwable 84 { 85 KernelEventFilter filterObject = filter == null ? NULL_FILTER : filter; 86 Object handbackObject = handback == null ? NULL : handback; 87 88 synchronized (eventListenerRegistry) 89 { 90 Map <Object , List <KernelEventListener>> handbacks = eventListenerRegistry.get(filterObject); 91 if (handbacks == null) 92 { 93 handbacks = CollectionsFactory.createConcurrentReaderMap(); 94 eventListenerRegistry.put(filterObject, handbacks); 95 } 96 List <KernelEventListener> listeners = handbacks.get(handbackObject); 97 if (listeners == null) 98 { 99 listeners = CollectionsFactory.createCopyOnWriteList(); 100 handbacks.put(handbackObject, listeners); 101 } 102 listeners.add(listener); 103 if (log.isTraceEnabled()) 104 log.trace("Registered listener: " + listener + " with filter=" + filter + " handback=" + handback + " on object " + this); 105 106 } 107 } 108 109 public void unregisterListener(KernelEventListener listener, KernelEventFilter filter, Object handback) throws Throwable 110 { 111 KernelEventFilter filterObject = filter == null ? NULL_FILTER : filter; 112 Object handbackObject = handback == null ? NULL : handback; 113 114 synchronized (eventListenerRegistry) 115 { 116 Map <Object , List <KernelEventListener>> handbacks = eventListenerRegistry.get(filterObject); 117 if (handbacks != null) 118 { 119 List <KernelEventListener> listeners = handbacks.get(handbackObject); 120 if (listeners != null && listeners.remove(listener)) 121 { 122 if (log.isTraceEnabled()) 123 log.trace("Unregistered listener: " + listener + " with filter=" + filter + " handback=" + handback + " on object " + this); 124 return; 125 } 126 } 127 } 128 throw new IllegalStateException ("Listener not registered."); 129 } 130 131 public void fireKernelEvent(KernelEvent event) 132 { 133 if (log.isTraceEnabled()) 134 log.trace("Firing event: " + event + " on object " + this); 135 if (eventListenerRegistry.isEmpty() == false) 136 { 137 for (Iterator i = eventListenerRegistry.entrySet().iterator(); i.hasNext();) 138 { 139 Map.Entry registryEntry = (Map.Entry ) i.next(); 140 141 Map handbacks = (Map ) registryEntry.getValue(); 142 if (handbacks != null) 143 { 144 KernelEventFilter filter = null; 145 Object filterObject = registryEntry.getKey(); 146 if (filterObject != NULL_FILTER) 147 filter = (KernelEventFilter) filterObject; 148 149 for (Iterator j = handbacks.entrySet().iterator(); j.hasNext();) 150 { 151 Map.Entry handbackEntry = (Map.Entry ) j.next(); 152 List listeners = (List ) handbackEntry.getValue(); 153 if (listeners != null) 154 { 155 Object handback = handbackEntry.getKey(); 156 if (handback == NULL) 157 handback = null; 158 159 for (ListIterator k = listeners.listIterator(); k.hasNext();) 160 { 161 KernelEventListener listener = (KernelEventListener) k.next(); 162 try 163 { 164 if (filter == null || filter.wantEvent(event, handback)) 165 fireKernelEvent(listener, event, handback); 166 } 167 catch (Throwable t) 168 { 169 log.debug("Ignored unhandled throwable: ", t); 170 } 171 } 172 } 173 } 174 } 175 } 176 } 177 } 178 179 186 protected void fireKernelEvent(KernelEventListener listener, KernelEvent event, Object handback) 187 { 188 listener.onEvent(event, handback); 189 } 190 191 196 protected long nextEmitterSequence() 197 { 198 synchronized (eventListenerRegistry) 199 { 200 return emitterSequence++; 201 } 202 } 203 } 204 | Popular Tags |