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 @@ ...@@ -21,12 +21,56 @@
package org.jivesoftware.messenger.event; package org.jivesoftware.messenger.event;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.group.Group;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList; 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 * @author Matt Tucker
*/ */
...@@ -63,40 +107,40 @@ public class GroupEventDispatcher { ...@@ -63,40 +107,40 @@ public class GroupEventDispatcher {
/** /**
* Dispatches an event to all listeners. * 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) { public static void dispatchEvent(Group group, EventType eventType, Map params) {
GroupEvent.EventType eventType = event.getEventType();
for (GroupEventListener listener : listeners) { for (GroupEventListener listener : listeners) {
try { try {
switch (eventType) { switch (eventType) {
case group_created: { case group_created: {
listener.groupCreated(event); listener.groupCreated(group, params);
break; break;
} }
case group_deleting: { case group_deleting: {
listener.groupDeleting(event); listener.groupDeleting(group, params);
break; break;
} }
case member_added: { case member_added: {
listener.memberAdded(event); listener.memberAdded(group, params);
break; break;
} }
case member_removed: { case member_removed: {
listener.memberRemoved(event); listener.memberRemoved(group, params);
break; break;
} }
case admin_added: { case admin_added: {
listener.adminAdded(event); listener.adminAdded(group, params);
break; break;
} }
case admin_removed: { case admin_removed: {
listener.adminRemoved(event); listener.adminRemoved(group, params);
break; break;
} }
case group_modified: { case group_modified: {
listener.groupModified(event); listener.groupModified(group, params);
break; break;
} }
default: default:
...@@ -108,4 +152,47 @@ public class GroupEventDispatcher { ...@@ -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 @@ ...@@ -20,6 +20,10 @@
package org.jivesoftware.messenger.event; package org.jivesoftware.messenger.event;
import org.jivesoftware.messenger.group.Group;
import java.util.Map;
/** /**
* Interface to listen for group events. Use the * Interface to listen for group events. Use the
* {@link GroupEventDispatcher#addListener(GroupEventListener)} * {@link GroupEventDispatcher#addListener(GroupEventListener)}
...@@ -32,49 +36,56 @@ public interface GroupEventListener { ...@@ -32,49 +36,56 @@ public interface GroupEventListener {
/** /**
* A group was created. * 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. * 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. * 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. * 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. * 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. * 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. * 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; ...@@ -16,7 +16,6 @@ import org.jivesoftware.util.Cacheable;
import org.jivesoftware.util.CacheSizes; import org.jivesoftware.util.CacheSizes;
import org.jivesoftware.database.DbConnectionManager; import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.messenger.XMPPServer; import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.event.GroupEvent;
import org.jivesoftware.messenger.event.GroupEventDispatcher; import org.jivesoftware.messenger.event.GroupEventDispatcher;
import org.jivesoftware.messenger.user.UserManager; import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.stringprep.Stringprep; import org.jivesoftware.stringprep.Stringprep;
...@@ -101,9 +100,8 @@ public class Group implements Cacheable { ...@@ -101,9 +100,8 @@ public class Group implements Cacheable {
Map params = new HashMap(); Map params = new HashMap();
params.put("type", "nameModified"); params.put("type", "nameModified");
params.put("originalValue", originalName); params.put("originalValue", originalName);
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_modified, GroupEventDispatcher.dispatchEvent(this, GroupEventDispatcher.EventType.group_modified,
this, params); params);
GroupEventDispatcher.dispatchEvent(event);
} }
catch (Exception e) { catch (Exception e) {
Log.error(e); Log.error(e);
...@@ -137,9 +135,8 @@ public class Group implements Cacheable { ...@@ -137,9 +135,8 @@ public class Group implements Cacheable {
Map params = new HashMap(); Map params = new HashMap();
params.put("type", "descriptionModified"); params.put("type", "descriptionModified");
params.put("originalValue", originalDescription); params.put("originalValue", originalDescription);
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_modified, GroupEventDispatcher.dispatchEvent(this,
this, params); GroupEventDispatcher.EventType.group_modified, params);
GroupEventDispatcher.dispatchEvent(event);
} }
catch (Exception e) { catch (Exception e) {
Log.error(e); Log.error(e);
...@@ -247,16 +244,14 @@ public class Group implements Cacheable { ...@@ -247,16 +244,14 @@ public class Group implements Cacheable {
if (adminCollection) { if (adminCollection) {
Map params = new HashMap(); Map params = new HashMap();
params.put("admin", user); params.put("admin", user);
GroupEvent event = new GroupEvent(GroupEvent.EventType.admin_removed, GroupEventDispatcher.dispatchEvent(Group.this,
Group.this, params); GroupEventDispatcher.EventType.admin_removed, params);
GroupEventDispatcher.dispatchEvent(event);
} }
else { else {
Map params = new HashMap(); Map params = new HashMap();
params.put("member", user); params.put("member", user);
GroupEvent event = new GroupEvent(GroupEvent.EventType.member_removed, GroupEventDispatcher.dispatchEvent(Group.this,
Group.this, params); GroupEventDispatcher.EventType.member_removed, params);
GroupEventDispatcher.dispatchEvent(event);
} }
} }
}; };
...@@ -293,16 +288,14 @@ public class Group implements Cacheable { ...@@ -293,16 +288,14 @@ public class Group implements Cacheable {
if (adminCollection) { if (adminCollection) {
Map params = new HashMap(); Map params = new HashMap();
params.put("admin", username); params.put("admin", username);
GroupEvent event = new GroupEvent(GroupEvent.EventType.admin_added, GroupEventDispatcher.dispatchEvent(Group.this,
Group.this, params); GroupEventDispatcher.EventType.admin_added, params);
GroupEventDispatcher.dispatchEvent(event);
} }
else { else {
Map params = new HashMap(); Map params = new HashMap();
params.put("member", username); params.put("member", username);
GroupEvent event = new GroupEvent(GroupEvent.EventType.member_added, GroupEventDispatcher.dispatchEvent(Group.this,
Group.this, params); GroupEventDispatcher.EventType.member_added, params);
GroupEventDispatcher.dispatchEvent(event);
} }
// Update the group users' roster -- TODO: remove and use event. // Update the group users' roster -- TODO: remove and use event.
...@@ -327,9 +320,8 @@ public class Group implements Cacheable { ...@@ -327,9 +320,8 @@ public class Group implements Cacheable {
params.put("type", "propertyModified"); params.put("type", "propertyModified");
params.put("propertyKey", key); params.put("propertyKey", key);
params.put("originalValue", originalValue); params.put("originalValue", originalValue);
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_modified, GroupEventDispatcher.dispatchEvent(Group.this,
Group.this, params); GroupEventDispatcher.EventType.group_modified, params);
GroupEventDispatcher.dispatchEvent(event);
} }
else { else {
insertProperty((String)key, (String)value); insertProperty((String)key, (String)value);
...@@ -337,9 +329,8 @@ public class Group implements Cacheable { ...@@ -337,9 +329,8 @@ public class Group implements Cacheable {
Map params = new HashMap(); Map params = new HashMap();
params.put("type", "propertyAdded"); params.put("type", "propertyAdded");
params.put("propertyKey", key); params.put("propertyKey", key);
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_modified, GroupEventDispatcher.dispatchEvent(Group.this,
Group.this, params); GroupEventDispatcher.EventType.group_modified, params);
GroupEventDispatcher.dispatchEvent(event);
} }
return properties.put((String)key, (String)value); return properties.put((String)key, (String)value);
} }
...@@ -383,9 +374,8 @@ public class Group implements Cacheable { ...@@ -383,9 +374,8 @@ public class Group implements Cacheable {
Map params = new HashMap(); Map params = new HashMap();
params.put("type", "propertyDeleted"); params.put("type", "propertyDeleted");
params.put("propertyKey", current.getKey()); params.put("propertyKey", current.getKey());
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_modified, GroupEventDispatcher.dispatchEvent(Group.this,
Group.this, params); GroupEventDispatcher.EventType.group_modified, params);
GroupEventDispatcher.dispatchEvent(event);
} }
}; };
} }
......
...@@ -18,7 +18,6 @@ import org.jivesoftware.util.Log; ...@@ -18,7 +18,6 @@ import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.user.User; import org.jivesoftware.messenger.user.User;
import org.jivesoftware.messenger.JiveGlobals; import org.jivesoftware.messenger.JiveGlobals;
import org.jivesoftware.messenger.XMPPServer; import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.event.GroupEvent;
import org.jivesoftware.messenger.event.GroupEventDispatcher; import org.jivesoftware.messenger.event.GroupEventDispatcher;
import org.jivesoftware.messenger.roster.RosterManager; import org.jivesoftware.messenger.roster.RosterManager;
...@@ -86,9 +85,8 @@ public class GroupManager { ...@@ -86,9 +85,8 @@ public class GroupManager {
groupCache.put(name, newGroup); groupCache.put(name, newGroup);
// Fire event. // Fire event.
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_created, GroupEventDispatcher.dispatchEvent(newGroup,
newGroup, Collections.EMPTY_MAP); GroupEventDispatcher.EventType.group_created, Collections.emptyMap());
GroupEventDispatcher.dispatchEvent(event);
} }
return newGroup; return newGroup;
} }
...@@ -124,9 +122,8 @@ public class GroupManager { ...@@ -124,9 +122,8 @@ public class GroupManager {
*/ */
public void deleteGroup(Group group) { public void deleteGroup(Group group) {
// Fire event. // Fire event.
GroupEvent event = new GroupEvent(GroupEvent.EventType.group_deleting, GroupEventDispatcher.dispatchEvent(group, GroupEventDispatcher.EventType.group_deleting,
group, Collections.EMPTY_MAP); Collections.emptyMap());
GroupEventDispatcher.dispatchEvent(event);
// Delete the group. // Delete the group.
provider.deleteGroup(group.getName()); provider.deleteGroup(group.getName());
......
...@@ -45,7 +45,7 @@ public class AdminConsoleTest extends TestCase { ...@@ -45,7 +45,7 @@ public class AdminConsoleTest extends TestCase {
String filename = TestUtils.prepareFilename( String filename = TestUtils.prepareFilename(
"./resources/org/jivesoftware/admin/AdminConsoleTest.admin-sidebar-01.xml"); "./resources/org/jivesoftware/admin/AdminConsoleTest.admin-sidebar-01.xml");
InputStream in = new FileInputStream(filename); InputStream in = new FileInputStream(filename);
AdminConsole.addModel(in); AdminConsole.addModel("test1", in);
in.close(); in.close();
String name = AdminConsole.getAppName(); String name = AdminConsole.getAppName();
assertEquals("Foo Bar", name); assertEquals("Foo Bar", name);
...@@ -58,7 +58,7 @@ public class AdminConsoleTest extends TestCase { ...@@ -58,7 +58,7 @@ public class AdminConsoleTest extends TestCase {
String filename = TestUtils.prepareFilename( String filename = TestUtils.prepareFilename(
"./resources/org/jivesoftware/admin/AdminConsoleTest.admin-sidebar-02.xml"); "./resources/org/jivesoftware/admin/AdminConsoleTest.admin-sidebar-02.xml");
InputStream in = new FileInputStream(filename); InputStream in = new FileInputStream(filename);
AdminConsole.addModel(in); AdminConsole.addModel("test2", in);
in.close(); in.close();
Collection tabs = AdminConsole.getModel().selectNodes("//tab"); Collection tabs = AdminConsole.getModel().selectNodes("//tab");
assertNotNull(tabs); assertNotNull(tabs);
...@@ -82,7 +82,7 @@ public class AdminConsoleTest extends TestCase { ...@@ -82,7 +82,7 @@ public class AdminConsoleTest extends TestCase {
String filename = TestUtils.prepareFilename( String filename = TestUtils.prepareFilename(
"./resources/org/jivesoftware/admin/AdminConsoleTest.admin-sidebar-03.xml"); "./resources/org/jivesoftware/admin/AdminConsoleTest.admin-sidebar-03.xml");
InputStream in = new FileInputStream(filename); InputStream in = new FileInputStream(filename);
AdminConsole.addModel(in); AdminConsole.addModel("test3", in);
in.close(); in.close();
boolean found = false; boolean found = false;
for (Iterator tabs=AdminConsole.getModel().selectNodes("//tab").iterator(); tabs.hasNext(); ) { 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