Commit 52c5c4e0 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Refactored group events.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@832 b35dd754-fafc-0310-a699-88a17e54d16e
parent c7608052
/**
* $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
......@@ -21,12 +21,56 @@
package org.jivesoftware.messenger.event;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.group.Group;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Dispatches group events.
* Dispatches group 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#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
*/
......@@ -63,40 +107,40 @@ public class GroupEventDispatcher {
/**
* Dispatches an event to all listeners.
*
* @param event the event.
* @param group the group.
* @param eventType the event type.
* @param params event parameters.
*/
public static void dispatchEvent(GroupEvent event) {
GroupEvent.EventType eventType = event.getEventType();
public static void dispatchEvent(Group group, EventType eventType, Map params) {
for (GroupEventListener listener : listeners) {
try {
switch (eventType) {
case group_created: {
listener.groupCreated(event);
listener.groupCreated(group, params);
break;
}
case group_deleting: {
listener.groupDeleting(event);
listener.groupDeleting(group, params);
break;
}
case member_added: {
listener.memberAdded(event);
listener.memberAdded(group, params);
break;
}
case member_removed: {
listener.memberRemoved(event);
listener.memberRemoved(group, params);
break;
}
case admin_added: {
listener.adminAdded(event);
listener.adminAdded(group, params);
break;
}
case admin_removed: {
listener.adminRemoved(event);
listener.adminRemoved(group, params);
break;
}
case group_modified: {
listener.groupModified(event);
listener.groupModified(group, params);
break;
}
default:
......@@ -108,4 +152,47 @@ public class GroupEventDispatcher {
}
}
}
/**
* 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
......@@ -20,6 +20,10 @@
package org.jivesoftware.messenger.event;
import org.jivesoftware.messenger.group.Group;
import java.util.Map;
/**
* Interface to listen for group events. Use the
* {@link GroupEventDispatcher#addListener(GroupEventListener)}
......@@ -32,49 +36,56 @@ public interface GroupEventListener {
/**
* A group was created.
*
* @param event the event.
* @param group the group.
* @param params event parameters.
*/
public void groupCreated(GroupEvent event);
public void groupCreated(Group group, Map params);
/**
* A group is being deleted.
*
* @param event the event.
* @param group the group.
* @param params event parameters.
*/
public void groupDeleting(GroupEvent event);
public void groupDeleting(Group group, Map params);
/**
* A group's name, description, or an extended property was changed.
*
* @param event the event.
* @param group the group.
* @param params event parameters.
*/
public void groupModified(GroupEvent event);
public void groupModified(Group group, Map params);
/**
* A member was added to a group.
*
* @param event the event.
* @param group the group.
* @param params event parameters.
*/
public void memberAdded(GroupEvent event);
public void memberAdded(Group group, Map params);
/**
* A member was removed from a group.
*
* @param event the event.
* @param group the group.
* @param params event parameters.
*/
public void memberRemoved(GroupEvent event);
public void memberRemoved(Group group, Map params);
/**
* An administrator was added to a group.
*
* @param event the event.
* @param group the group.
* @param params event parameters.
*/
public void adminAdded(GroupEvent event);
public void adminAdded(Group group, Map params);
/**
* An administrator was removed from a group.
*
* @param event the event.
* @param group the group.
* @param params event parameters.
*/
public void adminRemoved(GroupEvent event);
public void adminRemoved(Group group, Map params);
}
\ No newline at end of file
......@@ -16,7 +16,6 @@ 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;
......@@ -101,9 +100,8 @@ public class Group implements Cacheable {
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);
GroupEventDispatcher.dispatchEvent(this, GroupEventDispatcher.EventType.group_modified,
params);
}
catch (Exception e) {
Log.error(e);
......@@ -137,9 +135,8 @@ public class Group implements Cacheable {
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);
GroupEventDispatcher.dispatchEvent(this,
GroupEventDispatcher.EventType.group_modified, params);
}
catch (Exception e) {
Log.error(e);
......@@ -247,16 +244,14 @@ public class Group implements Cacheable {
if (adminCollection) {
Map params = new HashMap();
params.put("admin", user);
GroupEvent event = new GroupEvent(GroupEvent.EventType.admin_removed,
Group.this, params);
GroupEventDispatcher.dispatchEvent(event);
GroupEventDispatcher.dispatchEvent(Group.this,
GroupEventDispatcher.EventType.admin_removed, params);
}
else {
Map params = new HashMap();
params.put("member", user);
GroupEvent event = new GroupEvent(GroupEvent.EventType.member_removed,
Group.this, params);
GroupEventDispatcher.dispatchEvent(event);
GroupEventDispatcher.dispatchEvent(Group.this,
GroupEventDispatcher.EventType.member_removed, params);
}
}
};
......@@ -293,16 +288,14 @@ public class Group implements Cacheable {
if (adminCollection) {
Map params = new HashMap();
params.put("admin", username);
GroupEvent event = new GroupEvent(GroupEvent.EventType.admin_added,
Group.this, params);
GroupEventDispatcher.dispatchEvent(event);
GroupEventDispatcher.dispatchEvent(Group.this,
GroupEventDispatcher.EventType.admin_added, params);
}
else {
Map params = new HashMap();
params.put("member", username);
GroupEvent event = new GroupEvent(GroupEvent.EventType.member_added,
Group.this, params);
GroupEventDispatcher.dispatchEvent(event);
GroupEventDispatcher.dispatchEvent(Group.this,
GroupEventDispatcher.EventType.member_added, params);
}
// Update the group users' roster -- TODO: remove and use event.
......@@ -327,9 +320,8 @@ public class Group implements Cacheable {
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);
GroupEventDispatcher.dispatchEvent(Group.this,
GroupEventDispatcher.EventType.group_modified, params);
}
else {
insertProperty((String)key, (String)value);
......@@ -337,9 +329,8 @@ public class Group implements Cacheable {
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);
GroupEventDispatcher.dispatchEvent(Group.this,
GroupEventDispatcher.EventType.group_modified, params);
}
return properties.put((String)key, (String)value);
}
......@@ -383,9 +374,8 @@ public class Group implements Cacheable {
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);
GroupEventDispatcher.dispatchEvent(Group.this,
GroupEventDispatcher.EventType.group_modified, params);
}
};
}
......
......@@ -18,7 +18,6 @@ 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;
......@@ -86,9 +85,8 @@ public class GroupManager {
groupCache.put(name, newGroup);
// Fire event.
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_created,
newGroup, Collections.EMPTY_MAP);
GroupEventDispatcher.dispatchEvent(event);
GroupEventDispatcher.dispatchEvent(newGroup,
GroupEventDispatcher.EventType.group_created, Collections.emptyMap());
}
return newGroup;
}
......@@ -124,9 +122,8 @@ public class GroupManager {
*/
public void deleteGroup(Group group) {
// Fire event.
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_deleting,
group, Collections.EMPTY_MAP);
GroupEventDispatcher.dispatchEvent(event);
GroupEventDispatcher.dispatchEvent(group, GroupEventDispatcher.EventType.group_deleting,
Collections.emptyMap());
// Delete the group.
provider.deleteGroup(group.getName());
......
......@@ -45,7 +45,7 @@ public class AdminConsoleTest extends TestCase {
String filename = TestUtils.prepareFilename(
"./resources/org/jivesoftware/admin/AdminConsoleTest.admin-sidebar-01.xml");
InputStream in = new FileInputStream(filename);
AdminConsole.addModel(in);
AdminConsole.addModel("test1", in);
in.close();
String name = AdminConsole.getAppName();
assertEquals("Foo Bar", name);
......@@ -58,7 +58,7 @@ public class AdminConsoleTest extends TestCase {
String filename = TestUtils.prepareFilename(
"./resources/org/jivesoftware/admin/AdminConsoleTest.admin-sidebar-02.xml");
InputStream in = new FileInputStream(filename);
AdminConsole.addModel(in);
AdminConsole.addModel("test2", in);
in.close();
Collection tabs = AdminConsole.getModel().selectNodes("//tab");
assertNotNull(tabs);
......@@ -82,7 +82,7 @@ public class AdminConsoleTest extends TestCase {
String filename = TestUtils.prepareFilename(
"./resources/org/jivesoftware/admin/AdminConsoleTest.admin-sidebar-03.xml");
InputStream in = new FileInputStream(filename);
AdminConsole.addModel(in);
AdminConsole.addModel("test3", in);
in.close();
boolean found = false;
for (Iterator tabs=AdminConsole.getModel().selectNodes("//tab").iterator(); tabs.hasNext(); ) {
......
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