KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > roster > RosterManager


1 /**
2  * $RCSfile: RosterManager.java,v $
3  * $Revision: 1.15 $
4  * $Date: 2005/04/15 00:25:02 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.roster;
13
14 import org.xmpp.packet.JID;
15 import org.jivesoftware.util.Cache;
16 import org.jivesoftware.util.CacheManager;
17 import org.jivesoftware.messenger.container.BasicModule;
18 import org.jivesoftware.messenger.user.UserNotFoundException;
19 import org.jivesoftware.messenger.user.User;
20 import org.jivesoftware.messenger.user.UserManager;
21 import org.jivesoftware.messenger.SharedGroupException;
22 import org.jivesoftware.messenger.event.GroupEventListener;
23 import org.jivesoftware.messenger.event.GroupEventDispatcher;
24 import org.jivesoftware.messenger.group.Group;
25 import org.jivesoftware.messenger.group.GroupManager;
26 import org.jivesoftware.messenger.group.GroupNotFoundException;
27
28 import java.util.*;
29
30 /**
31  * A simple service that allows components to retrieve a roster based solely on the ID
32  * of the owner. Users have convenience methods for obtaining a roster associated with
33  * the owner. However there are many components that need to retrieve the roster
34  * based solely on the generic ID owner key. This interface defines a service that can
35  * do that. This allows classes that generically manage resource for resource owners
36  * (such as presence updates) to generically offer their services without knowing or
37  * caring if the roster owner is a user, chatbot, etc.
38  *
39  * @author Iain Shigeoka
40  */

41 public class RosterManager extends BasicModule implements GroupEventListener {
42
43     private Cache rosterCache = null;
44
45     public RosterManager() {
46         super("Roster Manager");
47         // Add the new instance as a listener of group events
48
GroupEventDispatcher.addListener(this);
49     }
50
51     /**
52      * Returns the roster for the given username.
53      *
54      * @param username the username to search for.
55      * @return the roster associated with the ID.
56      * @throws org.jivesoftware.messenger.user.UserNotFoundException if the ID does not correspond
57      * to a known entity on the server.
58      */

59     public Roster getRoster(String JavaDoc username) throws UserNotFoundException {
60         if (rosterCache == null) {
61             rosterCache = CacheManager.getCache("username2roster");
62         }
63         if (rosterCache == null) {
64             throw new UserNotFoundException("Could not load caches");
65         }
66         Roster roster = (Roster)rosterCache.get(username);
67         if (roster == null) {
68             // Not in cache so load a new one:
69
roster = new Roster(username);
70             rosterCache.put(username, roster);
71         }
72         if (roster == null) {
73             throw new UserNotFoundException(username);
74         }
75         return roster;
76     }
77
78     /**
79      * Removes the entire roster of a given user. This is necessary when a user
80      * account is being deleted from the server.
81      *
82      * @param user the user.
83      */

84     public void deleteRoster(JID user) {
85         try {
86             String JavaDoc username = user.getNode();
87             // Get the roster of the deleted user
88
Roster roster = (Roster)CacheManager.getCache("username2roster").get(username);
89             if (roster == null) {
90                 // Not in cache so load a new one:
91
roster = new Roster(username);
92             }
93             // Remove each roster item from the user's roster
94
for (RosterItem item : roster.getRosterItems()) {
95                 try {
96                     roster.deleteRosterItem(item.getJid(), false);
97                 }
98                 catch (SharedGroupException e) {
99                     // Do nothing. We shouldn't have this exception since we disabled the checkings
100
}
101             }
102             // Remove the cached roster from memory
103
CacheManager.getCache("username2roster").remove(username);
104
105             // Get the rosters that have a reference to the deleted user
106
RosterItemProvider rosteItemProvider = RosterItemProvider.getInstance();
107             Iterator<String JavaDoc> usernames = rosteItemProvider.getUsernames(user.toBareJID());
108             while (usernames.hasNext()) {
109                 username = usernames.next();
110                 // Get the roster that has a reference to the deleted user
111
roster = (Roster)CacheManager.getCache("username2roster").get(username);
112                 if (roster == null) {
113                     // Not in cache so load a new one:
114
roster = new Roster(username);
115                 }
116                 // Remove the deleted user reference from this roster
117
try {
118                     roster.deleteRosterItem(user, false);
119                 }
120                 catch (SharedGroupException e) {
121                     // Do nothing. We shouldn't have this exception since we disabled the checkings
122
}
123             }
124         }
125         catch (UnsupportedOperationException JavaDoc e) {
126             // Do nothing
127
}
128     }
129
130     /**
131      * Returns a collection with all the groups that the user may include in his roster. The
132      * following criteria will be used to select the groups: 1) Groups that are configured so that
133      * everybody can include in his roster, 2) Groups that are configured so that its users may
134      * include the group in their rosters and the user is a group user of the group and 3) User
135      * belongs to a Group that may see a Group that whose members may include the Group in their
136      * rosters.
137      *
138      * @param user the user to return his shared groups.
139      * @return a collection with all the groups that the user may include in his roster.
140      */

141     public Collection<Group> getSharedGroups(User user) {
142         Collection<Group> answer = new HashSet<Group>();
143         Collection<Group> groups = GroupManager.getInstance().getGroups();
144         for (Group group : groups) {
145             String JavaDoc showInRoster = group.getProperties().get("sharedRoster.showInRoster");
146             if ("onlyGroup".equals(showInRoster)) {
147                 if (group.isUser(user.getUsername())) {
148                     // The user belongs to the group so add the group to the answer
149
answer.add(group);
150                 }
151                 else {
152                     // Check if the user belongs to a group that may see this group
153
Collection<Group> groupList = parseGroups(group.getProperties().get("sharedRoster.groupList"));
154                     for (Group groupInList : groupList) {
155                         if (groupInList.isUser(user.getUsername())) {
156                             answer.add(group);
157                         }
158                     }
159                 }
160             }
161             else if ("everybody".equals(showInRoster)) {
162                 // Anyone can see this group so add the group to the answer
163
answer.add(group);
164             }
165         }
166         return answer;
167     }
168
169     /**
170      * Returns a collection of Groups obtained by parsing a comma delimited String with the name
171      * of groups.
172      *
173      * @param groupNames a comma delimited string with group names.
174      * @return a collection of Groups obtained by parsing a comma delimited String with the name
175      * of groups.
176      */

177     private Collection<Group> parseGroups(String JavaDoc groupNames) {
178         Collection<Group> answer = new HashSet<Group>();
179         if (groupNames != null) {
180             StringTokenizer tokenizer = new StringTokenizer(groupNames, ",");
181             while (tokenizer.hasMoreTokens()) {
182                 String JavaDoc groupName = tokenizer.nextToken();
183                 try {
184                     answer.add(GroupManager.getInstance().getGroup(groupName));
185                 }
186                 catch (GroupNotFoundException e) {
187                     // Do nothing. Silently ignore the invalid reference to the group
188
}
189             }
190         }
191         return answer;
192     }
193
194     public void groupCreated(Group group, Map params) {
195         //Do nothing
196
}
197
198     public void groupDeleting(Group group, Map params) {
199         // Iterate on all the group users and update their rosters
200
for (String JavaDoc deletedUser : getAffectedUsers(group)) {
201             groupUserDeleted(group, deletedUser);
202         }
203     }
204
205     public void groupModified(Group group, Map params) {
206         // Do nothing if no group property has been modified
207
if (!"propertyModified".equals(params.get("type"))) {
208              return;
209         }
210         String JavaDoc keyChanged = (String JavaDoc) params.get("propertyKey");
211         String JavaDoc originalValue = (String JavaDoc) params.get("originalValue");
212
213
214         if ("sharedRoster.showInRoster".equals(keyChanged)) {
215             String JavaDoc currentValue = group.getProperties().get("sharedRoster.showInRoster");
216             // Nothing has changed so do nothing.
217
if (currentValue.equals(originalValue)) {
218                 return;
219             }
220             // Get the users of the group
221
Collection<String JavaDoc> users = new HashSet<String JavaDoc>(group.getMembers());
222             users.addAll(group.getAdmins());
223             // Get the users whose roster will be affected
224
Collection<String JavaDoc> affectedUsers = getAffectedUsers(group, originalValue,
225                     group.getProperties().get("sharedRoster.groupList"));
226             // Remove the group members from the affected rosters
227
for (String JavaDoc deletedUser : users) {
228                 groupUserDeleted(group, affectedUsers, deletedUser);
229             }
230
231             // Simulate that the group users has been added to the group. This will cause to push
232
// roster items to the "affected" users for the group users
233
//Collection<Group> visibleGroups = getVisibleGroups(group);
234
for (String JavaDoc user : users) {
235                 groupUserAdded(group, user);
236                 /*for (Group visibleGroup : visibleGroups) {
237                     addSharedGroupToRoster(visibleGroup, user);
238                 }*/

239             }
240         }
241         else if ("sharedRoster.groupList".equals(keyChanged)) {
242             String JavaDoc currentValue = group.getProperties().get("sharedRoster.groupList");
243             // Nothing has changed so do nothing.
244
if (currentValue.equals(originalValue)) {
245                 return;
246             }
247             // Get the users of the group
248
Collection<String JavaDoc> users = new HashSet<String JavaDoc>(group.getMembers());
249             users.addAll(group.getAdmins());
250             // Get the users whose roster will be affected
251
Collection<String JavaDoc> affectedUsers = getAffectedUsers(group,
252                     group.getProperties().get("sharedRoster.showInRoster"), originalValue);
253             // Remove the group members from the affected rosters
254
for (String JavaDoc deletedUser : users) {
255                 groupUserDeleted(group, affectedUsers, deletedUser);
256             }
257
258             // Simulate that the group users has been added to the group. This will cause to push
259
// roster items to the "affected" users for the group users
260
//Collection<Group> visibleGroups = getVisibleGroups(group);
261
for (String JavaDoc user : users) {
262                 groupUserAdded(group, user);
263                 /*for (Group visibleGroup : visibleGroups) {
264                     addSharedGroupToRoster(visibleGroup, user);
265                 }*/

266             }
267         }
268         else if ("sharedRoster.displayName".equals(keyChanged)) {
269             String JavaDoc currentValue = group.getProperties().get("sharedRoster.displayName");
270             // Nothing has changed so do nothing.
271
if (currentValue.equals(originalValue)) {
272                 return;
273             }
274             // Do nothing if the group is not being shown in users' rosters
275
if (!isSharedGroup(group)) {
276                 return;
277             }
278             // Get all the affected users
279
Collection<String JavaDoc> users = getAffectedUsers(group);
280             // Iterate on all the affected users and update their rosters
281
for (String JavaDoc updatedUser : users) {
282                 // Get the roster to update.
283
Roster roster = (Roster) CacheManager.getCache("username2roster").get(updatedUser);
284                 if (roster != null) {
285                     // Update the roster with the new group display name
286
roster.shareGroupRenamed(users);
287                 }
288             }
289         }
290     }
291
292     /**
293      * Returns true if the specified Group may be included in a user roster. The decision is made
294      * based on the group properties that are configurable through the Admin Console.
295      *
296      * @param group the group to check if it may be considered a shared group.
297      * @return true if the specified Group may be included in a user roster.
298      */

299     public boolean isSharedGroup(Group group) {
300         String JavaDoc showInRoster = group.getProperties().get("sharedRoster.showInRoster");
301         if ("onlyGroup".equals(showInRoster) || "everybody".equals(showInRoster)) {
302             return true;
303         }
304         return false;
305     }
306
307     public void memberAdded(Group group, Map params) {
308         String JavaDoc addedUser = (String JavaDoc) params.get("member");
309         // Do nothing if the user was an admin that became a member
310
if (group.getAdmins().contains(addedUser)) {
311             return;
312         }
313         if (!isSharedGroup(group)) {
314             for (Group visibleGroup : getVisibleGroups(group)) {
315                 addSharedGroupToRoster(visibleGroup, addedUser);
316             }
317         }
318         else {
319             groupUserAdded(group, addedUser);
320         }
321     }
322
323     public void memberRemoved(Group group, Map params) {
324         String JavaDoc deletedUser = (String JavaDoc) params.get("member");
325         // Do nothing if the user is still an admin
326
if (group.getAdmins().contains(deletedUser)) {
327             return;
328         }
329         if (!isSharedGroup(group)) {
330             for (Group visibleGroup : getVisibleGroups(group)) {
331                 removeSharedGroupFromRoster(visibleGroup, deletedUser);
332             }
333         }
334         else {
335             groupUserDeleted(group, deletedUser);
336         }
337     }
338
339     public void adminAdded(Group group, Map params) {
340         String JavaDoc addedUser = (String JavaDoc) params.get("admin");
341         // Do nothing if the user was a member that became an admin
342
if (group.getMembers().contains(addedUser)) {
343             return;
344         }
345         if (!isSharedGroup(group)) {
346             for (Group visibleGroup : getVisibleGroups(group)) {
347                 addSharedGroupToRoster(visibleGroup, addedUser);
348             }
349         }
350         else {
351             groupUserAdded(group, addedUser);
352         }
353     }
354
355     public void adminRemoved(Group group, Map params) {
356         String JavaDoc deletedUser = (String JavaDoc) params.get("admin");
357         // Do nothing if the user is still a member
358
if (group.getMembers().contains(deletedUser)) {
359             return;
360         }
361         // Do nothing if the group is not being shown in group members' rosters
362
if (!isSharedGroup(group)) {
363             for (Group visibleGroup : getVisibleGroups(group)) {
364                 removeSharedGroupFromRoster(visibleGroup, deletedUser);
365             }
366         }
367         else {
368             groupUserDeleted(group, deletedUser);
369         }
370     }
371
372     /**
373      * Notification that a Group user has been added. Update the group users' roster accordingly.
374      *
375      * @param group the group where the user was added.
376      * @param addedUser the username of the user that has been added to the group.
377      */

378     private void groupUserAdded(Group group, String JavaDoc addedUser) {
379         // Get all the affected users
380
Collection<String JavaDoc> users = getAffectedUsers(group);
381         // Get the roster of the added user.
382
Roster addedUserRoster = (Roster) CacheManager.getCache("username2roster").get(addedUser);
383
384         // Iterate on all the affected users and update their rosters
385
for (String JavaDoc userToUpdate : users) {
386             if (!addedUser.equals(userToUpdate)) {
387                 // Get the roster to update
388
Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToUpdate);
389                 // Only update rosters in memory
390
if (roster != null) {
391                     roster.addSharedUser(group, addedUser);
392                 }
393                 // Update the roster of the newly added group user.
394
if (addedUserRoster != null) {
395                     try {
396                         User user = UserManager.getInstance().getUser(userToUpdate);
397                         Collection<Group> groups = GroupManager.getInstance().getGroups(user);
398                         addedUserRoster.addSharedUser(userToUpdate, groups, group);
399                     }
400                     catch (UserNotFoundException e) {}
401                 }
402             }
403         }
404     }
405
406     /**
407      * Notification that a Group user has been deleted. Update the group users' roster accordingly.
408      *
409      * @param group the group from where the user was deleted.
410      * @param deletedUser the username of the user that has been deleted from the group.
411      */

