GroupEventDispatcher.java 6.65 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright 2004 Jive Software.
 *
 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jivesoftware.messenger.event;

import org.jivesoftware.util.Log;
Matt Tucker's avatar
Matt Tucker committed
24
import org.jivesoftware.messenger.group.Group;
Matt Tucker's avatar
Matt Tucker committed
25 26

import java.util.List;
Matt Tucker's avatar
Matt Tucker committed
27
import java.util.Map;
Matt Tucker's avatar
Matt Tucker committed
28 29 30
import java.util.concurrent.CopyOnWriteArrayList;

/**
Matt Tucker's avatar
Matt Tucker committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
 * Dispatches group events. Each event has a {@link EventType type}
 * and optional parameters, as follows:<p>
 *
 * <table border="1">
 * <tr><th>Event Type</th><th>Extra Params</th></tr>
 * <tr><td>{@link EventType#group_created group_created}</td><td><i>None</i></td></tr>
 * <tr><td>{@link EventType#group_deleting group_deleting}</td><td><i>None</i></td></tr>
 * <tr><td>{@link EventType#member_added member_added}</td><td>A param named <tt>member</tt> with a String username as a
 *      payload</td></tr>
 * <tr><td>{@link EventType#member_removed member_removed}</td><td>A param named <tt>member</tt> with a String username as a
 *      payload</td></tr>
 * <tr><td>{@link EventType#admin_added admin_added}</td><td>A param named <tt>admin</tt> with a String username
 *      as a payload</td></tr>
 * <tr><td>{@link EventType#admin_removed admin_removed}</td><td>A param named <tt>admin</tt> with a String username
 *      as a payload</td></tr>
 * <tr valign="top"><td>{@link EventType#group_modified group_modified}</td><td>
 * <table><tr><td><b>Reason</b></td><td><b>Key</b></td><td><b>Value</b></td></tr>
 *      <tr><td colspan="3">Name modified</td></tr>
 *      <tr><td>&nbsp;</td><td>type</td><td>nameModified</td></tr>
 *      <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Name before it was modified)</i>
 * </td></tr>
 *      <tr><td colspan="3"><hr></td></tr>
 *      <tr><td colspan="3">Description modified</td></tr>
 *      <tr><td>&nbsp;</td><td>type</td><td>descriptionModified</td></tr>
 *      <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Description before it was
 * modified)</i></td></tr>
 *      <tr><td colspan="3"><hr></td></tr>
 *      <tr><td colspan="3">Property modified</td></tr>
 *      <tr><td>&nbsp;</td><td>type</td><td>propertyModified</td></tr>
 *      <tr><td>&nbsp;</td><td>propertyKey</td><td><i>(Name of the property)</i></td>
 * </tr>
 *      <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Property value before it was
 * modified)</i></td></tr>
 *      <tr><td colspan="3"><hr></td></tr>
 *      <tr><td colspan="3">Property added</td></tr>
 *      <tr><td>&nbsp;</td><td>type</td><td>propertyAdded</td></tr>
 *      <tr><td>&nbsp;</td><td>propertyKey</td><td><i>(Name of the new property)</i></td>
 * </tr>
 *      <tr><td colspan="3"><hr></td></tr>
 *      <tr><td colspan="3">Property deleted</td></tr>
 *      <tr><td>&nbsp;</td><td>type</td><td>propertyDeleted</td></tr>
 *      <tr><td>&nbsp;</td><td>propertyKey</td><td><i>(Name of the property deleted)</i></td></tr></table></td></tr>
 * </table>
Matt Tucker's avatar
Matt Tucker committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
 *
 * @author Matt Tucker
 */
public class GroupEventDispatcher {

    private static List<GroupEventListener> listeners =
            new CopyOnWriteArrayList<GroupEventListener>();

    private GroupEventDispatcher() {
        // Not instantiable.
    }

    /**
     * Registers a listener to receive events.
     *
     * @param listener the listener.
     */
    public static void addListener(GroupEventListener listener) {
        if (listener == null) {
            throw new NullPointerException();
        }
        listeners.add(listener);
    }

    /**
     * Unregisters a listener to receive events.
     *
     * @param listener the listener.
     */
    public static void removeListener(GroupEventListener listener) {
        listeners.remove(listener);
    }

    /**
     * Dispatches an event to all listeners.
     *
Matt Tucker's avatar
Matt Tucker committed
110 111 112
     * @param group the group.
     * @param eventType the event type.
     * @param params event parameters.
Matt Tucker's avatar
Matt Tucker committed
113
     */
Matt Tucker's avatar
Matt Tucker committed
114
    public static void dispatchEvent(Group group, EventType eventType, Map params) {
Matt Tucker's avatar
Matt Tucker committed
115 116 117 118
        for (GroupEventListener listener : listeners) {
            try {
                switch (eventType) {
                    case group_created: {
Matt Tucker's avatar
Matt Tucker committed
119
                        listener.groupCreated(group, params);
Matt Tucker's avatar
Matt Tucker committed
120 121 122
                        break;
                    }
                    case group_deleting: {
Matt Tucker's avatar
Matt Tucker committed
123
                        listener.groupDeleting(group, params);
Matt Tucker's avatar
Matt Tucker committed
124 125 126
                        break;
                    }
                    case member_added: {
Matt Tucker's avatar
Matt Tucker committed
127
                        listener.memberAdded(group, params);
Matt Tucker's avatar
Matt Tucker committed
128 129 130
                        break;
                    }
                    case member_removed: {
Matt Tucker's avatar
Matt Tucker committed
131
                        listener.memberRemoved(group, params);
Matt Tucker's avatar
Matt Tucker committed
132 133 134
                        break;
                    }
                    case admin_added: {
Matt Tucker's avatar
Matt Tucker committed
135
                        listener.adminAdded(group, params);
Matt Tucker's avatar
Matt Tucker committed
136 137 138
                        break;
                    }
                    case admin_removed: {
Matt Tucker's avatar
Matt Tucker committed
139
                        listener.adminRemoved(group, params);
Matt Tucker's avatar
Matt Tucker committed
140 141 142
                        break;
                    }
                    case group_modified: {
Matt Tucker's avatar
Matt Tucker committed
143
                        listener.groupModified(group, params);
Matt Tucker's avatar
Matt Tucker committed
144 145 146 147 148 149 150 151 152 153 154
                        break;
                    }
                    default:
                        break;
                }
            }
            catch (Exception e) {
                Log.error(e);
            }
        }
    }
Matt Tucker's avatar
Matt Tucker committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

    /**
     * Represents valid event types.
     */
    public enum EventType {

        /**
         * A new group was created.
         */
        group_created,

        /**
         * A group is about to be deleted. Note that this event is fired before
         * a group is actually deleted. This allows for referential cleanup
         * before the group is gone.
         */
        group_deleting,

        /**
         * The name, description, or extended property of a group was changed.
         */
        group_modified,

        /**
         * A member was added to a group.
         */
        member_added,

        /**
         * A member was removed from a group.
         */
        member_removed,

        /**
         * An administrator was added to a group.
         */
        admin_added,

        /**
         * An administrator was removed from a group.
         */
        admin_removed;
    }
Matt Tucker's avatar
Matt Tucker committed
198
}