Search in sources :

Example 46 with AttributeList

use of javax.management.AttributeList in project jdk8u_jdk by JetBrains.

the class XMBeanAttributes method doLoadAttributes.

// Don't call this in EDT, but execute returned Runnable inside
// EDT - typically in the done() method of a SwingWorker
// This method can return null.
private Runnable doLoadAttributes(final XMBean mbean, MBeanInfo infoOrNull) throws JMException, IOException {
    if (mbean == null)
        return null;
    final MBeanInfo curMBeanInfo = (infoOrNull == null) ? mbean.getMBeanInfo() : infoOrNull;
    final MBeanAttributeInfo[] attrsInfo = curMBeanInfo.getAttributes();
    final HashMap<String, Object> attrs = new HashMap<String, Object>(attrsInfo.length);
    final HashMap<String, Object> unavailableAttrs = new HashMap<String, Object>(attrsInfo.length);
    final HashMap<String, Object> viewableAttrs = new HashMap<String, Object>(attrsInfo.length);
    AttributeList list = null;
    try {
        list = mbean.getAttributes(attrsInfo);
    } catch (Exception e) {
        if (JConsole.isDebug()) {
            System.err.println("Error calling getAttributes() on MBean \"" + mbean.getObjectName() + "\". JConsole will " + "try to get them individually calling " + "getAttribute() instead. Exception:");
            e.printStackTrace(System.err);
        }
        list = new AttributeList();
        //Can't load all attributes, do it one after each other.
        for (int i = 0; i < attrsInfo.length; i++) {
            String name = null;
            try {
                name = attrsInfo[i].getName();
                Object value = mbean.getMBeanServerConnection().getAttribute(mbean.getObjectName(), name);
                list.add(new Attribute(name, value));
            } catch (Exception ex) {
                if (attrsInfo[i].isReadable()) {
                    unavailableAttrs.put(name, Utils.getActualException(ex).toString());
                }
            }
        }
    }
    try {
        int att_length = list.size();
        for (int i = 0; i < att_length; i++) {
            Attribute attribute = (Attribute) list.get(i);
            if (isViewable(attribute)) {
                viewableAttrs.put(attribute.getName(), attribute.getValue());
            } else
                attrs.put(attribute.getName(), attribute.getValue());
        }
        // check them one after the other.
        if (att_length < attrsInfo.length) {
            for (int i = 0; i < attrsInfo.length; i++) {
                MBeanAttributeInfo attributeInfo = attrsInfo[i];
                if (!attrs.containsKey(attributeInfo.getName()) && !viewableAttrs.containsKey(attributeInfo.getName()) && !unavailableAttrs.containsKey(attributeInfo.getName())) {
                    if (attributeInfo.isReadable()) {
                        // went wrong.
                        try {
                            Object v = mbean.getMBeanServerConnection().getAttribute(mbean.getObjectName(), attributeInfo.getName());
                            //What happens if now it is ok?
                            // Be pragmatic, add it to readable...
                            attrs.put(attributeInfo.getName(), v);
                        } catch (Exception e) {
                            //Put the exception that will be displayed
                            // in tooltip
                            unavailableAttrs.put(attributeInfo.getName(), Utils.getActualException(e).toString());
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        //sets all attributes unavailable except the writable ones
        for (int i = 0; i < attrsInfo.length; i++) {
            MBeanAttributeInfo attributeInfo = attrsInfo[i];
            if (attributeInfo.isReadable()) {
                unavailableAttrs.put(attributeInfo.getName(), Utils.getActualException(e).toString());
            }
        }
    }
    //one update at a time
    return new Runnable() {

        public void run() {
            synchronized (XMBeanAttributes.this) {
                XMBeanAttributes.this.mbean = mbean;
                XMBeanAttributes.this.mbeanInfo = curMBeanInfo;
                XMBeanAttributes.this.attributesInfo = attrsInfo;
                XMBeanAttributes.this.attributes = attrs;
                XMBeanAttributes.this.unavailableAttributes = unavailableAttrs;
                XMBeanAttributes.this.viewableAttributes = viewableAttrs;
                DefaultTableModel tableModel = (DefaultTableModel) getModel();
                // add attribute information
                emptyTable(tableModel);
                addTableData(tableModel, mbean, attrsInfo, attrs, unavailableAttrs, viewableAttrs);
                // update the model with the new data
                tableModel.newDataAvailable(new TableModelEvent(tableModel));
                // re-register for change events
                tableModel.addTableModelListener(attributesListener);
            }
        }
    };
}
Also used : MBeanInfo(javax.management.MBeanInfo) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) TableModelEvent(javax.swing.event.TableModelEvent) DefaultTableModel(javax.swing.table.DefaultTableModel) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JMException(javax.management.JMException) EventObject(java.util.EventObject)

Example 47 with AttributeList

use of javax.management.AttributeList in project jdk8u_jdk by JetBrains.

the class AttributeListTypeSafeTest method main.

public static void main(String[] args) throws Exception {
    // Test calling asList after adding non-Attribute by various means
    for (Op op : Op.values()) {
        AttributeList alist = new AttributeList();
        alist.add(new Attribute("foo", "bar"));
        doOp(alist, op);
        String what = "asList() after calling " + op + " with non-Attribute";
        try {
            List<Attribute> lista = alist.asList();
            fail(what + ": succeeded but should not have");
        } catch (IllegalArgumentException e) {
            System.out.println("OK: " + what + ": got IllegalArgumentException");
        }
    }
    // Test adding non-Attribute by various means after calling asList
    for (Op op : Op.values()) {
        AttributeList alist = new AttributeList();
        List<Attribute> lista = alist.asList();
        lista.add(new Attribute("foo", "bar"));
        String what = op + " with non-Attribute after calling asList()";
        try {
            doOp(alist, op);
            fail(what + ": succeeded but should not have");
        } catch (IllegalArgumentException e) {
            System.out.println("OK: " + what + ": got IllegalArgumentException");
        }
    }
    if (failure == null)
        System.out.println("TEST PASSED");
    else
        throw new Exception("TEST FAILED: " + failure);
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList)

Example 48 with AttributeList

use of javax.management.AttributeList in project jdk8u_jdk by JetBrains.

the class RMIConnectorLogAttributesTest method doTest.

private void doTest(JMXConnector connector) throws IOException, MalformedObjectNameException, ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException {
    MBeanServerConnection mbsc = connector.getMBeanServerConnection();
    ObjectName objName = new ObjectName("com.redhat.test.jmx:type=NameMBean");
    System.out.println("DEBUG: Calling createMBean");
    mbsc.createMBean(Name.class.getName(), objName);
    System.out.println("DEBUG: Calling setAttributes");
    AttributeList attList = new AttributeList();
    attList.add(new Attribute("FirstName", ANY_NAME));
    attList.add(new Attribute("LastName", ANY_NAME));
    mbsc.setAttributes(objName, attList);
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName) ObjectName(javax.management.ObjectName)

Example 49 with AttributeList

use of javax.management.AttributeList in project Ardent by adamint.

the class UsageUtils method getProcessCpuLoad.

// Credit http://stackoverflow.com/questions/18489273/how-to-get-percentage-of-cpu-usage-of-os-from-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);
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 50 with AttributeList

use of javax.management.AttributeList in project geode by apache.

the class MX4JModelMBean method getAttributes.

public AttributeList getAttributes(String[] attributes) {
    if (attributes == null)
        throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_NAMES_CANNOT_BE_NULL.toLocalizedString()));
    Logger logger = getLogger();
    AttributeList list = new AttributeList();
    for (int i = 0; i < attributes.length; ++i) {
        String attrName = attributes[i];
        Attribute attribute = null;
        try {
            Object value = getAttribute(attrName);
            attribute = new Attribute(attrName, value);
            list.add(attribute);
        } catch (Exception x) {
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("getAttribute for attribute " + attrName + " failed", x);
        // And go on with the next attribute
        }
    }
    return list;
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) AttributeNotFoundException(javax.management.AttributeNotFoundException) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedObjectNameException(javax.management.MalformedObjectNameException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ListenerNotFoundException(javax.management.ListenerNotFoundException) ImplementationException(mx4j.ImplementationException) RuntimeOperationsException(javax.management.RuntimeOperationsException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Aggregations

AttributeList (javax.management.AttributeList)56 Attribute (javax.management.Attribute)46 ReflectionException (javax.management.ReflectionException)22 AttributeNotFoundException (javax.management.AttributeNotFoundException)16 InstanceNotFoundException (javax.management.InstanceNotFoundException)16 MBeanException (javax.management.MBeanException)15 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)14 ObjectName (javax.management.ObjectName)14 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)10 MBeanServer (javax.management.MBeanServer)8 IOException (java.io.IOException)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 MBeanInfo (javax.management.MBeanInfo)7 RuntimeOperationsException (javax.management.RuntimeOperationsException)7 HashMap (java.util.HashMap)6 MalformedObjectNameException (javax.management.MalformedObjectNameException)6 ListenerNotFoundException (javax.management.ListenerNotFoundException)5 MBeanRegistrationException (javax.management.MBeanRegistrationException)5 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)5 ArrayList (java.util.ArrayList)4