Example #1
0
  /**
   * Removes the entire roster of a given user. This is necessary when a user account is being
   * deleted from the server.
   *
   * @param user the user.
   */
  public void deleteRoster(JID user) {
    try {
      String username = user.getNode();
      // Get the roster of the deleted user
      Roster roster = (Roster) CacheManager.getCache("username2roster").get(username);
      if (roster == null) {
        // Not in cache so load a new one:
        roster = new Roster(username);
      }
      // Remove each roster item from the user's roster
      for (RosterItem item : roster.getRosterItems()) {
        try {
          roster.deleteRosterItem(item.getJid(), false);
        } catch (SharedGroupException e) {
          // Do nothing. We shouldn't have this exception since we disabled the checkings
        }
      }
      // Remove the cached roster from memory
      CacheManager.getCache("username2roster").remove(username);

      // Get the rosters that have a reference to the deleted user
      RosterItemProvider rosteItemProvider = RosterItemProvider.getInstance();
      Iterator<String> usernames = rosteItemProvider.getUsernames(user.toBareJID());
      while (usernames.hasNext()) {
        username = usernames.next();
        // Get the roster that has a reference to the deleted user
        roster = (Roster) CacheManager.getCache("username2roster").get(username);
        if (roster == null) {
          // Not in cache so load a new one:
          roster = new Roster(username);
        }
        // Remove the deleted user reference from this roster
        try {
          roster.deleteRosterItem(user, false);
        } catch (SharedGroupException e) {
          // Do nothing. We shouldn't have this exception since we disabled the checkings
        }
      }
    } catch (UnsupportedOperationException e) {
      // Do nothing
    }
  }