Example usage for javax.management NotCompliantMBeanException NotCompliantMBeanException

List of usage examples for javax.management NotCompliantMBeanException NotCompliantMBeanException

Introduction

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

Prototype

public NotCompliantMBeanException(String message) 

Source Link

Document

Constructor that allows a specific error message to be specified.

Usage

From source file:com.ecyrd.management.SimpleMBean.java

/**
 *  Create a new SimpleMBean// w w w . ja  v  a 2 s.c o  m
 *  
 *  @throws NotCompliantMBeanException {@inheritDoc}
 */
protected SimpleMBean() throws NotCompliantMBeanException {
    //
    //  Create attributes
    //
    String[] attlist = getAttributeNames();
    MBeanAttributeInfo[] attributes = null;

    if (attlist != null) {
        attributes = new MBeanAttributeInfo[attlist.length];

        for (int i = 0; i < attlist.length; i++) {
            String name = attlist[i];
            name = StringUtils.capitalize(name);
            Method getter = findGetterSetter(getClass(), "get" + name, null);

            if (getter == null)
                getter = findGetterSetter(getClass(), "is" + name, null);

            Method setter = null;

            if (getter != null) {
                setter = findGetterSetter(getClass(), "set" + name, getter.getReturnType());
            }

            //
            //  Check, if there's a description available
            //
            Method descriptor = findGetterSetter(getClass(), "get" + name + "Description", null);
            String description = "";

            if (descriptor != null) {
                try {
                    description = (String) descriptor.invoke(this, (Object[]) null);
                } catch (Exception e) {
                    description = "Exception: " + e.getMessage();
                }
            }

            MBeanAttributeInfo info;
            try {
                info = new MBeanAttributeInfo(attlist[i], description, getter, setter);
            } catch (IntrospectionException e) {
                throw new NotCompliantMBeanException(e.getMessage());
            }

            attributes[i] = info;
        }
    }

    //
    //  Create operations.
    //
    String[] oplist = getMethodNames();
    MBeanOperationInfo[] operations = new MBeanOperationInfo[oplist.length];

    Method[] methods = getClass().getMethods();

    for (int i = 0; i < oplist.length; i++) {
        Method method = null;

        for (int m = 0; m < methods.length; m++) {
            if (methods[m].getName().equals(oplist[i])) {
                method = methods[m];
            }
        }

        if (method == null) {
            throw new NotCompliantMBeanException(
                    "Class declares method " + oplist[i] + ", yet does not implement it!");
        }

        MBeanOperationInfo info = new MBeanOperationInfo(method.getName(), method);

        operations[i] = info;
    }

    //
    //  Create the actual BeanInfo instance.
    //
    MBeanConstructorInfo[] constructors = null;
    MBeanNotificationInfo[] notifications = null;

    m_beanInfo = new MBeanInfo(getClass().getName(), getDescription(), attributes, constructors, operations,
            notifications);
}

From source file:org.ops4j.gaderian.management.TestMBeanRegistry.java

/**
 * Tests the handling of a not compliant mbean
 *//*from ww  w  . j a va  2 s .  c om*/
public void testNotCompliantHandling() throws Exception {
    Calculator calculatorMBean = new CalculatorImpl();
    ObjectName objectName = new ObjectName("gaderian:module=test");

    // Training
    expect(server.registerMBean(calculatorMBean, objectName))
            .andThrow(new NotCompliantMBeanException("Not compliant"));
    replayAllRegisteredMocks();

    // Registration must fail since the bean is not mbean compliant and a management
    // interface is not provided
    MBeanRegistry mbeanRegistry = new MBeanRegistryImpl(errorHandler, log, server, objectNameBuilder, null);
    try {
        mbeanRegistry.registerMBean(calculatorMBean, null, objectName);
        fail("Not compliant MBean registered");
    } catch (NotCompliantMBeanException expected) {
    }

    verifyAllRegisteredMocks();
}

From source file:org.ops4j.gaderian.management.TestMBeanRegistry.java

/**
 * Ensures that a bean that doesn't implement one of the standard JMX
 * interfaces (like DynamicMBean) is registered as StandardMBean.
 *//*from  www . j a v  a  2s .co m*/
public void testStandardMBean() throws Exception {
    Calculator calculatorMBean = new CalculatorImpl();
    ObjectName objectName = new ObjectName("gaderian:module=test");

    // Training
    expect(server.registerMBean(calculatorMBean, objectName))
            .andThrow(new NotCompliantMBeanException("Not compliant"));

    expect(server.registerMBean(isA(StandardMBean.class), eq(objectName)))
            .andReturn(new ObjectInstance(objectName, StandardMBean.class.getName()));

    replayAllRegisteredMocks();

    MBeanRegistry mbeanRegistry = new MBeanRegistryImpl(errorHandler, log, server, objectNameBuilder, null);

    // Management interface is specified
    mbeanRegistry.registerMBean(calculatorMBean, Calculator.class, objectName);

    verifyAllRegisteredMocks();
}