Example usage for javax.management.modelmbean ModelMBeanAttributeInfo ModelMBeanAttributeInfo

List of usage examples for javax.management.modelmbean ModelMBeanAttributeInfo ModelMBeanAttributeInfo

Introduction

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

Prototype


public ModelMBeanAttributeInfo(String name, String description, Method getter, Method setter)
        throws javax.management.IntrospectionException 

Source Link

Document

Constructs a ModelMBeanAttributeInfo object with a default descriptor.

Usage

From source file:org.jolokia.jmx.JolokiaMBeanServerTest.java

@Test
public void withModelMBean() throws MBeanException, InvalidTargetObjectTypeException, InstanceNotFoundException,
        InstanceAlreadyExistsException, NotCompliantMBeanException, MalformedObjectNameException,
        NoSuchMethodException, IntrospectionException {

    RequiredModelMBean modelMBean = new RequiredModelMBean();

    ModelMBeanInfo mbi = new ModelMBeanInfoSupport(JsonAnnoPlainTest.class.getName(), "JsonMBean Test",
            new ModelMBeanAttributeInfo[] { new ModelMBeanAttributeInfo("DeepDive", "description",
                    JsonAnnoPlainTest.class.getDeclaredMethod("getDeepDive"), null) },
            new ModelMBeanConstructorInfo[] {}, new ModelMBeanOperationInfo[] {},
            new ModelMBeanNotificationInfo[] {});
    modelMBean.setModelMBeanInfo(mbi);//  w  w w.j  a  va 2  s. com
    modelMBean.setManagedResource(new JsonAnnoPlainTest(), "ObjectReference");

    JolokiaMBeanServer server = new JolokiaMBeanServer();

    ObjectName oName = new ObjectName("test:type=jsonMBean");

    server.registerMBean(modelMBean, oName);
    MBeanServer plattformServer = ManagementFactory.getPlatformMBeanServer();

    Assert.assertTrue(plattformServer.isRegistered(oName));
    Assert.assertTrue(server.isRegistered(oName));

    server.unregisterMBean(oName);
    Assert.assertFalse(plattformServer.isRegistered(oName));
    Assert.assertFalse(server.isRegistered(oName));

}

From source file:com.espertech.esper.metrics.jmx.CommonJMXUtil.java

private static ModelMBeanAttributeInfo[] extractAttributeInfo(Object object) {
    Map<String, Method> getters = new HashMap<String, Method>();
    Map<String, Method> setters = new HashMap<String, Method>();
    Map<String, String> descriptions = new HashMap<String, String>();
    for (Method m : object.getClass().getMethods()) {
        JmxGetter getter = m.getAnnotation(JmxGetter.class);
        if (getter != null) {
            getters.put(getter.name(), m);
            descriptions.put(getter.name(), getter.description());
        }//from w  ww  .j a v  a2 s  .c om
        JmxSetter setter = m.getAnnotation(JmxSetter.class);
        if (setter != null) {
            setters.put(setter.name(), m);
            descriptions.put(setter.name(), setter.description());
        }
    }

    Set<String> attributes = new HashSet<String>(getters.keySet());
    attributes.addAll(setters.keySet());
    List<ModelMBeanAttributeInfo> infos = new ArrayList<ModelMBeanAttributeInfo>();
    for (String name : attributes) {
        try {
            Method getter = getters.get(name);
            Method setter = setters.get(name);
            ModelMBeanAttributeInfo info = new ModelMBeanAttributeInfo(name, descriptions.get(name), getter,
                    setter);
            Descriptor descriptor = info.getDescriptor();
            if (getter != null)
                descriptor.setField("getMethod", getter.getName());
            if (setter != null)
                descriptor.setField("setMethod", setter.getName());
            info.setDescriptor(descriptor);
            infos.add(info);
        } catch (IntrospectionException e) {
            throw new RuntimeException(e);
        }
    }

    return infos.toArray(new ModelMBeanAttributeInfo[infos.size()]);
}

From source file:net.lightbody.bmp.proxy.jetty.util.jmx.ModelMBeanImpl.java

/** Define an attribute on the managed object.
 * The meta data is defined by looking for standard getter and
 * setter methods. Descriptions are obtained with a call to
 * findDescription with the attribute name.
 * @param name The name of the attribute. Normal java bean
 * capitlization is enforced on this name.
 * @param writable If false, do not look for a setter.
 * @param onMBean .//from w  ww .j a va2 s  .c o m
 */
public synchronized void defineAttribute(String name, boolean writable, boolean onMBean) {
    _dirty = true;

    String uName = name.substring(0, 1).toUpperCase() + name.substring(1);
    name = java.beans.Introspector.decapitalize(name);
    Class oClass = onMBean ? this.getClass() : _object.getClass();

    Class type = null;
    Method getter = null;
    Method setter = null;
    Method[] methods = oClass.getMethods();
    for (int m = 0; m < methods.length; m++) {
        if ((methods[m].getModifiers() & Modifier.PUBLIC) == 0)
            continue;

        // Look for a getter
        if (methods[m].getName().equals("get" + uName) && methods[m].getParameterTypes().length == 0) {
            if (getter != null)
                throw new IllegalArgumentException("Multiple getters for attr " + name);
            getter = methods[m];
            if (type != null && !type.equals(methods[m].getReturnType()))
                throw new IllegalArgumentException("Type conflict for attr " + name);
            type = methods[m].getReturnType();
        }

        // Look for an is getter
        if (methods[m].getName().equals("is" + uName) && methods[m].getParameterTypes().length == 0) {
            if (getter != null)
                throw new IllegalArgumentException("Multiple getters for attr " + name);
            getter = methods[m];
            if (type != null && !type.equals(methods[m].getReturnType()))
                throw new IllegalArgumentException("Type conflict for attr " + name);
            type = methods[m].getReturnType();
        }

        // look for a setter
        if (writable && methods[m].getName().equals("set" + uName)
                && methods[m].getParameterTypes().length == 1) {
            if (setter != null)
                throw new IllegalArgumentException("Multiple setters for attr " + name);
            setter = methods[m];
            if (type != null && !type.equals(methods[m].getParameterTypes()[0]))
                throw new IllegalArgumentException("Type conflict for attr " + name);
            type = methods[m].getParameterTypes()[0];
        }
    }

    if (getter == null && setter == null)
        throw new IllegalArgumentException("No getter or setters found for " + name);

    try {
        // Remember the methods
        _getter.put(name, getter);
        _setter.put(name, setter);
        // create and add the info
        _attributes.add(new ModelMBeanAttributeInfo(name, findDescription(name), getter, setter));
    } catch (Exception e) {
        log.warn(LogSupport.EXCEPTION, e);
        throw new IllegalArgumentException(e.toString());
    }
}