import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Formatter;
import javax.management.*;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class Client {

    public static void main(String[] args) {
        try {
// Create an RMI connector client and
// connect it to the RMI connector server



            JMXServiceURL url = new JMXServiceURL(
                "service:jmx:rmi:///jndi/rmi://localhost:8999/jmxrmi");
            JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
            MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

            waitForEnterPressed();

// Get Total MBean Count and print
//
            int mbean_count = mbsc.getMBeanCount();
            String count = "TOTAL MBEAN COUNT: " + mbean_count;
            System.out.println(count);

// Get Domains and print
            echo("\nDOMAINS:");
            String domains[] = mbsc.getDomains();
            for (int i = 0; i < domains.length; i++) {
                echo("\tDomain[" + i + "] = " + domains[i]);;

            }

            System.out.println("ALL THE EXPOSED CLASSES");
            Set set = mbsc.queryNames(new ObjectName("org.apache.qpid:*"),null);
//Set set = mbsc.queryNames(null,"org.apache.org");
            System.out.println(set.size());
            Iterator it = set.iterator();

do
{
    echo("=======================================================================");
    ObjectName temp_object = (ObjectName) it.next();
// echo(temp_object.getCanonicalKeyPropertyListString());
    MBeanInfo bean_info = mbsc.getMBeanInfo(temp_object);
    MBeanAttributeInfo [] attr_info = bean_info.getAttributes();
    if(attr_info == null)
    {
        echo(temp_object.toString());
        echo("=======================================================================");

    }
    else
    {
        echo(temp_object.toString());
        echo("=======================================================================");
        for (MBeanAttributeInfo attr : attr_info)
        {
            Object toWrite = null;

            try {
                toWrite = mbsc.getAttribute(temp_object,attr.getName());
                if(toWrite == null)
                    echo (attr.getName() + " " + attr.getType() );
                else
                    echo(attr.getName() + " " + toWrite.toString() + " " + attr.getType());
            } catch (Exception x) {
                x.printStackTrace();
            }

        }

    }
}while(it.hasNext());



echo("\nClose the connection to the server");
jmxc.close();
echo("\nBye! Bye!");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void echo(String msg) {
        System.out.println(msg);
    }

    private static void sleep(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void waitForEnterPressed() {
        try {
            echo("\nPress to continue...");
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
