Example usage for javax.management MBeanServerConnection queryNames

List of usage examples for javax.management MBeanServerConnection queryNames

Introduction

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

Prototype

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

Source Link

Document

Gets the names of MBeans controlled by the MBean server.

Usage

From source file:org.talend.esb.monitoring.hq.DynamicMxFieldMeasurementPlugin.java

/**
 * Looks up for a ObjectName based on a given pattern ObjectName.
 * //from w  w w. j  av a2 s . c o  m
 * @param patternObjectName
 *            a pattern object name
 * @param connectionProps
 *            connection properties
 * @return Returns the found ObjectName, otherwise throws an exception
 * @throws MetricNotFoundException
 *             is thrown when no metric was found using specified pattern
 * @throws MetricUnreachableException
 *             is thrown when it is impossible to find the object name
 *             because of connection problems
 * @throws MetricInvalidException
 *             is thrown if patter format is wrong
 * @throws PluginException
 *             if some unexpected problem happened
 */
private String findObjectName(final String patternObjectName, final Properties connectionProps)
        throws MetricNotFoundException, MetricUnreachableException, MetricInvalidException, PluginException {

    ObjectName patternOn;
    try {
        patternOn = new ObjectName(patternObjectName);
    } catch (MalformedObjectNameException e) {
        throw new MetricInvalidException(MSG_ERR_MALFORMED, e);
    }

    JMXConnector jmxConnector = null;
    try {
        jmxConnector = MxUtil.getCachedMBeanConnector(connectionProps);
        final MBeanServerConnection conn = jmxConnector.getMBeanServerConnection();
        final Set<ObjectName> result = conn.queryNames(patternOn, null);

        if (result.iterator().hasNext()) {
            return result.iterator().next().getCanonicalName();
        }
    } catch (IOException e) {
        log.debug(MSG_ERR_CONNECT, e);
        throw new MetricUnreachableException(MSG_ERR_CONNECT, e);
    } catch (Exception e) {
        log.debug(MSG_ERR_UNEXPECTED, e);
        throw new PluginException(MSG_ERR_UNEXPECTED, e);
    } finally {
        // it's null-proof
        MxUtil.close(jmxConnector);
    }

    throw new MetricNotFoundException(MSG_ERR_NOTFOUND);
}

From source file:org.wso2.carbon.analytics.common.jmx.agent.JmxAgentWebInterface.java

/**
 * @param url      : The URL for the JMX server
 * @param userName : The User name for the JMX server
 * @param password : The password for the JMX server
 * @return : The name array of the MBeans which the JMX server. The format of the array is
 *         [Domain name][MBean Canonical name]
 * @throws IOException//from w w w.j a  va  2s  .  c  o m
 */
public String[][] getMBeans(String url, String userName, String password) throws IOException {

    JMXConnector jmxc = null;

    try {
        jmxc = getJmxConnector(url, userName, password);

        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

        int count = 0;
        Set<ObjectName> names = new TreeSet<ObjectName>(mbsc.queryNames(null, null));
        String[][] nameArr = new String[names.size()][2];

        for (ObjectName name : names) {
            nameArr[count][0] = name.getDomain();
            nameArr[count][1] = name.getCanonicalName();
            count++;
        }

        return nameArr;

    } catch (MalformedURLException e) {
        log.error(e);
        throw e;
    } catch (IOException e) {
        log.error(e);
        throw e;
    } finally {
        if (jmxc != null) {
            jmxc.close();
        }
    }
}

From source file:org.hyperic.hq.plugin.jboss.jmx.ServerQuery.java

