Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;

import java.util.List;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

public class Main {
    private static MBeanServerConnection connection;
    private static ObjectName defaultAuthenticator;

    /**
     * This one is tricky, You first obtain a String cursor of the Iterator of
     * Users, then you check if It have current, while true we invoke another
     * function called "getCurrentName" which returns the name of the user, then
     * I call advance function for the cursor to move forward, and invoke
     * haveCurrent again and assign it to the same boolean I entered the while
     * with (In order to get out of it!)
     *
     */
    public static List<String> getListOfUsers() throws RuntimeException {
        try {
            List<String> allUsers = new ArrayList<String>();

            String cursor = (String) connection.invoke(defaultAuthenticator, "listUsers",
                    new Object[] { "*", Integer.valueOf(9999) },
                    new String[] { "java.lang.String", "java.lang.Integer" });

            boolean haveCurrent = ((Boolean) connection.invoke(defaultAuthenticator, "haveCurrent",
                    new Object[] { cursor }, new String[] { "java.lang.String" })).booleanValue();

            while (haveCurrent) {
                String currentName = (String) connection.invoke(defaultAuthenticator, "getCurrentName",
                        new Object[] { cursor }, new String[] { "java.lang.String" });

                allUsers.add(currentName);
                connection.invoke(defaultAuthenticator, "advance", new Object[] { cursor },
                        new String[] { "java.lang.String" });

                haveCurrent = ((Boolean) connection.invoke(defaultAuthenticator, "haveCurrent",
                        new Object[] { cursor }, new String[] { "java.lang.String" })).booleanValue();
            }

            return allUsers;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}