Example usage for javax.management MBeanServer queryNames

List of usage examples for javax.management MBeanServer queryNames

Introduction

In this page you can find the example usage for javax.management MBeanServer queryNames.

Prototype

public Set<ObjectName> queryNames(ObjectName name, QueryExp query);

Source Link

Usage

From source file:org.apache.hadoop.hbase.util.JSONBean.java

/**
 * @param mBeanServer//from w  w  w.  ja  va  2s . c o  m
 * @param qry
 * @param attribute
 * @param description
 * @return Return non-zero if failed to find bean. 0
 * @throws IOException
 */
private static int write(final JsonGenerator jg, final MBeanServer mBeanServer, ObjectName qry,
        String attribute, final boolean description) throws IOException {
    LOG.trace("Listing beans for " + qry);
    Set<ObjectName> names = null;
    names = mBeanServer.queryNames(qry, null);
    jg.writeArrayFieldStart("beans");
    Iterator<ObjectName> it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = it.next();
        MBeanInfo minfo;
        String code = "";
        String descriptionStr = null;
        Object attributeinfo = null;
        try {
            minfo = mBeanServer.getMBeanInfo(oname);
            code = minfo.getClassName();
            if (description)
                descriptionStr = minfo.getDescription();
            String prs = "";
            try {
                if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                    prs = "modelerType";
                    code = (String) mBeanServer.getAttribute(oname, prs);
                }
                if (attribute != null) {
                    prs = attribute;
                    attributeinfo = mBeanServer.getAttribute(oname, prs);
                }
            } catch (RuntimeMBeanException e) {
                // UnsupportedOperationExceptions happen in the normal course of business,
                // so no need to log them as errors all the time.
                if (e.getCause() instanceof UnsupportedOperationException) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Getting attribute " + prs + " of " + oname + " threw " + e);
                    }
                } else {
                    LOG.error("Getting attribute " + prs + " of " + oname + " threw an exception", e);
                }
                return 0;
            } catch (AttributeNotFoundException e) {
                // If the modelerType attribute was not found, the class name is used
                // instead.
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (MBeanException e) {
                // The code inside the attribute getter threw an exception so log it,
                // and fall back on the class name
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (RuntimeException e) {
                // For some reason even with an MBeanException available to them
                // Runtime exceptionscan still find their way through, so treat them
                // the same as MBeanException
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (ReflectionException e) {
                // This happens when the code inside the JMX bean (setter?? from the
                // java docs) threw an exception, so log it and fall back on the
                // class name
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            }
        } catch (InstanceNotFoundException e) {
            //Ignored for some reason the bean was not found so don't output it
            continue;
        } catch (IntrospectionException e) {
            // This is an internal error, something odd happened with reflection so
            // log it and don't output the bean.
            LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
            continue;
        } catch (ReflectionException e) {
            // This happens when the code inside the JMX bean threw an exception, so
            // log it and don't output the bean.
            LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
            continue;
        }

        jg.writeStartObject();
        jg.writeStringField("name", oname.toString());
        if (description && descriptionStr != null && descriptionStr.length() > 0) {
            jg.writeStringField("description", descriptionStr);
        }
        jg.writeStringField("modelerType", code);
        if (attribute != null && attributeinfo == null) {
            jg.writeStringField("result", "ERROR");
            jg.writeStringField("message", "No attribute with name " + attribute + " was found.");
            jg.writeEndObject();
            jg.writeEndArray();
            jg.close();
            return -1;
        }

        if (attribute != null) {
            writeAttribute(jg, attribute, descriptionStr, attributeinfo);
        } else {
            MBeanAttributeInfo[] attrs = minfo.getAttributes();
            for (int i = 0; i < attrs.length; i++) {
                writeAttribute(jg, mBeanServer, oname, description, attrs[i]);
            }
        }
        jg.writeEndObject();
    }
    jg.writeEndArray();
    return 0;
}

From source file:com.beginner.core.utils.ProjectUtil.java

/**
 * ?Web???(???)/*from   ww w  .ja  v  a  2  s  .  c  om*/
 */
public static String getPort() {
    String result = "";
    try {
        MBeanServer mBeanServer = null;
        List<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null);
        if (mBeanServers.size() > 0) {
            for (MBeanServer _mBeanServer : mBeanServers) {
                mBeanServer = _mBeanServer;
                break;
            }
        }
        if (mBeanServer == null) {
            throw new IllegalStateException("?JVM?MBeanServer.");
        }
        Set<ObjectName> objectNames = null;
        objectNames = mBeanServer.queryNames(new ObjectName("Catalina:type=Connector,*"), null);
        if (objectNames == null || objectNames.size() <= 0) {
            throw new IllegalStateException("?JVM?MBeanServer : "
                    + mBeanServer.getDefaultDomain() + " ??.");
        }
        for (ObjectName objectName : objectNames) {
            String protocol = (String) mBeanServer.getAttribute(objectName, "protocol");
            if (protocol.equals("HTTP/1.1")) {
                int port = (Integer) mBeanServer.getAttribute(objectName, "port");
                result = port + StringUtils.EMPTY;
            }
        }
    } catch (Exception e) {
        logger.error("?WEB?HTTP/1.1???", e);
    }
    return result;
}

