1 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 41 public class RosterManager extends BasicModule implements GroupEventListener { 42 43 private Cache rosterCache = null; 44 45 public RosterManager() { 46 super("Roster Manager"); 47 GroupEventDispatcher.addListener(this); 49 } 50 51 59 public Roster getRoster(String 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 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 84 public void deleteRoster(JID user) { 85 try { 86 String username = user.getNode(); 87 Roster roster = (Roster)CacheManager.getCache("username2roster").get(username); 89 if (roster == null) { 90 roster = new Roster(username); 92 } 93 for (RosterItem item : roster.getRosterItems()) { 95 try { 96 roster.deleteRosterItem(item.getJid(), false); 97 } 98 catch (SharedGroupException e) { 99 } 101 } 102 CacheManager.getCache("username2roster").remove(username); 104 105 RosterItemProvider rosteItemProvider = RosterItemProvider.getInstance(); 107 Iterator<String > usernames = rosteItemProvider.getUsernames(user.toBareJID()); 108 while (usernames.hasNext()) { 109 username = usernames.next(); 110 roster = (Roster)CacheManager.getCache("username2roster").get(username); 112 if (roster == null) { 113 roster = new Roster(username); 115 } 116 try { 118 roster.deleteRosterItem(user, false); 119 } 120 catch (SharedGroupException e) { 121 } 123 } 124 } 125 catch (UnsupportedOperationException e) { 126 } 128 } 129 130 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 showInRoster = group.getProperties().get("sharedRoster.showInRoster"); 146 if ("onlyGroup".equals(showInRoster)) { 147 if (group.isUser(user.getUsername())) { 148 answer.add(group); 150 } 151 else { 152 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 answer.add(group); 164 } 165 } 166 return answer; 167 } 168 169 177 private Collection<Group> parseGroups(String groupNames) { 178 Collection<Group> answer = new HashSet<Group>(); 179 if (groupNames != null) { 180 StringTokenizer tokenizer = new StringTokenizer(groupNames, ","); 181 while (tokenizer.hasMoreTokens()) { 182 String groupName = tokenizer.nextToken(); 183 try { 184 answer.add(GroupManager.getInstance().getGroup(groupName)); 185 } 186 catch (GroupNotFoundException e) { 187 } 189 } 190 } 191 return answer; 192 } 193 194 public void groupCreated(Group group, Map params) { 195 } 197 198 public void groupDeleting(Group group, Map params) { 199 for (String deletedUser : getAffectedUsers(group)) { 201 groupUserDeleted(group, deletedUser); 202 } 203 } 204 205 public void groupModified(Group group, Map params) { 206 if (!"propertyModified".equals(params.get("type"))) { 208 return; 209 } 210 String keyChanged = (String ) params.get("propertyKey"); 211 String originalValue = (String ) params.get("originalValue"); 212 213 214 if ("sharedRoster.showInRoster".equals(keyChanged)) { 215 String currentValue = group.getProperties().get("sharedRoster.showInRoster"); 216 if (currentValue.equals(originalValue)) { 218 return; 219 } 220 Collection<String > users = new HashSet<String >(group.getMembers()); 222 users.addAll(group.getAdmins()); 223 Collection<String > affectedUsers = getAffectedUsers(group, originalValue, 225 group.getProperties().get("sharedRoster.groupList")); 226 for (String deletedUser : users) { 228 groupUserDeleted(group, affectedUsers, deletedUser); 229 } 230 231 for (String user : users) { 235 groupUserAdded(group, user); 236 239 } 240 } 241 else if ("sharedRoster.groupList".equals(keyChanged)) { 242 String currentValue = group.getProperties().get("sharedRoster.groupList"); 243 if (currentValue.equals(originalValue)) { 245 return; 246 } 247 Collection<String > users = new HashSet<String >(group.getMembers()); 249 users.addAll(group.getAdmins()); 250 Collection<String > affectedUsers = getAffectedUsers(group, 252 group.getProperties().get("sharedRoster.showInRoster"), originalValue); 253 for (String deletedUser : users) { 255 groupUserDeleted(group, affectedUsers, deletedUser); 256 } 257 258 for (String user : users) { 262 groupUserAdded(group, user); 263 266 } 267 } 268 else if ("sharedRoster.displayName".equals(keyChanged)) { 269 String currentValue = group.getProperties().get("sharedRoster.displayName"); 270 if (currentValue.equals(originalValue)) { 272 return; 273 } 274 if (!isSharedGroup(group)) { 276 return; 277 } 278 Collection<String > users = getAffectedUsers(group); 280 for (String updatedUser : users) { 282 Roster roster = (Roster) CacheManager.getCache("username2roster").get(updatedUser); 284 if (roster != null) { 285 roster.shareGroupRenamed(users); 287 } 288 } 289 } 290 } 291 292 299 public boolean isSharedGroup(Group group) { 300 String 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 addedUser = (String ) params.get("member"); 309 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 deletedUser = (String ) params.get("member"); 325 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 addedUser = (String ) params.get("admin"); 341 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 deletedUser = (String ) params.get("admin"); 357 if (group.getMembers().contains(deletedUser)) { 359 return; 360 } 361 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 378 private void groupUserAdded(Group group, String addedUser) { 379 Collection<String > users = getAffectedUsers(group); 381 Roster addedUserRoster = (Roster) CacheManager.getCache("username2roster").get(addedUser); 383 384 for (String userToUpdate : users) { 386 if (!addedUser.equals(userToUpdate)) { 387 Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToUpdate); 389 if (roster != null) { 391 roster.addSharedUser(group, addedUser); 392 } 393 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 412 private void groupUserDeleted(Group group, String deletedUser) { 413 groupUserDeleted(group, getAffectedUsers(group), deletedUser); 414 } 415 416 423 private void groupUserDeleted(Group group, Collection<String > users, String deletedUser) { 424 Roster deletedUserRoster = (Roster) CacheManager.getCache("username2roster").get(deletedUser); 426 427 for (String userToUpdate : users) { 429 Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToUpdate); 431 if (roster != null) { 433 roster.deleteSharedUser(group, deletedUser); 434 } 435 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 showInRoster = group.getProperties().get("sharedRoster.showInRoster"); 455 if ("onlyGroup".equals(showInRoster)) { 456 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 477 boolean isGroupPublic(Group group) { 478 String showInRoster = group.getProperties().get("sharedRoster.showInRoster"); 479 if ("everybody".equals(showInRoster)) { 480 return true; 481 } 482 return false; 483 } 484 485 493 boolean isGroupVisible(Group group, String username) { 494 String 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 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 520 private void addSharedGroupToRoster(Group group, String username) { 521 Collection<String > users = new HashSet<String >(group.getMembers()); 523 users.addAll(group.getAdmins()); 524 525 Roster userRoster = (Roster) CacheManager.getCache("username2roster").get(username); 527 528 for (String userToAdd : users) { 530 Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToAdd); 532 if (roster != null) { 534 roster.addSharedUser(group, username); 535 } 536 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 username) { 549 Collection<String > users = new HashSet<String >(group.getMembers()); 551 users.addAll(group.getAdmins()); 552 553 Roster userRoster = (Roster) CacheManager.getCache("username2roster").get(username); 555 556 for (String userToRemove : users) { 558 Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToRemove); 560 if (roster != null) { 562 roster.deleteSharedUser(group, username); 563 } 564 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 585 private Collection<String > getAffectedUsers(Group group) { 586 return getAffectedUsers(group, group.getProperties().get("sharedRoster.showInRoster"), 587 group.getProperties().get("sharedRoster.groupList")); 588 } 589 590 598 private Collection<String > getAffectedUsers(Group group, String showInRoster, String groupNames) { 599 if (!"onlyGroup".equals(showInRoster) && !"everybody".equals(showInRoster)) { 601 return new ArrayList<String >(); 602 } 603 Collection<String > users = new HashSet<String >(group.getMembers()); 605 users.addAll(group.getAdmins()); 606 if ("everybody".equals(showInRoster)) { 608 for (User user : UserManager.getInstance().getUsers()) { 610 users.add(user.getUsername()); 611 } 612 } 616 else { 617 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 > getSharedUsersForRoster(Group group, Roster roster) { 628 String showInRoster = group.getProperties().get("sharedRoster.showInRoster"); 629 String groupNames = group.getProperties().get("sharedRoster.groupList"); 630 631 if (!"onlyGroup".equals(showInRoster) && !"everybody".equals(showInRoster)) { 633 return new ArrayList<String >(); 634 } 635 636 Collection<String > users = new HashSet<String >(group.getMembers()); 638 users.addAll(group.getAdmins()); 639 640 if ("everybody".equals(showInRoster)) { 642 if (group.isUser(roster.getUsername())) { 645 for (User user : UserManager.getInstance().getUsers()) { 647 users.add(user.getUsername()); 648 } 649 } 650 } 651 else { 652 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 676 boolean hasMutualVisibility(String user, Collection<Group> groups, String otherUser, 677 Collection<Group> otherGroups) { 678 for (Group group : groups) { 679 for (Group otherGroup : otherGroups) { 680 if (!group.isUser(user) || !otherGroup.isUser(otherUser)) { 682 continue; 683 } 684 if (group == otherGroup) { 685 return true; 686 } 687 String showInRoster = group.getProperties().get("sharedRoster.showInRoster"); 688 String otherShowInRoster = otherGroup.getProperties().get("sharedRoster.showInRoster"); 689 if ("everybody".equals(showInRoster) && "everybody".equals(otherShowInRoster)) { 691 return true; 692 } 693 else if ("onlyGroup".equals(showInRoster) && "onlyGroup".equals(otherShowInRoster)) { 694 String groupNames = group.getProperties().get("sharedRoster.groupList"); 695 String otherGroupNames = otherGroup.getProperties().get("sharedRoster.groupList"); 696 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 String 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 String groupNames = group.getProperties().get("sharedRoster.groupList"); 716 if (groupNames != null && groupNames.contains(otherGroup.getName())) { 718 return true; 719 } 720 } 721 } 722 } 723 return false; 724 } 725 } | Popular Tags |