Example usage for javax.management Attribute getValue

List of usage examples for javax.management Attribute getValue

Introduction

In this page you can find the example usage for javax.management Attribute getValue.

Prototype

public Object getValue() 

Source Link

Document

Returns an Object that is the value of this attribute.

Usage

From source file:utilities.srvRutinas.java

public static double getProcessCpuLoad() throws Exception {

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
    AttributeList list = mbs.getAttributes(name, new String[] { "ProcessCpuLoad" });

    if (list.isEmpty())
        return Double.NaN;

    Attribute att = (Attribute) list.get(0);
    Double value = (Double) att.getValue();

    // usually takes a couple of seconds before we get real values
    if (value == -1.0)
        return Double.NaN;
    // returns a percentage value with 1 decimal point precision
    return ((int) (value * 1000) / 10.0);
}

From source file:org.apache.jackrabbit.oak.run.osgi.OakOSGiRepositoryFactory.java

/**
 * Registers the Platform MBeanServer as OSGi service. This would enable
 * Aries JMX Whitboard support to then register the JMX MBean which are registered as OSGi service
 * to be registered against the MBean server
 *//*from  ww  w  . ja  va  2s .  c  o m*/
private static void registerMBeanServer(PojoServiceRegistry registry) {
    MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
    Hashtable<String, Object> mbeanProps = new Hashtable<String, Object>();
    try {
        ObjectName beanName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
        AttributeList attrs = platformMBeanServer.getAttributes(beanName,
                new String[] { "MBeanServerId", "SpecificationName", "SpecificationVersion",
                        "SpecificationVendor", "ImplementationName", "ImplementationVersion",
                        "ImplementationVendor" });
        for (Object object : attrs) {
            Attribute attr = (Attribute) object;
            if (attr.getValue() != null) {
                mbeanProps.put(attr.getName(), attr.getValue().toString());
            }
        }
    } catch (Exception je) {
        log.info("Cannot set service properties of Platform MBeanServer service, registering without", je);
    }
    registry.registerService(MBeanServer.class.getName(), platformMBeanServer, mbeanProps);
}

From source file:org.hyperic.hq.plugin.weblogic.jmx.AttributeGetter.java

public Object getAttribute(MBeanServer server, String attrName)
        throws InstanceNotFoundException, ReflectionException, AttributeNotFoundException {

    long timeNow = System.currentTimeMillis();

    if ((timeNow - this.timestamp) > this.expire) {
        AttributeList attrList;//from   w ww  .ja v  a2 s .c om

        if (log.isDebugEnabled()) {
            log.debug("server.getAttributes(" + this.name + ", " + Arrays.asList(this.attrs) + ")");
        }

        attrList = server.getAttributes(this.name, this.attrs);

        if (attrList == null) {
            throw new AttributeNotFoundException(this.name.toString());
        }

        for (Iterator it = attrList.iterator(); it.hasNext();) {

            Attribute attr = (Attribute) it.next();

            this.values.put(attr.getName(), attr.getValue());
        }

        this.timestamp = timeNow;
    }

    Object value = this.values.get(attrName);

    if (value == null) {
        throw new AttributeNotFoundException(attrName);
    }

    return value;
}

From source file:com.tomcat.monitor.jmx.obj.bean.MServer.java

/**
 * Zugriff auf ausgewhlte Menge von MBean Attribute
 * // w ww .  j av  a 2  s . co m
 * @param accessorMbean
 *           ObjectName of mbean
 * @param attributes
 *           Map von Attribute und evtl. Path Zugriffs ausdrcken (Separator
 *           ";") see accessValue
 */
public Map<String, Object> values(ObjectName accessorMbean, Map<String, String> attributes) {

    String[] attrNames = new String[attributes.size()];
    attributes.keySet().toArray(attrNames);
    AttributeList list = null;
    try {
        list = mserver.getAttributes(accessorMbean, attrNames);
    } catch (Exception e) {
        logger.error(e);
    }
    Map<String, Object> values = new HashMap<String, Object>();
    if (list != null) {
        if (logger.isDebugEnabled())
            logger.debug("access: " + accessorMbean + " attr=" + list.toString());
        for (Iterator<Object> iter = list.iterator(); iter.hasNext();) {
            Attribute attr = (Attribute) iter.next();
            Object value = attr.getValue();
            String attributePath = attributes.get(attr.getName());
            if (attributePath != null) {
                StringTokenizer st = new StringTokenizer(attributePath, ";");
                while (st.hasMoreTokens()) {
                    String path = st.nextToken();
                    Object pathvalue = accessValue(path, value);
                    values.put(attr.getName() + "." + path, pathvalue);
                }
            } else
                values.put(attr.getName(), value);
        }
    }
    return values;
}

From source file:org.apache.hadoop.hdfs.server.common.MetricsLoggerTask.java

/**
 * Write metrics to the metrics appender when invoked.
 *///from   w w w . j  a v a 2s  . com
