Search in sources :

Example 51 with ModelMBeanAttributeInfo

use of javax.management.modelmbean.ModelMBeanAttributeInfo in project spring-framework by spring-projects.

the class AbstractMetadataAssemblerTests method testWithOnlySetter.

/**
	 * Tests the situation where the property only has a getter.
	 */
@Test
public void testWithOnlySetter() throws Exception {
    ModelMBeanInfo inf = getMBeanInfoFromAssembler();
    ModelMBeanAttributeInfo attr = inf.getAttribute("NickName");
    assertNotNull("Attribute should not be null", attr);
}
Also used : ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) Test(org.junit.Test)

Example 52 with ModelMBeanAttributeInfo

use of javax.management.modelmbean.ModelMBeanAttributeInfo in project spring-framework by spring-projects.

the class AbstractMetadataAssemblerTests method testReadWriteAttribute.

@Test
public void testReadWriteAttribute() throws Exception {
    ModelMBeanInfo inf = getMBeanInfoFromAssembler();
    ModelMBeanAttributeInfo attr = inf.getAttribute(NAME_ATTRIBUTE);
    assertTrue("The name attribute should be writable", attr.isWritable());
    assertTrue("The name attribute should be readable", attr.isReadable());
}
Also used : ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) Test(org.junit.Test)

Example 53 with ModelMBeanAttributeInfo

use of javax.management.modelmbean.ModelMBeanAttributeInfo in project jdk8u_jdk by JetBrains.

the class RequiredModelMBeanSetAttributeTest method main.

public static void main(String[] args) throws Exception {
    boolean ok = true;
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    // ModelMBeanAttributeInfo
    Descriptor somethingAttributeDescriptor = new DescriptorSupport(new String[] { "name=Something", "descriptorType=attribute", "getMethod=getSomething" });
    ModelMBeanAttributeInfo somethingAttributeInfo = new ModelMBeanAttributeInfo("Something", "java.lang.String", "Something attribute", true, true, false, somethingAttributeDescriptor);
    Descriptor somethingCachedAttributeDescriptor = new DescriptorSupport(new String[] { "name=SomethingCached", "descriptorType=attribute", "getMethod=getSomethingCached", "currencyTimeLimit=5000" });
    ModelMBeanAttributeInfo somethingCachedAttributeInfo = new ModelMBeanAttributeInfo("SomethingCached", "java.lang.String", "Something cached attribute", true, true, false, somethingCachedAttributeDescriptor);
    // ModelMBeanInfo
    ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(Resource.class.getName(), "Resource MBean", new ModelMBeanAttributeInfo[] { somethingAttributeInfo, somethingCachedAttributeInfo }, null, new ModelMBeanOperationInfo[] {}, null);
    // RequiredModelMBean
    ModelMBean mmb = new RequiredModelMBean(mmbi);
    mmb.setManagedResource(resource, "ObjectReference");
    ObjectName mmbName = new ObjectName(":type=ResourceMBean");
    mbs.registerMBean(mmb, mmbName);
    // Run tests
    System.out.println("\nTest that we receive ServiceNotFoundException");
    try {
        Attribute attr = new Attribute("Something", "Some string");
        mbs.setAttribute(mmbName, attr);
        System.out.println("TEST FAILED: Didn't caught exception");
        ok = false;
    } catch (MBeanException mbex) {
        Exception e = mbex.getTargetException();
        if (e == null || !(e instanceof ServiceNotFoundException)) {
            System.out.println("TEST FAILED: Caught wrong exception:" + e);
            ok = false;
        } else
            System.out.println("Received expected ServiceNotFoundException");
    } catch (Exception e) {
        System.out.println("TEST FAILED: Caught wrong exception: " + e);
        e.printStackTrace(System.out);
        ok = false;
    }
    //Now check that when caching is enabled, setAttribute is working
    System.out.println("\nTest that we are not receiving ServiceNotFoundException");
    try {
        Attribute attr = new Attribute("SomethingCached", "Some string");
        mbs.setAttribute(mmbName, attr);
        System.out.println("No exception thrown");
    } catch (Exception e) {
        System.out.println("TEST FAILED: Caught an exception: " + e);
        e.printStackTrace(System.out);
        ok = false;
    }
    if (ok)
        System.out.println("Test passed");
    else {
        System.out.println("TEST FAILED");
        throw new Exception("TEST FAILED");
    }
}
Also used : RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ModelMBean(javax.management.modelmbean.ModelMBean) Attribute(javax.management.Attribute) DescriptorSupport(javax.management.modelmbean.DescriptorSupport) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanException(javax.management.MBeanException) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ObjectName(javax.management.ObjectName) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) ServiceNotFoundException(javax.management.ServiceNotFoundException) Descriptor(javax.management.Descriptor) MBeanException(javax.management.MBeanException) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport) MBeanServer(javax.management.MBeanServer)

