Search in sources :

Example 41 with AttributeNotFoundException

use of javax.management.AttributeNotFoundException in project spf4j by zolyfarkas.

the class ExportedValuesMBean method setAttributes.

/**
 * {@inheritDoc}
 */
@Override
public AttributeList setAttributes(final AttributeList list) {
    AttributeList result = new AttributeList(list.size());
    for (Attribute attr : list.asList()) {
        ExportedValue<Object> eval = (ExportedValue<Object>) exportedValues.get(attr.getName());
        if (eval != null) {
            try {
                eval.set(attr.getValue());
                result.add(attr);
            } catch (AttributeNotFoundException | InvalidAttributeValueException | MBeanException | ReflectionException | InvalidObjectException | RuntimeException ex) {
                Logger.getLogger(ExportedValuesMBean.class.getName()).log(Level.WARNING, "Exception while setting attr {}", attr);
                Logger.getLogger(ExportedValuesMBean.class.getName()).log(Level.WARNING, "Exception detail", ex);
                JMRuntimeException jx = new JMRuntimeException("Exception while setting attributes " + list);
                jx.addSuppressed(ex);
                throw jx;
            }
        }
    }
    return result;
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) JMRuntimeException(javax.management.JMRuntimeException) MBeanException(javax.management.MBeanException) InvalidObjectException(java.io.InvalidObjectException) JMRuntimeException(javax.management.JMRuntimeException)

Example 42 with AttributeNotFoundException

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

the class ObjectMBean method setAttribute.

/* ------------------------------------------------------------ */
public void setAttribute(Attribute attr) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
    if (attr == null)
        return;
    if (LOG.isDebugEnabled())
        LOG.debug("setAttribute " + _managed + ":" + attr.getName() + "=" + attr.getValue());
    Method setter = (Method) _setters.get(attr.getName());
    if (setter == null)
        throw new AttributeNotFoundException(attr.getName());
    try {
        Object o = _managed;
        if (setter.getDeclaringClass().isInstance(this))
            o = this;
        // get the value
        Object value = attr.getValue();
        // convert from ObjectName if need be
        if (value != null && _convert.contains(attr.getName())) {
            if (value.getClass().isArray()) {
                Class<?> t = setter.getParameterTypes()[0].getComponentType();
                Object na = Array.newInstance(t, Array.getLength(value));
                for (int i = Array.getLength(value); i-- > 0; ) Array.set(na, i, _mbeanContainer.findBean((ObjectName) Array.get(value, i)));
                value = na;
            } else
                value = _mbeanContainer.findBean((ObjectName) value);
        }
        // do the setting
        setter.invoke(o, new Object[] { value });
    } catch (IllegalAccessException e) {
        LOG.warn(Log.EXCEPTION, e);
        throw new AttributeNotFoundException(e.toString());
    } catch (InvocationTargetException e) {
        LOG.warn(Log.EXCEPTION, e);
        throw new ReflectionException(new Exception(e.getCause()));
    }
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) AttributeNotFoundException(javax.management.AttributeNotFoundException) ReflectionException(javax.management.ReflectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException)

Example 43 with AttributeNotFoundException

use of javax.management.AttributeNotFoundException in project hadoop by apache.

the class JMXJsonServlet method listBeans.

