This commit is contained in:
Doug MacEachern 2009-02-09 14:11:16 -08:00
parent d573932902
commit e1f64cfe2d
2 changed files with 327 additions and 327 deletions

View File

@ -1,120 +1,120 @@
/* Copyright (C) [2004, 2005, 2006], Hyperic, Inc. /* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR. * This file is part of SIGAR.
* *
* SIGAR is free software; you can redistribute it and/or modify * SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as * it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed * published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more * PARTICULAR PURPOSE. See the GNU General Public License for more
* details. * details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA. * USA.
*/ */
package org.hyperic.sigar.cmd; package org.hyperic.sigar.cmd;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.management.MBeanAttributeInfo; import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo; import javax.management.MBeanInfo;
import javax.management.MBeanServer; import javax.management.MBeanServer;
import javax.management.MBeanServerFactory; import javax.management.MBeanServerFactory;
import javax.management.ObjectName; import javax.management.ObjectName;
import org.hyperic.sigar.SigarException; import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.jmx.SigarProcess; import org.hyperic.sigar.jmx.SigarProcess;
import org.hyperic.sigar.jmx.SigarRegistry; import org.hyperic.sigar.jmx.SigarRegistry;
public class Mx extends SigarCommandBase { public class Mx extends SigarCommandBase {
private boolean isRegistered; private boolean isRegistered;
public Mx(Shell shell) { public Mx(Shell shell) {
super(shell); super(shell);
} }
public Mx() { public Mx() {
super(); super();
} }
//java -Dcom.sun.management.jmxremote -jar sigar.jar //java -Dcom.sun.management.jmxremote -jar sigar.jar
public String getUsageShort() { public String getUsageShort() {
return "Register MBeans for use via jconsole, etc."; return "Register MBeans for use via jconsole, etc.";
} }
protected boolean validateArgs(String[] args) { protected boolean validateArgs(String[] args) {
return args.length <= 1; return args.length <= 1;
} }
public static MBeanServer getMBeanServer() public static MBeanServer getMBeanServer()
throws SigarException { throws SigarException {
List servers = List servers =
MBeanServerFactory.findMBeanServer(null); MBeanServerFactory.findMBeanServer(null);
if (servers.size() == 0) { if (servers.size() == 0) {
throw new SigarException("No MBeanServers available"); throw new SigarException("No MBeanServers available");
} }
else { else {
return (MBeanServer)servers.get(0); return (MBeanServer)servers.get(0);
} }
} }
private void register(MBeanServer server) throws SigarException { private void register(MBeanServer server) throws SigarException {
if (isRegistered) { if (isRegistered) {
return; return;
} }
SigarRegistry registry = new SigarRegistry(); SigarRegistry registry = new SigarRegistry();
try { try {
server.registerMBean(registry, null); server.registerMBean(registry, null);
SigarProcess proc = new SigarProcess(); SigarProcess proc = new SigarProcess();
server.registerMBean(proc, new ObjectName(proc.getObjectName())); server.registerMBean(proc, new ObjectName(proc.getObjectName()));
isRegistered = true; isRegistered = true;
} catch (Exception e) { } catch (Exception e) {
throw new SigarException(e.getMessage()); throw new SigarException(e.getMessage());
} }
} }
public void output(String[] args) throws SigarException { public void output(String[] args) throws SigarException {
MBeanServer server = getMBeanServer(); MBeanServer server = getMBeanServer();
register(server); register(server);
boolean hasQuery = args.length != 0; boolean hasQuery = args.length != 0;
try { try {
String query; String query;
if (hasQuery) { if (hasQuery) {
query = args[0]; query = args[0];
} }
else { else {
query = "sigar:*"; query = "sigar:*";
} }
Set beans = Set beans =
server.queryNames(new ObjectName(query), null); server.queryNames(new ObjectName(query), null);
println(beans.size() + " MBeans are registered..."); println(beans.size() + " MBeans are registered...");
for (Iterator it=beans.iterator(); it.hasNext();) { for (Iterator it=beans.iterator(); it.hasNext();) {
ObjectName name = (ObjectName)it.next(); ObjectName name = (ObjectName)it.next();
if (hasQuery) { if (hasQuery) {
MBeanInfo info = server.getMBeanInfo(name); MBeanInfo info = server.getMBeanInfo(name);
MBeanAttributeInfo[] attrs = info.getAttributes(); MBeanAttributeInfo[] attrs = info.getAttributes();
for (int i=0; i<attrs.length; i++) { for (int i=0; i<attrs.length; i++) {
String attr = attrs[i].getName(); String attr = attrs[i].getName();
Object val = server.getAttribute(name, attr); Object val = server.getAttribute(name, attr);
println(name + ":" + attr + "=" + val); println(name + ":" + attr + "=" + val);
} }
} }
else { else {
println(name.toString()); println(name.toString());
} }
} }
} catch (Exception e) { } catch (Exception e) {
throw new SigarException(e.getMessage()); throw new SigarException(e.getMessage());
} }
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
new Mx().processCommand(args); new Mx().processCommand(args);
} }
} }

