Example usage for javax.management AttributeList asList

List of usage examples for javax.management AttributeList asList

Introduction

In this page you can find the example usage for javax.management AttributeList asList.

Prototype

@SuppressWarnings("unchecked")
public List<Attribute> asList() 

Source Link

Document

Return a view of this list as a List .

Usage

From source file:com.amazonaws.client.metrics.support.JmxInfoProviderSupport.java

@Override
public long[] getFileDecriptorInfo() {
    MBeanServer mbsc = MBeans.getMBeanServer();
    AttributeList attributes;
    try {// w ww .  java 2 s .  com
        attributes = mbsc.getAttributes(new ObjectName("java.lang:type=OperatingSystem"),
                new String[] { "OpenFileDescriptorCount", "MaxFileDescriptorCount" });
        List<Attribute> attrList = attributes.asList();
        long openFdCount = (Long) attrList.get(0).getValue();
        long maxFdCount = (Long) attrList.get(1).getValue();
        long[] fdCounts = { openFdCount, maxFdCount };
        return fdCounts;
    } catch (Exception e) {
        LogFactory.getLog(SdkMBeanRegistrySupport.class).debug("Failed to retrieve file descriptor info", e);
    }
    return null;
}

From source file:org.rifidi.edge.configuration.RifidiService.java

/**
 * Set several attributes./*from  w ww . j a  v  a2 s  .  co  m*/
 * 
 * @param attributes
 */
public void setAttributes(AttributeList attributes) {
    for (Attribute attribute : attributes.asList()) {
        try {
            _setAttribute(attribute);
        } catch (AttributeNotFoundException e) {
            logger.error(e);
        } catch (InvalidAttributeValueException e) {
            logger.error(e);
        } catch (MBeanException e) {
            logger.error(e);
        } catch (ReflectionException e) {
            logger.error(e);
        }
    }
}

From source file:com.googlecode.jmxtrans.model.Query.java

public Iterable<Result> fetchResults(MBeanServerConnection mbeanServer, ObjectName queryName)
        throws InstanceNotFoundException, IntrospectionException, ReflectionException, IOException {
    MBeanInfo info = mbeanServer.getMBeanInfo(queryName);
    ObjectInstance oi = mbeanServer.getObjectInstance(queryName);

    List<String> attributes;
    if (attr.isEmpty()) {
        attributes = new ArrayList<>();
        for (MBeanAttributeInfo attrInfo : info.getAttributes()) {
            attributes.add(attrInfo.getName());
        }/*  ww w . j  a v  a  2 s  .c om*/
    } else {
        attributes = attr;
    }

    try {
        if (!attributes.isEmpty()) {
            logger.debug("Executing queryName [{}] from query [{}]", queryName.getCanonicalName(), this);

            AttributeList al = mbeanServer.getAttributes(queryName,
                    attributes.toArray(new String[attributes.size()]));

            return new JmxResultProcessor(this, oi, al.asList(), info.getClassName(), queryName.getDomain())
                    .getResults();
        }
    } catch (UnmarshalException ue) {
        if ((ue.getCause() != null) && (ue.getCause() instanceof ClassNotFoundException)) {
            logger.debug("Bad unmarshall, continuing. This is probably ok and due to something like this: "
                    + "http://ehcache.org/xref/net/sf/ehcache/distribution/RMICacheManagerPeerListener.html#52",
                    ue.getMessage());
        } else {
            throw ue;
        }
    }
    return ImmutableList.of();
}

From source file:org.apache.geode.management.internal.security.MBeanServerWrapper.java

@Override
public AttributeList setAttributes(ObjectName name, AttributeList attributes)
        throws InstanceNotFoundException, ReflectionException {
    // call setAttribute instead to use the authorization logic
    for (Attribute attribute : attributes.asList()) {
        try {//from  w ww.j av  a2s.co  m
            setAttribute(name, attribute);
        } catch (Exception e) {
            throw new GemFireSecurityException("error setting attribute " + attribute + " of " + name, e);
        }
    }
    return attributes;
}

From source file:io.soabase.core.SoaBundle.java

private void addMetrics(Environment environment) {
    Metric metric = new Gauge<Double>() {
        private double lastValue = 0.0;

        @Override//from   ww w  .j av  a  2 s .  co  m
        public Double getValue() {
            try {
                MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
                ObjectName name = ObjectName.getInstance("java.lang:type=OperatingSystem");
                AttributeList list = mbs.getAttributes(name, new String[] { "SystemCpuLoad" });
                if ((list != null) && (list.size() > 0)) {
                    // unfortunately, this bean reports bad values occasionally. Filter them out.
                    Object value = list.asList().get(0).getValue();
                    double d = (value instanceof Number) ? ((Number) value).doubleValue() : 0.0;
                    d = ((d > 0.0) && (d < 1.0)) ? d : lastValue;
                    lastValue = d;
                    return d;
                }
            } catch (Exception ignore) {
                // ignore
            }
            return lastValue;
        }
    };
    environment.metrics().register("system.cpu.load", metric);
}

From source file:org.helios.netty.jmx.MetricCollector.java

/**
 * Returns a simple map of NIO metrics/*from ww w  . ja v  a2  s  . c  o m*/
 * @return a simple map of NIO metrics
 */