// --------------------------------------------------------- Private Methods
private void listBeans(JsonGenerator jg, ObjectName qry, String attribute, HttpServletResponse response) throws IOException {
    LOG.debug("Listing beans for " + qry);
    Set<ObjectName> names = null;
    names = mBeanServer.queryNames(qry, null);
    jg.writeArrayFieldStart("beans");
    Iterator<ObjectName> it = names.iterator();
    while (it.hasNext()) {
        ObjectName oname = it.next();
        MBeanInfo minfo;
        String code = "";
        Object attributeinfo = null;
        try {
            minfo = mBeanServer.getMBeanInfo(oname);
            code = minfo.getClassName();
            String prs = "";
            try {
                if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                    prs = "modelerType";
                    code = (String) mBeanServer.getAttribute(oname, prs);
                }
                if (attribute != null) {
                    prs = attribute;
                    attributeinfo = mBeanServer.getAttribute(oname, prs);
                }
            } catch (AttributeNotFoundException e) {
                // If the modelerType attribute was not found, the class name is used
                // instead.
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (MBeanException e) {
                // The code inside the attribute getter threw an exception so log it,
                // and fall back on the class name
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (RuntimeException e) {
                // For some reason even with an MBeanException available to them
                // Runtime exceptionscan still find their way through, so treat them
                // the same as MBeanException
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            } catch (ReflectionException e) {
                // This happens when the code inside the JMX bean (setter?? from the
                // java docs) threw an exception, so log it and fall back on the 
                // class name
                LOG.error("getting attribute " + prs + " of " + oname + " threw an exception", e);
            }
        } catch (InstanceNotFoundException e) {
            //Ignored for some reason the bean was not found so don't output it
            continue;
        } catch (IntrospectionException e) {
            // This is an internal error, something odd happened with reflection so
            // log it and don't output the bean.
            LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
            continue;
        } catch (ReflectionException e) {
            // This happens when the code inside the JMX bean threw an exception, so
            // log it and don't output the bean.
            LOG.error("Problem while trying to process JMX query: " + qry + " with MBean " + oname, e);
            continue;
        }
        jg.writeStartObject();
        jg.writeStringField("name", oname.toString());
        jg.writeStringField("modelerType", code);
        if ((attribute != null) && (attributeinfo == null)) {
            jg.writeStringField("result", "ERROR");
            jg.writeStringField("message", "No attribute with name " + attribute + " was found.");
            jg.writeEndObject();
            jg.writeEndArray();
            jg.close();
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        if (attribute != null) {
            writeAttribute(jg, attribute, attributeinfo);
        } else {
            MBeanAttributeInfo[] attrs = minfo.getAttributes();
            for (int i = 0; i < attrs.length; i++) {
                writeAttribute(jg, oname, attrs[i]);
            }
        }
        jg.writeEndObject();
    }
    jg.writeEndArray();
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanInfo(javax.management.MBeanInfo) InstanceNotFoundException(javax.management.InstanceNotFoundException) IntrospectionException(javax.management.IntrospectionException) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) RuntimeMBeanException(javax.management.RuntimeMBeanException) MBeanException(javax.management.MBeanException)

Example 44 with AttributeNotFoundException

use of javax.management.AttributeNotFoundException in project geode by apache.

the class MBeanServerWrapper method getAttribute.

@Override
public Object getAttribute(ObjectName name, String attribute) throws MBeanException, InstanceNotFoundException, ReflectionException {
    ResourcePermission ctx = getOperationContext(name, attribute, false);
    this.securityService.authorize(ctx);
    Object result;
    try {
        result = mbs.getAttribute(name, attribute);
    } catch (AttributeNotFoundException nex) {
        return null;
    }
    return result;
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) ResourcePermission(org.apache.geode.security.ResourcePermission)

Example 45 with AttributeNotFoundException

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

Aggregations

AttributeNotFoundException (javax.management.AttributeNotFoundException)78 ReflectionException (javax.management.ReflectionException)57 MBeanException (javax.management.MBeanException)53 InstanceNotFoundException (javax.management.InstanceNotFoundException)40 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)31 Attribute (javax.management.Attribute)25 ObjectName (javax.management.ObjectName)24 RuntimeOperationsException (javax.management.RuntimeOperationsException)16 IntrospectionException (javax.management.IntrospectionException)13 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)13 Method (java.lang.reflect.Method)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)11 AttributeList (javax.management.AttributeList)11 MalformedObjectNameException (javax.management.MalformedObjectNameException)11 Test (org.testng.annotations.Test)9 MBeanInfo (javax.management.MBeanInfo)8 ListenerNotFoundException (javax.management.ListenerNotFoundException)7 RuntimeErrorException (javax.management.RuntimeErrorException)7 RuntimeMBeanException (javax.management.RuntimeMBeanException)7 InvalidTargetObjectTypeException (javax.management.modelmbean.InvalidTargetObjectTypeException)7