Search in sources :

Example 1 with ReflectionException

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

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

the class ObjectMBean method invoke.

/* ------------------------------------------------------------ */
public Object invoke(String name, Object[] params, String[] signature) throws MBeanException, ReflectionException {
    if (LOG.isDebugEnabled())
        LOG.debug("ObjectMBean:invoke " + name);
    String methodKey = name + "(";
    if (signature != null)
        for (int i = 0; i < signature.length; i++) methodKey += (i > 0 ? "," : "") + signature[i];
    methodKey += ")";
    ClassLoader old_loader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(_loader);
        Method method = (Method) _methods.get(methodKey);
        if (method == null)
            throw new NoSuchMethodException(methodKey);
        Object o = _managed;
        if (method.getDeclaringClass().isInstance(this)) {
            o = this;
        }
        return method.invoke(o, params);
    } catch (NoSuchMethodException e) {
        LOG.warn(Log.EXCEPTION, e);
        throw new ReflectionException(e);
    } catch (IllegalAccessException e) {
        LOG.warn(Log.EXCEPTION, e);
        throw new MBeanException(e);
    } catch (InvocationTargetException e) {
        LOG.warn(Log.EXCEPTION, e);
        throw new ReflectionException(new Exception(e.getCause()));
    } finally {
        Thread.currentThread().setContextClassLoader(old_loader);
    }
}
Also used : ReflectionException(javax.management.ReflectionException) MBeanException(javax.management.MBeanException) 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 3 with ReflectionException

use of javax.management.ReflectionException 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 4 with ReflectionException

use of javax.management.ReflectionException 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 5 with ReflectionException

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

the class JMXDataUpdater method updateClusterStatement.

private void updateClusterStatement(ObjectName mbeanName) throws IOException {
    try {
        AttributeList attributeList = this.mbs.getAttributes(mbeanName, PulseConstants.STATEMENT_MBEAN_ATTRIBUTES);
        // retrieve the full path of the region
        String statementDefinition = mbeanName.getKeyProperty("name");
        if (isQuoted(statementDefinition)) {
            statementDefinition = ObjectName.unquote(statementDefinition);
        }
        Cluster.Statement statement = cluster.getClusterStatements().get(statementDefinition);
        if (null == statement) {
            statement = new Cluster.Statement();
            statement.setQueryDefinition(statementDefinition);
        }
        for (int i = 0; i < attributeList.size(); i++) {
            Attribute attribute = (Attribute) attributeList.get(i);
            String name = attribute.getName();
            switch(name) {
                case PulseConstants.MBEAN_ATTRIBUTE_NUMTIMESCOMPILED:
                    statement.setNumTimesCompiled(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_NUMEXECUTION:
                    statement.setNumExecution(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_NUMEXECUTIONSINPROGRESS:
                    statement.setNumExecutionsInProgress(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_NUMTIMESGLOBALINDEXLOOKUP:
                    statement.setNumTimesGlobalIndexLookup(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_NUMROWSMODIFIED:
                    statement.setNumRowsModified(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_PARSETIME:
                    statement.setParseTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_BINDTIME:
                    statement.setBindTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_OPTIMIZETIME:
                    statement.setOptimizeTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_ROUTINGINFOTIME:
                    statement.setRoutingInfoTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_GENERATETIME:
                    statement.setGenerateTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_TOTALCOMPILATIONTIME:
                    statement.setTotalCompilationTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_EXECUTIONTIME:
                    statement.setExecutionTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_PROJECTIONTIME:
                    statement.setProjectionTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_TOTALEXECUTIONTIME:
                    statement.setTotalExecutionTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_ROWSMODIFICATIONTIME:
                    statement.setRowsModificationTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_QNNUMROWSSEEN:
                    statement.setqNNumRowsSeen(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_QNMSGSENDTIME:
                    statement.setqNMsgSendTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_QNMSGSERTIME:
                    statement.setqNMsgSerTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
                case PulseConstants.MBEAN_ATTRIBUTE_QNRESPDESERTIME:
                    statement.setqNRespDeSerTime(getLongAttribute(attribute.getValue(), attribute.getName()));
                    break;
            }
        }
        cluster.addClusterStatement(statementDefinition, statement);
    // TODO : to store data for sparklines later
    /*
       * region.getPutsPerSecTrend().add(region.getPutsRate());
       * region.getGetsPerSecTrend().add(region.getGetsRate());
       */
    } catch (InstanceNotFoundException | ReflectionException infe) {
        logger.warn(infe);
    }
}
Also used : ReflectionException(javax.management.ReflectionException) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) InstanceNotFoundException(javax.management.InstanceNotFoundException)

Aggregations

ReflectionException (javax.management.ReflectionException)173 InstanceNotFoundException (javax.management.InstanceNotFoundException)127 MBeanException (javax.management.MBeanException)105 AttributeNotFoundException (javax.management.AttributeNotFoundException)84 Attribute (javax.management.Attribute)56 IntrospectionException (javax.management.IntrospectionException)55 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)48 ObjectName (javax.management.ObjectName)47 AttributeList (javax.management.AttributeList)43 Test (org.testng.annotations.Test)41 MBeanInfo (javax.management.MBeanInfo)35 MalformedObjectNameException (javax.management.MalformedObjectNameException)31 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)30 IOException (java.io.IOException)25 InvocationTargetException (java.lang.reflect.InvocationTargetException)24 Method (java.lang.reflect.Method)22 RuntimeOperationsException (javax.management.RuntimeOperationsException)22 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)17 RuntimeErrorException (javax.management.RuntimeErrorException)17 ListenerNotFoundException (javax.management.ListenerNotFoundException)16