From source file:com.atolcd.alfresco.audit.web.scripts.ContentStoreInfoGet.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {

    try {//from   www  .  j a v  a2 s .  com
        // Map that will be passed to the template
        Map<String, Object> model = new HashMap<String, Object>();

        ObjectName query = new ObjectName("Alfresco:Name=ContentStore,Type=*,Root=*");
        MBeanServer mbs = getMBeanServerWithQuery(query);
        Set<ObjectName> storesName = mbs.queryNames(query, null);

        List<ContentStore> contentStores = new ArrayList<ContentStore>();

        for (ObjectName storeName : storesName) {
            Object writeSupported = mbs.getAttribute(storeName, "WriteSupported");
            Object totalSize = mbs.getAttribute(storeName, "SpaceUsed");

            contentStores.add(new ContentStore(storeName.getKeyProperty("Root"), String.valueOf(writeSupported),
                    String.valueOf(totalSize)));
        }

        model.put("contentStores", contentStores);

        return model;
    } catch (Exception e) {
        throw new WebScriptException("[ContentStoreInfoGet] Error in executeImpl function");
    }
}

From source file:com.atolcd.alfresco.audit.web.scripts.LuceneIndexesInfoGet.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {

    try {/*from w w  w  .ja va  2s .c om*/
        // Map that will be passed to the template
        Map<String, Object> model = new HashMap<String, Object>();

        ObjectName query = new ObjectName("Alfresco:Name=LuceneIndexes,*");
        MBeanServer mbs = getMBeanServerWithQuery(query);
        Set<ObjectName> luceneIndexesName = mbs.queryNames(query, null);
        List<LuceneIndex> luceneIndexes = new ArrayList<LuceneIndex>();

        for (ObjectName luceneIndexName : luceneIndexesName) {

            luceneIndexes.add(new LuceneIndex(luceneIndexName.getKeyProperty("Index"),
                    String.valueOf(mbs.getAttribute(luceneIndexName, "ActualSize")),
                    String.valueOf(mbs.getAttribute(luceneIndexName, "NumberOfDocuments")),
                    String.valueOf(mbs.getAttribute(luceneIndexName, "NumberOfFields")),
                    String.valueOf(mbs.getAttribute(luceneIndexName, "NumberOfIndexedFields")),
                    String.valueOf(mbs.getAttribute(luceneIndexName, "UsedSize"))));
        }

        model.put("luceneIndexes", luceneIndexes);

        return model;
    } catch (Exception e) {
        throw new WebScriptException(
                "[LuceneIndexesInfosGet] Error in executeImpl function : \n" + e.getMessage());
    }
}

From source file:org.nuxeo.ecm.core.management.jtajca.JtajcaManagementFeature.java

protected <T> void bind(Binder binder, MBeanServer mbs, Class<T> type) {
    final Set<ObjectName> names = mbs.queryNames(nameOf(type), null);
    for (ObjectName name : names) {
        T instance = type.cast(JMX.newMXBeanProxy(mbs, name, type));
        binder.bind(type).annotatedWith(Names.named(name.getKeyProperty("name"))).toInstance(instance);
    }//from  w  ww .  j  a v  a 2 s .  c om
}

From source file:org.apache.camel.example.management.ManagementExampleTest.java

@Test
public void testManagementExample() throws Exception {
    // Give it a bit of time to run
    Thread.sleep(2000);/*from  w w w .  ja va 2 s.  c  o m*/

    MBeanServer mbeanServer = context.getManagementStrategy().getManagementAgent().getMBeanServer();

    // Find the endpoints
    Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=endpoints,*"), null);

    // now there is no managed endpoint for the dead queue
    // CAMEL-7076 there is no spring-event:default endpoint any more
    assertEquals(4, set.size());

    // Find the routes
    set = mbeanServer.queryNames(new ObjectName("*:type=routes,*"), null);
    assertEquals(3, set.size());

    // Stop routes
    for (ObjectName on : set) {
        mbeanServer.invoke(on, "stop", null, null);
    }
}

From source file:net.gcolin.simplerepo.test.AbstractRepoTest.java

protected void setAttributeJmx(String name, String attribute, Object arguments) throws Exception {
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    ObjectName oname = server.queryNames(new ObjectName(name), null).iterator().next();
    server.setAttribute(oname, new Attribute(attribute, arguments));
}

From source file:net.gcolin.simplerepo.test.AbstractRepoTest.java

protected Object executeOperationJmx(String name, String operation, Object[] arguments, String[] signature)
        throws Exception {
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    ObjectName oname = server.queryNames(new ObjectName(name), null).iterator().next();
    return server.invoke(oname, operation, arguments, signature);
}

From source file:org.apache.cassandra.auth.JMXResource.java

@Override
public boolean exists() {
    if (!hasParent())
        return true;
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    try {//  www. jav a 2s .  c  om
        return !(mbs.queryNames(new ObjectName(name), null).isEmpty());
    } catch (MalformedObjectNameException e) {
        return false;
    } catch (NullPointerException e) {
        return false;
    }
}

From source file:org.apache.synapse.securevault.commons.MBeanRegistrar.java

public void registerMBean(Object mbeanInstance, String category, String id) {
    assertNull(mbeanInstance, "Mbean instance is null");
    assertNull(category, "Mbean instance category is null");
    assertNull(id, "Mbean instance name is null");
    try {//from ww  w. j a  v a  2s.co m
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        ObjectName name = new ObjectName(getObjectName(category, id));
        Set set = mbs.queryNames(name, null);
        if (set != null && set.isEmpty()) {
            mbs.registerMBean(mbeanInstance, name);
        } else {
            mbs.unregisterMBean(name);
            mbs.registerMBean(mbeanInstance, name);
        }
    } catch (Exception e) {
        log.warn("Error registering a MBean with name ' " + id + " ' and category name ' " + category
                + "' for JMX management", e);
    }
}