Example usage for javax.management.openmbean CompositeData values

List of usage examples for javax.management.openmbean CompositeData values

Introduction

In this page you can find the example usage for javax.management.openmbean CompositeData values.

Prototype

public Collection<?> values();

Source Link

Document

Returns an unmodifiable Collection view of the item values contained in this CompositeData instance.

Usage

From source file:com.zenoss.jmx.ValueExtractor.java

/**
 * Traverses a TabularData or CompositeData structure to return nested data
 * /*from   w w  w .j  a va2 s . c om*/
 * e.g. To get the the used perm gen memory before last garbage collection
 * value from the result of the lastGcInfo attribute of the
 * java.lang:type=GarbageCollector,name=Copy mbean the path used would be
 * 
 * memoryUsageBeforeGc.[Perm Gen].used
 * 
 * In general the non bracketed values are keys into CompositeData and the
 * bracketed values are indexes into TabularData. For TabularData indexes
 * with more than one value a comma separated list without spaces should be
 * used, spaces are treated as part of the values in the index array.
 * 
 * e.g. [key1,key2]
 * 
 * The brackets aren't mandatory for indexes but are included for clarity.
 * 
 * Curly brackets can be used after a table index to specify a column name.
 * 
 * e.g memoryUsageBeforeGc.[Perm Gen].{value}.used
 * 
 * The column name is only necessary when the table has more than two
 * columns. If the table has two columns then the value is read from the
 * column not used for the index.
 * 
 * @param obj
 *            TabularData, CompositeData, or Map
 * @param path
 *            dot separated string that represents a path through the object
 * @return Object the value at the end of the path
 * @throws JmxException
 *             if a path element doesn't exist
 */
public static Object getDataValue(final Object obj, String path) throws JmxException {
    if (!(obj instanceof TabularData) && !(obj instanceof CompositeData) && !(obj instanceof Map)) {

        throw new IllegalArgumentException("Cannot process object of type " + obj.getClass().getName());

    }

    _logger.debug("getDataValue: path is " + path);

    List<String> pathList = split(path);
    _logger.debug("getDataValue: pathList " + pathList);

    Object currentObj = obj;
    Iterator<String> pathElements = pathList.iterator();
    try {
        while (pathElements.hasNext()) {
            _logger.debug("getDataValue: current object is " + obj);
            String currentKey = pathElements.next();
            pathElements.remove();

            _logger.debug("getDataValue: currentKey: " + currentKey);

            if (currentObj instanceof TabularData) {
                _logger.debug("getDataValue: dealing with tabularData");
                TabularData tData = (TabularData) currentObj;

                String[] index = createTableIndex(currentKey);
                currentObj = getDataByTableIndex(tData, index);
                CompositeData cData = (CompositeData) currentObj;

                int columnCount = cData.values().size();
                String nextKey = null;

                // look ahead and look for explicit column
                if (!pathList.isEmpty()) {
                    nextKey = pathList.get(0);
                }

                if (nextKey != null && (isColumn(nextKey) || columnCount > 2)) {

                    String columnKey = pathElements.next();
                    pathElements.remove();
                    if (isColumn(columnKey)) {
                        _logger.debug("using explicit column key " + columnKey + " for tabular data");
                        // remove first and last char - should be curly
                        // brackets
                        columnKey = columnKey.substring(1, columnKey.length() - 1);
                    } else {
                        _logger.debug(
                                "using column key " + columnKey + " for tabular data. No curly brackets found");
                    }
                    currentObj = getData(cData, columnKey);
                } else if (cData.values().size() == 2) {
                    currentObj = getTableRowData(cData, index);
                }
            } else if (currentObj instanceof CompositeData) {
                _logger.debug("getDataValue: dealing with CompositeData");
                CompositeData cData = (CompositeData) currentObj;
                currentObj = getData(cData, currentKey);
            } else if (currentObj instanceof Map) {
                _logger.debug("getDataValue: dealing with Map");
                Map mData = (Map) currentObj;
                currentObj = getData(mData, currentKey);
            } else {
                // we still have a path but the object isn't composite or
                // tabluar
                String remainingPath = currentKey;
                for (String val : pathList) {
                    remainingPath += ".";
                    remainingPath += val;

                }
                _logger.warn(
                        "getDataValue: we still have a path but the " + "object isn't composite or tabluar");
                _logger.warn("getDataValue: remaining path is " + remainingPath);
                throw new JmxException("we still have a path but the "
                        + "object isn't composite or tabluar, remaining " + "" + "path is " + remainingPath);

            }

        }
    } catch (Exception e) {
        _logger.warn("could not get object for path " + path, e);
        throw new JmxException("could not get object for path " + path + "; " + e.getMessage(), e);
    }
    return currentObj;
}

From source file:com.zenoss.jmx.ValueExtractor.java

private static Object getTableRowData(CompositeData cData, String[] index) throws JmxException {
    Object result = null;//from w w w  .  ja va 2 s .  c om
    // This gets the data with a key not in index
    Set<String> keys = new HashSet<String>(Arrays.asList(index));

    for (String key : keys) {
        if (!cData.values().contains(key)) {
            _logger.warn(key + " not found in composite data row for tabular data");
            throw new JmxException(key + " not found in composite data row for tabular data");

        }
    }

    // find the first value that isn't a part of the index
    for (Object value : cData.values()) {
        if (!keys.contains(value)) {
            result = value;
        }
    }
    return result;
}

From source file:org.jolokia.converter.object.StringToOpenTypeConverterTest.java

@Test
public void compositeType() throws OpenDataException, AttributeNotFoundException, ParseException {
    CompositeTypeAndJson taj = new CompositeTypeAndJson(STRING, "verein", "FCN", INTEGER, "platz", 6, STRING,
            "trainer", null, BOOLEAN, "absteiger", false);
    for (Object input : new Object[] { taj.getJson(), taj.getJsonAsString() }) {
        CompositeData result = (CompositeData) converter.convertToObject(taj.getType(), input);
        assertEquals(result.get("verein"), "FCN");
        assertEquals(result.get("trainer"), null);
        assertEquals(result.get("platz"), 6);
        assertEquals(result.get("absteiger"), false);
        assertEquals(result.values().size(), 4);
    }//from  w  ww. j  av a  2  s  .com
}