public List<Presence> setMembersOnly(final boolean membersOnly, @Nonnull final Affiliation actorAffiliation, @Nullable final JID actorJid) throws ForbiddenException, NotAllowedException
    {
        if (actorAffiliation != Affiliation.owner) {
            throw new ForbiddenException();
        }

        final List<Presence> presences = new CopyOnWriteArrayList<>();
        final String actorNickname = findNickname(actorJid);

        if (membersOnly && !this.membersOnly) {
            Log.debug("Room '{}' was not members-only, but now is. Kick occupants that aren't a member.", getJID());
            final Set<JID> userAddressesToKick = getOccupants().stream()
                .filter(mucOccupant -> mucOccupant.getAffiliation().equals(Affiliation.none) || mucOccupant.getAffiliation().equals(Affiliation.outcast))
                .map(MUCOccupant::getUserAddress)
                .collect(Collectors.toSet());
            for (final JID userAddressToKick : userAddressesToKick) {
                presences.addAll(kickOccupant(userAddressToKick, actorAffiliation, null, actorJid, actorNickname, LocaleUtils.getLocalizedString("muc.roomIsNowMembersOnly"), 322));
            }
        }
        this.membersOnly = membersOnly;
        return presences;
    }
Sets if the room requires an invitation to enter. That is if the room is members-only.

@param membersOnly if true then the room is members-only.
@return the list of updated presences of all the occupants that aren't members of the room if
        the room is now members-only.
xmppserver/src/main/java/org/jivesoftware/openfire/muc/MUCRoom.java