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.synapse.transport.passthru.jmx.MBeanRegistrar.java

public void registerMBean(Object mBeanInstance, String category, String id) throws AxisFault {
    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 a2 s .com*/
        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);
    }
}

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

public boolean 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   w w w  . j  av a  2  s  .c  o 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);
        }
        return true;
    } catch (Exception e) {
        log.warn("Error registering a MBean with name ' " + id + " ' and category name ' " + category
                + "' for JMX management", e);
        return false;
    }
}

From source file:org.apache.hadoop.hdfs.server.common.MetricsLoggerTask.java

/**
 * Write metrics to the metrics appender when invoked.
 */// w  ww.j ava 2 s  .  c  om
@Override
public void run() {
    // Skip querying metrics if there are no known appenders.
    if (!metricsLog.isInfoEnabled() || !hasAppenders(metricsLog) || objectName == null) {
        return;
    }

    metricsLog.info(" >> Begin " + nodeName + " metrics dump");
    final MBeanServer server = ManagementFactory.getPlatformMBeanServer();

    // Iterate over each MBean.
    for (final ObjectName mbeanName : server.queryNames(objectName, null)) {
        try {
            MBeanInfo mBeanInfo = server.getMBeanInfo(mbeanName);
            final String mBeanNameName = MBeans.getMbeanNameName(mbeanName);
            final Set<String> attributeNames = getFilteredAttributes(mBeanInfo);

            final AttributeList attributes = server.getAttributes(mbeanName,
                    attributeNames.toArray(new String[attributeNames.size()]));

            for (Object o : attributes) {
                final Attribute attribute = (Attribute) o;
                final Object value = attribute.getValue();
                final String valueStr = (value != null) ? value.toString() : "null";
                // Truncate the value if it is too long
                metricsLog.info(mBeanNameName + ":" + attribute.getName() + "=" + trimLine(valueStr));
            }
        } catch (Exception e) {
            metricsLog.error("Failed to get " + nodeName + " metrics for mbean " + mbeanName.toString(), e);
        }
    }
    metricsLog.info(" << End " + nodeName + " metrics dump");
}

From source file:com.googlecode.psiprobe.beans.JBossResourceResolverBean.java

public List getApplicationResources() throws NamingException {

    List resources = new ArrayList();

    MBeanServer server = getMBeanServer();
    if (server != null) {
        try {/*from w w  w .j ava2s.c  o m*/
            Set dsNames = server.queryNames(new ObjectName("jboss.jca:service=ManagedConnectionPool,*"), null);
            for (Iterator it = dsNames.iterator(); it.hasNext();) {
                ObjectName managedConnectionPoolOName = (ObjectName) it.next();

                ApplicationResource resource = new ApplicationResource();
                resource.setName(managedConnectionPoolOName.getKeyProperty("name"));
                resource.setType("jboss");
                String criteria = (String) server.getAttribute(managedConnectionPoolOName, "Criteria");
                if ("ByApplication".equals(criteria)) {
                    resource.setAuth("Application");
                } else if ("ByContainerAndApplication".equals(criteria)) {
                    resource.setAuth("Both");
                } else {
                    resource.setAuth("Container");
                }
                DataSourceInfo dsInfo = new DataSourceInfo();
                dsInfo.setMaxConnections(
                        ((Integer) server.getAttribute(managedConnectionPoolOName, "MaxSize")).intValue());
                dsInfo.setEstablishedConnections(
                        ((Integer) server.getAttribute(managedConnectionPoolOName, "ConnectionCount"))
                                .intValue());
                dsInfo.setBusyConnections(
                        ((Long) server.getAttribute(managedConnectionPoolOName, "InUseConnectionCount"))
                                .intValue());

                ObjectName connectionFactoryOName = new ObjectName(
                        "jboss.jca:service=ManagedConnectionFactory,name=" + resource.getName());
                Element elm = (Element) server.getAttribute(connectionFactoryOName,
                        "ManagedConnectionFactoryProperties");

                if (elm != null) {
                    NodeList nl = elm.getChildNodes();
                    for (int i = 0; i < nl.getLength(); i++) {
                        Node n = nl.item(i);
                        Node na = n.getAttributes().getNamedItem("name");
                        if (na != null) {
                            if ("ConnectionURL".equals(na.getNodeValue())) {
                                dsInfo.setJdbcURL(n.getFirstChild().getNodeValue());
                            }

                            if ("UserName".equals(na.getNodeValue())) {
                                dsInfo.setUsername(n.getFirstChild().getNodeValue());
                            }

                            //
                            // JMS datasource
                            //
                            if ("JmsProviderAdapterJNDI".equals(na.getNodeValue())) {
                                dsInfo.setJdbcURL(n.getFirstChild().getNodeValue());
                                resource.setType("jms");
                            }
                        }
                    }
                }

                dsInfo.setResettable(true);

                resource.setDataSourceInfo(dsInfo);
                resources.add(resource);
            }
        } catch (Exception e) {
            //
            logger.fatal("There was an error querying JBoss JMX server:", e);
        }
    }
    return resources;
}

