Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.lang.reflect.Array;
- import java.util.HashMap;
- import java.util.Map;
- import javax.management.AttributeNotFoundException;
- import javax.management.InstanceNotFoundException;
- import javax.management.IntrospectionException;
- import javax.management.MBeanAttributeInfo;
- import javax.management.MBeanException;
- import javax.management.MBeanInfo;
- import javax.management.MBeanServerConnection;
- import javax.management.ObjectName;
- import javax.management.ReflectionException;
- import javax.management.openmbean.CompositeData;
- import javax.management.openmbean.CompositeType;
- import javax.management.openmbean.TabularData;
- import javax.management.remote.JMXConnector;
- import javax.management.remote.JMXConnectorFactory;
- import javax.management.remote.JMXServiceURL;
- public class JmxDump {
- static public MBeanServerConnection connect(String host, int port, String user, String password) throws IOException {
- String uri = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi";
- JMXServiceURL jmxserviceurl = new JMXServiceURL(uri);
- Map<String, Object> attributes = null;
- if(user != null && password != null ) {
- String[] credentials = new String[]{user, password};
- attributes = new HashMap<String, Object>();
- attributes.put("jmx.remote.credentials", credentials);
- }
- JMXConnector connector = JMXConnectorFactory.connect(jmxserviceurl, attributes);
- MBeanServerConnection connection = connector.getMBeanServerConnection();
- return connection;
- }
- static public void dumpTd(String name, TabularData td, String prefix) {
- System.out.println(prefix + "javax.management.openmbean.TabularData[" + td.size() +"] " + name);
- }
- static public void dumpCd(String name, CompositeData cd, String prefix) {
- System.out.println(prefix + "javax.management.openmbean.CompositeData " + name);
- if(cd != null) {
- CompositeType ct = cd.getCompositeType();
- for(Object key: ct.keySet()) {
- Object value = cd.get((String)key);
- dispatch(key.toString(), value.getClass().getName(), value, prefix + " ");
- }
- }
- }
- static public void dispatch(String name, String type, Object attr, String prefix) {
- if(type == null) {
- System.out.println(prefix + "null " + name);
- }
- else if(attr instanceof javax.management.openmbean.TabularData) {
- TabularData td = (TabularData) attr;
- dumpTd(name, td, prefix);
- }
- else if(attr instanceof javax.management.openmbean.CompositeData) {
- CompositeData cd = (CompositeData) attr;
- dumpCd(name, cd, prefix);
- }
- else if(attr.getClass().isArray()) {
- System.out.println(prefix + attr.getClass().getComponentType().getName() + "[" + Array.getLength(attr) + "]" + " " + name);
- }
- else {
- System.out.println(prefix + type + " " + name);
- }
- }
- static public void dump(MBeanServerConnection mbean) throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, IOException, IntrospectionException {
- for(Object nameObject: mbean.queryNames(null, null)) {
- ObjectName name = (ObjectName) nameObject;
- System.out.println(name);
- MBeanInfo info = mbean.getMBeanInfo(name);
- for(MBeanAttributeInfo attrInfo : info.getAttributes()) {
- Object attr;
- try {
- attr = mbean.getAttribute(name, attrInfo.getName());
- dispatch(attrInfo.getName(), attrInfo.getType(), attr, " ");
- } catch (Exception e) {
- String type = attrInfo.getType();
- if(type.startsWith("[L")) {
- type = type.substring(2, type.length() - 1 ) + "[]";
- }
- System.out.println(" " + type + " " + attrInfo.getName() +" (Unavailable)");
- }
- }
- }
- }
- /**
- * @param args
- * @throws IOException
- * @throws ReflectionException
- * @throws MBeanException
- * @throws IntrospectionException
- * @throws InstanceNotFoundException
- * @throws AttributeNotFoundException
- */
- public static void main(String[] args) throws IOException, AttributeNotFoundException, InstanceNotFoundException, IntrospectionException, MBeanException, ReflectionException {
- String host = args[0];
- int port = 0;
- if(args.length == 2 || args.length == 4) {
- port = Integer.parseInt(args[1]);
- }
- int loginbase = 2;
- if(args.length == 3) {
- loginbase = 3;
- }
- String user = null;
- String password = null;
- if(args.length > 2) {
- user = args[loginbase];
- password = args[loginbase + 1];
- }
- MBeanServerConnection mbean = connect(host, port, user, password);
- dump(mbean);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement