Example usage for javax.management MBeanInfo getAttributes

List of usage examples for javax.management MBeanInfo getAttributes

Introduction

In this page you can find the example usage for javax.management MBeanInfo getAttributes.

Prototype

public MBeanAttributeInfo[] getAttributes() 

Source Link

Document

Returns the list of attributes exposed for management.

Usage

From source file:org.apache.hadoop.hbase.metrics.MetricsMBeanBase.java

protected void init() {
    List<MBeanAttributeInfo> attributes = new ArrayList<MBeanAttributeInfo>();
    MBeanInfo parentInfo = super.getMBeanInfo();
    List<String> parentAttributes = new ArrayList<String>();
    for (MBeanAttributeInfo attr : parentInfo.getAttributes()) {
        attributes.add(attr);//  w ww. j  a v a2  s.c o m
        parentAttributes.add(attr.getName());
    }

    this.registryLength = this.registry.getMetricsList().size();

    for (MetricsBase metric : this.registry.getMetricsList()) {
        if (metric.getName() == null || parentAttributes.contains(metric.getName()))
            continue;

        // add on custom HBase metric types
        if (metric instanceof MetricsRate) {
            attributes.add(new MBeanAttributeInfo(metric.getName(), "java.lang.Float", metric.getDescription(),
                    true, false, false));
            extendedAttributes.put(metric.getName(), metric);
        } else if (metric instanceof MetricsString) {
            attributes.add(new MBeanAttributeInfo(metric.getName(), "java.lang.String", metric.getDescription(),
                    true, false, false));
            extendedAttributes.put(metric.getName(), metric);
            LOG.info("MetricsString added: " + metric.getName());
        } else if (metric instanceof MetricsHistogram) {

            String metricName = metric.getName() + MetricsHistogram.NUM_OPS_METRIC_NAME;
            attributes.add(new MBeanAttributeInfo(metricName, "java.lang.Long", metric.getDescription(), true,
                    false, false));
            extendedAttributes.put(metricName, metric);

            metricName = metric.getName() + MetricsHistogram.MIN_METRIC_NAME;
            attributes.add(new MBeanAttributeInfo(metricName, "java.lang.Long", metric.getDescription(), true,
                    false, false));
            extendedAttributes.put(metricName, metric);

            metricName = metric.getName() + MetricsHistogram.MAX_METRIC_NAME;
            attributes.add(new MBeanAttributeInfo(metricName, "java.lang.Long", metric.getDescription(), true,
                    false, false));
            extendedAttributes.put(metricName, metric);

            metricName = metric.getName() + MetricsHistogram.MEAN_METRIC_NAME;
            attributes.add(new MBeanAttributeInfo(metricName, "java.lang.Float", metric.getDescription(), true,
                    false, false));
            extendedAttributes.put(metricName, metric);

            metricName = metric.getName() + MetricsHistogram.STD_DEV_METRIC_NAME;
            attributes.add(new MBeanAttributeInfo(metricName, "java.lang.Float", metric.getDescription(), true,
                    false, false));
            extendedAttributes.put(metricName, metric);

            metricName = metric.getName() + MetricsHistogram.MEDIAN_METRIC_NAME;
            attributes.add(new MBeanAttributeInfo(metricName, "java.lang.Float", metric.getDescription(), true,
                    false, false));
            extendedAttributes.put(metricName, metric);

            metricName = metric.getName() + MetricsHistogram.SEVENTY_FIFTH_PERCENTILE_METRIC_NAME;
            attributes.add(new MBeanAttributeInfo(metricName, "java.lang.Float", metric.getDescription(), true,
                    false, false));
            extendedAttributes.put(metricName, metric);

            metricName = metric.getName() + MetricsHistogram.NINETY_FIFTH_PERCENTILE_METRIC_NAME;
            attributes.add(new MBeanAttributeInfo(metricName, "java.lang.Float", metric.getDescription(), true,
                    false, false));
            extendedAttributes.put(metricName, metric);

            metricName = metric.getName() + MetricsHistogram.NINETY_NINETH_PERCENTILE_METRIC_NAME;
            attributes.add(new MBeanAttributeInfo(metricName, "java.lang.Float", metric.getDescription(), true,
                    false, false));
            extendedAttributes.put(metricName, metric);
        }
        // else, its probably a hadoop metric already registered. Skip it.
    }

    LOG.info("new MBeanInfo");
    this.extendedInfo = new MBeanInfo(this.getClass().getName(), this.description,
            attributes.toArray(new MBeanAttributeInfo[0]), parentInfo.getConstructors(),
            parentInfo.getOperations(), parentInfo.getNotifications());
}

