From 913f1ca5a6bd651955af697db4da47a363f9b550 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Fri, 3 Oct 2008 01:27:01 +0000 Subject: [PATCH] mx cmd for testing MBeans --- .../java/src/org/hyperic/sigar/cmd/Mx.java | 81 +++++++++++++++++++ .../java/src/org/hyperic/sigar/cmd/Shell.java | 4 + 2 files changed, 85 insertions(+) create mode 100644 bindings/java/src/org/hyperic/sigar/cmd/Mx.java diff --git a/bindings/java/src/org/hyperic/sigar/cmd/Mx.java b/bindings/java/src/org/hyperic/sigar/cmd/Mx.java new file mode 100644 index 00000000..21b9c80a --- /dev/null +++ b/bindings/java/src/org/hyperic/sigar/cmd/Mx.java @@ -0,0 +1,81 @@ +/* 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.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."; + } + + private static MBeanServer getMBeanServer() { + List servers = + MBeanServerFactory.findMBeanServer(null); + + 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"); + } catch (Exception e) { + throw new SigarException(e.getMessage()); + } + } + + public static void main(String[] args) throws Exception { + new Mx().processCommand(args); + } +} diff --git a/bindings/java/src/org/hyperic/sigar/cmd/Shell.java b/bindings/java/src/org/hyperic/sigar/cmd/Shell.java index b781bbaf..a4bc5352 100644 --- a/bindings/java/src/org/hyperic/sigar/cmd/Shell.java +++ b/bindings/java/src/org/hyperic/sigar/cmd/Shell.java @@ -113,6 +113,10 @@ public class Shell extends ShellBase { //requires junit.jar registerCommandHandler("test", new SigarTestRunner(this)); } catch (NoClassDefFoundError e) { } + try { + //requires jre 1.5+ or mx4j + registerCommandHandler("mx", new Mx(this)); + } catch (NoClassDefFoundError e) { } } public void processCommand(ShellCommandHandler handler, String args[])