Example usage for javax.management.openmbean CompositeData getCompositeType

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

Introduction

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

Prototype

public CompositeType getCompositeType();

Source Link

Document

Returns the composite type of this composite data instance.

Usage

From source file:org.apache.hadoop.jmx.JMXJsonServlet.java

private void writeObject(JsonGenerator jg, Object value) throws IOException {
    if (value == null) {
        jg.writeNull();/* w ww . ja  v  a  2  s .  c  o m*/
    } else {
        Class<?> c = value.getClass();
        if (c.isArray()) {
            jg.writeStartArray();
            int len = Array.getLength(value);
            for (int j = 0; j < len; j++) {
                Object item = Array.get(value, j);
                writeObject(jg, item);
            }
            jg.writeEndArray();
        } else if (value instanceof Number) {
            Number n = (Number) value;
            jg.writeNumber(n.toString());
        } else if (value instanceof Boolean) {
            Boolean b = (Boolean) value;
            jg.writeBoolean(b);
        } else if (value instanceof CompositeData) {
            CompositeData cds = (CompositeData) value;
            CompositeType comp = cds.getCompositeType();
            Set<String> keys = comp.keySet();
            jg.writeStartObject();
            for (String key : keys) {
                writeAttribute(jg, key, cds.get(key));
            }
            jg.writeEndObject();
        } else if (value instanceof TabularData) {
            TabularData tds = (TabularData) value;
            jg.writeStartArray();
            for (Object entry : tds.values()) {
                writeObject(jg, entry);
            }
            jg.writeEndArray();
        } else {
            jg.writeString(value.toString());
        }
    }
}

From source file:com.streamsets.datacollector.http.JMXJsonServlet.java

private void writeObject(JsonGenerator jg, Object value) throws IOException {
    if (value == null) {
        jg.writeNull();/*  ww  w .ja  v a2 s  .c o  m*/
    } else {
        Class<?> c = value.getClass();
        if (c.isArray()) {
            jg.writeStartArray();
            int len = Array.getLength(value);
            for (int j = 0; j < len; j++) {
                Object item = Array.get(value, j);
                writeObject(jg, item);
            }
            jg.writeEndArray();
        } else if (value instanceof Number) {
            Number n = (Number) value;
            jg.writeNumber(n.toString());
        } else if (value instanceof Boolean) {
            Boolean b = (Boolean) value;
            jg.writeBoolean(b);
        } else if (value instanceof CompositeData) {
            CompositeData cds = (CompositeData) value;
            CompositeType comp = cds.getCompositeType();
            Set<String> keys = comp.keySet();
            jg.writeStartObject();
            for (String key : keys) {
                writeAttribute(jg, key, cds.get(key));
            }
            jg.writeEndObject();
        } else if (value instanceof TabularData) {
            TabularData tds = (TabularData) value;
            jg.writeStartArray();
            for (Object entry : tds.values()) {
                writeObject(jg, entry);
            }
            jg.writeEndArray();
        } else if (value instanceof GaugeValue) {
            ((GaugeValue) value).serialize(jg);
        } else {
            jg.writeString(value.toString());
        }
    }
}

From source file:com.zabbix.gateway.JMXItemChecker.java

private void findPrimitiveAttributes(JSONArray counters, ObjectName name, String descr, String attrPath,
        Object attribute) throws JSONException {
    logger.trace("drilling down with attribute path '{}'", attrPath);

    if (isPrimitiveAttributeType(attribute.getClass())) {
        logger.trace("found attribute of a primitive type: {}", attribute.getClass());

        JSONObject counter = new JSONObject();

        counter.put("{#JMXDESC}", null == descr ? name + "," + attrPath : descr);
        counter.put("{#JMXOBJ}", name);
        counter.put("{#JMXATTR}", attrPath);
        counter.put("{#JMXTYPE}", attribute.getClass().getName());
        counter.put("{#JMXVALUE}", attribute.toString());

        counters.put(counter);//from   www .j  a v a 2s  .c o m
    } else if (attribute instanceof CompositeData) {
        logger.trace("found attribute of a composite type: {}", attribute.getClass());

        CompositeData comp = (CompositeData) attribute;

        for (String key : comp.getCompositeType().keySet())
            findPrimitiveAttributes(counters, name, descr, attrPath + "." + key, comp.get(key));
    } else if (attribute instanceof TabularDataSupport) {
        logger.trace("found attribute of a known TabularDataSupport, unsupported type: {}",
                attribute.getClass());
    } else if (attribute.getClass().isArray()) {
        logger.trace("found attribute of a known, unsupported type: {}", attribute.getClass());
        Object[] array = (Object[]) attribute;
        for (Object obj : array) {
            logger.trace("Object value " + obj.toString());
            findPrimitiveAttributes(counters, name, descr, obj.toString(), obj);
        }
    } else
        logger.trace("found attribute of an unknown, unsupported type: {}", attribute.getClass());
}