@Override
public void run() {
    // Skip querying metrics if there are no known appenders.
    if (!metricsLog.isInfoEnabled() || !hasAppenders(metricsLog) || objectName == null) {
        return;
    }

    metricsLog.info(" >> Begin " + nodeName + " metrics dump");
    final MBeanServer server = ManagementFactory.getPlatformMBeanServer();

    // Iterate over each MBean.
    for (final ObjectName mbeanName : server.queryNames(objectName, null)) {
        try {
            MBeanInfo mBeanInfo = server.getMBeanInfo(mbeanName);
            final String mBeanNameName = MBeans.getMbeanNameName(mbeanName);
            final Set<String> attributeNames = getFilteredAttributes(mBeanInfo);

            final AttributeList attributes = server.getAttributes(mbeanName,
                    attributeNames.toArray(new String[attributeNames.size()]));

            for (Object o : attributes) {
                final Attribute attribute = (Attribute) o;
                final Object value = attribute.getValue();
                final String valueStr = (value != null) ? value.toString() : "null";
                // Truncate the value if it is too long
                metricsLog.info(mBeanNameName + ":" + attribute.getName() + "=" + trimLine(valueStr));
            }
        } catch (Exception e) {
            metricsLog.error("Failed to get " + nodeName + " metrics for mbean " + mbeanName.toString(), e);
        }
    }
    metricsLog.info(" << End " + nodeName + " metrics dump");
}

From source file:org.wso2.carbon.metrics.impl.ReporterTest.java

private SortedMap<String, Object> values(AttributeList attributes) {
    final TreeMap<String, Object> values = new TreeMap<String, Object>();
    if (attributes != null) {
        for (Object o : attributes) {
            final Attribute attribute = (Attribute) o;
            values.put(attribute.getName(), attribute.getValue());
        }// w w  w  .j  a v a2s.com
    }
    return values;
}

From source file:org.hyperic.hq.product.jmx.MxQuery.java

public void getAttributes(MBeanServerConnection mServer, ObjectName name, String[] attrs)
        throws PluginException {

    if (attrs.length == 0) {
        return;//w  w w. j  av  a2s  . c o m
    }

    AttributeList mBeanAttrs;

    try {
        mBeanAttrs = mServer.getAttributes(name, attrs);
    } catch (RemoteException e) {
        throw new PluginException("Cannot connect to server", e);
    } catch (InstanceNotFoundException e) {
        throw new PluginException("Cannot find MBean [" + name + "]", e);
    } catch (ReflectionException e) {
        throw new PluginException("MBean reflection exception", e);
    } catch (IOException e) {
        throw new PluginException("Cannot connect to server", e);
    }

    for (Iterator it = mBeanAttrs.iterator(); it.hasNext();) {
        Attribute attr = (Attribute) it.next();
        Object value = attr.getValue();
        if (value != null) {
            setAttribute(attr.getName(), value.toString());
        }
    }
}

From source file:org.hyperic.hq.plugin.websphere.jmx.WebSphereQuery.java

public boolean getAttributes(AdminClient mServer, ObjectName name, String[] attrNames) {

    AttributeList list;/*from w w  w.j  av  a  2s  .  co  m*/

    if (attrNames.length == 0) {
        return true;
    }

    try {
        list = mServer.getAttributes(name, attrNames);
    } catch (InstanceNotFoundException e) {
        logAttrFailure(name, e);
        return false;
    } catch (ReflectionException e) {
        logAttrFailure(name, e);
        return false;
    } catch (ConnectorException e) {
        logAttrFailure(name, e);
        return false;
    }

    if (list == null) {
        return false;
    }

    for (int i = 0; i < list.size(); i++) {
        Attribute attr = (Attribute) list.get(i);
        Object obj = attr.getValue();
        if (obj != null) {
            this.attrs.put(attr.getName(), obj.toString());
        }
    }

    return true;
}

From source file:com.zavakid.mushroom.impl.MetricsSourceAdapter.java

@Override
public AttributeList getAttributes(String[] attributes) {
    updateJmxCache();/*from w  ww. j  a  v  a  2s  .c om*/
    synchronized (this) {
        AttributeList ret = new AttributeList();
        for (String key : attributes) {
            Attribute attr = attrCache.get(key);
            if (LOG.isDebugEnabled()) {
                LOG.debug(key + ": " + attr.getName() + "=" + attr.getValue());
            }
            ret.add(attr);
        }
        return ret;
    }
}

From source file:com.cyberway.issue.crawler.datamodel.credential.HtmlFormCredential.java

/**
 * @param context CrawlURI context to use.
 * @return Form inputs as convenient map.  Returns null if no form items.
 * @throws AttributeNotFoundException//from  ww w.jav a2  s.c  o  m
 */
public Map<String, Object> getFormItems(final CrawlURI context) throws AttributeNotFoundException {
    Map<String, Object> result = null;
    MapType items = (MapType) getAttribute(ATTR_FORM_ITEMS, context);
    if (items != null) {
        for (Iterator i = items.iterator(context); i.hasNext();) {
            Attribute a = (Attribute) i.next();
            if (result == null) {
                result = new HashMap<String, Object>();
            }
            result.put(a.getName(), a.getValue());
        }
    }
    return result;
}