Example usage for javax.management MBeanAttributeInfo getName

List of usage examples for javax.management MBeanAttributeInfo getName

Introduction

In this page you can find the example usage for javax.management MBeanAttributeInfo getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the feature.

Usage

From source file:org.jminix.console.resource.AttributeResource.java

@Override
public void acceptRepresentation(Representation entity) throws ResourceException {
    String value = new Form(entity).getFirstValue("value");

    String domain = getRequest().getAttributes().get("domain").toString();

    String mbean = new EncoderBean().decode(getRequest().getAttributes().get("mbean").toString());

    String attributeName = new EncoderBean().decode(getRequest().getAttributes().get("attribute").toString());

    MBeanServerConnection server = getServer();

    try {/*  www. j av a 2s. co m*/

        String type = "java.lang.String";
        for (MBeanAttributeInfo info : server.getMBeanInfo(new ObjectName(domain + ":" + mbean))
                .getAttributes()) {
            if (info.getName().equals(attributeName)) {
                type = info.getType();
            }
        }

        Object attribute = new ValueParser().parse(value, type);

        if (attribute != null) {
            server.setAttribute(new ObjectName(domain + ":" + mbean), new Attribute(attributeName, attribute));
        }

        String queryString = getQueryString();
        if (queryString == null) {
            queryString = "?";
        }
        queryString += "ok=1";
        getResponse().redirectPermanent(new EncoderBean().encode(attributeName) + queryString);
    } catch (InstanceNotFoundException e) {
        throw new RuntimeException(e);
    } catch (AttributeNotFoundException e) {
        throw new RuntimeException(e);
    } catch (InvalidAttributeValueException e) {
        throw new RuntimeException(e);
    } catch (MalformedObjectNameException e) {
        throw new RuntimeException(e);
    } catch (MBeanException e) {
        throw new RuntimeException(e);
    } catch (ReflectionException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }

}

From source file:org.apache.hadoop.hdfs.tools.JMXGet.java

/**
 * print all attributes' values/*  w w  w .  j  a v a  2s .  co  m*/
 */
public void printAllValues() throws Exception {
    err("List of all the available keys:");

    Object val = null;

    for (ObjectName oname : hadoopObjectNames) {
        err(">>>>>>>>jmx name: " + oname.getCanonicalKeyPropertyListString());
        MBeanInfo mbinfo = mbsc.getMBeanInfo(oname);
        MBeanAttributeInfo[] mbinfos = mbinfo.getAttributes();

        for (MBeanAttributeInfo mb : mbinfos) {
            val = mbsc.getAttribute(oname, mb.getName());
            System.out.format(format, mb.getName(), (val == null) ? "" : val.toString());
        }
    }
}

From source file:org.springframework.integration.jmx.DefaultMBeanObjectConverter.java

@Override
public Object convert(MBeanServerConnection connection, ObjectInstance instance) {
    Map<String, Object> attributeMap = new HashMap<String, Object>();

    try {/*w  w  w.  j  ava  2 s. co m*/
        ObjectName objName = instance.getObjectName();
        if (!connection.isRegistered(objName)) {
            return attributeMap;
        }

        MBeanInfo info = connection.getMBeanInfo(objName);
        MBeanAttributeInfo[] attributeInfos = info.getAttributes();

        for (MBeanAttributeInfo attrInfo : attributeInfos) {
            // we don't need to repeat name of this as an attribute
            if ("ObjectName".equals(attrInfo.getName()) || !this.filter.accept(objName, attrInfo.getName())) {
                continue;
            }

            Object value;
            try {
                value = connection.getAttribute(objName, attrInfo.getName());
            } catch (RuntimeMBeanException e) {
                // N.B. standard MemoryUsage MBeans will throw an exception when some
                // measurement is unsupported. Logging at trace rather than debug to
                // avoid confusion.
                if (log.isTraceEnabled()) {
                    log.trace("Error getting attribute '" + attrInfo.getName() + "' on '" + objName + "'", e);
                }

                // try to unwrap the exception somewhat; not sure this is ideal
                Throwable t = e;
                while (t.getCause() != null) {
                    t = t.getCause();
                }
                value = String.format("%s[%s]", t.getClass().getName(), t.getMessage());
            }

            attributeMap.put(attrInfo.getName(), checkAndConvert(value));
        }

    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }

    return attributeMap;
}

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 om*/
 */
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.apache.synapse.commons.snmp.SNMPAgent.java