From source file:com.stumbleupon.hbaseadmin.JMXQuery.java

protected StringBuffer recurseCompositeData(StringBuffer buffer, String indent, String name,
        CompositeData data) {
    indent = addNameToBuffer(buffer, indent, name);
    Iterator i = data.getCompositeType().keySet().iterator();
    while (i.hasNext()) {
        String key = (String) i.next();
        Object o = data.get(key);
        if (o instanceof CompositeData) {
            recurseCompositeData(buffer, indent + " ", key, (CompositeData) o);
        } else if (o instanceof TabularData) {
            recurseTabularData(buffer, indent, key, (TabularData) o);
        } else {//from  w  w w . ja v  a 2  s  . c  o m
            buffer.append(indent);
            buffer.append(key);
            buffer.append(": ");
            buffer.append(o);
            buffer.append("\n");
        }
    }
    return buffer;
}

From source file:org.wso2.andes.management.ui.views.ViewUtility.java

/**
 * Populates the given composite with the CompositeData. Creates required widgets to hold the data types
 * @param toolkit/*from w  ww .  ja  v  a 2  s.c o m*/
 * @param parent
 * @param data CompositeData
 */
@SuppressWarnings("unchecked")
private static void populateCompositeWithCompositeData(FormToolkit toolkit, Composite parent,
        CompositeData data) {
    Control[] oldControls = parent.getChildren();
    for (int i = 0; i < oldControls.length; i++) {
        oldControls[i].dispose();
    }

    Composite compositeHolder = toolkit.createComposite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(4, false);
    layout.horizontalSpacing = 10;
    compositeHolder.setLayout(layout);
    compositeHolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    // ItemNames in composite data
    List<String> itemNames = new ArrayList<String>(data.getCompositeType().keySet());

    for (String itemName : itemNames) {
        OpenType itemType = data.getCompositeType().getType(itemName);
        Label keyLabel = toolkit.createLabel(compositeHolder, itemName, SWT.TRAIL);
        GridData layoutData = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
        layoutData.minimumWidth = 70;
        keyLabel.setLayoutData(layoutData);

        if (itemType.isArray()) {
            OpenType type = ((ArrayType) itemType).getElementOpenType();
            //  If Byte array and mimetype is text, convert to text string
            if (type.getClassName().equals(Byte.class.getName())) {
                String mimeType = null;
                String encoding = null;
                if (data.containsKey("MimeType")) {
                    mimeType = (String) data.get("MimeType");
                }
                if (data.containsKey("Encoding")) {
                    encoding = (String) data.get("Encoding");
                }

                if (encoding == null || encoding.length() == 0) {
                    encoding = Charset.defaultCharset().name();
                }

                if ("text/plain".equals(mimeType)) {
                    convertByteArray(toolkit, compositeHolder, data, itemName, encoding);
                } else {
                    handleBinaryMessageContent(toolkit, compositeHolder, data, itemName, encoding);
                }
            }
            // If array of any other supported type, show as a list of String array
            else if (SUPPORTED_ARRAY_DATATYPES.contains(type.getClassName())) {
                convertArrayItemForDisplay(compositeHolder, data, itemName);
            } else {
                setNotSupportedDataType(toolkit, compositeHolder);
            }
        } else if (itemType instanceof TabularType) {
            Composite composite = toolkit.createComposite(compositeHolder, SWT.NONE);
            composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 3, 1));
            layout = new GridLayout();
            layout.marginHeight = 0;
            layout.marginWidth = 0;
            composite.setLayout(layout);
            createTabularDataHolder(toolkit, composite, (TabularDataSupport) data.get(itemName));
        } else {
            Text valueText = toolkit.createText(compositeHolder, String.valueOf(data.get(itemName)),
                    SWT.READ_ONLY | SWT.BORDER);
            valueText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 3, 1));
        }
    }

    // layout the composite after creating new widgets.
    parent.layout();
}

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

