Search in sources :

Example 66 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project jetty.project by eclipse.

the class ObjectMBean method defineAttribute.

/* ------------------------------------------------------------ */
/**
     * TODO update to new behavior
     *
     * 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 method the method to define
     * @param attributeAnnotation "description" or "access:description" or "type:access:description"  where type is
     * one of: <ul>
     * <li>"Object" The field/method is on the managed object.
     * <li>"MBean" The field/method is on the mbean proxy object
     * <li>"MObject" The field/method is on the managed object and value should be converted to MBean reference
     * <li>"MMBean" The field/method is on the mbean proxy object and value should be converted to MBean reference
     * </ul>
     * the access is either "RW" or "RO".
     * @return the mbean attribute info for the method
     */
public MBeanAttributeInfo defineAttribute(Method method, ManagedAttribute attributeAnnotation) {
    // determine the name of the managed attribute
    String name = attributeAnnotation.name();
    if ("".equals(name)) {
        name = toVariableName(method.getName());
    }
    if (_attributes.contains(name)) {
        // we have an attribute named this already
        return null;
    }
    String description = attributeAnnotation.value();
    boolean readonly = attributeAnnotation.readonly();
    boolean onMBean = attributeAnnotation.proxied();
    boolean convert = false;
    // determine if we should convert
    Class<?> return_type = method.getReturnType();
    // get the component type
    Class<?> component_type = return_type;
    while (component_type.isArray()) {
        component_type = component_type.getComponentType();
    }
    // Test to see if the returnType or any of its super classes are managed objects
    convert = isAnnotationPresent(component_type, ManagedObject.class);
    String uName = name.substring(0, 1).toUpperCase(Locale.ENGLISH) + name.substring(1);
    Class<?> oClass = onMBean ? this.getClass() : _managed.getClass();
    if (LOG.isDebugEnabled())
        LOG.debug("defineAttribute {} {}:{}:{}:{}", name, onMBean, readonly, oClass, description);
    Method setter = null;
    // dig out a setter if one exists
    if (!readonly) {
        String declaredSetter = attributeAnnotation.setter();
        if (LOG.isDebugEnabled())
            LOG.debug("DeclaredSetter: {}", declaredSetter);
        Method[] methods = oClass.getMethods();
        for (int m = 0; m < methods.length; m++) {
            if ((methods[m].getModifiers() & Modifier.PUBLIC) == 0)
                continue;
            if (!"".equals(declaredSetter)) {
                // look for a declared setter
                if (methods[m].getName().equals(declaredSetter) && methods[m].getParameterCount() == 1) {
                    if (setter != null) {
                        LOG.warn("Multiple setters for mbean attr {} in {}", name, oClass);
                        continue;
                    }
                    setter = methods[m];
                    if (!component_type.equals(methods[m].getParameterTypes()[0])) {
                        LOG.warn("Type conflict for mbean attr {} in {}", name, oClass);
                        continue;
                    }
                    if (LOG.isDebugEnabled())
                        LOG.debug("Declared Setter: " + declaredSetter);
                }
            }
            // look for a setter
            if (methods[m].getName().equals("set" + uName) && methods[m].getParameterCount() == 1) {
                if (setter != null) {
                    LOG.warn("Multiple setters for mbean attr {} in {}", name, oClass);
                    continue;
                }
                setter = methods[m];
                if (!return_type.equals(methods[m].getParameterTypes()[0])) {
                    LOG.warn("Type conflict for mbean attr {} in {}", name, oClass);
                    continue;
                }
            }
        }
    }
    if (convert) {
        if (component_type == null) {
            LOG.warn("No mbean type for {} on {}", name, _managed.getClass());
            return null;
        }
        if (component_type.isPrimitive() && !component_type.isArray()) {
            LOG.warn("Cannot convert mbean primative {}", name);
            return null;
        }
        if (LOG.isDebugEnabled())
            LOG.debug("passed convert checks {} for type {}", name, component_type);
    }
    try {
        // Remember the methods
        _getters.put(name, method);
        _setters.put(name, setter);
        MBeanAttributeInfo info = null;
        if (convert) {
            _convert.add(name);
            if (component_type.isArray()) {
                info = new MBeanAttributeInfo(name, OBJECT_NAME_ARRAY_CLASS, description, true, setter != null, method.getName().startsWith("is"));
            } else {
                info = new MBeanAttributeInfo(name, OBJECT_NAME_CLASS, description, true, setter != null, method.getName().startsWith("is"));
            }
        } else {
            info = new MBeanAttributeInfo(name, description, method, setter);
        }
        _attributes.add(name);
        return info;
    } catch (Exception e) {
        LOG.warn(e);
        throw new IllegalArgumentException(e.toString());
    }
}
Also used : ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) Method(java.lang.reflect.Method) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) AttributeNotFoundException(javax.management.AttributeNotFoundException) ReflectionException(javax.management.ReflectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException)

