Search in sources :

Example 46 with Descriptor

use of javax.management.Descriptor in project Activiti by Activiti.

the class MBeanInfoAssembler method extractMbeanOperations.

private void extractMbeanOperations(Object managedBean, Set<ManagedOperationInfo> operations, Set<ModelMBeanOperationInfo> mBeanOperations) {
    for (ManagedOperationInfo info : operations) {
        ModelMBeanOperationInfo mbean = new ModelMBeanOperationInfo(info.getDescription(), info.getOperation());
        Descriptor opDesc = mbean.getDescriptor();
        mbean.setDescriptor(opDesc);
        mBeanOperations.add(mbean);
        LOG.trace("Assembled operation: {}", mbean);
    }
}
Also used : ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) Descriptor(javax.management.Descriptor)

Example 47 with Descriptor

use of javax.management.Descriptor in project camel by apache.

the class MBeanInfoAssembler method extractMbeanAttributes.

private void extractMbeanAttributes(Object managedBean, Map<String, ManagedAttributeInfo> attributes, Set<ModelMBeanAttributeInfo> mBeanAttributes, Set<ModelMBeanOperationInfo> mBeanOperations) throws IntrospectionException {
    for (ManagedAttributeInfo info : attributes.values()) {
        ModelMBeanAttributeInfo mbeanAttribute = new ModelMBeanAttributeInfo(info.getKey(), info.getDescription(), info.getGetter(), info.getSetter());
        // add missing attribute descriptors, this is needed to have attributes accessible
        Descriptor desc = mbeanAttribute.getDescriptor();
        desc.setField("mask", info.isMask() ? "true" : "false");
        if (info.getGetter() != null) {
            desc.setField("getMethod", info.getGetter().getName());
            // attribute must also be added as mbean operation
            ModelMBeanOperationInfo mbeanOperation = new ModelMBeanOperationInfo(info.getKey(), info.getGetter());
            Descriptor opDesc = mbeanOperation.getDescriptor();
            opDesc.setField("mask", info.isMask() ? "true" : "false");
            mbeanOperation.setDescriptor(opDesc);
            mBeanOperations.add(mbeanOperation);
        }
        if (info.getSetter() != null) {
            desc.setField("setMethod", info.getSetter().getName());
            // attribute must also be added as mbean operation
            ModelMBeanOperationInfo mbeanOperation = new ModelMBeanOperationInfo(info.getKey(), info.getSetter());
            mBeanOperations.add(mbeanOperation);
        }
        mbeanAttribute.setDescriptor(desc);
        mBeanAttributes.add(mbeanAttribute);
        LOG.trace("Assembled attribute: {}", mbeanAttribute);
    }
}
Also used : ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) Descriptor(javax.management.Descriptor)

Example 48 with Descriptor

use of javax.management.Descriptor in project camel by apache.

the class MBeanInfoAssembler method extractMbeanOperations.

private void extractMbeanOperations(Object managedBean, Set<ManagedOperationInfo> operations, Set<ModelMBeanOperationInfo> mBeanOperations) {
    for (ManagedOperationInfo info : operations) {
        ModelMBeanOperationInfo mbean = new ModelMBeanOperationInfo(info.getDescription(), info.getOperation());
        Descriptor opDesc = mbean.getDescriptor();
        opDesc.setField("mask", info.isMask() ? "true" : "false");
        mbean.setDescriptor(opDesc);
        mBeanOperations.add(mbean);
        LOG.trace("Assembled operation: {}", mbean);
    }
}
Also used : ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) Descriptor(javax.management.Descriptor)

Example 49 with Descriptor

use of javax.management.Descriptor 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 50 with Descriptor

use of javax.management.Descriptor 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

Descriptor (javax.management.Descriptor)73 RuntimeOperationsException (javax.management.RuntimeOperationsException)18 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)16 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)16 DescriptorSupport (javax.management.modelmbean.DescriptorSupport)11 ModelMBeanOperationInfo (javax.management.modelmbean.ModelMBeanOperationInfo)11 Method (java.lang.reflect.Method)10 ImmutableDescriptor (javax.management.ImmutableDescriptor)9 MBeanException (javax.management.MBeanException)9 AttributeNotFoundException (javax.management.AttributeNotFoundException)8 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)8 MBeanInfo (javax.management.MBeanInfo)8 MBeanServer (javax.management.MBeanServer)8 ObjectName (javax.management.ObjectName)8 ServiceNotFoundException (javax.management.ServiceNotFoundException)8 ArrayList (java.util.ArrayList)7 InstanceNotFoundException (javax.management.InstanceNotFoundException)7 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)7 MBeanOperationInfo (javax.management.MBeanOperationInfo)7 MBeanParameterInfo (javax.management.MBeanParameterInfo)7