private void findServices(MBeanServerConnection mServer, ServiceQuery query) throws PluginException {

    query.initialize();/*w  w  w.  j a  v a 2  s .c o m*/

    Set _services;
    ObjectName name;
    String mbeanClass = null;

    if (query instanceof GenericServiceQuery) {
        mbeanClass = ((GenericServiceQuery) query).getMBeanClass();
    }

    try {
        name = new ObjectName(query.getQueryName());
        _services = mServer.queryNames(name, null);
        log.debug("[findServices] " + name + " -> (" + mbeanClass + ") '" + _services.size() + "'");
    } catch (MalformedObjectNameException e) {
        String msg = query.getQueryName() + ": " + e.getMessage();
        throw new IllegalArgumentException(msg);
    } catch (RemoteException e) {
        throw new PluginException("Cannot connect to JBoss", e);
    } catch (IOException e) {
        throw new PluginException("Cannot connecto to JBoss", e);
    }

    for (Iterator it = _services.iterator(); it.hasNext();) {
        name = (ObjectName) it.next();
        if (checkClass(mServer, name, mbeanClass)) {
            boolean apply = query.apply(name);
            log.debug("[findServices] " + (apply ? "+" : "-") + " name='" + name + "'");
            if (apply) {

                ServiceQuery service = query.cloneInstance();
                service.setObjectName(name);

                this.services.add(service);
                service.setParent(this);
                service.setServerQuery(this);
                service.getAttributes(mServer);
            }
        }
    }
    log.debug("***********************************************");
}

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

private void discoverDynamicServices(WeblogicDiscover discover, MBeanServerConnection mServer,
        ServerQuery parent, ArrayList services, Set serviceTypes)
        throws PluginException, WeblogicDiscoverException {
    try {/* w  w  w . jav a2  s  .  co m*/
        final Set objectNames = mServer.queryNames(new ObjectName(MBeanUtil.DYNAMIC_SERVICE_DOMAIN + ":*"),
                null);
        //Only WebLogic Admin servers have auto-inventory plugins - have to construct a ServerInfo for the WebLogic server
        String[] platformTypes = ((ServerTypeInfo) plugin.getTypeInfo()).getPlatformTypes();
        ServerTypeInfo server = new ServerTypeInfo(parent.getResourceType(), parent.getDescription(),
                parent.getVersion());
        server.setValidPlatformTypes(platformTypes);
        for (Iterator iterator = objectNames.iterator(); iterator.hasNext();) {
            final ObjectName objectName = (ObjectName) iterator.next();
            final MBeanInfo serviceInfo = mServer.getMBeanInfo(objectName);
            if (serviceInfo instanceof ModelMBeanInfo) {
                ServiceType identityType = serviceTypeFactory.getServiceType(
                        plugin.getProductPlugin().getName(), server, (ModelMBeanInfo) serviceInfo, objectName);
                //identityType could be null if MBean is not to be exported
                if (identityType != null) {
                    ServiceType serviceType;
                    if (!serviceTypes.contains(identityType)) {
                        serviceType = serviceTypeFactory.create(plugin.getProductPlugin(), server,
                                (ModelMBeanInfo) serviceInfo, objectName);
                        if (serviceType != null) {
                            serviceTypes.add(serviceType);
                        }
                    } else {
                        serviceType = findServiceType(identityType.getInfo().getName(), serviceTypes);
                    }
                    final String shortServiceType = identityType.getServiceName();
                    DynamicServiceQuery dynamicServiceQuery = new DynamicServiceQuery();
                    dynamicServiceQuery.setParent(parent);
                    dynamicServiceQuery.setType(shortServiceType);
                    dynamicServiceQuery.setAttributeNames(serviceType.getCustomProperties().getOptionNames());
                    dynamicServiceQuery.setName(objectName.getKeyProperty("name"));
                    dynamicServiceQuery.getDynamicAttributes(mServer, objectName);
                    services.add(dynamicServiceQuery);
                }
            }
        }
    } catch (Exception e) {
        throw new PluginException(e.getMessage(), e);
    }
}

From source file:org.hyperic.hq.product.jmx.MxLiveDataPlugin.java

