Commit 04cf830e authored by Matt Tucker's avatar Matt Tucker Committed by matt

Added user events (JM-246).


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1212 b35dd754-fafc-0310-a699-88a17e54d16e
parent e3655332
/**
* $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;
import org.jivesoftware.messenger.user.User;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Dispatches user 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#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>
* <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">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 {
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.
*/
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);
}
}
}
/**
* 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,
}
}
\ No newline at end of file
/**
* $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.messenger.user.User;
import java.util.Map;
/**
* Interface to listen for group events. Use the
* {@link GroupEventDispatcher#addListener(GroupEventListener)}
* method to register for events.
*
* @author Matt Tucker
*/
public interface UserEventListener {
/**
* A user was created.
*
* @param user the user.
* @param params event parameters.
*/
public void userCreated(User user, Map params);
/**
* A user is being deleted.
*
* @param user the user.
* @param params event parameters.
*/
public void userDeleting(User user, Map params);
/**
* A user's name, email, or an extended property was changed.
*
* @param user the user.
* @param params event parameters.
*/
public void userModified(User user, Map params);
}
\ No newline at end of file
...@@ -104,7 +104,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -104,7 +104,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
// prober // prober
try { try {
User probeeUser = UserManager.getInstance().getUser(probee.getNode()); User probeeUser = UserManager.getInstance().getUser(probee.getNode());
String presenceXML = probeeUser.getProperty(lastPresenceProp); String presenceXML = probeeUser.getProperties().get(lastPresenceProp);
if (presenceXML != null) { if (presenceXML != null) {
try { try {
// Parse the element // Parse the element
...@@ -176,7 +176,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -176,7 +176,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
} }
try { try {
User probeeUser = UserManager.getInstance().getUser(username); User probeeUser = UserManager.getInstance().getUser(username);
probeeUser.deleteProperty(lastPresenceProp); probeeUser.getProperties().remove(lastPresenceProp);
} }
catch (UserNotFoundException e) { catch (UserNotFoundException e) {
} }
...@@ -188,7 +188,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager ...@@ -188,7 +188,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
} }
try { try {
User probeeUser = UserManager.getInstance().getUser(username); User probeeUser = UserManager.getInstance().getUser(username);
probeeUser.setProperty(lastPresenceProp, presence.toXML()); probeeUser.getProperties().put(lastPresenceProp, presence.toXML());
} }
catch (UserNotFoundException e) { catch (UserNotFoundException e) {
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
package org.jivesoftware.messenger.user; package org.jivesoftware.messenger.user;
import org.jivesoftware.messenger.JiveGlobals; import org.jivesoftware.messenger.JiveGlobals;
import org.jivesoftware.messenger.event.UserEventDispatcher;
import org.jivesoftware.util.Cache; import org.jivesoftware.util.Cache;
import org.jivesoftware.util.CacheManager; import org.jivesoftware.util.CacheManager;
import org.jivesoftware.util.ClassUtils; import org.jivesoftware.util.ClassUtils;
...@@ -21,6 +22,7 @@ import org.jivesoftware.stringprep.StringprepException; ...@@ -21,6 +22,7 @@ import org.jivesoftware.stringprep.StringprepException;
import java.util.Collection; import java.util.Collection;
import java.util.Set; import java.util.Set;
import java.util.Collections;
/** /**
* Manages users, including loading, creating and deleting. * Manages users, including loading, creating and deleting.
...@@ -53,9 +55,10 @@ public class UserManager { ...@@ -53,9 +55,10 @@ public class UserManager {
} }
/** /**
* Returns the currently-installed UserProvider. Note, in most cases the user * Returns the currently-installed UserProvider. <b>Warning:</b> in virtually all
* provider should not be used directly. Instead, the appropriate methods * cases the user provider should not be used directly. Instead, the appropriate
* in UserManager should be called. * methods in UserManager should be called. Direct access to the user provider is
* only provided for special-case logic.
* *
* @return the current UserProvider. * @return the current UserProvider.
*/ */
...@@ -104,6 +107,11 @@ public class UserManager { ...@@ -104,6 +107,11 @@ public class UserManager {
} }
User user = provider.createUser(username, password, name, email); User user = provider.createUser(username, password, name, email);
userCache.put(username, user); userCache.put(username, user);
// Fire event.
UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_created,
Collections.emptyMap());
return user; return user;
} }
...@@ -125,6 +133,11 @@ public class UserManager { ...@@ -125,6 +133,11 @@ public class UserManager {
catch (StringprepException se) { catch (StringprepException se) {
throw new IllegalArgumentException("Invalid username: " + username, se); throw new IllegalArgumentException("Invalid username: " + username, se);
} }
// Fire event.
UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_deleting,
Collections.emptyMap());
provider.deleteUser(user.getUsername()); provider.deleteUser(user.getUsername());
// Remove the user from cache. // Remove the user from cache.
userCache.remove(user.getUsername()); userCache.remove(user.getUsername());
......
...@@ -53,10 +53,10 @@ ...@@ -53,10 +53,10 @@
User user = webManager.getUser(); User user = webManager.getUser();
if (user != null) { if (user != null) {
if (range == DEFAULT_RANGE) { if (range == DEFAULT_RANGE) {
user.deleteProperty(USER_RANGE_PROP); user.getProperties().remove(USER_RANGE_PROP);
} }
else { else {
user.setProperty(USER_RANGE_PROP, String.valueOf(range)); user.getProperties().put(USER_RANGE_PROP, String.valueOf(range));
} }
} }
} }
...@@ -65,10 +65,10 @@ ...@@ -65,10 +65,10 @@
if (webManager.getUser() != null) { if (webManager.getUser() != null) {
User user = webManager.getUser(); User user = webManager.getUser();
try { try {
range = Integer.parseInt(user.getProperty(USER_RANGE_PROP)); range = Integer.parseInt(user.getProperties().get(USER_RANGE_PROP));
} }
catch (Exception e) { catch (Exception e) {
user.deleteProperty(USER_RANGE_PROP); user.getProperties().remove(USER_RANGE_PROP);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment