Search in sources :

Example 66 with AttributeNotFoundException

use of javax.management.AttributeNotFoundException in project jmxtrans by jmxtrans.

the class TreeWalker2 method walkTree.

public void walkTree(MBeanServerConnection connection, Server server) throws Exception {
    // key here is null, null returns everything!
    Set<ObjectName> mbeans = connection.queryNames(null, null);
    for (ObjectName name : mbeans) {
        MBeanInfo info = connection.getMBeanInfo(name);
        MBeanAttributeInfo[] attrs = info.getAttributes();
        Query.Builder queryBuilder = Query.builder().setObj(name.getCanonicalName()).addOutputWriterFactory(new StdOutWriter(ImmutableList.<String>of(), false, false, null, Collections.<String, Object>emptyMap()));
        for (MBeanAttributeInfo attrInfo : attrs) {
            queryBuilder.addAttr(attrInfo.getName());
        }
        Query query = queryBuilder.build();
        try {
            Iterable<Result> results = server.execute(query);
            query.runOutputWritersForQuery(server, results);
        } catch (AttributeNotFoundException anfe) {
            log.error("Error", anfe);
        }
    }
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanInfo(javax.management.MBeanInfo) Query(com.googlecode.jmxtrans.model.Query) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ObjectName(javax.management.ObjectName) StdOutWriter(com.googlecode.jmxtrans.model.output.StdOutWriter) Result(com.googlecode.jmxtrans.model.Result)

Example 67 with AttributeNotFoundException

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

the class JSONBean method write.

/**
 * @return Return non-zero if failed to find bean. 0
 */
private static int write(JsonWriter writer, MBeanServer mBeanServer, ObjectName qry, String attribute, boolean description, ObjectName excluded) throws IOException {
    LOG.debug("Listing beans for {}", qry);
    Set<ObjectName> names = null;
    names = mBeanServer.queryNames(qry, null);
    writer.name("beans").beginArray();
    Iterator<ObjectName> it = names.iterator();
    Pattern[] matchingPattern = null;
    while (it.hasNext()) {
        ObjectName oname = it.next();
        if (excluded != null && excluded.apply(oname)) {
            continue;
        }
        MBeanInfo minfo;
        String code = "";
        String descriptionStr = null;
        Object attributeinfo = null;
        try {
            minfo = mBeanServer.getMBeanInfo(oname);
            code = minfo.getClassName();
            if (description) {
                descriptionStr = minfo.getDescription();
            }
            String prs = "";
            try {
                if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) {
                    prs = "modelerType";
                    code = (String) mBeanServer.getAttribute(oname, prs);
                }
                if (attribute != null) {
                    String[] patternAttr = null;
                    if (attribute.contains(ASTERICK)) {
                        if (attribute.contains(COMMA)) {
                            patternAttr = attribute.split(COMMA);
                        } else {
                            patternAttr = new String[1];
                            patternAttr[0] = attribute;
                        }
                        matchingPattern = new Pattern[patternAttr.length];
                        for (int i = 0; i < patternAttr.length; i++) {
                            matchingPattern[i] = Pattern.compile(patternAttr[i]);
                        }
                        // nullify the attribute
                        attribute = null;
                    } else {
                        prs = attribute;
                        attributeinfo = mBeanServer.getAttribute(oname, prs);
                    }
                }
            } catch (RuntimeMBeanException e) {
                // so no need to log them as errors all the time.
                if (e.getCause() instanceof UnsupportedOperationException) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Getting attribute " + prs + " of " + oname + " threw " + e);
                    }
                } else {
                    LOG.error("Getting attribute " + prs + " of " + oname + " threw an exception", e);
                }
                return 0;
            } 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;
        }
        writer.beginObject();
        writer.name("name").value(oname.toString());
        if (description && descriptionStr != null && descriptionStr.length() > 0) {
            writer.name("description").value(descriptionStr);
        }
        writer.name("modelerType").value(code);
        if (attribute != null && attributeinfo == null) {
            writer.name("result").value("ERROR");
            writer.name("message").value("No attribute with name " + attribute + " was found.");
            writer.endObject();
            writer.endArray();
            writer.close();
            return -1;
        }
        if (attribute != null) {
            writeAttribute(writer, attribute, descriptionStr, attributeinfo);
        } else {
            MBeanAttributeInfo[] attrs = minfo.getAttributes();
            for (int i = 0; i < attrs.length; i++) {
                writeAttribute(writer, mBeanServer, oname, description, matchingPattern, attrs[i]);
            }
        }
        writer.endObject();
    }
    writer.endArray();
    return 0;
}
Also used : RuntimeMBeanException(javax.management.RuntimeMBeanException) Pattern(java.util.regex.Pattern) 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 68 with AttributeNotFoundException

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

the class ContextResourceLinkMBean method getAttribute.

/**
 * Obtain and return the value of a specific attribute of this MBean.
 *
 * @param name Name of the requested attribute
 *
 * @exception AttributeNotFoundException if this attribute is not
 *  supported by this MBean
 * @exception MBeanException if the initializer of an object
 *  throws an exception
 * @exception ReflectionException if a Java reflection exception
 *  occurs when invoking the getter
 */