private Object queryMBeans(String pattern, Properties props) throws PluginException {

    MBeanServerConnection mServer;
    try {/*  w  w w  .j  ava2  s.c om*/
        mServer = MxUtil.getMBeanServer(props);
    } catch (Exception e) {
        throw new PluginException("getMBeanServer(" + props.getProperty(MxUtil.PROP_JMX_URL) + "): " + e, e);
    }
    ObjectName query;
    try {
        query = new ObjectName(pattern);
    } catch (Exception e) {
        throw new PluginException("Invalid query '" + pattern + "': " + e);
    }
    Map res = new HashMap();
    try {
        Iterator beans = mServer.queryNames(query, null).iterator();
        while (beans.hasNext()) {
            ObjectName obj = (ObjectName) beans.next();
            Map bean = new HashMap();
            Map attrs = new LinkedHashMap();
            bean.put(PROP_ATTRIBUTE + "s", attrs);
            res.put(obj.toString(), bean);

            MBeanInfo info = mServer.getMBeanInfo(obj);
            MBeanAttributeInfo[] attrInfo = info.getAttributes();
            for (int i = 0; i < attrInfo.length; i++) {
                MBeanAttributeInfo mia = attrInfo[i];
                String name = mia.getName();
                Map attr = new HashMap();
                Object val;
                try {
                    val = mServer.getAttribute(obj, name);
                } catch (Exception e) {
                    continue; //XXX
                }

                if (val == null) {
                    val = "-";
                }
                attr.put("Value", val);
                attr.put("Description", mia.getDescription());
                attr.put("isWritable", new Boolean(mia.isWritable()));
                attrs.put(name, attr);
            }

            bean.put(PROP_METHOD + "s", info.getOperations());
        }
    } catch (Exception e) {
        throw new PluginException("Error in query '" + pattern + "': " + e, e);
    }

    return res;
}

From source file:com.spotify.reaper.cassandra.JmxProxy.java

public ColumnFamilyStoreMBeanIterator(MBeanServerConnection mbeanServerConn)
        throws MalformedObjectNameException, NullPointerException, IOException {
    ObjectName query = new ObjectName("org.apache.cassandra.db:type=ColumnFamilies,*");
    resIter = mbeanServerConn.queryNames(query, null).iterator();
    this.mbeanServerConn = mbeanServerConn;
}

From source file:org.hyperic.hq.plugin.jboss.JBossDetector.java

@Override
public Set discoverServiceTypes(ConfigResponse serverConfig) throws PluginException {
    Set serviceTypes = new HashSet();

    MBeanServerConnection mServer;

    try {//from   w w  w . j av  a  2 s.  co  m
        mServer = JBossUtil.getMBeanServerConnection(serverConfig.toProperties());
    } catch (Exception e) {
        throw new PluginException(e.getMessage(), e);
    }

    try {
        final Set<ObjectName> objectNames = mServer.queryNames(
                new ObjectName(org.hyperic.hq.product.jmx.MBeanUtil.DYNAMIC_SERVICE_DOMAIN + ":*"), null);
        //TODO have to instantiate here due to classloading issues with MBeanServerConnectionConnection in 1.4 agent.  Can make an instance variable when agent can be 1.5 compliant.
        ServiceTypeFactory serviceTypeFactory = new ServiceTypeFactory();
        serviceTypes = serviceTypeFactory.create(getProductPlugin(), (ServerTypeInfo) getTypeInfo(), mServer,
                objectNames);
    } catch (Exception e) {
        throw new PluginException(e.getMessage(), e);
    }
    return serviceTypes;
}

From source file:VerboseGC.java

/**
 * Constructs a PrintGCStat object to monitor a remote JVM.
 *///from w  w w . ja v  a  2 s. c om
