UserEventDispatcher.java 6.23 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision: 1705 $
 * $Date: 2005-07-26 14:10:33 -0300 (Tue, 26 Jul 2005) $
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * 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.
19 20
 */

21
package org.jivesoftware.openfire.event;
22 23 24 25 26

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

27 28 29 30
import org.jivesoftware.openfire.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

31 32 33 34 35
/**
 * Dispatches user events. Each event has a {@link EventType type}
 * and optional parameters, as follows:<p>
 *
 * <table border="1">
36
 * <caption></caption>
37 38 39 40
 * <tr><th>Event Type</th><th>Extra Params</th></tr>
 * <tr><td>{@link EventType#user_created user_created}</td><td><i>None</i></td></tr>
 * <tr><td>{@link EventType#user_deleting user_deleting}</td><td><i>None</i></td></tr>
 * <tr valign="top"><td>{@link EventType#user_modified user_modified}</td><td>
41 42 43
 * <table>
 * <caption></caption>
 *      <tr><td><b>Reason</b></td><td><b>Key</b></td><td><b>Value</b></td></tr>
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
 *      <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">Email modified</td></tr>
 *      <tr><td>&nbsp;</td><td>type</td><td>emailModified</td></tr>
 *      <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Email before it was
 * modified)</i></td></tr>
 *      <tr><td colspan="3"><hr></td></tr>
 *
 *      <tr><td colspan="3">Password modified</td></tr>
 *      <tr><td>&nbsp;</td><td>type</td><td>passwordModified</td></tr>
 *      <tr><td colspan="3"><hr></td></tr>
 *
 *      <tr><td colspan="3">Creation date modified</td></tr>
 *      <tr><td>&nbsp;</td><td>type</td><td>creationDateModified</td></tr>
 *      <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Creation date before it was
 * modified)</i></td></tr>
 *      <tr><td colspan="3"><hr></td></tr>
 *
 *      <tr><td colspan="3">Modification date modified</td></tr>
 *      <tr><td>&nbsp;</td><td>type</td><td>modificationDateModified</td></tr>
 *      <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Modification date 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>
 *
 * @author Matt Tucker
 */
public class UserEventDispatcher {

92 93
	private static final Logger Log = LoggerFactory.getLogger(UserEventDispatcher.class);

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    private static List<UserEventListener> listeners =
            new CopyOnWriteArrayList<UserEventListener>();

    private UserEventDispatcher() {
        // Not instantiable.
    }

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

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

    /**
     * Dispatches an event to all listeners.
     *
     * @param user the user.
     * @param eventType the event type.
     * @param params event parameters.
     */
129
    public static void dispatchEvent(User user, EventType eventType, Map<String,Object> params) {
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        for (UserEventListener listener : listeners) {
            try {
                switch (eventType) {
                    case user_created: {
                        listener.userCreated(user, params);
                        break;
                    }
                    case user_deleting: {
                        listener.userDeleting(user, params);
                        break;
                    }
                    case user_modified: {
                        listener.userModified(user, params);
                        break;
                    }
                    default:
                        break;
                }
            }
            catch (Exception e) {
150
                Log.error(e.getMessage(), e);
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
            }
        }
    }

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

        /**
         * A new user was created.
         */
        user_created,

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

        /**
         * The name, email, password, or extended property of a user was changed.
         */
        user_modified,
    }
}