Search in sources :

Example 31 with Descriptor

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

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

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

use of javax.management.Descriptor in project kernel by exoplatform.

the class ExoMBeanInfoBuilder method buildOperationInfo.

private ModelMBeanOperationInfo buildOperationInfo(Method method, String description, Role role, Collection<ManagedMethodParameterMetaData> parametersMD, ImpactType impactType) {
    ModelMBeanOperationInfo operationInfo = new ModelMBeanOperationInfo(description, method);
    // 
    if (description == null) {
        description = "Management operation";
    }
    // 
    MBeanParameterInfo[] parameterInfos = operationInfo.getSignature();
    for (ManagedMethodParameterMetaData parameterMD : parametersMD) {
        int i = parameterMD.getIndex();
        MBeanParameterInfo parameterInfo = parameterInfos[i];
        String parameterName = parameterInfo.getName();
        String parameterDescription = operationInfo.getSignature()[i].getDescription();
        if (parameterMD.getName() != null) {
            parameterName = parameterMD.getName();
        } else if (parameterMD.getDescription() != null) {
            parameterDescription = parameterMD.getDescription();
        }
        parameterInfos[i] = new MBeanParameterInfo(parameterName, parameterInfo.getType(), parameterDescription);
    }
    // 
    int jmxImpact;
    switch(impactType) {
        case READ:
            jmxImpact = MBeanOperationInfo.INFO;
            break;
        case IDEMPOTENT_WRITE:
        case WRITE:
            jmxImpact = MBeanOperationInfo.ACTION;
            break;
        default:
            throw new AssertionError();
    }
    // 
    Descriptor operationDescriptor = operationInfo.getDescriptor();
    operationDescriptor.setField("role", role.name);
    // 
    return new ModelMBeanOperationInfo(operationInfo.getName(), description, parameterInfos, operationInfo.getReturnType(), jmxImpact, operationDescriptor);
}
Also used : ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) Descriptor(javax.management.Descriptor) MBeanParameterInfo(javax.management.MBeanParameterInfo) ManagedMethodParameterMetaData(org.exoplatform.management.spi.ManagedMethodParameterMetaData)

Example 35 with Descriptor

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

the class ModelMBeanAssembler method getModelMbeanInfo.

public ModelMBeanInfo getModelMbeanInfo(Class<?> clazz) {
    supporter.clear();
    ManagedResource mr = getManagedResource(clazz);
    if (mr == null) {
        // the class is not need to expose to jmx
        return null;
    }
    // Clazz get all the method which should be managemed
    Descriptor mbeanDescriptor = supporter.buildMBeanDescriptor(mr);
    // add the notification
    ManagedNotification[] mns = getManagedNotifications(clazz);
    for (int k = 0; k < mns.length; k++) {
        supporter.addModelMBeanNotification(mns[k].notificationTypes(), mns[k].name(), mns[k].description(), null);
    }
    Method[] methods = clazz.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        ManagedAttribute ma = getManagedAttribute(methods[i]);
        // add Attribute to the ModelMBean
        if (ma != null) {
            String attributeName = getAttributeName(methods[i].getName());
            if (!supporter.checkAttribute(attributeName)) {
                String attributeType = getAttributeType(methods, attributeName);
                ManagedAttributeInfo mai = getAttributInfo(methods, attributeName, attributeType, ma);
                Descriptor attributeDescriptor = supporter.buildAttributeDescriptor(ma, attributeName, mai.is, mai.read, mai.write);
                // should setup the description
                supporter.addModelMBeanAttribute(mai.fname, mai.ftype, mai.read, mai.write, mai.is, mai.description, attributeDescriptor);
                Method method;
                // add the attribute methode to operation
                if (mai.read) {
                    if (mai.is) {
                        method = findMethodByName(methods, "is" + attributeName);
                    } else {
                        method = findMethodByName(methods, "get" + attributeName);
                    }
                    addAttributeOperation(method);
                }
                if (mai.write) {
                    method = findMethodByName(methods, "set" + attributeName);
                    addAttributeOperation(method);
                }
            }
        } else {
            // add Operation to the ModelMBean
            ManagedOperation mo = getManagedOperation(methods[i]);
            if (mo != null) {
                Class<?>[] types = methods[i].getParameterTypes();
                ManagedOperationParameter[] mop = getManagedOperationParameters(methods[i]);
                String[] paramTypes = new String[types.length];
                String[] paramNames = new String[types.length];
                String[] paramDescs = new String[types.length];
                for (int j = 0; j < types.length; j++) {
                    paramTypes[j] = types[j].getName();
                    if (j < mop.length) {
                        paramDescs[j] = mop[j].description();
                        paramNames[j] = mop[j].name();
                    } else {
                        paramDescs[j] = "";
                        paramNames[j] = types[j].getName();
                    }
                }
                Descriptor operationDescriptor = supporter.buildOperationDescriptor(mo, methods[i].getName());
                supporter.addModelMBeanMethod(methods[i].getName(), paramTypes, paramNames, paramDescs, mo.description(), methods[i].getReturnType().getName(), operationDescriptor);
            }
        }
    }
    return supporter.buildModelMBeanInfo(mbeanDescriptor);
}
Also used : Method(java.lang.reflect.Method) ManagedResource(org.apache.cxf.management.annotation.ManagedResource) ManagedAttribute(org.apache.cxf.management.annotation.ManagedAttribute) ManagedOperation(org.apache.cxf.management.annotation.ManagedOperation) ManagedNotification(org.apache.cxf.management.annotation.ManagedNotification) ManagedOperationParameter(org.apache.cxf.management.annotation.ManagedOperationParameter) Descriptor(javax.management.Descriptor)

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