Search in sources :

Example 1 with AttributeNotFoundException

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

the class ObjectMBean method getAttribute.

/* ------------------------------------------------------------ */
public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException {
    Method getter = (Method) _getters.get(name);
    if (getter == null) {
        throw new AttributeNotFoundException(name);
    }
    try {
        Object o = _managed;
        if (getter.getDeclaringClass().isInstance(this))
            // mbean method
            o = this;
        // get the attribute
        Object r = getter.invoke(o, (java.lang.Object[]) null);
        // convert to ObjectName if the type has the @ManagedObject annotation
        if (r != null) {
            if (r.getClass().isArray()) {
                if (r.getClass().getComponentType().isAnnotationPresent(ManagedObject.class)) {
                    ObjectName[] on = new ObjectName[Array.getLength(r)];
                    for (int i = 0; i < on.length; i++) {
                        on[i] = _mbeanContainer.findMBean(Array.get(r, i));
                    }
                    r = on;
                }
            } else if (r instanceof Collection<?>) {
                @SuppressWarnings("unchecked") Collection<Object> c = (Collection<Object>) r;
                if (!c.isEmpty() && c.iterator().next().getClass().isAnnotationPresent(ManagedObject.class)) {
                    // check the first thing out
                    ObjectName[] on = new ObjectName[c.size()];
                    int i = 0;
                    for (Object obj : c) {
                        on[i++] = _mbeanContainer.findMBean(obj);
                    }
                    r = on;
                }
            } else {
                Class<?> clazz = r.getClass();
                while (clazz != null) {
                    if (clazz.isAnnotationPresent(ManagedObject.class)) {
                        ObjectName mbean = _mbeanContainer.findMBean(r);
                        if (mbean != null) {
                            return mbean;
                        } else {
                            return null;
                        }
                    }
                    clazz = clazz.getSuperclass();
                }
            }
        }
        return r;
    } 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) 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) ObjectName(javax.management.ObjectName) Collection(java.util.Collection) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject) ManagedObject(org.eclipse.jetty.util.annotation.ManagedObject)

Example 2 with AttributeNotFoundException

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

the class JSONBean method write.

/**
   * @param mBeanServer
   * @param qry
   * @param attribute
   * @param description
   * @return Return non-zero if failed to find bean. 0
   * @throws IOException
   */
private static int write(final JsonGenerator jg, final MBeanServer mBeanServer, ObjectName qry, String attribute, final boolean description) throws IOException {
    LOG.trace("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 = "";
        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) {
                    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;
        }
        jg.writeStartObject();
        jg.writeStringField("name", oname.toString());
        if (description && descriptionStr != null && descriptionStr.length() > 0) {
            jg.writeStringField("description", descriptionStr);
        }
        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();
            return -1;
        }
        if (attribute != null) {
            writeAttribute(jg, attribute, descriptionStr, attributeinfo);
        } else {
            MBeanAttributeInfo[] attrs = minfo.getAttributes();
            for (int i = 0; i < attrs.length; i++) {
                writeAttribute(jg, mBeanServer, oname, description, attrs[i]);
            }
        }
        jg.writeEndObject();
    }
    jg.writeEndArray();
    return 0;
}
Also used : RuntimeMBeanException(javax.management.RuntimeMBeanException) 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 3 with AttributeNotFoundException

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

the class JMXDataUpdater method updateMemberClient.

/**
   * function used for getting member clients from mbean and update the clients information in
   * member object's client arraylist
   */