Example 54 with ModelMBeanAttributeInfo

use of javax.management.modelmbean.ModelMBeanAttributeInfo in project geode by apache.

the class MX4JModelMBean method getAttribute.

public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
    if (attribute == null)
        throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_NAME_CANNOT_BE_NULL.toLocalizedString()));
    Logger logger = getLogger();
    // I want the real info, not its clone
    ModelMBeanInfo info = getModelMBeanInfo();
    if (info == null)
        throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_MODELMBEANINFO_IS_NULL.toLocalizedString());
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("ModelMBeanInfo is: " + info);
    // This is a clone, we use it read only
    ModelMBeanAttributeInfo attrInfo = info.getAttribute(attribute);
    if (attrInfo == null)
        throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_CANNOT_FIND_MODELMBEANATTRIBUTEINFO_FOR_ATTRIBUTE_0.toLocalizedString(attribute));
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Attribute info is: " + attrInfo);
    if (!attrInfo.isReadable())
        throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_0_IS_NOT_READABLE.toLocalizedString(attribute));
    // This returns a clone of the mbean descriptor, we use it read only
    Descriptor mbeanDescriptor = info.getMBeanDescriptor();
    if (mbeanDescriptor == null)
        throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_MBEAN_DESCRIPTOR_CANNOT_BE_NULL.toLocalizedString());
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("MBean descriptor is: " + mbeanDescriptor);
    // This descriptor is a clone
    Descriptor attributeDescriptor = attrInfo.getDescriptor();
    if (attributeDescriptor == null)
        throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_DESCRIPTOR_FOR_ATTRIBUTE_0_CANNOT_BE_NULL.toLocalizedString(attribute));
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Attribute descriptor is: " + attributeDescriptor);
    Object returnValue = null;
    String lastUpdateField = "lastUpdatedTimeStamp";
    int staleness = getStaleness(attributeDescriptor, mbeanDescriptor, lastUpdateField);
    if (staleness == ALWAYS_STALE || staleness == STALE) {
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("Value is stale");
        String getter = (String) attributeDescriptor.getFieldValue("getMethod");
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("getMethod field is: " + getter);
        if (getter == null) {
            // No getter, use default value
            returnValue = attributeDescriptor.getFieldValue("default");
            if (returnValue != null) {
                // Check if the return type is of the same type
                // As an extension allow covariant return type
                Class returned = returnValue.getClass();
                Class declared = loadClassWithContextClassLoader(attrInfo.getType());
                checkAssignability(returned, declared);
            }
            if (logger.isEnabledFor(Logger.DEBUG))
                logger.debug("getAttribute for attribute " + attribute + " returns default value: " + returnValue);
        } else {
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("Invoking attribute getter...");
            // As an extension, allow attributes to be called on target objects also
            Object target = resolveTargetObject(attributeDescriptor);
            returnValue = invokeMethod(target, getter, new Class[0], new Object[0]);
            if (logger.isEnabledFor(Logger.DEBUG))
                logger.debug("Returned value is: " + returnValue);
            if (returnValue != null) {
                // Check if the return type is of the same type
                // As an extension allow covariant return type
                Class returned = returnValue.getClass();
                Class declared = loadClassWithContextClassLoader(attrInfo.getType());
                checkAssignability(returned, declared);
            }
            // Cache the new value only if caching is needed
            if (staleness != ALWAYS_STALE) {
                attributeDescriptor.setField("value", returnValue);
                attributeDescriptor.setField(lastUpdateField, Long.valueOf(System.currentTimeMillis()));
                if (logger.isEnabledFor(Logger.TRACE))
                    logger.trace("Returned value has been cached");
                // And now replace the descriptor with the updated clone
                info.setDescriptor(attributeDescriptor, "attribute");
            }
            if (logger.isEnabledFor(Logger.DEBUG))
                logger.debug("getAttribute for attribute " + attribute + " returns invoked value: " + returnValue);
        }
    } else {
        // Return cached value
        returnValue = attributeDescriptor.getFieldValue("value");
        if (returnValue != null) {
            // Check if the return type is of the same type
            // As an extension allow covariant return type
            Class returned = returnValue.getClass();
            Class declared = loadClassWithContextClassLoader(attrInfo.getType());
            checkAssignability(returned, declared);
        }
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("getAttribute for attribute " + attribute + " returns cached value: " + returnValue);
    }
    // Puff, everything went ok
    return returnValue;
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) Descriptor(javax.management.Descriptor) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 55 with ModelMBeanAttributeInfo

