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
// prober
try {
User probeeUser = UserManager.getInstance().getUser(probee.getNode());
String presenceXML = probeeUser.getProperty(lastPresenceProp);
String presenceXML = probeeUser.getProperties().get(lastPresenceProp);
if (presenceXML != null) {
try {
// Parse the element
......@@ -176,7 +176,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
}
try {
User probeeUser = UserManager.getInstance().getUser(username);
probeeUser.deleteProperty(lastPresenceProp);
probeeUser.getProperties().remove(lastPresenceProp);
}
catch (UserNotFoundException e) {
}
......@@ -188,7 +188,7 @@ public class PresenceManagerImpl extends BasicModule implements PresenceManager
}
try {
User probeeUser = UserManager.getInstance().getUser(username);
probeeUser.setProperty(lastPresenceProp, presence.toXML());
probeeUser.getProperties().put(lastPresenceProp, presence.toXML());
}
catch (UserNotFoundException e) {
}
......
......@@ -13,12 +13,14 @@ package org.jivesoftware.messenger.user;
import org.jivesoftware.messenger.roster.Roster;
import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.event.UserEventDispatcher;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.Cacheable;
import org.jivesoftware.util.CacheSizes;
import org.jivesoftware.database.DbConnectionManager;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
......@@ -98,6 +100,12 @@ public class User implements Cacheable {
try {
UserManager.getUserProvider().setPassword(username, password);
// Fire event.
Map params = new HashMap();
params.put("type", "passwordModified");
UserEventDispatcher.dispatchEvent(this, UserEventDispatcher.EventType.user_modified,
params);
}
catch (UserNotFoundException unfe) {
Log.error(unfe);
......@@ -114,8 +122,16 @@ public class User implements Cacheable {
}
try {
String originalName = this.name;
UserManager.getUserProvider().setName(username, name);
this.name = name;
// Fire event.
Map params = new HashMap();
params.put("type", "nameModified");
params.put("originalValue", originalName);
UserEventDispatcher.dispatchEvent(this, UserEventDispatcher.EventType.user_modified,
params);
}
catch (UserNotFoundException unfe) {
Log.error(unfe);
......@@ -132,8 +148,15 @@ public class User implements Cacheable {
}
try {
String originalEmail= this.email;
UserManager.getUserProvider().setEmail(username, email);
this.email = email;
// Fire event.
Map params = new HashMap();
params.put("type", "emailModified");
params.put("originalValue", originalEmail);
UserEventDispatcher.dispatchEvent(this, UserEventDispatcher.EventType.user_modified,
params);
}
catch (UserNotFoundException unfe) {
Log.error(unfe);
......@@ -150,8 +173,16 @@ public class User implements Cacheable {
}
try {
Date originalCreationDate = this.creationDate;
UserManager.getUserProvider().setCreationDate(username, creationDate);
this.creationDate = creationDate;
// Fire event.
Map params = new HashMap();
params.put("type", "creationDateModified");
params.put("originalValue", originalCreationDate);
UserEventDispatcher.dispatchEvent(this, UserEventDispatcher.EventType.user_modified,
params);
}
catch (UserNotFoundException unfe) {
Log.error(unfe);
......@@ -168,8 +199,16 @@ public class User implements Cacheable {
}
try {
Date originalModificationDate = this.modificationDate;
UserManager.getUserProvider().setCreationDate(username, modificationDate);
this.modificationDate = modificationDate;
// Fire event.
Map params = new HashMap();
params.put("type", "nameModified");
params.put("originalValue", originalModificationDate);
UserEventDispatcher.dispatchEvent(this, UserEventDispatcher.EventType.user_modified,
params);
}
catch (UserNotFoundException unfe) {
Log.error(unfe);
......@@ -177,78 +216,20 @@ public class User implements Cacheable {
}
/**
* Returns an extended property of the user. Each user can have an arbitrary number of extended
* properties. This lets particular skins or filters provide enhanced functionality that is not
* part of the base interface.
*
* @param name the name of the property to get.
* @return the value of the property
*/
public String getProperty(String name) {
if (properties == null) {
loadPropertiesFromDb();
}
return properties.get(name);
}
/**
* Sets an extended property of the user. Each user can have an arbitrary number of extended
* properties. This lets particular skins or filters provide enhanced functionality that is not
* part of the base interface. Property names and values must be valid Strings. If <tt>null</tt>
* or an empty length String is used, a NullPointerException will be thrown.
*
* @param name the name of the property to set.
* @param value the new value for the property.
*/
public void setProperty(String name, String value) {
if (properties == null) {
loadPropertiesFromDb();
}
// Make sure the property name and value aren't null.
if (name == null || value == null || "".equals(name) || "".equals(value)) {
throw new NullPointerException("Cannot set property with empty or null value.");
}
// See if we need to update a property value or insert a new one.
if (properties.containsKey(name)) {
// Only update the value in the database if the property value
// has changed.
if (!(value.equals(properties.get(name)))) {
properties.put(name, value);
updatePropertyInDb(name, value);
}
}
else {
properties.put(name, value);
insertPropertyIntoDb(name, value);
}
}
/**
* Deletes an extended property. If the property specified by <code>name</code> does not exist,
* this method will do nothing.
* Returns all extended properties of the group. Groups
* have an arbitrary number of extended properties.
*
* @param name the name of the property to delete.
* @return the extended properties.
*/
public void deleteProperty(String name) {
public Map<String,String> getProperties() {
synchronized (this) {
if (properties == null) {
loadPropertiesFromDb();
properties = new ConcurrentHashMap<String, String>();
loadProperties();
}
if (properties.remove(name) != null) {
// Only delete the propery from the DB if it was removed from memory
deletePropertyFromDb(name);
}
}
/**
* Returns a Collection of all the names of the extended user properties.
*
* @return a Collection of all the property names.
*/
public Collection<String> getPropertyNames() {
if (properties == null) {
loadPropertiesFromDb();
}
return Collections.unmodifiableCollection(properties.keySet());
// Return a wrapper that will intercept add and remove commands.
return new PropertiesMap();
}
/**
......@@ -300,92 +281,169 @@ public class User implements Cacheable {
}
}
private synchronized void loadPropertiesFromDb() {
properties = new Hashtable<String,String>();
/**
* Map implementation that updates the database when properties are modified.
*/
private class PropertiesMap extends AbstractMap {
public Object put(Object key, Object value) {
Object answer;
if (properties.containsKey(key)) {
String originalValue = properties.get(key);
answer = properties.put((String)key, (String)value);
updateProperty((String)key, (String)value);
// Fire event.
Map params = new HashMap();
params.put("type", "propertyModified");
params.put("propertyKey", key);
params.put("originalValue", originalValue);
UserEventDispatcher.dispatchEvent(User.this,
UserEventDispatcher.EventType.user_modified, params);
}
else {
answer = properties.put((String)key, (String)value);
insertProperty((String)key, (String)value);
// Fire event.
Map params = new HashMap();
params.put("type", "propertyAdded");
params.put("propertyKey", key);
UserEventDispatcher.dispatchEvent(User.this,
UserEventDispatcher.EventType.user_modified, params);
}
return answer;
}
public Set<Entry> entrySet() {
return new PropertiesEntrySet();
}
}
/**
* Set implementation that updates the database when properties are deleted.
*/
private class PropertiesEntrySet extends AbstractSet {
public int size() {
return properties.entrySet().size();
}
public Iterator iterator() {
return new Iterator() {
Iterator iter = properties.entrySet().iterator();
Map.Entry current = null;
public boolean hasNext() {
return iter.hasNext();
}
public Object next() {
current = (Map.Entry)iter.next();
return current;
}
public void remove() {
if (current == null) {
throw new IllegalStateException();
}
deleteProperty((String)current.getKey());
iter.remove();
// Fire event.
Map params = new HashMap();
params.put("type", "propertyDeleted");
params.put("propertyKey", current.getKey());
UserEventDispatcher.dispatchEvent(User.this,
UserEventDispatcher.EventType.user_modified, params);
}
};
}
}
private void loadProperties() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setString(1, username);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
properties.put(rs.getString(1), rs.getString(2));
}
rs.close();
}
catch (SQLException e) {
Log.error(e);
catch (SQLException sqle) {
Log.error(sqle);
}
finally {
try { if (pstmt != null) { pstmt.close(); } }
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); }
}
}
private void insertPropertyIntoDb(String name, String value) {
private void insertProperty(String propName, String propValue) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(INSERT_PROPERTY);
pstmt.setString(1, username);
pstmt.setString(2, name);
pstmt.setString(3, value);
pstmt.setString(1, name);
pstmt.setString(2, propName);
pstmt.setString(3, propValue);
pstmt.executeUpdate();
}
catch (SQLException e) {
Log.error(e);
}
finally {
try { if (pstmt != null) { pstmt.close(); } }
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); }
}
}
private void updatePropertyInDb(String name, String value) {
private void updateProperty(String propName, String propValue) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(UPDATE_PROPERTY);
pstmt.setString(1, value);
pstmt.setString(2, name);
pstmt.setString(3, username);
pstmt.setString(1, propValue);
pstmt.setString(2, propName);
pstmt.setString(3, name);
pstmt.executeUpdate();
}
catch (SQLException e) {
Log.error(e);
}
finally {
try { if (pstmt != null) { pstmt.close(); } }
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); }
}
}
private void deletePropertyFromDb(String name) {
private void deleteProperty(String propName) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(DELETE_PROPERTY);
pstmt.setString(1, username);
pstmt.setString(2, name);
pstmt.setString(1, name);
pstmt.setString(2, propName);
pstmt.executeUpdate();
}
catch (SQLException e) {
Log.error(e);
}
finally {
try { if (pstmt != null) { pstmt.close(); } }
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
try { if (con != null) con.close(); }
catch (Exception e) { Log.error(e); }
}
}
......
......@@ -12,6 +12,7 @@
package org.jivesoftware.messenger.user;
import org.jivesoftware.messenger.JiveGlobals;
import org.jivesoftware.messenger.event.UserEventDispatcher;
import org.jivesoftware.util.Cache;
import org.jivesoftware.util.CacheManager;
import org.jivesoftware.util.ClassUtils;
......@@ -21,6 +22,7 @@ import org.jivesoftware.stringprep.StringprepException;
import java.util.Collection;
import java.util.Set;
import java.util.Collections;
/**
* Manages users, including loading, creating and deleting.
......@@ -53,9 +55,10 @@ public class UserManager {
}
/**
* Returns the currently-installed UserProvider. Note, in most cases the user
* provider should not be used directly. Instead, the appropriate methods
* in UserManager should be called.
* Returns the currently-installed UserProvider. <b>Warning:</b> in virtually all
* cases the user provider should not be used directly. Instead, the appropriate
* methods in UserManager should be called. Direct access to the user provider is
* only provided for special-case logic.
*
* @return the current UserProvider.
*/
......@@ -104,6 +107,11 @@ public class UserManager {
}
User user = provider.createUser(username, password, name, email);
userCache.put(username, user);
// Fire event.
UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_created,
Collections.emptyMap());
return user;
}
......@@ -125,6 +133,11 @@ public class UserManager {
catch (StringprepException se) {
throw new IllegalArgumentException("Invalid username: " + username, se);
}
// Fire event.
UserEventDispatcher.dispatchEvent(user, UserEventDispatcher.EventType.user_deleting,
Collections.emptyMap());
provider.deleteUser(user.getUsername());
// Remove the user from cache.
userCache.remove(user.getUsername());
......
......@@ -53,10 +53,10 @@
User user = webManager.getUser();
if (user != null) {
if (range == DEFAULT_RANGE) {
user.deleteProperty(USER_RANGE_PROP);
user.getProperties().remove(USER_RANGE_PROP);
}
else {
user.setProperty(USER_RANGE_PROP, String.valueOf(range));
user.getProperties().put(USER_RANGE_PROP, String.valueOf(range));
}
}
}
......@@ -65,10 +65,10 @@
if (webManager.getUser() != null) {
User user = webManager.getUser();
try {
range = Integer.parseInt(user.getProperty(USER_RANGE_PROP));
range = Integer.parseInt(user.getProperties().get(USER_RANGE_PROP));
}
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