412     private void groupUserDeleted(Group group, String JavaDoc deletedUser) {
413         groupUserDeleted(group, getAffectedUsers(group), deletedUser);
414     }
415
416     /**
417      * Notification that a Group user has been deleted. Update the group users' roster accordingly.
418      *
419      * @param group the group from where the user was deleted.
420      * @param users the users to update their rosters
421      * @param deletedUser the username of the user that has been deleted from the group.
422      */

423     private void groupUserDeleted(Group group, Collection<String JavaDoc> users, String JavaDoc deletedUser) {
424         // Get the roster of the deleted user.
425
Roster deletedUserRoster = (Roster) CacheManager.getCache("username2roster").get(deletedUser);
426
427         // Iterate on all the affected users and update their rosters
428
for (String JavaDoc userToUpdate : users) {
429             // Get the roster to update
430
Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToUpdate);
431             // Only update rosters in memory
432
if (roster != null) {
433                 roster.deleteSharedUser(group, deletedUser);
434             }
435             // Update the roster of the newly deleted group user.
436
if (deletedUserRoster != null) {
437                 try {
438                     User user = UserManager.getInstance().getUser(userToUpdate);
439                     Collection<Group> groups = GroupManager.getInstance().getGroups(user);
440                     deletedUserRoster.deleteSharedUser(userToUpdate, groups, group);
441                 }
442                 catch (UserNotFoundException e) {}
443             }
444         }
445     }
446
447     private Collection<Group> getVisibleGroups(Group groupToCheck) {
448         Collection<Group> answer = new HashSet<Group>();
449         Collection<Group> groups = GroupManager.getInstance().getGroups();
450         for (Group group : groups) {
451             if (groupToCheck == group) {
452                 continue;
453             }
454             String JavaDoc showInRoster = group.getProperties().get("sharedRoster.showInRoster");
455             if ("onlyGroup".equals(showInRoster)) {
456                 // Check if the user belongs to a group that may see this group
457
Collection<Group> groupList = parseGroups(group.getProperties().get(
458                         "sharedRoster.groupList"));
459                 if (groupList.contains(groupToCheck)) {
460                     answer.add(group);
461                 }
462             }
463             else if ("everybody".equals(showInRoster)) {
464                 answer.add(group);
465             }
466         }
467         return answer;
468     }
469
470     /**
471      * Returns true if a given group is visible to any user. That means, if any user can
472      * see the group in his roster.
473      *
474      * @param group the group to check if the user can see.
475      * @return true if a given group is visible by any user.
476      */