From source file:org.taverna.server.master.interaction.InteractionFeedSupport.java

@PostConstruct
void determinePorts() {
    try {//from   w w  w  .  ja v  a 2s.c  o m
        MBeanServer mbs = getPlatformMBeanServer();
        for (ObjectName obj : mbs.queryNames(new ObjectName("*:type=Connector,*"),
                match(attr("protocol"), value("HTTP/1.1")))) {
            String scheme = mbs.getAttribute(obj, "scheme").toString();
            String port = obj.getKeyProperty("port");
            endPoints.put(scheme, new URL(scheme + "://localhost:" + port));
        }
        getLog(getClass()).info("installed feed port publication mapping for " + endPoints.keySet());
    } catch (Exception e) {
        getLog(getClass()).error("failure in determining local port mapping", e);
    }
}

From source file:org.alfresco.solr.servlet.Solr4X509ServletFilter.java

private int getHttpsPort() {
    try {//from w ww.ja va2s . co  m
        MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
        QueryExp query = Query.eq(Query.attr("Scheme"), Query.value("https"));
        Set<ObjectName> objectNames = mBeanServer.queryNames(null, query);

        if (objectNames != null && objectNames.size() > 0) {
            for (ObjectName objectName : objectNames) {
                String name = objectName.toString();
                if (name.indexOf("port=") > -1) {
                    String[] parts = name.split("port=");
                    String port = parts[1];
                    try {
                        int portNum = Integer.parseInt(port);
                        return portNum;
                    } catch (NumberFormatException e) {
                        logger.error("Error parsing https port:" + port);
                        return -1;
                    }
                }
            }
        }
    } catch (Throwable t) {
        logger.error("Error getting https port:", t);
    }

    return -1;
}

From source file:com.googlecode.psiprobe.beans.ResourceResolverBean.java

public List getApplicationResources() throws NamingException {
    logger.info("Reading GLOBAL resources");
    List resources = new ArrayList();

    MBeanServer server = getMBeanServer();
    if (server != null) {
        try {/* ww  w  . j av  a2  s.  c  om*/
            Set dsNames = server.queryNames(new ObjectName("Catalina:type=Resource,resourcetype=Global,*"),
                    null);
            for (Iterator it = dsNames.iterator(); it.hasNext();) {
                ObjectName objectName = (ObjectName) it.next();
                ApplicationResource resource = new ApplicationResource();

                logger.info("reading resource: " + objectName);
                resource.setName(getStringAttribute(server, objectName, "name"));
                resource.setType(getStringAttribute(server, objectName, "type"));
                resource.setScope(getStringAttribute(server, objectName, "scope"));
                resource.setAuth(getStringAttribute(server, objectName, "auth"));
                resource.setDescription(getStringAttribute(server, objectName, "description"));

                lookupResource(resource, true, true);

                resources.add(resource);
            }
        } catch (Exception e) {
            logger.error("There was an error querying JMX server:", e);
        }
    }
    return resources;
}

From source file:org.apache.openejb.server.cli.command.LocalJMXCommand.java