/**
 * @param mBean    : The name of the MBean
 * @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 set of attributes in a MBean
 * @throws MalformedObjectNameException//from w  w w.ja  v a  2s.c o  m
 * @throws IntrospectionException
 * @throws InstanceNotFoundException
 * @throws IOException
 * @throws ReflectionException
 */
public String[][] getMBeanAttributeInfo(String mBean, String url, String userName, String password)
        throws MalformedObjectNameException, IntrospectionException, InstanceNotFoundException, IOException,
        ReflectionException {

    JMXConnector jmxc = getJmxConnector(url, userName, password);

    MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
    ObjectName mBeanName = new ObjectName(mBean);

    MBeanAttributeInfo[] attrs = mbsc.getMBeanInfo(mBeanName).getAttributes();

    ArrayList<String[]> strAttrs = new ArrayList<String[]>();

    for (MBeanAttributeInfo info : attrs) {

        //check the suitability of the attribute
        try {
            Object result = mbsc.getAttribute(mBeanName, info.getName());

            //if this is an instance of a primary data type supported by cassandra
            if (result instanceof String || result instanceof Integer || result instanceof Double
                    || result instanceof Long || result instanceof Boolean || result instanceof Float) {
                strAttrs.add(new String[] { info.getName() });
            }

            //if this is a composite data type
            if (result instanceof CompositeData) {
                CompositeData cd = (CompositeData) result;
                ArrayList<String> keys = new ArrayList<String>();
                //add the attribute name
                keys.add(info.getName());
                for (String key : cd.getCompositeType().keySet()) {
                    //check whether the key returns a primary data type
                    Object attrValue = cd.get(key);
                    if (attrValue instanceof String || attrValue instanceof Integer
                            || attrValue instanceof Double || attrValue instanceof Long
                            || attrValue instanceof Boolean || attrValue instanceof Float) {
                        keys.add(key);
                    }
                }
                //if this composite data object has keys which returns attributes with
                // primary data types
                if (keys.size() > 1) {
                    strAttrs.add(keys.toArray(new String[keys.size()]));
                }
            }
        } catch (MBeanException e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());
        } catch (AttributeNotFoundException e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());
        } catch (UnmarshalException e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());
        } catch (RuntimeOperationsException e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());

        } catch (RuntimeMBeanException e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());
        } catch (ReflectionException e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());
        } catch (Exception e) {
            log.error("Removed the attribute " + info.getName() + " of " + mBean + " from the UI list due to: "
                    + e.getMessage());
        }
    }

    //close the connection
    jmxc.close();

    return strAttrs.toArray(new String[strAttrs.size()][]);
}

From source file:com.appleframework.jmx.core.services.MBeanServiceImpl.java

private CompositeType getCompositeType(ServerConnection connection, ObjectName objectName,
        ObjectAttributeInfo attrInfo) {//from   w ww  .j  ava  2  s  .c o  m
    CompositeData compositeData = (CompositeData) connection.getAttribute(objectName, attrInfo.getName());
    return compositeData.getCompositeType();
}

From source file:org.opennms.tools.jmxconfiggenerator.jmxconfig.JmxDatacollectionConfiggenerator.java

private CompAttrib createCompAttrib(MBeanServerConnection jmxServerConnection, ObjectInstance jmxObjectInstance,
        MBeanAttributeInfo jmxMBeanAttributeInfo) {
    Boolean contentAdded = false;

    CompAttrib xmlCompAttrib = xmlObjectFactory.createCompAttrib();
    xmlCompAttrib.setName(jmxMBeanAttributeInfo.getName());
    xmlCompAttrib.setType("Composite");
    xmlCompAttrib.setAlias(jmxMBeanAttributeInfo.getName());

    CompositeData compositeData;
    try {/*from w  ww  . ja va 2  s . c om*/
        logger.debug("Try to get composite data");
        compositeData = (CompositeData) jmxServerConnection.getAttribute(jmxObjectInstance.getObjectName(),
                jmxMBeanAttributeInfo.getName());
        if (compositeData == null)
            logger.warn(
                    "compositeData is null. jmxObjectInstance.getObjectName: '{}', jmxMBeanAttributeInfo.getName: '{}'");
        if (compositeData != null) {
            logger.debug("compositeData.getCompositeType: '{}'", compositeData.getCompositeType());
            Set<String> keys = compositeData.getCompositeType().keySet();
            for (String key : keys) {
                Object compositeEntry = compositeData.get(key);
                if (numbers.contains(compositeEntry.getClass().getName())) {
                    contentAdded = true;
                    CompMember xmlCompMember = xmlObjectFactory.createCompMember();
                    xmlCompMember.setName(key);

                    logger.debug("composite member pure alias: '{}'",
                            jmxMBeanAttributeInfo.getName() + StringUtils.capitalize(key));
                    String alias = NameTools
                            .trimByDictionary(jmxMBeanAttributeInfo.getName() + StringUtils.capitalize(key));
                    alias = createAndRegisterUniceAlias(alias);
                    xmlCompMember.setAlias(alias);
                    logger.debug("composite member trimmed alias: '{}'", alias);

                    xmlCompMember.setType("gauge");
                    xmlCompAttrib.getCompMember().add(xmlCompMember);

                } else {
                    logger.debug("composite member key '{}' object's class '{}' was not a number.", key,
                            compositeEntry.getClass().getName());
                }
            }
        }
    } catch (Exception e) {
        logger.error("killed in action: '{}'", e.getMessage());
    }

    if (contentAdded) {
        logger.debug("xmlCompAttrib returned by createCompAttrib it's '{}'", xmlCompAttrib);
        return xmlCompAttrib;
    }
    return null;
}

