1 31 32 package org.opencms.main; 33 34 import java.util.ArrayList ; 35 import java.util.Collections ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.Map ; 40 41 import org.apache.commons.logging.Log; 42 43 61 public class CmsEventManager { 62 63 64 protected static final I_CmsEventListener[] EVENT_LIST = new I_CmsEventListener[0]; 65 66 67 private static final Log LOG = CmsLog.getLog(CmsEventManager.class); 68 69 70 private Map m_eventListeners; 71 72 75 public CmsEventManager() { 76 77 m_eventListeners = new HashMap (); 78 } 79 80 85 public void addCmsEventListener(I_CmsEventListener listener) { 86 87 addCmsEventListener(listener, null); 88 } 89 90 96 public void addCmsEventListener(I_CmsEventListener listener, int[] eventTypes) { 97 98 synchronized (m_eventListeners) { 99 if (eventTypes == null) { 100 eventTypes = new int[] {I_CmsEventListener.LISTENERS_FOR_ALL_EVENTS.intValue()}; 102 } 103 for (int i = 0; i < eventTypes.length; i++) { 104 Integer eventType = new Integer (eventTypes[i]); 106 List listeners = (List )m_eventListeners.get(eventType); 107 if (listeners == null) { 108 listeners = new ArrayList (); 109 m_eventListeners.put(eventType, listeners); 110 } 111 if (!listeners.contains(listener)) { 112 listeners.add(listener); 114 } 115 } 116 } 117 } 118 119 124 public void fireEvent(CmsEvent event) { 125 126 fireEventHandler((List )m_eventListeners.get(event.getTypeInteger()), event); 127 fireEventHandler((List )m_eventListeners.get(I_CmsEventListener.LISTENERS_FOR_ALL_EVENTS), event); 128 } 129 130 135 public void fireEvent(int type) { 136 137 fireEvent(type, Collections.EMPTY_MAP); 138 } 139 140 146 public void fireEvent(int type, Map data) { 147 148 fireEvent(new CmsEvent(type, data)); 149 } 150 151 156 public void removeCmsEventListener(I_CmsEventListener listener) { 157 158 synchronized (m_eventListeners) { 159 Iterator it = m_eventListeners.keySet().iterator(); 160 while (it.hasNext()) { 161 List listeners = (List )m_eventListeners.get(it.next()); 162 listeners.remove(listener); 163 } 164 } 165 } 166 167 172 protected Map getEventListeners() { 173 174 return m_eventListeners; 175 } 176 177 182 protected void initialize(CmsEventManager base) { 183 184 m_eventListeners = new HashMap (base.getEventListeners()); 185 } 186 187 193 protected void fireEventHandler(List listeners, CmsEvent event) { 194 195 if (!LOG.isDebugEnabled()) { 196 if ((listeners != null) && (listeners.size() > 0)) { 198 I_CmsEventListener[] list = (I_CmsEventListener[])listeners.toArray(EVENT_LIST); 200 for (int i = 0; i < list.length; i++) { 202 list[i].cmsEvent(event); 204 } 205 } 206 } else { 207 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_EVENT_1, event.toString())); 210 if ((listeners != null) && (listeners.size() > 0)) { 211 I_CmsEventListener[] list = (I_CmsEventListener[])listeners.toArray(EVENT_LIST); 213 if (event.getData() != null) { 215 Iterator i = event.getData().keySet().iterator(); 216 while (i.hasNext()) { 217 String key = (String )i.next(); 218 Object value = event.getData().get(key); 219 LOG.debug(Messages.get().getBundle().key( 220 Messages.LOG_DEBUG_EVENT_VALUE_3, 221 key, 222 value, 223 event.toString())); 224 } 225 } else { 226 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_NO_EVENT_VALUE_1, event.toString())); 227 } 228 for (int j = 0; j < list.length; j++) { 230 LOG.debug(Messages.get().getBundle().key( 231 Messages.LOG_DEBUG_EVENT_LISTENERS_3, 232 list[j], 233 new Integer (j), 234 event.toString())); 235 } 236 for (int i = 0; i < list.length; i++) { 238 LOG.debug(Messages.get().getBundle().key( 239 Messages.LOG_DEBUG_EVENT_START_LISTENER_3, 240 list[i], 241 new Integer (i), 242 event.toString())); 243 list[i].cmsEvent(event); 245 LOG.debug(Messages.get().getBundle().key( 246 Messages.LOG_DEBUG_EVENT_END_LISTENER_3, 247 list[i], 248 new Integer (i), 249 event.toString())); 250 } 251 } else { 252 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_EVENT_NO_LISTENER_1, event.toString())); 253 } 254 LOG.debug(Messages.get().getBundle().key(Messages.LOG_DEBUG_EVENT_COMPLETE_1, event.toString())); 255 } 256 } 257 } | Popular Tags |