@Override
public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException {
    // Validate the input parameters
    if (name == null) {
        throw new RuntimeOperationsException(new IllegalArgumentException(sm.getString("mBean.nullName")), sm.getString("mBean.nullName"));
    }
    ContextResourceLink cl = doGetManagedResource();
    String value = null;
    if ("global".equals(name)) {
        return cl.getGlobal();
    } else if ("description".equals(name)) {
        return cl.getDescription();
    } else if ("name".equals(name)) {
        return cl.getName();
    } else if ("type".equals(name)) {
        return cl.getType();
    } else {
        value = (String) cl.getProperty(name);
        if (value == null) {
            throw new AttributeNotFoundException(sm.getString("mBean.attributeNotFound", name));
        }
    }
    return value;
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 69 with AttributeNotFoundException

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

the class ManagedBean method getSetter.

public Method getSetter(String aname, BaseModelMBean bean, Object resource) throws AttributeNotFoundException, ReflectionException {
    Method m = null;
    AttributeInfo attrInfo = attributes.get(aname);
    if (attrInfo == null) {
        throw new AttributeNotFoundException(sm.getString("managedMBean.noAttribute", aname, resource));
    }
    // Look up the actual operation to be used
    String setMethod = attrInfo.getSetMethod();
    String argType = attrInfo.getType();
    Class<?>[] signature = new Class[] { BaseModelMBean.getAttributeClass(argType) };
    Object object = null;
    NoSuchMethodException exception = null;
    try {
        object = bean;
        m = object.getClass().getMethod(setMethod, signature);
    } catch (NoSuchMethodException e) {
        exception = e;
    }
    if (m == null && resource != null) {
        try {
            object = resource;
            m = object.getClass().getMethod(setMethod, signature);
            exception = null;
        } catch (NoSuchMethodException e) {
            exception = e;
        }
    }
    if (exception != null) {
        throw new ReflectionException(exception, sm.getString("managedMBean.noSet", setMethod, resource));
    }
    return m;
}
Also used : MBeanAttributeInfo(javax.management.MBeanAttributeInfo) ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) Method(java.lang.reflect.Method)

Example 70 with AttributeNotFoundException

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

the class BaseModelMBean method getAttribute.

// key: operation val: invoke method
// private Hashtable invokeAttMap=new Hashtable();
/**
 * Obtain and return the value of a specific attribute of this MBean.
 *
 * @param name Name of the requested attribute
 *
 * @exception AttributeNotFoundException if this attribute is not
 *  supported by this MBean
 * @exception MBeanException if the initializer of an object
 *  throws an exception
 * @exception ReflectionException if a Java reflection exception
 *  occurs when invoking the getter
 */
@Override
public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException {
    // Validate the input parameters
    if (name == null) {
        throw new RuntimeOperationsException(new IllegalArgumentException(sm.getString("baseModelMBean.nullAttributeName")), sm.getString("baseModelMBean.nullAttributeName"));
    }
    if ((resource instanceof DynamicMBean) && !(resource instanceof BaseModelMBean)) {
        return ((DynamicMBean) resource).getAttribute(name);
    }
    Method m = managedBean.getGetter(name, this, resource);
    Object result = null;
    try {
        Class<?> declaring = m.getDeclaringClass();
        // but this is the catalina class.
        if (declaring.isAssignableFrom(this.getClass())) {
            result = m.invoke(this, NO_ARGS_PARAM);
        } else {
            result = m.invoke(resource, NO_ARGS_PARAM);
        }
    } catch (InvocationTargetException e) {
        Throwable t = e.getTargetException();
        if (t == null) {
            t = e;
        }
        if (t instanceof RuntimeException) {
            throw new RuntimeOperationsException((RuntimeException) t, sm.getString("baseModelMBean.invokeError", name));
        } else if (t instanceof Error) {
            throw new RuntimeErrorException((Error) t, sm.getString("baseModelMBean.invokeError", name));
        } else {
            throw new MBeanException(e, sm.getString("baseModelMBean.invokeError", name));
        }
    } catch (Exception e) {
        throw new MBeanException(e, sm.getString("baseModelMBean.invokeError", name));
    }
    // FIXME - should we validate the return type?
    return result;
}
Also used : DynamicMBean(javax.management.DynamicMBean) RuntimeErrorException(javax.management.RuntimeErrorException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidTargetObjectTypeException(javax.management.modelmbean.InvalidTargetObjectTypeException) RuntimeErrorException(javax.management.RuntimeErrorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) MBeanException(javax.management.MBeanException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Aggregations

AttributeNotFoundException (javax.management.AttributeNotFoundException)77 ReflectionException (javax.management.ReflectionException)57 MBeanException (javax.management.MBeanException)54 InstanceNotFoundException (javax.management.InstanceNotFoundException)40 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)30 Attribute (javax.management.Attribute)26 ObjectName (javax.management.ObjectName)24 RuntimeOperationsException (javax.management.RuntimeOperationsException)16 IntrospectionException (javax.management.IntrospectionException)13 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)13 AttributeList (javax.management.AttributeList)12 MalformedObjectNameException (javax.management.MalformedObjectNameException)11 Method (java.lang.reflect.Method)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 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