@Override
protected void registerManagedObjects() {
    log.info("Initializing Synapse SNMP MIB");
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectInstance> instances = mbs.queryMBeans(null, null);

    try {//from  w  w  w  .  ja  v  a2 s .  co m
        for (ObjectInstance instance : instances) {
            ObjectName objectName = instance.getObjectName();
            if (objectName.getDomain().equals("org.apache.synapse")) {
                String oidString = SynapseMIBUtils.getOID(objectName);
                if (oidString == null) {
                    continue;
                }

                MBeanInfo info = mbs.getMBeanInfo(objectName);
                MBeanAttributeInfo[] attributes = info.getAttributes();
                List<String> attributeNames = new ArrayList<String>();
                List<String> mapAttributes = new ArrayList<String>();
                for (MBeanAttributeInfo attributeInfo : attributes) {
                    attributeNames.add(attributeInfo.getName());
                    if (Map.class.getName().equals(attributeInfo.getType())) {
                        mapAttributes.add(attributeInfo.getName());
                    }
                }
                Collections.sort(attributeNames);

                doRegister(attributeNames, mapAttributes, oidString, objectName);
            }
        }
    } catch (Exception e) {
        log.error("Error while initializing the SNMP MIB", e);
    }
}

From source file:org.jolokia.jmx.JsonDymamicMBeanImplTest.java

@Test
public void checkMBeanInfo() throws Exception {
    MBeanInfo info = platformServer.getMBeanInfo(testName);
    MBeanAttributeInfo aInfo[] = info.getAttributes();
    Map<String, MBeanAttributeInfo> attributes = new HashMap<String, MBeanAttributeInfo>();
    for (MBeanAttributeInfo a : aInfo) {
        attributes.put(a.getName(), a);
    }/*w  w  w .j  av a2  s .  co  m*/

    assertEquals(attributes.get("Chili").getType(), String.class.getName());
    assertEquals(attributes.get("Numbers").getType(), String.class.getName());
    assertEquals(attributes.get("User").getType(), String.class.getName());

    MBeanOperationInfo oInfo[] = info.getOperations();
    Map<String, MBeanOperationInfo> ops = new HashMap<String, MBeanOperationInfo>();
    for (MBeanOperationInfo o : oInfo) {
        ops.put(o.getName(), o);
    }

    assertEquals(ops.get("lookup").getReturnType(), String.class.getName());
    assertEquals(ops.get("epoch").getReturnType(), "long");
    assertEquals(ops.get("charTest").getReturnType(), "char");

    MBeanParameterInfo p[] = ops.get("lookup").getSignature();
    assertEquals(p[0].getType(), String.class.getName());
    assertEquals(p[1].getType(), String.class.getName());

    p = ops.get("epoch").getSignature();
    assertEquals(p[0].getType(), String.class.getName());

    p = ops.get("charTest").getSignature();
    assertEquals(p[0].getType(), Character.class.getName());

}

From source file:flens.query.JMXQuery.java

private Map<String, Object> getAttributes(MBeanInfo info) {
    Map<String, Object> outc = new HashMap<>();
    MBeanAttributeInfo[] atts = info.getAttributes();
    for (MBeanAttributeInfo att : atts) {
        Map<String, Object> out = new HashMap<>();
        outc.put(att.getName(), out);
        out.put("description", att.getDescription());
        out.put("type", att.getType());
    }/*from w ww  .  j  ava2  s.  c om*/
    return outc;
}

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()));
        }// w  w w.j  a v  a 2s. c o m
    }
}

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()));
        }/* w  w w .jav a 2  s .co m*/
    }
}

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);/*from www.ja  v a  2  s  . c om*/
        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());
}