477     boolean isGroupPublic(Group group) {
478         String JavaDoc showInRoster = group.getProperties().get("sharedRoster.showInRoster");
479         if ("everybody".equals(showInRoster)) {
480             return true;
481         }
482         return false;
483     }
484
485     /**
486      * Returns true if a given group is visible to a given user. That means, if the user can
487      * see the group in his roster.
488      *
489      * @param group the group to check if the user can see.
490      * @param username the user to check if he may see the group.
491      * @return true if a given group is visible to a given user.
492      */

493     boolean isGroupVisible(Group group, String JavaDoc username) {
494         String JavaDoc showInRoster = group.getProperties().get("sharedRoster.showInRoster");
495         if ("everybody".equals(showInRoster)) {
496             return true;
497         }
498         else if ("onlyGroup".equals(showInRoster)) {
499             if (group.isUser(username)) {
500                  return true;
501             }
502             // Check if the user belongs to a group that may see this group
503
Collection<Group> groupList = parseGroups(group.getProperties().get(
504                     "sharedRoster.groupList"));
505             for (Group groupInList : groupList) {
506                 if (groupInList.isUser(username)) {
507                     return true;
508                 }
509             }
510         }
511         return false;
512     }
513
514     /**
515      * Adds the group users of the given shared group to the roster of the specified user.
516      *
517      * @param group the shared group to add to the roster of a user.
518      * @param username the name of the user to add a shared group to his roster.
519      */