From source file:net.sf.ehcache.management.ManagementServiceTest.java

private void traverseMBeanAttributes(MBeanServerConnection connection, String type)
        throws JMException, IOException {
    Set objectNames = connection.queryNames(new ObjectName("net.sf.ehcache:type=" + type + ",*"), null);
    for (Iterator iterator = objectNames.iterator(); iterator.hasNext();) {
        ObjectName objectName = (ObjectName) iterator.next();
        MBeanInfo mBeanInfo = connection.getMBeanInfo(objectName);
        MBeanAttributeInfo[] attributes = mBeanInfo.getAttributes();
        for (int i = 0; i < attributes.length; i++) {
            MBeanAttributeInfo attribute = attributes[i];
            LOG.info(attribute.getName() + " " + connection.getAttribute(objectName, attribute.getName()));
        }//from   w  ww . j  a  v  a  2 s.  co m
    }
}

From source file:net.sf.ehcache.management.ManagementServiceTest.java

private void traverseMBeanAttributesUsingMBeanServer(String type) throws JMException {
    Set objectNames = mBeanServer.queryNames(new ObjectName("net.sf.ehcache:type=" + type + ",*"), null);
    for (Iterator iterator = objectNames.iterator(); iterator.hasNext();) {
        ObjectName objectName = (ObjectName) iterator.next();
        MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(objectName);
        MBeanAttributeInfo[] attributes = mBeanInfo.getAttributes();
        for (int i = 0; i < attributes.length; i++) {
            MBeanAttributeInfo attribute = attributes[i];
            LOG.info(attribute.getName() + " " + mBeanServer.getAttribute(objectName, attribute.getName()));
        }//from   w  w  w . j  a  v  a2  s .  c o  m
    }
}

From source file:org.apache.hadoop.hbase.TestStochasticBalancerJmxMetrics.java

/**
 * Read the attributes from Hadoop->HBase->Master->Balancer in JMX
 * @throws IOException //from   w  w w.j  ava  2  s  .c  o m
 */
