org.jivesoftware.openfire.event.UserEventDispatcher Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2004-2008 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.openfire.event;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import org.jivesoftware.openfire.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Dispatches user events. Each event has a {@link EventType type}
* and optional parameters, as follows:
*
*
*
* Event Type Extra Params
* {@link EventType#user_created user_created} None
* {@link EventType#user_deleting user_deleting} None
* {@link EventType#user_modified user_modified}
*
*
* Reason Key Value
* Name modified
* type nameModified
* originalValue (Name before it was modified)
*
*
* Email modified
* type emailModified
* originalValue (Email before it was
* modified)
*
*
* Password modified
* type passwordModified
*
*
* Creation date modified
* type creationDateModified
* originalValue (Creation date before it was
* modified)
*
*
* Modification date modified
* type modificationDateModified
* originalValue (Modification date before it was
* modified)
*
*
* Property modified
* type propertyModified
* propertyKey (Name of the property)
*
* originalValue (Property value before it was
* modified)
*
* Property added
* type propertyAdded
* propertyKey (Name of the new property)
*
*
* Property deleted
* type propertyDeleted
* propertyKey (Name of the property deleted)
*
*
* @author Matt Tucker
*/
public class UserEventDispatcher {
private static final Logger Log = LoggerFactory.getLogger(UserEventDispatcher.class);
private static List listeners =
new CopyOnWriteArrayList<>();
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.
*/
public static void dispatchEvent(User user, EventType eventType, Map params) {
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) {
Log.error(e.getMessage(), e);
}
}
}
/**
* 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,
}
}