Search in sources :

Example 31 with ModelMBeanAttributeInfo

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

the class MX4JModelMBean method removeAttributeChangeNotificationListener.

// Not in the spec but needed
private void removeAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback) throws MBeanException, RuntimeOperationsException, ListenerNotFoundException {
    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().removeNotificationListener(listener, filter, handback);
    Logger logger = getLogger();
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Listener " + listener + " for attribute " + attributeName + " removed 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)

Example 32 with ModelMBeanAttributeInfo

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

the class MX4JModelMBean method setAttribute.

public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
    if (attribute == null)
        throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_CANNOT_BE_NULL.toLocalizedString()));
    Logger logger = getLogger();
    // No need to synchronize: I work mostly on clones
    // 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);
    String attrName = attribute.getName();
    Object attrValue = attribute.getValue();
    // This is a clone, we use it read only
    ModelMBeanAttributeInfo attrInfo = info.getAttribute(attrName);
    if (attrInfo == null)
        throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_CANNOT_FIND_MODELMBEANATTRIBUTEINFO_FOR_ATTRIBUTE_0.toLocalizedString(attrName));
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Attribute info is: " + attrInfo);
    if (!attrInfo.isWritable())
        throw new AttributeNotFoundException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_0_IS_NOT_WRITABLE.toLocalizedString(attrName));
    // 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(attrName));
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Attribute descriptor is: " + attributeDescriptor);
    String lastUpdateField = "lastUpdatedTimeStamp";
    Object oldValue = null;
    try {
        oldValue = getAttribute(attrName);
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Previous value of attribute " + attrName + ": " + oldValue);
    } catch (Exception x) {
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Cannot get previous value of attribute " + attrName, x);
    }
    // Check if setMethod is present
    String method = (String) attributeDescriptor.getFieldValue("setMethod");
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("setMethod field is: " + method);
    if (method != null) {
        Class declared = loadClassWithContextClassLoader(attrInfo.getType());
        if (attrValue != null) {
            Class parameter = attrValue.getClass();
            checkAssignability(parameter, declared);
        }
        // As an extension, allow attributes to be called on target objects also
        Object target = resolveTargetObject(attributeDescriptor);
        invokeMethod(target, method, new Class[] { declared }, new Object[] { attrValue });
        // Cache the value only if currencyTimeLimit is not 0, ie it is not always stale
        int staleness = getStaleness(attributeDescriptor, mbeanDescriptor, lastUpdateField);
        if (staleness != ALWAYS_STALE) {
            attributeDescriptor.setField("value", attrValue);
            attributeDescriptor.setField(lastUpdateField, Long.valueOf(System.currentTimeMillis()));
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("Attribute's value has been cached");
        } else {
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("Always stale, avoiding to cache attribute's value");
        }
    } else {
        if (attrValue != null) {
            Class parameter = attrValue.getClass();
            Class declared = loadClassWithContextClassLoader(attrInfo.getType());
            checkAssignability(parameter, declared);
        }
        // Always store the value in the descriptor: no setMethod
        attributeDescriptor.setField("value", attrValue);
    }
    // And now replace the descriptor with the updated clone
    info.setDescriptor(attributeDescriptor, "attribute");
    // Send notifications to listeners
    if (logger.isEnabledFor(Logger.TRACE))
        logger.trace("Sending attribute change notifications");
    sendAttributeChangeNotification(new Attribute(attrName, oldValue), attribute);
    // Persist this ModelMBean
    boolean persistNow = shouldPersistNow(attributeDescriptor, mbeanDescriptor, lastUpdateField);
    if (persistNow) {
        if (logger.isEnabledFor(Logger.TRACE))
            logger.trace("Persisting this ModelMBean...");
        try {
            store();
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("ModelMBean persisted successfully");
        } catch (Exception x) {
            logger.error(LocalizedStrings.MX4JModelMBean_CANNOT_STORE_MODELMBEAN_AFTER_SETATTRIBUTE, x);
            if (x instanceof MBeanException)
                throw (MBeanException) x;
            else
                throw new MBeanException(x);
        }
    }
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) Attribute(javax.management.Attribute) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) AttributeNotFoundException(javax.management.AttributeNotFoundException) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedObjectNameException(javax.management.MalformedObjectNameException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ListenerNotFoundException(javax.management.ListenerNotFoundException) ImplementationException(mx4j.ImplementationException) RuntimeOperationsException(javax.management.RuntimeOperationsException) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) Descriptor(javax.management.Descriptor) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 33 with ModelMBeanAttributeInfo

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