protected Map<String, Long> getNio() {
    Map<String, Long> map = new HashMap<String, Long>(NIO_ATTRS.length);
    try {
        AttributeList attrs = ManagementFactory.getPlatformMBeanServer().getAttributes(directNio, NIO_ATTRS);
        for (Attribute attr : attrs.asList()) {
            map.put(attr.getName(), (Long) attr.getValue());
        }
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
    return map;
}

From source file:org.fishwife.jrugged.spring.jmx.TestWebMBeanAdapter.java

@Test
public void testGetAttributeValues() throws Exception {
    MBeanAttributeInfo[] attributeInfoArray = new MBeanAttributeInfo[2];
    MBeanAttributeInfo mockAttributeInfo1 = createMock(MBeanAttributeInfo.class);
    MBeanAttributeInfo mockAttributeInfo2 = createMock(MBeanAttributeInfo.class);
    attributeInfoArray[0] = mockAttributeInfo1;
    attributeInfoArray[1] = mockAttributeInfo2;
    expect(mockMBeanInfo.getAttributes()).andReturn(attributeInfoArray);

    String attributeName1 = "attribute_name_1";
    String attributeName2 = "attribute_name_2";
    expect(mockAttributeInfo1.getName()).andReturn(attributeName1);
    expect(mockAttributeInfo2.getName()).andReturn(attributeName2);

    AttributeList mockAttributeList = createMock(AttributeList.class);
    expect(mockMBeanServer.getAttributes(eq(mockObjectName), anyObject(String[].class)))
            .andReturn(mockAttributeList);

    List<Attribute> attributeList = new ArrayList<Attribute>();
    Attribute mockAttribute1 = createMock(Attribute.class);
    Attribute mockAttribute2 = createMock(Attribute.class);

    attributeList.add(mockAttribute1);//  w  w  w.  j a  v  a  2s .c  om
    attributeList.add(mockAttribute2);

    expect(mockAttributeList.asList()).andReturn(attributeList);

    String name1 = "name 1";
    String value1 = "value 1";
    expect(mockAttribute1.getName()).andReturn(name1);
    expect(mockAttribute1.getValue()).andReturn(value1);
    expect(mockSanitizer.escapeValue(value1)).andReturn(value1);

    String name2 = "name 2";
    String value2 = "value 2";
    expect(mockAttribute2.getName()).andReturn(name2);
    expect(mockAttribute2.getValue()).andReturn(value2);
    expect(mockSanitizer.escapeValue(value2)).andReturn(value2);

    replay(mockMBeanServer, mockSanitizer, mockObjectName, mockMBeanInfo, mockAttributeInfo1,
            mockAttributeInfo2, mockAttributeList, mockAttribute1, mockAttribute2);

    Map<String, Object> attributeValueMap = webMBeanAdapter.getAttributeValues();

    assertEquals(2, attributeValueMap.size());
    assertEquals(value1, attributeValueMap.get(name1));
    assertEquals(value2, attributeValueMap.get(name2));

    verify(mockMBeanServer, mockSanitizer, mockObjectName, mockMBeanInfo, mockAttributeInfo1,
            mockAttributeInfo2, mockAttributeList, mockAttribute1, mockAttribute2);
}

From source file:org.rifidi.edge.configuration.DefaultConfigurationImpl.java

@Override
public AttributeList setAttributes(final AttributeList attributes) {
    assert (attributes != null);
    RifidiService service = target.get();
    if (service != null) {
        service.setAttributes(attributes);
    }/*from  ww w. jav  a  2  s.com*/

    // keep track of changed attributes since there might be an error
    AttributeList changedAttributes = new AttributeList();

    for (Attribute attribute : attributes.asList()) {

        String attrName = attribute.getName();
        Integer pos = nameToPos.get(attrName);
        if (pos == null) {
            logger.error("Error when trying to set " + attribute.getName());
        } else {
            this.attributes.set(pos, attribute);
            changedAttributes.add(this.attributes.get(pos));
        }

    }

    notifierService.attributesChanged(getServiceID(), (AttributeList) changedAttributes);
    return (AttributeList) changedAttributes.clone();
}

From source file:org.rifidi.edge.rest.SensorManagerServiceRestletImpl.java

/**
 * Validate that properties for a readzone are valid and there is at least
 * the minimum required properties//  w  ww.  java 2s  .c om
 * 
 * @param attributes
 *            list of attributes to validate
 * @throws Exception
 *             if there is a non valid property or the properties list has
 *             an invalid property for a readzone
 */
private void validateReadzoneProperties(AttributeList attributes) throws Exception {

    for (Attribute attribute : attributes.asList()) {

        // Validate if this property is valid for readzone
        boolean isValidProperty = false;
        for (int i = 0; i < ReadZoneValidProperties.length; i++) {

            if (attribute.getName().trim().equals(ReadZoneValidProperties[i])) {
                isValidProperty = true;
                break;
            }

        }

        if (!isValidProperty) {

            String validProperties = "";
            for (int i = 0; i < ReadZoneValidProperties.length; i++) {
                validProperties += ReadZoneValidProperties[i] + "\n";
            }

            throw new Exception("Invalid property " + attribute.getName() + " for readzone. Valid ones are: "
                    + validProperties);
        }

    }
}

From source file:org.rifidi.edge.rest.SensorManagerServiceRestletImpl.java

private Object getAttributeValue(AttributeList attributes, String attributeName) throws Exception {

    for (Attribute attr : attributes.asList()) {

        if (attr.getName().equals(attributeName)) {
            return attr.getValue();
        }//  w w  w .j  a va 2 s.c o m

    }

    throw new Exception("The property " + attributeName + " is required and is not present");

}