public PrintGCStat(MBeanServerConnection server) throws IOException {
    // Create the platform mxbean proxies
    this.rmbean = newPlatformMXBeanProxy(server, RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
    this.mmbean = newPlatformMXBeanProxy(server, MEMORY_MXBEAN_NAME, MemoryMXBean.class);
    ObjectName poolName = null;
    ObjectName gcName = null;
    try {
        poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*");
        gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*");
    } catch (MalformedObjectNameException e) {
        // should not reach here
        assert (false);
    }

    Set mbeans = server.queryNames(poolName, null);
    if (mbeans != null) {
        pools = new ArrayList<MemoryPoolMXBean>();
        Iterator iterator = mbeans.iterator();
        while (iterator.hasNext()) {
            ObjectName objName = (ObjectName) iterator.next();
            MemoryPoolMXBean p = newPlatformMXBeanProxy(server, objName.getCanonicalName(),
                    MemoryPoolMXBean.class);
            pools.add(p);
        }
    }

    mbeans = server.queryNames(gcName, null);
    if (mbeans != null) {
        gcmbeans = new ArrayList<GarbageCollectorMXBean>();
        Iterator iterator = mbeans.iterator();
        while (iterator.hasNext()) {
            ObjectName objName = (ObjectName) iterator.next();
            GarbageCollectorMXBean gc = newPlatformMXBeanProxy(server, objName.getCanonicalName(),
                    GarbageCollectorMXBean.class);
            gcmbeans.add(gc);
        }
    }
}

From source file:org.hyperic.hq.product.jmx.MxServerQuery.java

private void findServices(MBeanServerConnection mServer, MxServiceQuery query) throws PluginException {

    boolean isDebug = log.isDebugEnabled();

    query.initialize();//from ww  w . ja  va2  s  .com

    Set services;
    ObjectName name;
    String mbeanClass = query.getMBeanClass();

    String filter = query.getObjectNameFilter();
    StringMatcher matcher = null;
    if (filter != null) {
        matcher = new StringMatcher();
        if (filter.charAt(0) == '!') {
            matcher.setExcludes(filter.substring(1));
        } else {
            matcher.setIncludes(filter);
        }
    }

    try {
        name = new ObjectName(query.getQueryName());
        log.debug("[findServices] name=" + name);
        services = mServer.queryNames(name, null);
        log.debug("[findServices] services=(" + services.size() + ")" + services);
    } catch (MalformedObjectNameException e) {
        String msg = query.getQueryName() + ": " + e.getMessage();
        throw new IllegalArgumentException(msg);
    } catch (RemoteException e) {
        throw new PluginException("Cannot connect to server", e);
    } catch (IOException e) {
        throw new PluginException("Cannot connect to server", e);
    }

    for (Iterator it = services.iterator(); it.hasNext();) {
        name = (ObjectName) it.next();

        if ((matcher != null) && !matcher.matches(name.toString())) {
            if (isDebug) {
                log.debug("[" + name + "] !matches(" + matcher + ")");
            }
            continue;
        }

        if (!query.apply(name)) {
            continue;
        }

        if (mbeanClass != null) {
            try {
                MBeanInfo info = mServer.getMBeanInfo(name);
                if (!info.getClassName().matches(mbeanClass)) {
                    if (isDebug) {
                        log.debug("[" + name + "] " + info.getClassName() + " !instanceof " + mbeanClass);
                    }
                    continue;
                }
            } catch (Exception e) {
                log.error("mServer.getMBeanInfo(" + name + "): " + e);
                continue;
            }
        }

        MxServiceQuery service = query.cloneInstance();
        service.setObjectName(name);

        this.services.add(service);
        service.setParent(this);
        service.setServerQuery(this);
        service.getAttributes(mServer);
    }
}

From source file:com.betfair.testing.utils.cougar.helpers.CougarHelpers.java

public boolean makeServerConnection(JMXConnector jmxConnector)
        throws IOException, MBeanException, AttributeNotFoundException, InstanceNotFoundException,
        ReflectionException, MalformedObjectNameException {

    MBeanServerConnection mBeanServerConnection = jmxConnector.getMBeanServerConnection();
    Set<ObjectName> mbeans = mBeanServerConnection.queryNames(new ObjectName("CoUGAR:name=healthChecker,*"),
            null);/* w  w  w. j av  a 2  s.c  om*/
    if (!mbeans.isEmpty()) {
        mBeanServerConnection.getAttribute(mbeans.iterator().next(), "SystemInService");
        return true;
    }
    return false;
}