Example 67 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project jetty.project by eclipse.

the class ObjectMBean method getMBeanInfo.

public MBeanInfo getMBeanInfo() {
    try {
        if (_info == null) {
            // Start with blank lazy lists attributes etc.
            String desc = null;
            List<MBeanAttributeInfo> attributes = new ArrayList<MBeanAttributeInfo>();
            List<MBeanConstructorInfo> constructors = new ArrayList<MBeanConstructorInfo>();
            List<MBeanOperationInfo> operations = new ArrayList<MBeanOperationInfo>();
            List<MBeanNotificationInfo> notifications = new ArrayList<MBeanNotificationInfo>();
            // Find list of classes that can influence the mbean
            Class<?> o_class = _managed.getClass();
            List<Class<?>> influences = new ArrayList<Class<?>>();
            // always add MBean itself
            influences.add(this.getClass());
            influences = findInfluences(influences, _managed.getClass());
            if (LOG.isDebugEnabled())
                LOG.debug("Influence Count: {}", influences.size());
            // Process Type Annotations
            ManagedObject primary = o_class.getAnnotation(ManagedObject.class);
            if (primary != null) {
                desc = primary.value();
            } else {
                if (LOG.isDebugEnabled())
                    LOG.debug("No @ManagedObject declared on {}", _managed.getClass());
            }
            // For each influence
            for (int i = 0; i < influences.size(); i++) {
                Class<?> oClass = influences.get(i);
                ManagedObject typeAnnotation = oClass.getAnnotation(ManagedObject.class);
                if (LOG.isDebugEnabled())
                    LOG.debug("Influenced by: " + oClass.getCanonicalName());
                if (typeAnnotation == null) {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Annotations not found for: {}", oClass.getCanonicalName());
                    continue;
                }
                for (Method method : oClass.getDeclaredMethods()) {
                    ManagedAttribute methodAttributeAnnotation = method.getAnnotation(ManagedAttribute.class);
                    if (methodAttributeAnnotation != null) {
                        // TODO sort out how a proper name could get here, its a method name as an attribute at this point.
                        if (LOG.isDebugEnabled())
                            LOG.debug("Attribute Annotation found for: {}", method.getName());
                        MBeanAttributeInfo mai = defineAttribute(method, methodAttributeAnnotation);
                        if (mai != null) {
                            attributes.add(mai);
                        }
                    }
                    ManagedOperation methodOperationAnnotation = method.getAnnotation(ManagedOperation.class);
                    if (methodOperationAnnotation != null) {
                        if (LOG.isDebugEnabled())
                            LOG.debug("Method Annotation found for: {}", method.getName());
                        MBeanOperationInfo oi = defineOperation(method, methodOperationAnnotation);
                        if (oi != null) {
                            operations.add(oi);
                        }
                    }
                }
            }
            _info = new MBeanInfo(o_class.getName(), desc, (MBeanAttributeInfo[]) attributes.toArray(new MBeanAttributeInfo[attributes.size()]), (MBeanConstructorInfo[]) constructors.toArray(new MBeanConstructorInfo[constructors.size()]), (MBeanOperationInfo[]) operations.toArray(new MBeanOperationInfo[operations.size()]), (MBeanNotificationInfo[]) notifications.toArray(new MBeanNotificationInfo[notifications.size()]));
        }
    } catch (RuntimeException e) {
        LOG.warn(e);
        throw e;
    }
    return _info;
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ManagedAttribute(org.eclipse.jetty.util.annotation.ManagedAttribute) ManagedOperation(org.eclipse.jetty.util.annotation.ManagedOperation) MBeanConstructorInfo(javax.management.MBeanConstructorInfo) MBeanNotificationInfo(javax.management.MBeanNotificationInfo) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject)