private Set<String> readJmxMetrics() throws IOException {
    JMXConnector connector = null;
    ObjectName target = null;
    MBeanServerConnection mb = null;
    try {
        connector = JMXConnectorFactory.connect(JMXListener.buildJMXServiceURL(connectorPort, connectorPort));
        mb = connector.getMBeanServerConnection();

        Hashtable<String, String> pairs = new Hashtable<>();
        pairs.put("service", "HBase");
        pairs.put("name", "Master");
        pairs.put("sub", "Balancer");
        target = new ObjectName("Hadoop", pairs);
        MBeanInfo beanInfo = mb.getMBeanInfo(target);

        Set<String> existingAttrs = new HashSet<String>();
        for (MBeanAttributeInfo attrInfo : beanInfo.getAttributes()) {
            existingAttrs.add(attrInfo.getName());
        }
        return existingAttrs;
    } catch (Exception e) {
        LOG.warn("Failed to get bean!!! " + target, e);
        if (mb != null) {
            Set<ObjectInstance> instances = mb.queryMBeans(null, null);
            Iterator<ObjectInstance> iterator = instances.iterator();
            System.out.println("MBean Found:");
            while (iterator.hasNext()) {
                ObjectInstance instance = iterator.next();
                System.out.println("Class Name: " + instance.getClassName());
                System.out.println("Object Name: " + instance.getObjectName());
            }
        }
    } finally {
        if (connector != null) {
            try {
                connector.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:org.xmatthew.spy2servers.component.spy.jmx.JmxSpySupportComponent.java

public Map<String, Object> getAttributesAsMap(String mbeanName, MBeanServerConnection mbsc, Set<String> keys)
        throws Exception {
    if (CollectionUtils.isBlankCollection(keys)) {
        return null;
    }//from www.j a va2s  . c  o  m

    ObjectName mbean = new ObjectName(mbeanName);
    MBeanInfo info = mbsc.getMBeanInfo(mbean);
    if (info != null) {
        MBeanAttributeInfo[] attributes;
        attributes = info.getAttributes();
        if (attributes == null) {
            return null;
        }

        int size = attributes.length;
        if (size == 0) {
            return null;
        }

        Map<String, Object> beansMap = new HashMap<String, Object>(keys.size());

        String name;
        for (int i = 0; i < size; i++) {
            name = attributes[i].getName();
            if (keys.contains(name)) {
                try {
                    beansMap.put(attributes[i].getName(), mbsc.getAttribute(mbean, attributes[i].getName()));
                } catch (Exception e) {
                    //ignore it
                }
            }
        }
        return beansMap;
    }
    return null;
}

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

protected void listOptions(MBeanServerConnection mbsc, ObjectInstance instance)
        throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
    final MBeanInfo info = mbsc.getMBeanInfo(instance.getObjectName());
    final MBeanAttributeInfo[] attributes = info.getAttributes();

    if (attributes.length > 0) {
        System.out.println("Attributes:");

        for (int i = 0; i < attributes.length; ++i) {
            System.out.println(' ' + attributes[i].getName() + ": " + attributes[i].getDescription() + " (type="
                    + attributes[i].getType() + ")");
        }/*  w w  w.j av  a 2 s .co  m*/
    }

    MBeanOperationInfo[] operations = info.getOperations();

    if (operations.length > 0) {
        System.out.println("Operations:");

        for (int i = 0; i < operations.length; ++i) {
            final MBeanParameterInfo[] params = operations[i].getSignature();
            final StringBuffer paramsStrBuffer = new StringBuffer();

            if (params != null) {
                for (int j = 0; j < params.length; ++j) {
                    paramsStrBuffer.append("\n   name=");
                    paramsStrBuffer.append(params[j].getName());
                    paramsStrBuffer.append(" type=");
                    paramsStrBuffer.append(params[j].getType());
                    paramsStrBuffer.append(" ");
                    paramsStrBuffer.append(params[j].getDescription());
                }
            }
            System.out.println(' ' + operations[i].getName() + ": " + operations[i].getDescription()
                    + "\n  Parameters " + params.length + ", return type=" + operations[i].getReturnType()
                    + paramsStrBuffer.toString());
        }
    }
}

From source file:com.boundary.plugin.sdk.jmx.MBeansTransformer.java

/**
 * Iterates over the attributes of an MBean
 * @param name {@link ObjectName}/*from   w  w w . jav  a2 s. c  o  m*/
 */
private void traverseAttributes(ObjectName name) {
    MBeanServerConnection connection = this.client.getMBeanServerConnection();
    MBeanInfo info;
    HashSet<String> checkTypes = new HashSet<String>();
    checkTypes.add("long");
    checkTypes.add("int");
    checkTypes.add("javax.management.openmbean.CompositeData");
    checkTypes.add("[Ljavax.management.openmbean.CompositeData;");
    try {
        info = connection.getMBeanInfo(name);
        MBeanAttributeInfo[] attributes = info.getAttributes();
        for (MBeanAttributeInfo attrInfo : attributes) {
            if (checkTypes.contains(attrInfo.getType())) {
                transform.beginAttribute(name, attrInfo);
                transform.endAttribute();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.echocat.jemoni.carbon.jmx.Jmx2CarbonBridge.java

public void updateMBeanIndex() throws Exception {
    final Configuration configuration = _configuration;
    synchronized (this) {
        final Map<Rule, Set<AttributeDefinitions>> ruleToAttributeNames = new HashMap<>();
        if (configuration != null && configuration.hasItems()) {
            final Set<ObjectName> objectNames = _server.queryNames(null, null);
            for (ObjectName objectName : objectNames) {
                try {
                    final MBeanInfo mBeanInfo = _server.getMBeanInfo(objectName);
                    final MBeanAttributeInfo[] attributes = mBeanInfo.getAttributes();

                    for (Rule rule : configuration) {
                        final Set<AttributeDefinition> singleAttributeDefinitions = new HashSet<>();
                        for (MBeanAttributeInfo attribute : attributes) {
                            final AttributeDefinition attributeDefinition = findDefinitionFor(objectName,
                                    attribute);
                            if (rule.apply(attributeDefinition)) {
                                singleAttributeDefinitions.add(attributeDefinition);
                            }//  w w w . j a  va  2 s  .c  om
                        }
                        if (!singleAttributeDefinitions.isEmpty()) {
                            Set<AttributeDefinitions> attributeNames = ruleToAttributeNames.get(rule);
                            if (attributeNames == null) {
                                attributeNames = new HashSet<>();
                                ruleToAttributeNames.put(rule, attributeNames);
                            }
                            attributeNames
                                    .add(new AttributeDefinitions(objectName, singleAttributeDefinitions));
                        }
                    }
                } catch (InstanceNotFoundException ignored) {
                }
            }
        }
        startThreads(configuration, ruleToAttributeNames);
    }
}

From source file:org.xmatthew.spy2servers.component.spy.jmx.JmxSpySupportComponent.java

public Map<String, Object> getAttributesAsMap(String mbeanName, MBeanServerConnection mbsc) throws Exception {
    ObjectName mbean = new ObjectName(mbeanName);
    MBeanInfo info = null;
    try {/*  w ww  . j a  va2s  . co  m*/
        info = mbsc.getMBeanInfo(mbean);
    } catch (Exception e) {
        //ignore the exception
    }

    if (info != null) {
        MBeanAttributeInfo[] attributes;
        attributes = info.getAttributes();
        if (attributes == null) {
            return null;
        }

        int size = attributes.length;
        if (size == 0) {
            return null;
        }

        Map<String, Object> beansMap = new HashMap<String, Object>(size);
        for (int i = 0; i < size; i++) {
            try {
                beansMap.put(attributes[i].getName(), mbsc.getAttribute(mbean, attributes[i].getName()));
            } catch (Exception e) {
                //ignore the exception
            }
        }
        return beansMap;
    }
    return null;
}

From source file:net.sbbi.upnp.jmx.UPNPMBeanService.java

private String getDeviceSSDP(MBeanInfo info) throws IllegalArgumentException {

    MBeanOperationInfo[] ops = info.getOperations();
    MBeanAttributeInfo[] atts = info.getAttributes();

    if ((ops == null || ops.length == 0) && (atts == null || atts.length == 0)) {
        throw new IllegalArgumentException(
                "MBean has no operation and no attribute and cannot be exposed as an UPNP device, provide at least one attribute");
    }/*from w  w  w  .ja va  2 s  .c  om*/
    Set deployedActionNames = new HashSet();
    operationsStateVariables = new HashMap();
    StringBuffer rtrVal = new StringBuffer();
    rtrVal.append("<?xml version=\"1.0\" ?>\r\n");
    rtrVal.append("<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">\r\n");
    rtrVal.append("<specVersion><major>1</major><minor>0</minor></specVersion>\r\n");

    if (ops != null && ops.length > 0) {
        rtrVal.append("<actionList>\r\n");
        for (int i = 0; i < ops.length; i++) {
            MBeanOperationInfo op = ops[i];
            StringBuffer action = new StringBuffer();
            if (deployedActionNames.contains(op.getName())) {
                log.debug("The " + op.getName()
                        + " is allready deplyoed and cannot be reused, skipping operation deployment");
                continue;
            }
            action.append("<action>\r\n");
            action.append("<name>");
            action.append(op.getName());
            action.append("</name>\r\n");
            action.append("<argumentList>\r\n");
            // output argument
            action.append("<argument>\r\n");
            action.append("<name>");
            // TODO handle specific output vars
            String outVarName = op.getName() + "_out";
            String actionOutDataType = ServiceStateVariable.getUPNPDataTypeMapping(op.getReturnType());
            if (actionOutDataType == null)
                actionOutDataType = ServiceStateVariableTypes.STRING;
            action.append(outVarName);
            action.append("</name>\r\n");
            action.append("<direction>out</direction>\r\n");
            action.append("<relatedStateVariable>");
            action.append(outVarName);
            action.append("</relatedStateVariable>\r\n");
            action.append("</argument>\r\n");

            // handle now for all input argument
            boolean nonPrimitiveInputType = false;
            boolean duplicatedInputVarname = false;
            Map operationsInputStateVariables = new HashMap();
            if (op.getSignature() != null) {
                for (int z = 0; z < op.getSignature().length; z++) {
                    MBeanParameterInfo param = op.getSignature()[z];
                    // do some sanity checks
                    String actionInDataType = ServiceStateVariable.getUPNPDataTypeMapping(param.getType());
                    if (actionInDataType == null) {
                        nonPrimitiveInputType = true;
                        log.debug("The " + param.getType()
                                + " type is not an UPNP compatible data type, use only primitives");
                        break;
                    }
                    String inVarName = param.getName();
                    // check that if the name does allready exists it
                    // has the same type
                    String existing = (String) operationsStateVariables.get(inVarName);
                    if (existing != null && !existing.equals(actionInDataType)) {
                        String msg = "The operation " + op.getName() + " " + inVarName
                                + " parameter already exists for another method with another data type ("
                                + existing + ") either match the data type or change the parameter name"
                                + " in you MBeanParameterInfo object for this operation";
                        duplicatedInputVarname = true;
                        log.debug(msg);
                        break;
                    }
                    action.append("<argument>\r\n");
                    action.append("<name>");
                    operationsInputStateVariables.put(inVarName, actionInDataType);
                    action.append(inVarName);
                    action.append("</name>\r\n");
                    action.append("<direction>in</direction>\r\n");
                    action.append("<relatedStateVariable>");
                    action.append(inVarName);
                    action.append("</relatedStateVariable>\r\n");
                    action.append("</argument>\r\n");
                }
            }

            action.append("</argumentList>\r\n");
            action.append("</action>\r\n");
            // finally the action is only added to the UPNP SSDP if no problems have been detected
            // with the input parameters type and names.
            if (!nonPrimitiveInputType && !duplicatedInputVarname) {
                operationsStateVariables.putAll(operationsInputStateVariables);
                operationsStateVariables.put(outVarName, actionOutDataType);
                rtrVal.append(action.toString());
                deployedActionNames.add(op.getName());
            }
        }
        rtrVal.append("</actionList>\r\n");
    } else {
        rtrVal.append("<actionList/>\r\n");
    }

    // now append the operation created state vars
    rtrVal.append("<serviceStateTable>\r\n");

    for (Iterator i = operationsStateVariables.keySet().iterator(); i.hasNext();) {
        String name = (String) i.next();
        String type = (String) operationsStateVariables.get(name);
        // TODO handle sendevents with mbean notifications ???
        // TODO handle defaultValue and allowedValueList values
        rtrVal.append("<stateVariable sendEvents=\"no\">\r\n");
        rtrVal.append("<name>");
        rtrVal.append(name);
        rtrVal.append("</name>\r\n");
        rtrVal.append("<dataType>");
        rtrVal.append(type);
        rtrVal.append("</dataType>\r\n");
        rtrVal.append("</stateVariable>\r\n");
    }

    if (atts != null && atts.length > 0) {
        for (int i = 0; i < atts.length; i++) {
            MBeanAttributeInfo att = atts[i];
            if (att.isReadable()) {
                rtrVal.append("<stateVariable sendEvents=\"no\">\r\n");
                rtrVal.append("<name>");
                rtrVal.append(att.getName());
                rtrVal.append("</name>\r\n");
                rtrVal.append("<dataType>");
                // TODO check if this works
                String stateVarType = ServiceStateVariable.getUPNPDataTypeMapping(att.getType());
                if (stateVarType == null)
                    stateVarType = ServiceStateVariableTypes.STRING;
                rtrVal.append(stateVarType);
                rtrVal.append("</dataType>\r\n");
                rtrVal.append("</stateVariable>\r\n");
            }
        }
    }
    rtrVal.append("</serviceStateTable>\r\n");
    rtrVal.append("</scpd>");
    return rtrVal.toString();
}