Commit e1287128 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Group is using MemberCollection again as a wrapper for group users.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@783 b35dd754-fafc-0310-a699-88a17e54d16e
parent 4a4279b5
......@@ -19,7 +19,6 @@ import org.jivesoftware.messenger.XMPPServer;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
......@@ -48,8 +47,8 @@ public class Group implements Cacheable {
private String name;
private String description;
private Map<String, String> properties;
private Collection<String> members = new CopyOnWriteArrayList<String>();
private Collection<String> administrators = new CopyOnWriteArrayList<String>();
private Collection<String> members;
private Collection<String> administrators;
/**
* Constructs a new group.
......@@ -67,8 +66,8 @@ public class Group implements Cacheable {
this.groupManager = GroupManager.getInstance();
this.name = name;
this.description = description;
this.members.addAll(members);
this.administrators.addAll(administrators);
this.members = members;
this.administrators = administrators;
}
/**
......@@ -144,35 +143,23 @@ public class Group implements Cacheable {
}
/**
* Returns a Collection of the group administrators. Use <code>getUsers()</code> to get the
* complete list of group users.
* Returns a Collection of the group administrators.
*
* @return a Collection of the group administrators.
*/
public Collection<String> getAdmins() {
return Collections.unmodifiableCollection(administrators);
// Return a wrapper that will intercept add and remove commands.
return new MemberCollection(administrators, true);
}
/**
* Returns a Collection of the group members. Use <code>getUsers()</code> to get the complete
* list of group users.
* Returns a Collection of the group members.
*
* @return a Collection of the group members.
*/
public Collection<String> getMembers() {
return Collections.unmodifiableCollection(members);
}
/**
* Returns a Collection with all the group users. The collection will include group members
* as well as group administrators.
*
* @return a Collection with all the group users.
*/
public Collection<String> getUsers() {
Collection<String> answer = new ArrayList<String>(members);
answer.addAll(administrators);
return Collections.unmodifiableCollection(answer);
// Return a wrapper that will intercept add and remove commands.
return new MemberCollection(members, false);
}
/**
......@@ -185,110 +172,84 @@ public class Group implements Cacheable {
return members.contains(username) || administrators.contains(username);
}
/**
* Returns true if the provided username belongs to a member of the group.
*
* @param username the username to check.
* @return true if the provided username belongs to a member of the group.
*/
public boolean isMember(String username) {
return members.contains(username);
public int getCachedSize() {
// Approximate the size of the object in bytes by calculating the size
// of each field.
int size = 0;
size += CacheSizes.sizeOfObject(); // overhead of object
size += CacheSizes.sizeOfString(name);
size += CacheSizes.sizeOfString(description);
return size;
}
/**
* Returns true if the provided username belongs to an administrator of the group.
*
* @param username the username to check.
* @return true if the provided username belongs to an administrator of the group.
* Collection implementation that notifies the GroupProvider of any
* changes to the collection.
*/
public boolean isAdmin(String username) {
return administrators.contains(username);
}
private class MemberCollection extends AbstractCollection {
/**
* Adds a new user as a member of the group. The roster of all group users that are currently
* logged into the server will be updated.
*
* @param user the user to add as a member of the goup.
*/
public void addMember(String user) {
if (members.contains(user)) {
return;
}
members.add(user);
userAdded(user, false);
}
private Collection<String> users;
private boolean adminCollection;
/**
* Removes a member from the group. The roster of all group users that are currently
* logged into the server will be updated.
*
* @param user the user to remove as a member of the group.
*/
public void removeMember(String user) {
if (members.remove(user)) {
userRemoved(user);
public MemberCollection(Collection<String> users, boolean adminCollection) {
this.users = users;
this.adminCollection = adminCollection;
}
}
/**
* Adds a new user as an administrator of the group. The roster of all group users that are
* currently logged into the server will be updated.
*
* @param user the user to add as an administrator of the goup.
*/
public void addAdmin(String user) {
if (administrators.contains(user)) {
return;
}
administrators.add(user);
userAdded(user, true);
}
public Iterator iterator() {
return new Iterator() {
/**
* Removes an administrator from the group. The roster of all group users that are currently
* logged into the server will be updated.
*
* @param user the user to remove as an administrator of the group.
*/
public void removeAdmin(String user) {
if (administrators.remove(user)) {
userRemoved(user);
}
}
Iterator iter = users.iterator();
Object current = null;
/**
* Update backend store and update group users' roster.
*
* @param user the user that was added to the group.
*/
private void userAdded(String user, boolean administrator) {
// Add the new group user to the backend store
provider.addMember(name, user, administrator);
// Update the group users' roster
XMPPServer.getInstance().getRosterManager().groupUserAdded(this, user);
}
public boolean hasNext() {
return iter.hasNext();
}
/**
* Update backend store and update group users' roster.
*
* @param user the user that was removed from the group.
*/
private void userRemoved(String user) {
// Remove the group user from the backend store
provider.deleteMember(name, user);
// Update the group users' roster
XMPPServer.getInstance().getRosterManager().groupUserDeleted(this, user);
}
public Object next() {
current = iter.next();
return current;
}
public int getCachedSize() {
// Approximate the size of the object in bytes by calculating the size
// of each field.
int size = 0;
size += CacheSizes.sizeOfObject(); // overhead of object
size += CacheSizes.sizeOfString(name);
size += CacheSizes.sizeOfString(description);
return size;
public void remove() {
if (current == null) {
throw new IllegalStateException();
}
iter.remove();
String user = (String) current;
// Remove the group user from the backend store
provider.deleteMember(name, user);
// Update the group users' roster
XMPPServer.getInstance().getRosterManager().groupUserDeleted(Group.this, user);
}
};
}
public int size() {
return users.size();
}
public boolean add(Object member) {
String user = (String) member;
if (adminCollection) {
if (members.contains(user)) {
throw new IllegalArgumentException("The user is already a member of the group");
}
}
else {
if (administrators.contains(user)) {
throw new IllegalArgumentException("The user is already an admin of the group");
}
}
if (users.add(user)) {
// Add the group user to the backend store
provider.addMember(name, user, adminCollection);
// Update the group users' roster
XMPPServer.getInstance().getRosterManager().groupUserAdded(Group.this, user);
return true;
}
return false;
}
}
/**
......
......@@ -134,11 +134,11 @@ public class GroupManager {
*/
public void deleteUser(User user) {
for (Group group : getGroups(user)) {
if (group.isAdmin(user.getUsername())) {
group.removeAdmin(user.getUsername());
if (group.getAdmins().contains(user.getUsername())) {
group.getAdmins().remove(user.getUsername());
}
else {
group.removeMember(user.getUsername());
group.getMembers().remove(user.getUsername());
}
}
}
......
......@@ -401,8 +401,11 @@ public class Roster implements Cacheable {
// will have one entry in the map associated with all the groups
Map<JID,List<String>> sharedGroupUsers = new HashMap<JID,List<String>>();
for (Group group : sharedGroups) {
// Get all the group users
Collection<String> users = new ArrayList<String>(group.getMembers());
users.addAll(group.getAdmins());
// Add the users of the group to the general list of users to process
for (String groupUser : group.getUsers()) {
for (String groupUser : users) {
// Add the user to the answer if the user doesn't belong to the personal roster
// (since we have already added the user to the answer)
JID jid = new JID(groupUser, XMPPServer.getInstance().getServerInfo().getName(),
......
......@@ -20,6 +20,8 @@ import org.jivesoftware.messenger.SharedGroupException;
import org.jivesoftware.messenger.group.Group;
import java.util.Iterator;
import java.util.Collection;
import java.util.ArrayList;
/**
* A simple service that allows components to retrieve a roster based solely on the ID
......@@ -125,8 +127,11 @@ public class RosterManager extends BasicModule {
* @param group the group that has been deleted.
*/
public void groupDeleted(Group group) {
// Get all the group users
Collection<String> users = new ArrayList<String>(group.getMembers());
users.addAll(group.getAdmins());
// Iterate on all the group users and update their rosters
for (String deletedUser : group.getUsers()) {
for (String deletedUser : users) {
groupUserDeleted(group, deletedUser);
}
}
......@@ -138,8 +143,11 @@ public class RosterManager extends BasicModule {
* @param addedUser the username of the user that has been added to the group.
*/
public void groupUserAdded(Group group, String addedUser) {
// Get all the group users
Collection<String> users = new ArrayList<String>(group.getMembers());
users.addAll(group.getAdmins());
// Iterate on all the group users and update their rosters
for (String userToUpdate : group.getUsers()) {
for (String userToUpdate : users) {
if (!addedUser.equals(userToUpdate)) {
// Get the roster to update
Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToUpdate);
......@@ -158,8 +166,11 @@ public class RosterManager extends BasicModule {
* @param deletedUser the username of the user that has been deleted from the group.
*/
public void groupUserDeleted(Group group, String deletedUser) {
// Get all the group users
Collection<String> users = new ArrayList<String>(group.getMembers());
users.addAll(group.getAdmins());
// Iterate on all the group users and update their rosters
for (String userToUpdate : group.getUsers()) {
for (String userToUpdate : users) {
if (!deletedUser.equals(userToUpdate)) {
// Get the roster to update
Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToUpdate);
......
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