Example 68 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project hazelcast by hazelcast.

the class HazelcastMBean method attributeInfos.

private MBeanAttributeInfo[] attributeInfos() {
    MBeanAttributeInfo[] array = new MBeanAttributeInfo[attributeMap.size()];
    int i = 0;
    for (BeanInfo beanInfo : attributeMap.values()) {
        array[i++] = beanInfo.getAttributeInfo();
    }
    return array;
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Example 69 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project jmxtrans by jmxtrans.

the class TreeWalker2 method walkTree.

public void walkTree(MBeanServerConnection connection, Server server) throws Exception {
    // key here is null, null returns everything!
    Set<ObjectName> mbeans = connection.queryNames(null, null);
    for (ObjectName name : mbeans) {
        MBeanInfo info = connection.getMBeanInfo(name);
        MBeanAttributeInfo[] attrs = info.getAttributes();
        Query.Builder queryBuilder = Query.builder().setObj(name.getCanonicalName()).addOutputWriterFactory(new StdOutWriter(ImmutableList.<String>of(), false, false, Collections.<String, Object>emptyMap()));
        for (MBeanAttributeInfo attrInfo : attrs) {
            queryBuilder.addAttr(attrInfo.getName());
        }
        Query query = queryBuilder.build();
        try {
            Iterable<Result> results = server.execute(query);
            query.runOutputWritersForQuery(server, results);
        } catch (AttributeNotFoundException anfe) {
            log.error("Error", anfe);
        }
    }
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanInfo(javax.management.MBeanInfo) Query(com.googlecode.jmxtrans.model.Query) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) StdOutWriter(com.googlecode.jmxtrans.model.output.StdOutWriter) Result(com.googlecode.jmxtrans.model.Result)

Example 70 with MBeanAttributeInfo

use of javax.management.MBeanAttributeInfo in project jmxtrans by jmxtrans.

the class Query method fetchResults.

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());
        }
    } 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();
}
Also used : MBeanInfo(javax.management.MBeanInfo) AttributeList(javax.management.AttributeList) UnmarshalException(java.rmi.UnmarshalException) ObjectInstance(javax.management.ObjectInstance) ToString(lombok.ToString) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Aggregations

MBeanAttributeInfo (javax.management.MBeanAttributeInfo)106 MBeanInfo (javax.management.MBeanInfo)75 ObjectName (javax.management.ObjectName)45 MBeanOperationInfo (javax.management.MBeanOperationInfo)24 Test (org.junit.Test)21 MBeanServer (javax.management.MBeanServer)15 ArrayList (java.util.ArrayList)13 AttributeNotFoundException (javax.management.AttributeNotFoundException)12 ReflectionException (javax.management.ReflectionException)12 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)11 IOException (java.io.IOException)10 AttributeList (javax.management.AttributeList)10 Attribute (javax.management.Attribute)9 InstanceNotFoundException (javax.management.InstanceNotFoundException)9 IntrospectionException (javax.management.IntrospectionException)9 MBeanParameterInfo (javax.management.MBeanParameterInfo)9 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)9 HashMap (java.util.HashMap)8 MBeanConstructorInfo (javax.management.MBeanConstructorInfo)7 MBeanException (javax.management.MBeanException)7