Search in sources :

Example 91 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project tomee by apache.

the class ManagedMBean method getMBeanInfo.

public MBeanInfo getMBeanInfo() {
    final List<MBeanAttributeInfo> attributes = new ArrayList<>(this.attributes);
    final List<MBeanOperationInfo> operations = new ArrayList<>(this.operations);
    for (final Member member : dynamic) {
        try {
            final ManagedCollection managedCollection = member.getAnnotation(ManagedCollection.class);
            final Collection collection = (Collection) member.get();
            for (final Object o : collection) {
                try {
                    final Field field = o.getClass().getDeclaredField(managedCollection.key());
                    field.setAccessible(true);
                    final Object key = field.get(o);
                    final ManagedMBean bean = new ManagedMBean(o, key.toString());
                    Collections.addAll(attributes, bean.getMBeanInfo().getAttributes());
                    Collections.addAll(operations, bean.getMBeanInfo().getOperations());
                    attributesMap.putAll(bean.attributesMap);
                    operationsMap.putAll(bean.operationsMap);
                } catch (final Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
    operations.sort(MBeanFeatureInfoComparator.INSTANCE);
    attributes.sort(MBeanFeatureInfoComparator.INSTANCE);
    if (filterAttributes) {
        final Iterator<MBeanAttributeInfo> iterator = attributes.iterator();
        while (iterator.hasNext()) {
            final MBeanAttributeInfo info = iterator.next();
            if (includes.matcher(info.getName()).matches()) {
                continue;
            }
            if (excludes.matcher(info.getName()).matches()) {
                iterator.remove();
            }
        }
    }
    return new MBeanInfo(this.getClass().getName(), "", attributes.toArray(new MBeanAttributeInfo[attributes.size()]), new MBeanConstructorInfo[0], operations.toArray(new MBeanOperationInfo[operations.size()]), EMPTY_NOTIFICATIONS);
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) ArrayList(java.util.ArrayList) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) AttributeNotFoundException(javax.management.AttributeNotFoundException) ReflectionException(javax.management.ReflectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) Field(java.lang.reflect.Field) Collection(java.util.Collection)

Example 92 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project tomee by apache.

the class LocalJMXCommand method invoke.

private void invoke(final String value) {
    if (!value.contains("(") || !value.contains(")")) {
        streamManager.writeErr("method should follow the format: <methoName>(<arg1>,<arg2>,...)");
        return;
    }
    int open = value.indexOf("(");
    int close = value.lastIndexOf(")");
    final String name = value.substring(0, open).trim();
    final String rawArgs = value.substring(open + 1, close).trim();
    final ObjectName on;
    try {
        on = new ObjectName(value.substring(close + 1).trim());
    } catch (MalformedObjectNameException e) {
        streamManager.writeErr(e);
        return;
    }
    final MBeanServer server = LocalMBeanServer.get();
    final String[] args;
    if (rawArgs == null || rawArgs.isEmpty()) {
        args = new String[0];
    } else {
        args = rawArgs.split(",");
    }
    try {
        final MBeanInfo minfo = server.getMBeanInfo(on);
        final MBeanOperationInfo[] methods = minfo.getOperations();
        MBeanOperationInfo operation = null;
        for (int i = 0; i < methods.length; i++) {
            if (methods[i].getName().equals(name)) {
                operation = methods[i];
                break;
            }
        }
        if (operation == null) {
            streamManager.writeErr("can't find operation '" + name + "'");
            return;
        }
        final Object[] passedArgs = new Object[args.length];
        final String[] passedArgTypes = new String[args.length];
        for (int i = 0; i < passedArgs.length; i++) {
            final String expected = operation.getSignature()[i].getType();
            if (!String.class.getName().equals(expected)) {
                passedArgs[i] = propertyEditorRegistry.getValue(expected, args[i], Thread.currentThread().getContextClassLoader());
            } else {
                passedArgs[i] = args[i];
            }
            passedArgTypes[i] = expected;
        }
        streamManager.writeOut(stringify(server.invoke(on, name, passedArgs, passedArgTypes)));
    } catch (Exception e) {
        streamManager.writeErr(e);
        return;
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) RuntimeMBeanException(javax.management.RuntimeMBeanException) MalformedObjectNameException(javax.management.MalformedObjectNameException) ObjectName(javax.management.ObjectName) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) MBeanServer(javax.management.MBeanServer)

Example 93 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project spring-boot by spring-projects.

the class MBeanInfoFactoryTests method getMBeanInfoShouldUseJmxOperationResponseMapper.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
void getMBeanInfoShouldUseJmxOperationResponseMapper() {
    JmxOperationResponseMapper mapper = mock(JmxOperationResponseMapper.class);
    given(mapper.mapResponseType(String.class)).willReturn((Class) Integer.class);
    MBeanInfoFactory factory = new MBeanInfoFactory(mapper);
    MBeanInfo info = factory.getMBeanInfo(new TestExposableJmxEndpoint(new TestJmxOperation()));
    MBeanOperationInfo operationInfo = info.getOperations()[0];
    assertThat(operationInfo.getReturnType()).isEqualTo(Integer.class.getName());
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) Test(org.junit.jupiter.api.Test)

Example 94 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project Payara by payara.

the class ConfigBeanJMXSupport method duckTypedToMBeanOperationInfo.

/**
 *    DuckTyped methods are <em>always</em> exposed as operations, never as Attributes.
 */
public MBeanOperationInfo duckTypedToMBeanOperationInfo(final DuckTypedInfo info) {
    final Descriptor descriptor = descriptor(info.duckTyped());
    final String name = info.name();
    final Class<?> type = remoteType(info.returnType());
    final String description = "@DuckTyped " + name + " of " + mIntf.getName();
    // how to tell?
    final int impact = MBeanOperationInfo.UNKNOWN;
    final List<MBeanParameterInfo> paramInfos = new ArrayList<MBeanParameterInfo>();
    int i = 0;
    for (final Class<?> paramClass : info.signature()) {
        final String paramName = "p" + i;
        final String paramType = remoteType(paramClass).getName();
        final String paramDescription = "parameter " + i;
        final MBeanParameterInfo paramInfo = new MBeanParameterInfo(paramName, paramType, paramDescription, null);
        paramInfos.add(paramInfo);
        ++i;
    }
    final MBeanParameterInfo[] paramInfosArray = CollectionUtil.toArray(paramInfos, MBeanParameterInfo.class);
    return new MBeanOperationInfo(name, description, paramInfosArray, type.getName(), impact, descriptor);
}
Also used : MBeanOperationInfo(javax.management.MBeanOperationInfo) ArrayList(java.util.ArrayList) Descriptor(javax.management.Descriptor) MBeanParameterInfo(javax.management.MBeanParameterInfo)

Example 95 with MBeanOperationInfo

use of javax.management.MBeanOperationInfo in project Payara by payara.

the class MBeanInterfaceGenerator method generateOperations.

protected String generateOperations(MBeanOperationInfo[] infos) {
    final StringBuilder buf = new StringBuilder();
    for (int i = 0; i < infos.length; ++i) {
        final MBeanOperationInfo info = infos[i];
        final String name = info.getName();
        final String returnType = info.getReturnType();
        final MBeanParameterInfo[] paramInfos = info.getSignature();
        final String[] paramTypes = new String[paramInfos.length];
        for (int p = 0; p < paramInfos.length; ++p) {
            paramTypes[p] = paramInfos[p].getType();
        }
        final String[] paramNames = getParamNames(info);
        if (mEmitComments) {
            final String comment = getOperationComment(info, paramNames);
            if (comment.length() != 0) {
                buf.append(NEWLINE).append(indent(comment)).append(NEWLINE);
            }
        }
        final String method = formMethod(returnType, name, paramTypes, paramNames);
        buf.append(indent(method)).append(NEWLINE);
    }
    return (buf.toString());
}
Also used : MBeanOperationInfo(javax.management.MBeanOperationInfo) MBeanParameterInfo(javax.management.MBeanParameterInfo)

Aggregations

MBeanOperationInfo (javax.management.MBeanOperationInfo)117 MBeanInfo (javax.management.MBeanInfo)76 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)59 MBeanParameterInfo (javax.management.MBeanParameterInfo)38 ObjectName (javax.management.ObjectName)33 MBeanNotificationInfo (javax.management.MBeanNotificationInfo)23 ArrayList (java.util.ArrayList)22 MBeanConstructorInfo (javax.management.MBeanConstructorInfo)20 InstanceNotFoundException (javax.management.InstanceNotFoundException)19 ReflectionException (javax.management.ReflectionException)17 Test (org.junit.Test)16 IntrospectionException (javax.management.IntrospectionException)13 MBeanServer (javax.management.MBeanServer)13 MalformedObjectNameException (javax.management.MalformedObjectNameException)9 Test (org.testng.annotations.Test)9 Method (java.lang.reflect.Method)8 Attribute (javax.management.Attribute)8 Descriptor (javax.management.Descriptor)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)5