Commit aeb5f334 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Added group events.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@830 b35dd754-fafc-0310-a699-88a17e54d16e
parent f3ed4109
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.messenger.event;
import java.util.Map;
import java.util.Date;
/**
* Base interface for events. Every event has a date the event was generated and
* optional parameters.
*
* @author Matt Tucker
*/
public interface Event {
/**
* Returns the date this event was created.
*
* @return the date this event was created.
*/
public Date getDate();
/**
* Returns a map of event parameters, which can contain extra information about
* the event.
*
* @return map of event parameters.
*/
public Map getParams();
}
\ 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.group.Group;
import java.util.Map;
import java.util.Date;
/**
* Group event. Event types with extened parameters (if any) are 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>
*
* @author Matt Tucker
*/
public class GroupEvent implements Event {
private EventType eventType;
private Group group;
private Map params;
private Date date;
/**
* Constructs a new group event.
*
* @param eventType the event type.
* @param group the group triggering the event.
* @param params event parameters.
*/
public GroupEvent(EventType eventType, Group group, Map params) {
this.eventType = eventType;
this.group = group;
this.params = params;
this.date = new Date();
}
/**
* Returns the type of the event.
*
* @return the type of the event.
*/
public EventType getEventType() {
return eventType;
}
/**
* Returns the Group that triggered the event.
*
* @return the Group that triggered the event.
*/
public Group getGroup() {
return group;
}
public Map getParams() {
return params;
}
public Date getDate() {
return date;
}
/**
* 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;
}
}
\ 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.util.Log;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Dispatches group events.
*
* @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.
*
* @param event the event.
*/
public static void dispatchEvent(GroupEvent event) {
GroupEvent.EventType eventType = event.getEventType();
for (GroupEventListener listener : listeners) {
try {
switch (eventType) {
case group_created: {
listener.groupCreated(event);
break;
}
case group_deleting: {
listener.groupDeleting(event);
break;
}
case member_added: {
listener.memberAdded(event);
break;
}
case member_removed: {
listener.memberRemoved(event);
break;
}
case admin_added: {
listener.adminAdded(event);
break;
}
case admin_removed: {
listener.adminRemoved(event);
break;
}
case group_modified: {
listener.groupModified(event);
break;
}
default:
break;
}
}
catch (Exception e) {
Log.error(e);
}
}
}
}
\ 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;
/**
* Interface to listen for group events. Use the
* {@link GroupEventDispatcher#addListener(GroupEventListener)}
* method to register for events.
*
* @author Matt Tucker
*/
public interface GroupEventListener {
/**
* A group was created.
*
* @param event the event.
*/
public void groupCreated(GroupEvent event);
/**
* A group is being deleted.
*
* @param event the event.
*/
public void groupDeleting(GroupEvent event);
/**
* A group's name, description, or an extended property was changed.
*
* @param event the event.
*/
public void groupModified(GroupEvent event);
/**
* A member was added to a group.
*
* @param event the event.
*/
public void memberAdded(GroupEvent event);
/**
* A member was removed from a group.
*
* @param event the event.
*/
public void memberRemoved(GroupEvent event);
/**
* An administrator was added to a group.
*
* @param event the event.
*/
public void adminAdded(GroupEvent event);
/**
* An administrator was removed from a group.
*
* @param event the event.
*/
public void adminRemoved(GroupEvent event);
}
\ No newline at end of file
......@@ -16,6 +16,8 @@ import org.jivesoftware.util.Cacheable;
import org.jivesoftware.util.CacheSizes;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.event.GroupEvent;
import org.jivesoftware.messenger.event.GroupEventDispatcher;
import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.stringprep.Stringprep;
......@@ -89,10 +91,19 @@ public class Group implements Cacheable {
*/
public void setName(String name) {
try {
String originalName = this.name;
provider.setName(this.name, name);
groupManager.groupCache.remove(this.name);
this.name = name;
groupManager.groupCache.put(name, this);
// Fire event.
Map params = new HashMap();
params.put("type", "nameModified");
params.put("originalValue", originalName);
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_modified,
this, params);
GroupEventDispatcher.dispatchEvent(event);
}
catch (Exception e) {
Log.error(e);
......@@ -119,8 +130,16 @@ public class Group implements Cacheable {
*/
public void setDescription(String description) {
try {
String originalDescription = this.description;
provider.setDescription(name, description);
this.description = description;
// Fire event.
Map params = new HashMap();
params.put("type", "descriptionModified");
params.put("originalValue", originalDescription);
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_modified,
this, params);
GroupEventDispatcher.dispatchEvent(event);
}
catch (Exception e) {
Log.error(e);
......@@ -218,12 +237,27 @@ public class Group implements Cacheable {
throw new IllegalStateException();
}
String user = (String)current;
// Update the group users' roster
// Update the group users' roster -- TODO: remove and use event.
XMPPServer.getInstance().getRosterManager().groupUserDeleted(Group.this, user);
// Remove the user from the collection in memory
// Remove the user from the collection in memory.
iter.remove();
// Remove the group user from the backend store
// Remove the group user from the backend store.
provider.deleteMember(name, user);
// Fire event.
if (adminCollection) {
Map params = new HashMap();
params.put("admin", user);
GroupEvent event = new GroupEvent(GroupEvent.EventType.admin_removed,
Group.this, params);
GroupEventDispatcher.dispatchEvent(event);
}
else {
Map params = new HashMap();
params.put("member", user);
GroupEvent event = new GroupEvent(GroupEvent.EventType.member_removed,
Group.this, params);
GroupEventDispatcher.dispatchEvent(event);
}
}
};
}
......@@ -252,9 +286,26 @@ public class Group implements Cacheable {
}
}
if (users.add(username)) {
// Add the group user to the backend store
// Add the group user to the backend store.
provider.addMember(name, username, adminCollection);
// Update the group users' roster
// Fire event.
if (adminCollection) {
Map params = new HashMap();
params.put("admin", username);
GroupEvent event = new GroupEvent(GroupEvent.EventType.admin_added,
Group.this, params);
GroupEventDispatcher.dispatchEvent(event);
}
else {
Map params = new HashMap();
params.put("member", username);
GroupEvent event = new GroupEvent(GroupEvent.EventType.member_added,
Group.this, params);
GroupEventDispatcher.dispatchEvent(event);
}
// Update the group users' roster -- TODO: remove and use event.
XMPPServer.getInstance().getRosterManager().groupUserAdded(Group.this, username);
return true;
}
......@@ -269,10 +320,26 @@ public class Group implements Cacheable {
public Object put(Object key, Object value) {
if (properties.containsKey(key)) {
String originalValue = properties.get(key);
updateProperty((String)key, (String)value);
// Fire event.
Map params = new HashMap();
params.put("type", "propertyModified");
params.put("propertyKey", key);
params.put("originalValue", originalValue);
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_modified,
Group.this, params);
GroupEventDispatcher.dispatchEvent(event);
}
else {
insertProperty((String)key, (String)value);
// Fire event.
Map params = new HashMap();
params.put("type", "propertyAdded");
params.put("propertyKey", key);
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_modified,
Group.this, params);
GroupEventDispatcher.dispatchEvent(event);
}
return properties.put((String)key, (String)value);
}
......@@ -312,6 +379,13 @@ public class Group implements Cacheable {
}
deleteProperty((String)current.getKey());
iter.remove();
// Fire event.
Map params = new HashMap();
params.put("type", "propertyDeleted");
params.put("propertyKey", current.getKey());
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_modified,
Group.this, params);
GroupEventDispatcher.dispatchEvent(event);
}
};
}
......
......@@ -18,9 +18,12 @@ import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.user.User;
import org.jivesoftware.messenger.JiveGlobals;
import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.event.GroupEvent;
import org.jivesoftware.messenger.event.GroupEventDispatcher;
import org.jivesoftware.messenger.roster.RosterManager;
import java.util.Collection;
import java.util.Collections;
/**
* Manages groups.
......@@ -81,6 +84,11 @@ public class GroupManager {
// The group doesn't already exist so we can create a new group
newGroup = provider.createGroup(name);
groupCache.put(name, newGroup);
// Fire event.
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_created,
newGroup, Collections.EMPTY_MAP);
GroupEventDispatcher.dispatchEvent(event);
}
return newGroup;
}
......@@ -115,6 +123,11 @@ public class GroupManager {
* @param group the group to delete.
*/
public void deleteGroup(Group group) {
// Fire event.
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_deleting,
group, Collections.EMPTY_MAP);
GroupEventDispatcher.dispatchEvent(event);
// Delete the group.
provider.deleteGroup(group.getName());
......@@ -127,8 +140,10 @@ public class GroupManager {
}
/**
* Deletes a user from all the groups where he/she belongs. The most probable cause for this
* request is that the user has been deleted from the system.
* Deletes a user from all the groups where he/she belongs. The most probable cause
* for this request is that the user has been deleted from the system.
*
* TODO: remove this method and use events isntead.
*
* @param user the deleted user from the system.
*/
......
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