520     private void addSharedGroupToRoster(Group group, String JavaDoc username) {
521         // Get the group users to add to the user's roster
522
Collection<String JavaDoc> users = new HashSet<String JavaDoc>(group.getMembers());
523         users.addAll(group.getAdmins());
524
525         // Get the roster of the user from which we need to add the shared group users
526
Roster userRoster = (Roster) CacheManager.getCache("username2roster").get(username);
527
528         // Iterate on all the group users and update their rosters
529
for (String JavaDoc userToAdd : users) {
530             // Get the roster to update
531
Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToAdd);
532             // Only update rosters in memory
533
if (roster != null) {
534                 roster.addSharedUser(group, username);
535             }
536             // Update the roster of the user
537
if (userRoster != null) {
538                 try {
539                     User user = UserManager.getInstance().getUser(userToAdd);
540                     Collection<Group> groups = GroupManager.getInstance().getGroups(user);
541                     userRoster.addSharedUser(userToAdd, groups, group);
542                 }
543                 catch (UserNotFoundException e) {}
544             }
545         }
546     }
547
548     private void removeSharedGroupFromRoster(Group group, String JavaDoc username) {
549         // Get the group users to remove from the user's roster
550
Collection<String JavaDoc> users = new HashSet<String JavaDoc>(group.getMembers());
551         users.addAll(group.getAdmins());
552
553         // Get the roster of the user from which we need to remove the shared group users
554
Roster userRoster = (Roster) CacheManager.getCache("username2roster").get(username);
555
556         // Iterate on all the group users and update their rosters
557
for (String JavaDoc userToRemove : users) {
558             // Get the roster to update
559
Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToRemove);
560             // Only update rosters in memory
561
if (roster != null) {
562                 roster.deleteSharedUser(group, username);
563             }
564             // Update the roster of the user
565
if (userRoster != null) {
566                 try {
567                     User user = UserManager.getInstance().getUser(userToRemove);
568                     Collection<Group> groups = GroupManager.getInstance().getGroups(user);
569                     userRoster.deleteSharedUser(userToRemove, groups, group);
570                 }
571                 catch (UserNotFoundException e) {}
572             }
573         }
574     }
575
576         /**
577      * Returns all the users that are related to a shared group. This is the logic that we are
578      * using: 1) If the group visiblity is configured as "Everybody" then all users in the system or
579      * all logged users in the system will be returned (configurable thorugh the "filterOffline"
580      * flag), 2) if the group visiblity is configured as "onlyGroup" then all the group users will
581      * be included in the answer and 3) if the group visiblity is configured as "onlyGroup" and
582      * the group allows other groups to include the group in the groups users' roster then all
583      * the users of the allowed groups will be included in the answer.
584      */