use of javax.management.modelmbean.ModelMBeanAttributeInfo in project geode by apache.

the class MX4JModelMBean method addAttributeChangeNotificationListener.

public void addAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback) throws MBeanException, RuntimeOperationsException, IllegalArgumentException {
    if (listener == null)
        throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_LISTENER_CANNOT_BE_NULL.toLocalizedString()));
    AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
    if (attributeName != null) {
        filter.enableAttribute(attributeName);
    } else {
        MBeanAttributeInfo[] ai = m_modelMBeanInfo.getAttributes();
        for (int i = 0; i < ai.length; i++) {
            Descriptor d = ((ModelMBeanAttributeInfo) ai[i]).getDescriptor();
            filter.enableAttribute((String) d.getFieldValue("name"));
        }
    }
    getAttributeChangeBroadcaster().addNotificationListener(listener, filter, handback);
    Logger logger = getLogger();
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Listener " + listener + " for attribute " + attributeName + " added successfully, handback is " + handback);
}
Also used : ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) AttributeChangeNotificationFilter(javax.management.AttributeChangeNotificationFilter) Descriptor(javax.management.Descriptor) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Aggregations

ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)55 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)25 Test (org.junit.Test)18 Descriptor (javax.management.Descriptor)15 ModelMBeanOperationInfo (javax.management.modelmbean.ModelMBeanOperationInfo)9 DescriptorSupport (javax.management.modelmbean.DescriptorSupport)6 ModelMBeanInfoSupport (javax.management.modelmbean.ModelMBeanInfoSupport)6 Method (java.lang.reflect.Method)4 RuntimeOperationsException (javax.management.RuntimeOperationsException)4 FileLogger (mx4j.log.FileLogger)4 Logger (mx4j.log.Logger)4 MBeanLogger (mx4j.log.MBeanLogger)4 Attribute (javax.management.Attribute)3 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)3 MBeanServer (javax.management.MBeanServer)3 ObjectName (javax.management.ObjectName)3 ModelMBean (javax.management.modelmbean.ModelMBean)3 ModelMBeanNotificationInfo (javax.management.modelmbean.ModelMBeanNotificationInfo)3 RequiredModelMBean (javax.management.modelmbean.RequiredModelMBean)3 ArrayList (java.util.ArrayList)2