private void listMBeans() {
    final MBeanServer mBeanServer = LocalMBeanServer.get();

    final Set<ObjectName> names;
    try {/*from  w  w w  . j  a  v  a2 s .  c  o  m*/
        names = mBeanServer.queryNames(null, null);
    } catch (Exception e) {
        streamManager.writeErr(e);
        return;
    }

    final Iterator<ObjectName> it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = it.next();
        streamManager.writeOut("Name: " + oname.toString());

        try {
            final MBeanInfo minfo = mBeanServer.getMBeanInfo(oname);
            String code = minfo.getClassName();
            if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                code = (String) mBeanServer.getAttribute(oname, "modelerType");
            }
            streamManager.writeOut("  + modelerType: " + code);

            MBeanAttributeInfo attrs[] = minfo.getAttributes();
            Object value = null;

            for (int i = 0; i < attrs.length; i++) {
                if (!attrs[i].isReadable()) {
                    continue;
                }

                final String attName = attrs[i].getName();
                if ("modelerType".equals(attName)) {
                    continue;
                }

                if (attName.indexOf("=") >= 0 || attName.indexOf(":") >= 0 || attName.indexOf(" ") >= 0) {
                    continue;
                }

                try {
                    value = mBeanServer.getAttribute(oname, attName);
                } catch (RuntimeMBeanException uoe) {
                    // ignored
                } catch (Throwable t) {
                    streamManager.writeErr(new Exception(t));
                    continue;
                }

                try {
                    String valueString = stringify(value);
                    streamManager.writeOut("  + " + attName + ": " + valueString);
                } catch (Throwable t) {
                    streamManager.writeErr(new Exception(t));
                }
            }
        } catch (Throwable t) {
            streamManager.writeErr(new Exception(t));
        }
        streamManager.writeOut("");
    }
}

From source file:org.hyperic.hq.plugin.weblogic.jmx.WeblogicDiscover.java

private void discoverInit(MBeanServer mServer) throws Exception {

    // only exists on the admin server
    final String scope = "*:Type=ApplicationConfig,*";

    for (Iterator it = mServer.queryNames(new ObjectName(scope), null).iterator(); it.hasNext();) {
        ObjectName oName = (ObjectName) it.next();
        if (this.domain == null) {
            this.domain = oName.getDomain();
        }//from   w  w w  .ja va  2 s .c om
        if (this.adminName == null) {
            this.adminName = oName.getKeyProperty("Location");
        }

        String name = oName.getKeyProperty("Name");

        // special case for console so we can control it
        if (name.equals("console")) {
            continue;
        }

        boolean isInternal = ((Boolean) mServer.getAttribute(oName, "InternalApp")).booleanValue();

        if (isInternal) {
            this.internalApps.put(name, Boolean.TRUE);
        }
    }
}

From source file:org.apache.camel.web.util.JMXRouteStatistics.java

@SuppressWarnings("unchecked")
public Object getRouteStatistic(CamelContext camelContext, String routeID, String attribute) {
    // only possible if JMX is enabled
    if (!(camelContext.getManagementStrategy() instanceof ManagedManagementStrategy)) {
        return null;
    }//  ww w. jav  a2s . c  om

    try {
        MBeanServer server = camelContext.getManagementStrategy().getManagementAgent().getMBeanServer();
        String domain = camelContext.getManagementStrategy().getManagementAgent().getMBeanObjectDomainName();

        ObjectName objName = new ObjectName(domain + ":type=routes,*");
        List<ObjectName> cacheList = new LinkedList(server.queryNames(objName, null));
        for (Iterator<ObjectName> iter = cacheList.iterator(); iter.hasNext();) {
            objName = iter.next();
            String keyProps = objName.getCanonicalKeyPropertyListString();
            ObjectName objectInfoName = new ObjectName(domain + ":" + keyProps);
            String currentRouteID = (String) server.getAttribute(objectInfoName, "RouteId");
            if (currentRouteID.equals(routeID)) {
                Object value = server.getAttribute(objectInfoName, attribute);
                if (value instanceof Date) {
                    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
                    return df.format(value);
                } else {
                    return value;
                }
            }
        }
    } catch (Exception e) {
        LOG.warn("Error getting route statistic from JMX. This exception will be ignored.", e);
    }

    return null;
}