/* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
 * This file is part of SIGAR.
 * 
 * SIGAR is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as
 * published by the Free Software Foundation. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

package org.hyperic.sigar.cmd;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.jmx.SigarRegistry;

public class Mx extends SigarCommandBase {

    private boolean isRegistered;

    public Mx(Shell shell) {
        super(shell);
    }

    public Mx() {
        super();
    }
    //java -Dcom.sun.management.jmxremote -jar sigar.jar
    public String getUsageShort() {
        return "Register MBeans for use via jconsole, etc.";
    }

    public static MBeanServer getMBeanServer()
        throws SigarException {
        List servers = 
            MBeanServerFactory.findMBeanServer(null);
    
        if (servers.size() == 0) {
            throw new SigarException("No MBeanServers available");
        }
        else {
            return (MBeanServer)servers.get(0);
        }
    }

    private void register(MBeanServer server) throws SigarException {
        if (isRegistered) {
            return;
        }
        SigarRegistry registry = new SigarRegistry();
        try {
            server.registerMBean(registry, null);
            isRegistered = true;
        } catch (Exception e) {
            throw new SigarException(e.getMessage());
        }
    }

    public void output(String[] args) throws SigarException {
        MBeanServer server = getMBeanServer();
        register(server);
        try {
            Set beans =
                server.queryNames(new ObjectName("sigar:*"), null);
            println(beans.size() + " MBeans are registered...");
            for (Iterator it=beans.iterator(); it.hasNext();) {
                println(it.next().toString());
            }
        } catch (Exception e) {
            throw new SigarException(e.getMessage());
        }
    }

    public static void main(String[] args) throws Exception {
        new Mx().processCommand(args);
    }
}

@@ -848,24 +848,105 @@
 til.Set;%0D%0A%0D%0A
+import javax.management.MBeanAttributeInfo;%0D%0Aimport javax.management.MBeanInfo;%0D%0A
 import javax
@@ -1491,32 +1491,128 @@
 tc.%22;%0D%0A    %7D%0D%0A%0D%0A
+    protected boolean validateArgs(String%5B%5D args) %7B%0D%0A        return args.length %3C= 1;%0D%0A    %7D%0D%0A%0D%0A
     public stati
@@ -2495,37 +2495,259 @@
 rver);%0D%0A        
-try %7B
+boolean hasQuery = args.length != 0;%0D%0A        try %7B%0D%0A            String query;%0D%0A            if (hasQuery) %7B%0D%0A                query = args%5B0%5D;%0D%0A            %7D%0D%0A            else %7B%0D%0A                query = %22sigar:*%22;%0D%0A            %7D
 %0D%0A            Se
@@ -2806,25 +2806,21 @@
 ectName(
-%22sigar:*%22
+query
 ), null)
@@ -2973,38 +2973,624 @@
     
-println(it.next().toString());
+ObjectName name = (ObjectName)it.next(); %0D%0A                if (hasQuery) %7B%0D%0A                    MBeanInfo info = server.getMBeanInfo(name);%0D%0A                    MBeanAttributeInfo%5B%5D attrs = info.getAttributes();%0D%0A                    for (int i=0; i%3Cattrs.length; i++) %7B%0D%0A                        String attr = attrs%5Bi%5D.getName();%0D%0A                        Object val = server.getAttribute(name, attr);%0D%0A                        println(name + %22:%22 + attr + %22=%22 + val);%0D%0A                    %7D                    %0D%0A                %7D%0D%0A                else %7B%0D%0A                    println(name.toString());%0D%0A                %7D
 %0D%0A  

add query support