From source file:org.opennms.features.jmxconfiggenerator.jmxconfig.JmxDatacollectionConfiggenerator.java

private CompAttrib createCompAttrib(MBeanServerConnection jmxServerConnection, ObjectInstance jmxObjectInstance,
        MBeanAttributeInfo jmxMBeanAttributeInfo) {
    Boolean contentAdded = false;

    CompAttrib xmlCompAttrib = xmlObjectFactory.createCompAttrib();
    xmlCompAttrib.setName(jmxMBeanAttributeInfo.getName());
    xmlCompAttrib.setType("Composite");
    xmlCompAttrib.setAlias(jmxMBeanAttributeInfo.getName());

    CompositeData compositeData;
    try {//from  w w w .j av  a2  s .c o m
        logger.debug("Try to get composite data");
        compositeData = (CompositeData) jmxServerConnection.getAttribute(jmxObjectInstance.getObjectName(),
                jmxMBeanAttributeInfo.getName());
        if (compositeData == null) {
            logger.warn(
                    "compositeData is null. jmxObjectInstance.getObjectName: '{}', jmxMBeanAttributeInfo.getName: '{}'");
        }
        if (compositeData != null) {
            logger.debug("compositeData.getCompositeType: '{}'", compositeData.getCompositeType());
            Set<String> keys = compositeData.getCompositeType().keySet();
            for (String key : keys) {
                Object compositeEntry = compositeData.get(key);
                if (numbers.contains(compositeEntry.getClass().getName())) {
                    contentAdded = true;
                    CompMember xmlCompMember = xmlObjectFactory.createCompMember();
                    xmlCompMember.setName(key);

                    logger.debug("composite member pure alias: '{}'",
                            jmxMBeanAttributeInfo.getName() + StringUtils.capitalize(key));
                    String alias = nameCutter
                            .trimByDictionary(jmxMBeanAttributeInfo.getName() + StringUtils.capitalize(key));
                    alias = createAndRegisterUniqueAlias(alias);
                    xmlCompMember.setAlias(alias);
                    logger.debug("composite member trimmed alias: '{}'", alias);

                    xmlCompMember.setType("gauge");
                    xmlCompAttrib.getCompMember().add(xmlCompMember);

                } else {
                    logger.debug("composite member key '{}' object's class '{}' was not a number.", key,
                            compositeEntry.getClass().getName());
                }
            }
        }
    } catch (Exception e) {
        logger.error("killed in action: '{}'", e.getMessage());
    }

    if (contentAdded) {
        logger.debug("xmlCompAttrib returned by createCompAttrib it's '{}'", xmlCompAttrib);
        return xmlCompAttrib;
    }
    return null;
}

From source file:com.heliosapm.opentsdb.TSDBSubmitterImpl.java

/**
 * Decoposes the passed composite data instance to a map of numeric values keyed by the composite type key
 * @param cd The composite data instance
 * @return a map of numeric values keyed by the composite type key
 *///w w w .  j  ava 2  s  .  com
protected Map<String, Number> fromOpenType(final CompositeData cd) {
    if (cd == null)
        return Collections.emptyMap();
    final Map<String, Number> map = new LinkedHashMap<String, Number>();
    final CompositeType ct = cd.getCompositeType();
    for (final String key : ct.keySet()) {
        final Object value = cd.get(key);
        if (value != null && (value instanceof Number)) {
            map.put(key, (Number) value);
        }
    }
    return map;
}