private void updateMemberClient(ObjectName mbeanName) throws IOException {
    try {
        String memberName = mbeanName.getKeyProperty(PulseConstants.MBEAN_KEY_PROPERTY_MEMBER);
        if (cluster.getMembersHMap().containsKey(memberName)) {
            Cluster.Member existingMember = cluster.getMembersHMap().get(memberName);
            HashMap<String, Cluster.Client> memberClientsHM = new HashMap<String, Cluster.Client>();
            existingMember.setMemberPort("" + this.mbs.getAttribute(mbeanName, PulseConstants.MBEAN_ATTRIBUTE_PORT));
            this.mbs.getAttribute(mbeanName, PulseConstants.MBEAN_ATTRIBUTE_HOSTNAMEFORCLIENTS_ALT);
            existingMember.setHostnameForClients((String) this.mbs.getAttribute(mbeanName, PulseConstants.MBEAN_ATTRIBUTE_HOSTNAMEFORCLIENTS_ALT));
            existingMember.setBindAddress((String) this.mbs.getAttribute(mbeanName, PulseConstants.MBEAN_ATTRIBUTE_BINDADDRESS));
            CompositeData[] compositeData = (CompositeData[]) (this.mbs.invoke(mbeanName, PulseConstants.MBEAN_OPERATION_SHOWALLCLIENTS, null, null));
            for (CompositeData cmd : compositeData) {
                Cluster.Client client = new Cluster.Client();
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_CLIENTID)) {
                    client.setId((String) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_CLIENTID));
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_NAME)) {
                    client.setName((String) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_NAME));
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_HOSTNAME)) {
                    client.setHost((String) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_HOSTNAME));
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_QUEUESIZE)) {
                    client.setQueueSize((Integer) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_QUEUESIZE));
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_PROCESSCPUTIME)) {
                    client.setProcessCpuTime((Long) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_PROCESSCPUTIME));
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_UPTIME)) {
                    client.setUptime((Long) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_UPTIME));
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_NUMOFTHREADS)) {
                    client.setThreads((Integer) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_NUMOFTHREADS));
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_NUMOFGETS)) {
                    client.setGets((Integer) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_NUMOFGETS));
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_NUMOFPUTS)) {
                    client.setPuts((Integer) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_NUMOFPUTS));
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_CPUS)) {
                    client.setCpus((Integer) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_CPUS));
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_CPUS)) {
                    client.setCpuUsage(0);
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_CONNECTED)) {
                    client.setConnected((Boolean) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_CONNECTED));
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_CLIENTCQCOUNT)) {
                    client.setClientCQCount((Integer) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_CLIENTCQCOUNT));
                }
                if (cmd.containsKey(PulseConstants.COMPOSITE_DATA_KEY_SUBSCRIPTIONENABLED)) {
                    client.setSubscriptionEnabled((Boolean) cmd.get(PulseConstants.COMPOSITE_DATA_KEY_SUBSCRIPTIONENABLED));
                }
                memberClientsHM.put(client.getId(), client);
            }
            existingMember.updateMemberClientsHMap(memberClientsHM);
        }
    } catch (InstanceNotFoundException | ReflectionException | AttributeNotFoundException | MBeanException infe) {
        logger.warn(infe);
    }
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) HashMap(java.util.HashMap) CompositeData(javax.management.openmbean.CompositeData) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException)

Example 4 with AttributeNotFoundException

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

the class MBeanJMXAdapter method isAttributeAvailable.

public static boolean isAttributeAvailable(String attributeName, String objectName) {
    try {
        ObjectName objName = new ObjectName(objectName);
        mbeanServer.getAttribute(objName, attributeName);
    } catch (MalformedObjectNameException e) {
        return false;
    } catch (NullPointerException e) {
        return false;
    } catch (AttributeNotFoundException e) {
        return false;
    } catch (InstanceNotFoundException e) {
        return false;
    } catch (MBeanException e) {
        return false;
    } catch (ReflectionException e) {
        return false;
    }
    return true;
}
Also used : ReflectionException(javax.management.ReflectionException) MalformedObjectNameException(javax.management.MalformedObjectNameException) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName)

Example 5 with AttributeNotFoundException

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

the class HealthCheckMBean method getAttribute.

@Override
public Object getAttribute(final String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
    // we should call getAttributes - and not vice versa to have the result
    // of a single check call - and not do a check call for each attribute
    final AttributeList result = this.getAttributes(new String[] { attribute });
    if (result.size() == 0) {
        throw new AttributeNotFoundException(attribute);
    }
    final Attribute attr = (Attribute) result.get(0);
    return attr.getValue();
}
Also used : AttributeNotFoundException(javax.management.AttributeNotFoundException) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList)

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