Java MBean getMBeanAttributeMap(MBeanServerConnection server, ObjectName objectName, String delimeter, String... attributeNames)

Here you can find the source of getMBeanAttributeMap(MBeanServerConnection server, ObjectName objectName, String delimeter, String... attributeNames)

Description

Retrieves maps of attribute values keyed by attribute name, in turn keyed by the ObjectName of the MBean.

License

Open Source License

Parameter

Parameter Description
server An MBeanServerConnection
objectName An ObjectName which can be absolute or a wildcard.
delimeter The delimeter for composite type compound names
attributeNames An array of absolute or compound attribute names.

Return

a map of results. TODO: TabularData TODO: Collections / Maps / Arrays --> ref by index

Declaration

public static Map<ObjectName, Map<String, Object>> getMBeanAttributeMap(MBeanServerConnection server,
        ObjectName objectName, String delimeter, String... attributeNames) 

Method Source Code

//package com.java2s;
/**//from   w  ww .  ja v a  2s  .  c o m
 * Helios, OpenSource Monitoring
 * Brought to you by the Helios Development Group
 *
 * Copyright 2007, Helios Development Group and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 *
 */

import java.util.Collection;

import java.util.HashMap;

import java.util.Map;

import javax.management.openmbean.CompositeData;

public class Main {
    /**
     * Retrieves maps of attribute values keyed by attribute name, in turn keyed by the ObjectName of the MBean.
     * @param server An MBeanServerConnection
     * @param objectName An ObjectName which can be absolute or a wildcard.
     * @param delimeter The delimeter for composite type compound names
     * @param attributeNames An array of absolute or compound attribute names.
     * @return a map of results.
     * TODO: TabularData
     * TODO: Collections / Maps / Arrays --> ref by index
     */
    public static Map<ObjectName, Map<String, Object>> getMBeanAttributeMap(MBeanServerConnection server,
            ObjectName objectName, String delimeter, String... attributeNames) {
        if (server == null)
            throw new RuntimeException("MBeanServerConnection was null", new Throwable());
        if (objectName == null)
            throw new RuntimeException("ObjectName was null", new Throwable());
        if (attributeNames == null || attributeNames.length < 1)
            throw new RuntimeException("Attribute names array was null or zero length", new Throwable());
        String[] rootNames = new String[attributeNames.length];
        Map<String, String> compoundNames = new HashMap<String, String>();
        for (int i = 0; i < attributeNames.length; i++) {
            String rootKey = null;
            if (attributeNames[i].contains(delimeter)) {
                String[] fragments = attributeNames[i].split(Pattern.quote(delimeter));
                rootKey = fragments[0];
                compoundNames.put(rootKey, attributeNames[i]);
            } else {
                rootKey = attributeNames[i];
            }
            rootNames[i] = rootKey;
        }
        Map<ObjectName, Map<String, Object>> map = new HashMap<ObjectName, Map<String, Object>>();
        try {
            for (ObjectName on : server.queryNames(objectName, null)) {
                AttributeList attrs = null;
                try {
                    attrs = server.getAttributes(on, rootNames);
                    if (attrs.size() < 1)
                        continue;
                } catch (Exception e) {
                    continue;
                }
                Map<String, Object> attrMap = new HashMap<String, Object>();
                map.put(on, attrMap);
                for (Attribute attr : attrs.asList()) {
                    Object value = attr.getValue();
                    if (value == null)
                        continue;
                    String name = attr.getName();
                    if (value instanceof CompositeData && compoundNames.containsKey(name)) {
                        try {
                            name = compoundNames.get(name);
                            value = extractCompositeData((CompositeData) value, delimeter, name);
                        } catch (Exception e) {
                            continue;
                        }
                    }
                    attrMap.put(name, value);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to acquire attribute names for ObjectName [" + objectName
                    + "] for MBeanServer [" + server + "]", e);
        }

        return map;
    }

    /**
     * Retrieves maps of attribute values keyed by attribute name, in turn keyed by the ObjectName of the MBean.
     * @param server An MBeanServerConnection
     * @param objectName An ObjectName which can be absolute or a wildcard.
     * @param delimeter The delimeter for composite type compound names
     * @param attributeNames An collection of absolute or compound attribute names.
     * @return a map of results.
     */
    public static Map<ObjectName, Map<String, Object>> getMBeanAttributeMap(MBeanServerConnection server,
            ObjectName objectName, String delimeter, Collection<String> attributeNames) {
        if (attributeNames == null || attributeNames.size() < 1)
            throw new RuntimeException("Attribute names collection was null or zero size", new Throwable());
        return getMBeanAttributeMap(server, objectName, delimeter,
                attributeNames.toArray(new String[attributeNames.size()]));
    }

    /**
     * Returns a String->Object Map of the named attributes from the Mbean.
     * @param on The object name of the MBean.
     * @param server The MBeanServerConnection the MBean is registered in.
     * @param attributes An array of attribute names to retrieve.
     * @return A name value map of the requested attributes.
     */
    public static Map<String, Object> getAttributes(ObjectName on, MBeanServerConnection server,
            String... attributes) {
        try {
            Map<String, Object> attrs = new HashMap<String, Object>(attributes.length);
            AttributeList attributeList = server.getAttributes(on, attributes);

            for (int i = 0; i < attributeList.size(); i++) {
                Attribute at = (Attribute) attributeList.get(i);
                if (isIn(at.getName(), attributes)) {
                    attrs.put(at.getName(), at.getValue());
                }
            }
            return attrs;
        } catch (Exception e) {
            throw new RuntimeException("Failed to getAttributes on [" + on + "]", e);
        }
    }

    /**
     * Extracts a composite data field from a CompositeData instance using a compound name.
     * @param cd The composite data instance
     * @param name The compound attribute name
     */
    public static Object extractCompositeData(final CompositeData cd, final String delimeter, final String name) {
        String[] fragments = name.split(Pattern.quote(delimeter));
        CompositeData ref = cd;
        Object value = null;
        for (int i = 1; i < fragments.length; i++) {
            value = ref.get(fragments[i]);
            if (value instanceof CompositeData) {
                ref = (CompositeData) value;
            } else {
                break;
            }
        }
        return value;
    }

    /**
     * Inspects the array to see if it contains the passed string.
     * @param name
     * @param array
     * @return true if the array contains the passed string.
     */
    public static boolean isIn(String name, String[] array) {
        if (array == null || name == null)
            return false;
        for (String s : array) {
            if (s.equals(name))
                return true;
        }
        return false;

    }
}

Related

  1. getDescription(MBeanFeatureInfo element)
  2. getDynamicMBean(String mbeanName)
  3. getJmxNameInternal(final Class mBeanClass, final String name)
  4. getMatchingObjectNames(CharSequence wildcardEq, CharSequence wildcardWc, MBeanServerConnection conn)
  5. getMBeanAttribute(final MBeanServerConnection connection, final String nameString, final String attributeName)
  6. getMBeanAttributes(final MBeanServer mBeanServer, final String objectNameString)
  7. getMBeanAttributeValue(final MBeanServerConnection connection, final ObjectName objName, final String attributeName)
  8. getMBeanConnections(final Map connectors)
  9. getMBeanName(final String serviceName, final String nameName)