the class StatisticAttributeInfo method createAttributeInfo.

@Override
public ModelMBeanAttributeInfo createAttributeInfo() {
    Descriptor desc = new DescriptorSupport(new String[] { "name=" + this.displayName, // always stale
    "descriptorType=attribute", // always stale
    "currencyTimeLimit=-1", "displayName=" + this.displayName, "getMethod=getValue" });
    Assert.assertTrue(this.stat != null, "Stat target object is null!");
    desc.setField("targetObject", this.stat);
    ModelMBeanAttributeInfo info = new // name
    ModelMBeanAttributeInfo(// name
    this.displayName, // type
    this.type, // description
    this.description, // isReadable
    this.readable, // isWritable
    this.writeable, // isIs
    this.is, desc);
    return info;
}
Also used : ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) Descriptor(javax.management.Descriptor) DescriptorSupport(javax.management.modelmbean.DescriptorSupport)

Example 34 with ModelMBeanAttributeInfo

use of javax.management.modelmbean.ModelMBeanAttributeInfo in project kernel by exoplatform.

the class ExoMBeanInfoBuilder method build.

/**
 * Build the info.
 *
 * @return returns the info
 * @throws IllegalStateException raised by any build time issue
 */
public ModelMBeanInfo build() throws IllegalStateException {
    String mbeanDescription = "Exo model mbean";
    if (typeMD.getDescription() != null) {
        mbeanDescription = typeMD.getDescription();
    }
    // 
    ArrayList<ModelMBeanOperationInfo> operations = new ArrayList<ModelMBeanOperationInfo>();
    for (ManagedMethodMetaData methodMD : typeMD.getMethods()) {
        ModelMBeanOperationInfo operationInfo = buildOperationInfo(methodMD.getMethod(), methodMD.getDescription(), Role.OP, methodMD.getParameters(), methodMD.getImpact());
        operations.add(operationInfo);
    }
    // 
    Map<String, ModelMBeanAttributeInfo> attributeInfos = new HashMap<String, ModelMBeanAttributeInfo>();
    for (ManagedPropertyMetaData propertyMD : typeMD.getProperties()) {
        Method getter = propertyMD.getGetter();
        if (getter != null) {
            Role role;
            String getterName = getter.getName();
            if (getterName.startsWith("get") && getterName.length() > 3) {
                role = Role.GET;
            } else if (getterName.startsWith("is") && getterName.length() > 2) {
                role = Role.IS;
            } else {
                throw new AssertionError();
            }
            Collection<ManagedMethodParameterMetaData> blah = Collections.emptyList();
            ModelMBeanOperationInfo operationInfo = buildOperationInfo(getter, propertyMD.getGetterDescription(), role, blah, ImpactType.READ);
            operations.add(operationInfo);
        }
        // 
        Method setter = propertyMD.getSetter();
        if (setter != null) {
            ManagedMethodParameterMetaData s = new ManagedMethodParameterMetaData(0);
            s.setDescription(propertyMD.getSetterParameter().getDescription());
            s.setName(propertyMD.getSetterParameter().getName());
            Collection<ManagedMethodParameterMetaData> blah = Collections.singletonList(s);
            ModelMBeanOperationInfo operationInfo = buildOperationInfo(setter, propertyMD.getSetterDescription(), Role.SET, blah, ImpactType.IDEMPOTENT_WRITE);
            operations.add(operationInfo);
        }
        // 
        try {
            String attributeDescription = propertyMD.getDescription() != null ? propertyMD.getDescription() : ("Managed attribute " + propertyMD.getName());
            // 
            ModelMBeanAttributeInfo attributeInfo = new ModelMBeanAttributeInfo(propertyMD.getName(), attributeDescription, getter, setter);
            // 
            Descriptor attributeDescriptor = attributeInfo.getDescriptor();
            if (getter != null) {
                attributeDescriptor.setField("getMethod", getter.getName());
            }
            if (setter != null) {
                attributeDescriptor.setField("setMethod", setter.getName());
            }
            attributeDescriptor.setField("currencyTimeLimit", "-1");
            attributeDescriptor.setField("persistPolicy", "Never");
            attributeInfo.setDescriptor(attributeDescriptor);
            // 
            ModelMBeanAttributeInfo previous = attributeInfos.put(propertyMD.getName(), attributeInfo);
            if (previous != null) {
                throw new IllegalArgumentException();
            }
        } catch (IntrospectionException e) {
            throw new AssertionError(e);
        }
    }
    // 
    return new ModelMBeanInfoSupport(typeMD.getType().getName(), mbeanDescription, attributeInfos.values().toArray(new ModelMBeanAttributeInfo[attributeInfos.size()]), new ModelMBeanConstructorInfo[0], operations.toArray(new ModelMBeanOperationInfo[operations.size()]), new ModelMBeanNotificationInfo[0]);
}
Also used : ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) ManagedPropertyMetaData(org.exoplatform.management.spi.ManagedPropertyMetaData) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IntrospectionException(javax.management.IntrospectionException) ManagedMethodMetaData(org.exoplatform.management.spi.ManagedMethodMetaData) Method(java.lang.reflect.Method) ManagedMethodParameterMetaData(org.exoplatform.management.spi.ManagedMethodParameterMetaData) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) Descriptor(javax.management.Descriptor) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport)

