Search in sources :

Example 11 with Logger

use of mx4j.log.Logger in project geode by apache.

the class MX4JModelMBean method setAttributes.

public AttributeList setAttributes(AttributeList attributes) {
    if (attributes == null)
        throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_ATTRIBUTE_LIST_CANNOT_BE_NULL.toLocalizedString()));
    Logger logger = getLogger();
    AttributeList list = new AttributeList();
    for (Iterator i = attributes.iterator(); i.hasNext(); ) {
        Attribute attribute = (Attribute) i.next();
        String name = attribute.getName();
        try {
            setAttribute(attribute);
            list.add(attribute);
        } catch (Exception x) {
            if (logger.isEnabledFor(Logger.TRACE))
                logger.trace("setAttribute for attribute " + name + " failed", x);
        // And go on with the next one
        }
    }
    return list;
}
Also used : Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) Iterator(java.util.Iterator) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) 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) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 12 with Logger

use of mx4j.log.Logger in project geode by apache.

the class MX4JModelMBean method getFieldTimeValue.

private Long getFieldTimeValue(Descriptor descriptor, Descriptor mbean, String field) {
    Logger logger = getLogger();
    Object value = descriptor.getFieldValue(field);
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("Descriptor's " + field + " field: " + value);
    if (value == null && mbean != null) {
        value = mbean.getFieldValue(field);
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("MBean's " + field + " field: " + value);
        if (value == null)
            return null;
    }
    if (value instanceof Number)
        return Long.valueOf(((Number) value).longValue());
    if (value instanceof String) {
        try {
            long ctl = Long.parseLong((String) value);
            return Long.valueOf(ctl);
        } catch (NumberFormatException x) {
            return Long.valueOf(0);
        }
    }
    return Long.valueOf(0);
}
Also used : Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger)

Example 13 with Logger

use of mx4j.log.Logger in project geode by apache.

the class MX4JModelMBean method invokeMethod.

private Object invokeMethod(Object target, String methodName, Class[] params, Object[] args) throws MBeanException, ReflectionException {
    // First try on this instance, then on the target
    Object realTarget = null;
    Method method = null;
    try {
        realTarget = this;
        method = realTarget.getClass().getMethod(methodName, params);
    } catch (NoSuchMethodException x) {
        realTarget = target;
    }
    if (realTarget == null)
        throw new MBeanException(new ServiceNotFoundException(LocalizedStrings.MX4JModelMBean_COULD_NOT_FIND_TARGET.toLocalizedString()));
    if (method == null) {
        try {
            method = realTarget.getClass().getMethod(methodName, params);
        } catch (NoSuchMethodException x) {
            throw new ReflectionException(x);
        }
    }
    try {
        Object value = method.invoke(realTarget, args);
        Logger logger = getLogger();
        if (logger.isEnabledFor(Logger.DEBUG))
            logger.debug("Method invocation returned value: " + value);
        return value;
    } catch (IllegalAccessException x) {
        throw new ReflectionException(x);
    } catch (IllegalArgumentException x) {
        throw new MBeanException(x);
    } catch (InvocationTargetException x) {
        Throwable t = x.getTargetException();
        if (t instanceof Error)
            throw new MBeanException(new RuntimeErrorException((Error) t));
        else
            throw new MBeanException((Exception) t);
    }
}
Also used : ReflectionException(javax.management.ReflectionException) RuntimeErrorException(javax.management.RuntimeErrorException) Method(java.lang.reflect.Method) Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServiceNotFoundException(javax.management.ServiceNotFoundException) MBeanException(javax.management.MBeanException)

Example 14 with Logger

use of mx4j.log.Logger 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 15 with Logger

use of mx4j.log.Logger in project geode by apache.

the class MX4JModelMBean method setModelMBeanInfo.

public void setModelMBeanInfo(ModelMBeanInfo modelMBeanInfo) throws MBeanException, RuntimeOperationsException {
    if (modelMBeanInfo == null)
        throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_MODELMBEANINFO_CANNOT_BE_NULL.toLocalizedString()));
    if (!isModelMBeanInfoValid(modelMBeanInfo))
        throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_MODELMBEANINFO_IS_INVALID.toLocalizedString()));
    m_modelMBeanInfo = (ModelMBeanInfo) modelMBeanInfo.clone();
    Logger logger = getLogger();
    if (logger.isEnabledFor(Logger.DEBUG))
        logger.debug("ModelMBeanInfo successfully set to: " + m_modelMBeanInfo);
    // Only now the MBean can be registered in the MBeanServer
    m_canBeRegistered = true;
}
Also used : Logger(mx4j.log.Logger) FileLogger(mx4j.log.FileLogger) MBeanLogger(mx4j.log.MBeanLogger) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Aggregations

FileLogger (mx4j.log.FileLogger)19 Logger (mx4j.log.Logger)19 MBeanLogger (mx4j.log.MBeanLogger)19 RuntimeOperationsException (javax.management.RuntimeOperationsException)10 MBeanException (javax.management.MBeanException)9 Descriptor (javax.management.Descriptor)7 MalformedObjectNameException (javax.management.MalformedObjectNameException)6 InvalidTargetObjectTypeException (javax.management.modelmbean.InvalidTargetObjectTypeException)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 AttributeNotFoundException (javax.management.AttributeNotFoundException)5 InstanceNotFoundException (javax.management.InstanceNotFoundException)5 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)5 ReflectionException (javax.management.ReflectionException)5 RuntimeErrorException (javax.management.RuntimeErrorException)5 ServiceNotFoundException (javax.management.ServiceNotFoundException)5 ModelMBeanInfo (javax.management.modelmbean.ModelMBeanInfo)5 ListenerNotFoundException (javax.management.ListenerNotFoundException)4 MBeanRegistrationException (javax.management.MBeanRegistrationException)4 ModelMBeanAttributeInfo (javax.management.modelmbean.ModelMBeanAttributeInfo)4 ImplementationException (mx4j.ImplementationException)4