View File

@ -1,207 +1,207 @@
/* Copyright (C) [2004, 2005, 2006], Hyperic, Inc. /* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR. * This file is part of SIGAR.
* *
* SIGAR is free software; you can redistribute it and/or modify * SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as * it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed * published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more * PARTICULAR PURPOSE. See the GNU General Public License for more
* details. * details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA. * USA.
*/ */
package org.hyperic.sigar.jmx; package org.hyperic.sigar.jmx;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import javax.management.AttributeNotFoundException; import javax.management.AttributeNotFoundException;
import javax.management.MBeanAttributeInfo; import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException; import javax.management.MBeanException;
import javax.management.MBeanInfo; import javax.management.MBeanInfo;
import javax.management.ReflectionException; import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar; import org.hyperic.sigar.Sigar;
public class ReflectedMBean extends AbstractMBean { public class ReflectedMBean extends AbstractMBean {
private Map methods; private Map methods;
private Map attrs = new HashMap(); private Map attrs = new HashMap();
private String type; private String type;
private String name; private String name;
private SigarInvokerJMX invoker; private SigarInvokerJMX invoker;
protected String getType() { protected String getType() {
return this.type; return this.type;
} }
void setType(String type) { void setType(String type) {
this.type = type; this.type = type;
} }
//static attributes //static attributes
void putAttribute(String name, Object val) { void putAttribute(String name, Object val) {
this.attrs.put(name, val); this.attrs.put(name, val);
} }
void putAttributes(Map attrs) { void putAttributes(Map attrs) {
this.attrs.putAll(attrs); this.attrs.putAll(attrs);
} }
void putAttributes(Object obj) { void putAttributes(Object obj) {
Method[] methods = obj.getClass().getDeclaredMethods(); Method[] methods = obj.getClass().getDeclaredMethods();
for (int i=0; i<methods.length; i++) { for (int i=0; i<methods.length; i++) {
Method method = methods[i]; Method method = methods[i];
if (method.getParameterTypes().length != 0) { if (method.getParameterTypes().length != 0) {
continue; continue;
} }
String name = method.getName(); String name = method.getName();
if (!name.startsWith("get")) { if (!name.startsWith("get")) {
continue; continue;
} }
name = name.substring(3); name = name.substring(3);
try { try {
putAttribute(name, putAttribute(name,
method.invoke(obj, new Object[0])); method.invoke(obj, new Object[0]));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
private String getMBeanClassName() { private String getMBeanClassName() {
String type = getType(); String type = getType();
final String list = "List"; final String list = "List";
if (type.endsWith(list)) { if (type.endsWith(list)) {
type = type =
type.substring(0, type.length() - list.length()); type.substring(0, type.length() - list.length());
} }
return "org.hyperic.sigar." + type; return "org.hyperic.sigar." + type;
} }
private Class getMBeanClass() { private Class getMBeanClass() {
try { try {
return Class.forName(getMBeanClassName()); return Class.forName(getMBeanClassName());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
} }
protected ReflectedMBean(Sigar sigar, String type) { protected ReflectedMBean(Sigar sigar, String type) {
super(sigar, CACHED_5SEC); super(sigar, CACHED_5SEC);
this.type = type; this.type = type;
this.name = this.name =
MBEAN_DOMAIN + ":" + MBEAN_DOMAIN + ":" +
MBEAN_ATTR_TYPE + "=" + getType(); MBEAN_ATTR_TYPE + "=" + getType();
} }
protected ReflectedMBean(Sigar sigar, String type, String arg) { protected ReflectedMBean(Sigar sigar, String type, String arg) {
this(sigar, type); this(sigar, type);
this.name += ",Name=" + encode(arg); this.name += ",Name=" + encode(arg);
} }
private String encode(String arg) { private String encode(String arg) {
return arg.replaceAll(":", "%3A"); return arg.replaceAll(":", "%3A");
} }
public String getObjectName() { public String getObjectName() {
return this.name; return this.name;
} }
protected SigarInvokerJMX getInvoker() { protected SigarInvokerJMX getInvoker() {
if (this.invoker == null) { if (this.invoker == null) {
this.invoker = this.invoker =
SigarInvokerJMX.getInstance(this.sigar, getObjectName()); SigarInvokerJMX.getInstance(this.sigar, getObjectName());
this.invoker.setType(getType()); this.invoker.setType(getType());
} }
return this.invoker; return this.invoker;
} }
public Object getAttribute(String name) public Object getAttribute(String name)
throws AttributeNotFoundException, throws AttributeNotFoundException,
MBeanException, ReflectionException { MBeanException, ReflectionException {
Object val = this.attrs.get(name); Object val = this.attrs.get(name);
if (val != null) { if (val != null) {
return val; return val;
} }
try { try {
return getInvoker().invoke(name); return getInvoker().invoke(name);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new ReflectionException(e); throw new ReflectionException(e);
} }
} }
private Map getMethods() { private Map getMethods() {
if (this.methods != null) { if (this.methods != null) {
return this.methods; return this.methods;
} }
this.methods = new LinkedHashMap(); this.methods = new LinkedHashMap();
Method[] methods = getMBeanClass().getDeclaredMethods(); Method[] methods = getMBeanClass().getDeclaredMethods();
for (int i=0; i<methods.length; i++) { for (int i=0; i<methods.length; i++) {
String name = methods[i].getName(); String name = methods[i].getName();
if (!name.startsWith("get")) { if (!name.startsWith("get")) {
continue; continue;
} }
name = name.substring(3); name = name.substring(3);
this.methods.put(name, methods[i]); this.methods.put(name, methods[i]);
} }
return this.methods; return this.methods;
} }
protected MBeanAttributeInfo[] getAttributeInfo() { protected MBeanAttributeInfo[] getAttributeInfo() {
Map methods = getMethods(); Map methods = getMethods();
MBeanAttributeInfo[] attrs = MBeanAttributeInfo[] attrs =
new MBeanAttributeInfo[methods.size() + this.attrs.size()]; new MBeanAttributeInfo[methods.size() + this.attrs.size()];
int i=0; int i=0;
for (Iterator it=methods.entrySet().iterator(); for (Iterator it=methods.entrySet().iterator();
it.hasNext();) it.hasNext();)
{ {
Map.Entry entry = (Map.Entry)it.next(); Map.Entry entry = (Map.Entry)it.next();
String name = (String)entry.getKey(); String name = (String)entry.getKey();
Method method = (Method)entry.getValue(); Method method = (Method)entry.getValue();
attrs[i++] = attrs[i++] =
new MBeanAttributeInfo(name, new MBeanAttributeInfo(name,
method.getReturnType().getName(), method.getReturnType().getName(),
name + " MBean", name + " MBean",
true, // isReadable true, // isReadable
false, // isWritable false, // isWritable
false); // isIs false); // isIs
} }
for (Iterator it=this.attrs.entrySet().iterator(); for (Iterator it=this.attrs.entrySet().iterator();
it.hasNext();) it.hasNext();)
{ {
Map.Entry entry = (Map.Entry)it.next(); Map.Entry entry = (Map.Entry)it.next();
String name = (String)entry.getKey(); String name = (String)entry.getKey();
Object obj = entry.getValue(); Object obj = entry.getValue();
attrs[i++] = attrs[i++] =
new MBeanAttributeInfo(name, new MBeanAttributeInfo(name,
obj.getClass().getName(), obj.getClass().getName(),
name + " MBean", name + " MBean",
true, // isReadable true, // isReadable
false, // isWritable false, // isWritable
false); // isIs false); // isIs
} }
return attrs; return attrs;
} }
public MBeanInfo getMBeanInfo() { public MBeanInfo getMBeanInfo() {
MBeanInfo info = MBeanInfo info =
new MBeanInfo(getClass().getName(), new MBeanInfo(getClass().getName(),
getMBeanClassName(), getMBeanClassName(),
getAttributeInfo(), getAttributeInfo(),
null, //constructors null, //constructors
null, //operations null, //operations
null); //notifications null); //notifications
return info; return info;
} }
} }