GetNumberActiveUsers.java 3.01 KB
Newer Older
Gaston Dombiak's avatar
Gaston Dombiak committed
1 2 3 4
/**
 * $Revision: $
 * $Date: $
 *
5
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
Gaston Dombiak's avatar
Gaston Dombiak committed
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Gaston Dombiak's avatar
Gaston Dombiak committed
18 19
 */

20
package org.jivesoftware.openfire.commands.admin;
Gaston Dombiak's avatar
Gaston Dombiak committed
21 22

import org.dom4j.Element;
23 24 25 26
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.commands.AdHocCommand;
import org.jivesoftware.openfire.commands.SessionData;
import org.jivesoftware.openfire.session.ClientSession;
Gaston Dombiak's avatar
Gaston Dombiak committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Command that allows to retrieve the number of online users who are active at any one moment.
 * Active users are those users that have sent an available presence.
 *
 * @author Gaston Dombiak
 */
public class GetNumberActiveUsers extends AdHocCommand {

43 44
    @Override
	protected void addStageInformation(SessionData data, Element command) {
Gaston Dombiak's avatar
Gaston Dombiak committed
45 46 47
        //Do nothing since there are no stages
    }

48 49
    @Override
	public void execute(SessionData data, Element command) {
Gaston Dombiak's avatar
Gaston Dombiak committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        DataForm form = new DataForm(DataForm.Type.result);

        FormField field = form.addField();
        field.setType(FormField.Type.hidden);
        field.setVariable("FORM_TYPE");
        field.addValue("http://jabber.org/protocol/admin");

        field = form.addField();
        field.setLabel(getLabel());
        field.setVariable("activeusersnum");
        // Make sure that we are only counting based on bareJIDs and not fullJIDs
        Collection<ClientSession> sessions = SessionManager.getInstance().getSessions();
        Set<String> users = new HashSet<String>(sessions.size());
        for (ClientSession session : sessions) {
            if (session.getPresence().isAvailable()) {
                users.add(session.getAddress().toBareJID());
            }
        }
        field.addValue(users.size());

        command.add(form.getElement());
    }

73 74
    @Override
	protected List<Action> getActions(SessionData data) {
Gaston Dombiak's avatar
Gaston Dombiak committed
75 76 77 78
        //Do nothing since there are no stages
        return null;
    }

79 80
    @Override
	public String getCode() {
Gaston Dombiak's avatar
Gaston Dombiak committed
81 82 83
        return "http://jabber.org/protocol/admin#get-active-users-num";
    }

84 85
    @Override
	public String getDefaultLabel() {
Gaston Dombiak's avatar
Gaston Dombiak committed
86 87 88 89
        // TODO Use i18n
        return "Number of Active Users";
    }

90 91
    @Override
	protected Action getExecuteAction(SessionData data) {
Gaston Dombiak's avatar
Gaston Dombiak committed
92 93 94 95
        //Do nothing since there are no stages
        return null;
    }

96 97
    @Override
	public int getMaxStages(SessionData data) {
Gaston Dombiak's avatar
Gaston Dombiak committed
98 99 100
        return 0;
    }
}