001: /**
002: * $RCSfile$
003: * $Revision: 8589 $
004: * $Date: 2007-06-21 13:08:03 -0700 (Thu, 21 Jun 2007) $
005: *
006: * Copyright (C) 2004-2005 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.util;
011:
012: import java.util.Map;
013: import java.util.Set;
014: import java.util.concurrent.CopyOnWriteArraySet;
015:
016: /**
017: * Dispatches property events. Each event has a {@link EventType type}
018: * and optional parameters, as follows:<p>
019: *
020: * <table border="1">
021: * <tr><th>Event Type</th><th>Extra Params</th></tr>
022: * <tr><td>{@link EventType#property_set property_set}</td><td>A param named <tt>value</tt> that
023: * has the value of the property set.</td></tr>
024: * <tr><td>{@link EventType#property_deleted property_deleted}</td><td><i>None</i></td></tr>
025: * <tr><td>{@link EventType#xml_property_set xml_property_set}</td><td>A param named <tt>value</tt> that
026: * has the value of the property set.</td></tr>
027: * <tr><td>{@link EventType#xml_property_deleted xml_property_deleted}</td><td><i>None</i></td></tr>
028: * </table>
029: *
030: * @author Matt Tucker
031: */
032: public class PropertyEventDispatcher {
033:
034: private static Set<PropertyEventListener> listeners = new CopyOnWriteArraySet<PropertyEventListener>();
035:
036: private PropertyEventDispatcher() {
037: // Not instantiable.
038: }
039:
040: /**
041: * Registers a listener to receive events.
042: *
043: * @param listener the listener.
044: */
045: public static void addListener(PropertyEventListener listener) {
046: if (listener == null) {
047: throw new NullPointerException();
048: }
049: listeners.add(listener);
050: }
051:
052: /**
053: * Unregisters a listener to receive events.
054: *
055: * @param listener the listener.
056: */
057: public static void removeListener(PropertyEventListener listener) {
058: listeners.remove(listener);
059: }
060:
061: /**
062: * Dispatches an event to all listeners.
063: *
064: * @param property the property.
065: * @param eventType the event type.
066: * @param params event parameters.
067: */
068: public static void dispatchEvent(String property,
069: EventType eventType, Map<String, Object> params) {
070: for (PropertyEventListener listener : listeners) {
071: try {
072: switch (eventType) {
073: case property_set: {
074: listener.propertySet(property, params);
075: break;
076: }
077: case property_deleted: {
078: listener.propertyDeleted(property, params);
079: break;
080: }
081: case xml_property_set: {
082: listener.xmlPropertySet(property, params);
083: break;
084: }
085: case xml_property_deleted: {
086: listener.xmlPropertyDeleted(property, params);
087: break;
088: }
089: default:
090: break;
091: }
092: } catch (Exception e) {
093: Log.error(e);
094: }
095: }
096: }
097:
098: /**
099: * Represents valid event types.
100: */
101: public enum EventType {
102:
103: /**
104: * A property was set.
105: */
106: property_set,
107:
108: /**
109: * A property was deleted.
110: */
111: property_deleted,
112:
113: /**
114: * An XML property was set.
115: */
116: xml_property_set,
117:
118: /**
119: * An XML property was deleted.
120: */
121: xml_property_deleted;
122: }
123: }
|