Search in sources :

Example 26 with AttributeList

use of javax.management.AttributeList in project intellij-community by JetBrains.

the class CpuTimings method getProcessCpuLoad.

public static double getProcessCpuLoad() {
    try {
        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);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 27 with AttributeList

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

the class MBeanSupport method setAttributes.

public final AttributeList setAttributes(AttributeList attributes) {
    final AttributeList result = new AttributeList(attributes.size());
    for (Object attrObj : attributes) {
        // We can't use AttributeList.asList because it has side-effects
        Attribute attr = (Attribute) attrObj;
        try {
            setAttribute(attr);
            result.add(new Attribute(attr.getName(), attr.getValue()));
        } catch (Exception e) {
        // OK: attribute is not included in returned list, per spec
        // XXX: log the exception
        }
    }
    return result;
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) AttributeNotFoundException(javax.management.AttributeNotFoundException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ReflectionException(javax.management.ReflectionException)

Example 28 with AttributeList

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

the class MBeanSupport method getAttributes.

public final AttributeList getAttributes(String[] attributes) {
    final AttributeList result = new AttributeList(attributes.length);
    for (String attrName : attributes) {
        try {
            final Object attrValue = getAttribute(attrName);
            result.add(new Attribute(attrName, attrValue));
        } catch (Exception e) {
        // OK: attribute is not included in returned list, per spec
        // XXX: log the exception
        }
    }
    return result;
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) AttributeNotFoundException(javax.management.AttributeNotFoundException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ReflectionException(javax.management.ReflectionException)

Example 29 with AttributeList

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

the class DynamicMBeanWrapper method setAttributes.

@Override
public AttributeList setAttributes(final AttributeList attributes) {
    final AttributeList list = new AttributeList();
    for (final Object o : attributes) {
        final Attribute attr = (Attribute) o;
        try {
            setAttribute(attr);
            list.add(attr);
        } catch (final Exception ignore) {
        // no-op
        }
    }
    return list;
}
Also used : ManagedAttribute(org.apache.openejb.api.jmx.ManagedAttribute) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) AttributeNotFoundException(javax.management.AttributeNotFoundException) IntrospectionException(javax.management.IntrospectionException) ReflectionException(javax.management.ReflectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException)

Example 30 with AttributeList

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

the class JMXResourceProvider method listChildren.

/**
     * @see org.apache.sling.api.resource.ResourceProvider#listChildren(org.apache.sling.api.resource.Resource)
     */
public Iterator<Resource> listChildren(final Resource parent) {
    final PathInfo info = this.parse(parent.getPath());
    if (info != null) {
        if (info.isRoot || info.mbeanInfo == null) {
            // list all MBeans
            final Set<ObjectName> names = this.queryObjectNames(info.isRoot ? null : info.pathInfo);
            final Set<String> filteredNames = new HashSet<String>();
            final String prefix = (info.isRoot ? null : info.pathInfo + "/");
            for (final ObjectName name : names) {
                final String path = this.convertObjectNameToResourcePath(name);
                final String testName = (info.isRoot ? path : path.substring(prefix.length()));
                final int sep = testName.indexOf('/');
                if (sep == -1) {
                    filteredNames.add(":" + name.getCanonicalName());
                } else {
                    filteredNames.add(testName.substring(0, sep));
                }
            }
            final List<String> sortedNames = new ArrayList<String>(filteredNames);
            Collections.sort(sortedNames);
            final Iterator<String> iter = sortedNames.iterator();
            return new Iterator<Resource>() {

                private Resource next;

                {
                    seek();
                }

                private void seek() {
                    while (iter.hasNext() && this.next == null) {
                        final String name = iter.next();
                        if (name.startsWith(":")) {
                            try {
                                final ObjectName on = new ObjectName(name.substring(1));
                                final MBeanInfo info = mbeanServer.getMBeanInfo(on);
                                final String path = convertObjectNameToResourcePath(on);
                                final int sep = path.lastIndexOf('/');
                                this.next = new MBeanResource(mbeanServer, parent.getResourceResolver(), path, parent.getPath() + "/" + path.substring(sep + 1), info, on);
                            } catch (final IntrospectionException e) {
                            // ignore
                            } catch (final InstanceNotFoundException e) {
                            // ignore
                            } catch (final ReflectionException e) {
                            // ignore
                            } catch (final MalformedObjectNameException e) {
                            // ignore
                            }
                        } else {
                            this.next = new RootResource(parent.getResourceResolver(), parent.getPath() + '/' + name);
                        }
                    }
                }

                public boolean hasNext() {
                    return next != null;
                }

                public Resource next() {
                    if (next != null) {
                        final Resource rsrc = next;
                        this.next = null;
                        seek();
                        return rsrc;
                    }
                    throw new NoSuchElementException();
                }

                public void remove() {
                    throw new UnsupportedOperationException("remove");
                }
            };
        } else {
            if (info.pathInfo == null) {
                final MBeanResource parentResource;
                if (parent instanceof MBeanResource) {
                    parentResource = (MBeanResource) parent;
                } else {
                    parentResource = (MBeanResource) this.getResource(parent.getResourceResolver(), parent.getPath());
                }
                final List<Resource> list = new ArrayList<Resource>();
                list.add(new AttributesResource(parent.getResourceResolver(), parent.getPath() + "/mbean:attributes", parentResource));
                return list.iterator();
            } else if (info.pathInfo.equals("mbean:attributes")) {
                final AttributesResource parentResource;
                if (parent instanceof AttributesResource) {
                    parentResource = (AttributesResource) parent;
                } else {
                    parentResource = (AttributesResource) this.getResource(parent.getResourceResolver(), parent.getPath());
                }
                final MBeanResource parentMBeanResource = (MBeanResource) parentResource.getParent();
                final AttributeList result = parentMBeanResource.getAttributes();
                final MBeanAttributeInfo[] infos = info.mbeanInfo.getAttributes();
                final Map<String, MBeanAttributeInfo> infoMap = new HashMap<String, MBeanAttributeInfo>();
                for (final MBeanAttributeInfo i : infos) {
                    infoMap.put(i.getName(), i);
                }
                final Iterator iter = result.iterator();
                return new Iterator<Resource>() {

                    public void remove() {
                        throw new UnsupportedOperationException("remove");
                    }

                    public Resource next() {
                        final Attribute attr = (Attribute) iter.next();
                        return new AttributeResource(parent.getResourceResolver(), parent.getPath() + "/" + attr.getName(), infoMap.get(attr.getName()), attr.getValue(), parentResource);
                    }

                    public boolean hasNext() {
                        return iter.hasNext();
                    }
                };
            } else if (info.pathInfo.startsWith("mbean:attributes/")) {
                Resource checkParentResource = parent;
                if (!(checkParentResource instanceof AttributeResource) && !(checkParentResource instanceof MapResource)) {
                    checkParentResource = this.getResource(parent.getResourceResolver(), parent.getPath());
                }
                final AttributeResource parentResource;
                if (checkParentResource instanceof AttributeResource) {
                    parentResource = (AttributeResource) checkParentResource;
                } else {
                    parentResource = ((MapResource) checkParentResource).getAttributeResource();
                }
                final String attrPath = info.pathInfo.substring("mbean:attributes/".length());
                final int pos = attrPath.indexOf('/');
                final String subPath;
                if (pos == -1) {
                    subPath = null;
                } else {
                    subPath = attrPath.substring(pos + 1);
                }
                return parentResource.getChildren(parent.getPath(), subPath);
            }
        }
    }
    return null;
}
Also used : MBeanInfo(javax.management.MBeanInfo) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) ArrayList(java.util.ArrayList) IntrospectionException(javax.management.IntrospectionException) Iterator(java.util.Iterator) HashSet(java.util.HashSet) ReflectionException(javax.management.ReflectionException) MalformedObjectNameException(javax.management.MalformedObjectNameException) InstanceNotFoundException(javax.management.InstanceNotFoundException) Resource(org.apache.sling.api.resource.Resource) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) NoSuchElementException(java.util.NoSuchElementException)

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