585     private Collection<String JavaDoc> getAffectedUsers(Group group) {
586         return getAffectedUsers(group, group.getProperties().get("sharedRoster.showInRoster"),
587                 group.getProperties().get("sharedRoster.groupList"));
588     }
589
590     /**
591      * This method is similar to {@link #getAffectedUsers(Group)} except that it receives
592      * some group properties. The group properties are passed as parameters since the called of this
593      * method may want to obtain the related users of the group based in some properties values.
594      *
595      * This is useful when the group is being edited and some properties has changed and we need to
596      * obtain the related users of the group based on the previous group state.
597      */

598     private Collection<String JavaDoc> getAffectedUsers(Group group, String JavaDoc showInRoster, String JavaDoc groupNames) {
599         // Answer an empty collection if the group is not being shown in users' rosters
600
if (!"onlyGroup".equals(showInRoster) && !"everybody".equals(showInRoster)) {
601             return new ArrayList<String JavaDoc>();
602         }
603         // Add the users of the group
604
Collection<String JavaDoc> users = new HashSet<String JavaDoc>(group.getMembers());
605         users.addAll(group.getAdmins());
606         // Check if anyone can see this shared group
607
if ("everybody".equals(showInRoster)) {
608             // Add all users in the system
609
for (User user : UserManager.getInstance().getUsers()) {
610                 users.add(user.getUsername());
611             }
612             // Add all logged users. We don't need to add all users in the system since only the
613
// logged ones will be affected.
614
//users.addAll(SessionManager.getInstance().getSessionUsers());
615
}
616         else {
617             // Add the users that may see the group
618
Collection<Group> groupList = parseGroups(groupNames);
619             for (Group groupInList : groupList) {
620                 users.addAll(groupInList.getMembers());
621                 users.addAll(groupInList.getAdmins());
622             }
623         }
624         return users;
625     }
626     
627     Collection<String JavaDoc> getSharedUsersForRoster(Group group, Roster roster) {
628         String JavaDoc showInRoster = group.getProperties().get("sharedRoster.showInRoster");
629         String JavaDoc groupNames = group.getProperties().get("sharedRoster.groupList");
630         
631         // Answer an empty collection if the group is not being shown in users' rosters
632
if (!"onlyGroup".equals(showInRoster) && !"everybody".equals(showInRoster)) {
633             return new ArrayList<String JavaDoc>();
634         }
635         
636         // Add the users of the group
637
Collection<String JavaDoc> users = new HashSet<String JavaDoc>(group.getMembers());
638         users.addAll(group.getAdmins());
639         
640         // Check if anyone can see this shared group
641
if ("everybody".equals(showInRoster)) {
642             // If the user of the roster belongs to the public group then we should return all users
643
// in the system since they all need to be in the roster with subscription "from"
644
if (group.isUser(roster.getUsername())) {
645                 // Add all users in the system
646
for (User user : UserManager.getInstance().getUsers()) {
647                     users.add(user.getUsername());
648                 }
649             }
650         }
651         else {
652             // Add the users that may see the group
653
Collection<Group> groupList = parseGroups(groupNames);
654             for (Group groupInList : groupList) {
655                 users.addAll(groupInList.getMembers());
656                 users.addAll(groupInList.getAdmins());
657             }
658         }
659         return users;
660     }
661
662     /**
663      * Returns true if a group in the first collection may mutually see a group of the
664      * second collection. More precisely, return true if both collections contain a public
665      * group (i.e. anybody can see the group) or if both collection have a group that may see
666      * each other and the users are members of those groups or if one group is public and the
667      * other group allowed the public group to see it.
668      *
669      * @param user the name of the user associated to the first collection of groups.
670      * @param groups a collection of groups to check against the other collection of groups.
671      * @param otherUser the name of the user associated to the second collection of groups.
672      * @param otherGroups the other collection of groups to check against the first collection.
673      * @return true if a group in the first collection may mutually see a group of the
674      * second collection.
675      */

