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.
* 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);
}
}
/* 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);
}
}

View File

@ -1,207 +1,207 @@
/* 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.jmx;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.management.AttributeNotFoundException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar;
public class ReflectedMBean extends AbstractMBean {
private Map methods;
private Map attrs = new HashMap();
private String type;
private String name;
private SigarInvokerJMX invoker;
protected String getType() {
return this.type;
}
void setType(String type) {
this.type = type;
}
//static attributes
void putAttribute(String name, Object val) {
this.attrs.put(name, val);
}
void putAttributes(Map attrs) {
this.attrs.putAll(attrs);
}
void putAttributes(Object obj) {
Method[] methods = obj.getClass().getDeclaredMethods();
for (int i=0; i<methods.length; i++) {
Method method = methods[i];
if (method.getParameterTypes().length != 0) {
continue;
}
String name = method.getName();
if (!name.startsWith("get")) {
continue;
}
name = name.substring(3);
try {
putAttribute(name,
method.invoke(obj, new Object[0]));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private String getMBeanClassName() {
String type = getType();
final String list = "List";
if (type.endsWith(list)) {
type =
type.substring(0, type.length() - list.length());
}
return "org.hyperic.sigar." + type;
}
private Class getMBeanClass() {
try {
return Class.forName(getMBeanClassName());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
protected ReflectedMBean(Sigar sigar, String type) {
super(sigar, CACHED_5SEC);
this.type = type;
this.name =
MBEAN_DOMAIN + ":" +
MBEAN_ATTR_TYPE + "=" + getType();
}
protected ReflectedMBean(Sigar sigar, String type, String arg) {
this(sigar, type);
this.name += ",Name=" + encode(arg);
}
private String encode(String arg) {
return arg.replaceAll(":", "%3A");
}
public String getObjectName() {
return this.name;
}
protected SigarInvokerJMX getInvoker() {
if (this.invoker == null) {
this.invoker =
SigarInvokerJMX.getInstance(this.sigar, getObjectName());
this.invoker.setType(getType());
}
return this.invoker;
}
public Object getAttribute(String name)
throws AttributeNotFoundException,
MBeanException, ReflectionException {
Object val = this.attrs.get(name);
if (val != null) {
return val;
}
try {
return getInvoker().invoke(name);
} catch (Exception e) {
e.printStackTrace();
throw new ReflectionException(e);
}
}
private Map getMethods() {
if (this.methods != null) {
return this.methods;
}
this.methods = new LinkedHashMap();
Method[] methods = getMBeanClass().getDeclaredMethods();
for (int i=0; i<methods.length; i++) {
String name = methods[i].getName();
if (!name.startsWith("get")) {
continue;
}
name = name.substring(3);
this.methods.put(name, methods[i]);
}
return this.methods;
}
protected MBeanAttributeInfo[] getAttributeInfo() {
Map methods = getMethods();
MBeanAttributeInfo[] attrs =
new MBeanAttributeInfo[methods.size() + this.attrs.size()];
int i=0;
for (Iterator it=methods.entrySet().iterator();
it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
String name = (String)entry.getKey();
Method method = (Method)entry.getValue();
attrs[i++] =
new MBeanAttributeInfo(name,
method.getReturnType().getName(),
name + " MBean",
true, // isReadable
false, // isWritable
false); // isIs
}
for (Iterator it=this.attrs.entrySet().iterator();
it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
String name = (String)entry.getKey();
Object obj = entry.getValue();
attrs[i++] =
new MBeanAttributeInfo(name,
obj.getClass().getName(),
name + " MBean",
true, // isReadable
false, // isWritable
false); // isIs
}
return attrs;
}
public MBeanInfo getMBeanInfo() {
MBeanInfo info =
new MBeanInfo(getClass().getName(),
getMBeanClassName(),
getAttributeInfo(),
null, //constructors
null, //operations
null); //notifications
return info;
}
}
/* 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.jmx;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.management.AttributeNotFoundException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.ReflectionException;
import org.hyperic.sigar.Sigar;
public class ReflectedMBean extends AbstractMBean {
private Map methods;
private Map attrs = new HashMap();
private String type;
private String name;
private SigarInvokerJMX invoker;
protected String getType() {
return this.type;
}
void setType(String type) {
this.type = type;
}
//static attributes
void putAttribute(String name, Object val) {
this.attrs.put(name, val);
}
void putAttributes(Map attrs) {
this.attrs.putAll(attrs);
}
void putAttributes(Object obj) {
Method[] methods = obj.getClass().getDeclaredMethods();
for (int i=0; i<methods.length; i++) {
Method method = methods[i];
if (method.getParameterTypes().length != 0) {
continue;
}
String name = method.getName();
if (!name.startsWith("get")) {
continue;
}
name = name.substring(3);
try {
putAttribute(name,
method.invoke(obj, new Object[0]));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private String getMBeanClassName() {
String type = getType();
final String list = "List";
if (type.endsWith(list)) {
type =
type.substring(0, type.length() - list.length());
}
return "org.hyperic.sigar." + type;
}
private Class getMBeanClass() {
try {
return Class.forName(getMBeanClassName());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
protected ReflectedMBean(Sigar sigar, String type) {
super(sigar, CACHED_5SEC);
this.type = type;
this.name =
MBEAN_DOMAIN + ":" +
MBEAN_ATTR_TYPE + "=" + getType();
}
protected ReflectedMBean(Sigar sigar, String type, String arg) {
this(sigar, type);
this.name += ",Name=" + encode(arg);
}
private String encode(String arg) {
return arg.replaceAll(":", "%3A");
}
public String getObjectName() {
return this.name;
}
protected SigarInvokerJMX getInvoker() {
if (this.invoker == null) {
this.invoker =
SigarInvokerJMX.getInstance(this.sigar, getObjectName());
this.invoker.setType(getType());
}
return this.invoker;
}
public Object getAttribute(String name)
throws AttributeNotFoundException,
MBeanException, ReflectionException {
Object val = this.attrs.get(name);
if (val != null) {
return val;
}
try {
return getInvoker().invoke(name);
} catch (Exception e) {
e.printStackTrace();
throw new ReflectionException(e);
}
}
private Map getMethods() {
if (this.methods != null) {
return this.methods;
}
this.methods = new LinkedHashMap();
Method[] methods = getMBeanClass().getDeclaredMethods();
for (int i=0; i<methods.length; i++) {
String name = methods[i].getName();
if (!name.startsWith("get")) {
continue;
}
name = name.substring(3);
this.methods.put(name, methods[i]);
}
return this.methods;
}
protected MBeanAttributeInfo[] getAttributeInfo() {
Map methods = getMethods();
MBeanAttributeInfo[] attrs =
new MBeanAttributeInfo[methods.size() + this.attrs.size()];
int i=0;
for (Iterator it=methods.entrySet().iterator();
it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
String name = (String)entry.getKey();
Method method = (Method)entry.getValue();
attrs[i++] =
new MBeanAttributeInfo(name,
method.getReturnType().getName(),
name + " MBean",
true, // isReadable
false, // isWritable
false); // isIs
}
for (Iterator it=this.attrs.entrySet().iterator();
it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
String name = (String)entry.getKey();
Object obj = entry.getValue();
attrs[i++] =
new MBeanAttributeInfo(name,
obj.getClass().getName(),
name + " MBean",
true, // isReadable
false, // isWritable
false); // isIs
}
return attrs;
}
public MBeanInfo getMBeanInfo() {
MBeanInfo info =
new MBeanInfo(getClass().getName(),
getMBeanClassName(),
getAttributeInfo(),
null, //constructors
null, //operations
null); //notifications
return info;
}
}