/* 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.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;

import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.jmx.SigarProcess;
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.";
    }

    protected boolean validateArgs(String[] args) {
        return args.length <= 1;
    }

    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);
            SigarProcess proc = new SigarProcess();
            server.registerMBean(proc, new ObjectName(proc.getObjectName()));
            isRegistered = true;
        } catch (Exception e) {
            throw new SigarException(e.getMessage());
        }
    }

    public void output(String[] args) throws SigarException {
        MBeanServer server = getMBeanServer();
        register(server);
        boolean hasQuery = args.length != 0;
        try {
            String query;
            if (hasQuery) {
                query = args[0];
            }
            else {
                query = "sigar:*";
            }
            Set beans =
                server.queryNames(new ObjectName(query), null);
            println(beans.size() + " MBeans are registered...");
            for (Iterator it=beans.iterator(); it.hasNext();) {
                ObjectName name = (ObjectName)it.next(); 
                if (hasQuery) {
                    MBeanInfo info = server.getMBeanInfo(name);
                    MBeanAttributeInfo[] attrs = info.getAttributes();
                    for (int i=0; i<attrs.length; i++) {
                        String attr = attrs[i].getName();
                        Object val = server.getAttribute(name, attr);
                        println(name + ":" + attr + "=" + val);
                    }                    
                }
                else {
                    println(name.toString());
                }
            }
        } catch (Exception e) {
            throw new SigarException(e.getMessage());
        }
    }

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

@@ -2444,32 +2444,443 @@
        %7D%0A    %7D%0A%0A
+    private void jconsole() %7B%0A        String pid = String.valueOf(this.sigar.getPid());%0A        String%5B%5D argv = %7B %22jconsole%22, pid %7D;%0A        println(%22exec(jconsole, %22 + pid + %22)%22);%0A        try %7B%0A            Process p = Runtime.getRuntime().exec(argv);%0A            p.waitFor();%0A            println(%22jconsole exited%22);%0A        %7D catch (Exception e) %7B%0A            println(e.getMessage());%0A        %7D        %0A    %7D%0A%0A
     public void 
@@ -3029,24 +3029,53 @@
 y = 
-args.length != 0
+false;%0A        boolean launchJconsole = false
 ;%0A  
@@ -3072,35 +3072,100 @@
  false;%0A        
+S
 tr
-y
+ing query = %22sigar:*%22;%0A%0A        for (int i=0; i%3Cargs.length; i++)
  %7B%0A            S
@@ -3170,21 +3170,29 @@
  String 
-query
+arg = args%5Bi%5D
 ;%0A      
@@ -3205,134 +3205,207 @@
 if (
-hasQuery) %7B%0A                query = args%5B0%5D;%0A            %7D%0A            else %7B%0A                query = %22sigar:*%22;%0A            %7D
+arg.equals(%22-jconsole%22)) %7B%0A                launchJconsole = true;%0A            %7D%0A            else %7B%0A                query = arg;%0A                hasQuery = true;%0A            %7D%0A        %7D%0A%0A        try %7B
 %0A   
@@ -4341,32 +4341,96 @@
 e());%0A        %7D%0A
+        if (launchJconsole) %7B%0A            jconsole();%0A        %7D%0A
     %7D%0A%0A    publi

add mx -jconsole option