676     boolean hasMutualVisibility(String JavaDoc user, Collection<Group> groups, String JavaDoc otherUser,
677             Collection<Group> otherGroups) {
678         for (Group group : groups) {
679             for (Group otherGroup : otherGroups) {
680                 // Skip this groups if the users are not group users of the groups
681
if (!group.isUser(user) || !otherGroup.isUser(otherUser)) {
682                     continue;
683                 }
684                 if (group == otherGroup) {
685                      return true;
686                 }
687                 String JavaDoc showInRoster = group.getProperties().get("sharedRoster.showInRoster");
688                 String JavaDoc otherShowInRoster = otherGroup.getProperties().get("sharedRoster.showInRoster");
689                 // Return true if both groups are public groups (i.e. anybody can see them)
690
if ("everybody".equals(showInRoster) && "everybody".equals(otherShowInRoster)) {
691                     return true;
692                 }
693                 else if ("onlyGroup".equals(showInRoster) && "onlyGroup".equals(otherShowInRoster)) {
694                     String JavaDoc groupNames = group.getProperties().get("sharedRoster.groupList");
695                     String JavaDoc otherGroupNames = otherGroup.getProperties().get("sharedRoster.groupList");
696                     // Return true if each group may see the other group
697
if (groupNames != null && otherGroupNames != null) {
698                         if (groupNames.contains(otherGroup.getName()) &&
699                                 otherGroupNames.contains(group.getName())) {
700                             return true;
701                         }
702                     }
703                 }
704                 else if ("everybody".equals(showInRoster) && "onlyGroup".equals(otherShowInRoster)) {
705                     // Return true if one group is public and the other group allowed the public
706
// group to see him
707
String JavaDoc otherGroupNames = otherGroup.getProperties().get("sharedRoster.groupList");
708                     if (otherGroupNames != null && otherGroupNames.contains(group.getName())) {
709                             return true;
710                     }
711                 }
712                 else if ("onlyGroup".equals(showInRoster) && "everybody".equals(otherShowInRoster)) {
713                     // Return true if one group is public and the other group allowed the public
714
// group to see him
715
String JavaDoc groupNames = group.getProperties().get("sharedRoster.groupList");
716                     // Return true if each group may see the other group
717
if (groupNames != null && groupNames.contains(otherGroup.getName())) {
718                             return true;
719                     }
720                 }
721             }
722         }
723         return false;
724     }
725 }
Popular Tags