Example 35 with ModelMBeanAttributeInfo

use of javax.management.modelmbean.ModelMBeanAttributeInfo in project orientdb by orientechnologies.

the class OPerformanceStatisticManagerMBean method populateReadSpeedFromFile.

private void populateReadSpeedFromFile(List<MBeanAttributeInfo> performanceAttributes, Collection<String> components) {
    final MBeanAttributeInfo readSpeedFromFile = new ModelMBeanAttributeInfo(READ_SPEED_FROM_FILE, long.class.getName(), "Read speed from file system in pages per second", true, false, false);
    performanceAttributes.add(readSpeedFromFile);
    for (String component : components) {
        final MBeanAttributeInfo componentReadSpeedFromFile = new ModelMBeanAttributeInfo(READ_SPEED_FROM_FILE + COMPONENT_SEPARATOR + component, long.class.getName(), "Read speed from file system in pages per second for component " + component, true, false, false);
        performanceAttributes.add(componentReadSpeedFromFile);
    }
}
Also used : ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo)

Aggregations

ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)56 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)25 Test (org.junit.Test)18 Descriptor (javax.management.Descriptor)16 ModelMBeanOperationInfo (javax.management.modelmbean.ModelMBeanOperationInfo)10 ModelMBeanInfoSupport (javax.management.modelmbean.ModelMBeanInfoSupport)7 DescriptorSupport (javax.management.modelmbean.DescriptorSupport)6 Method (java.lang.reflect.Method)5 RuntimeOperationsException (javax.management.RuntimeOperationsException)4 FileLogger (mx4j.log.FileLogger)4 Logger (mx4j.log.Logger)4 MBeanLogger (mx4j.log.MBeanLogger)4 ArrayList (java.util.ArrayList)3 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