Search in sources :

Example 46 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] = PropertyEditors.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)

Aggregations

MBeanOperationInfo (javax.management.MBeanOperationInfo)46 MBeanInfo (javax.management.MBeanInfo)33 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)24 ObjectName (javax.management.ObjectName)18 MBeanParameterInfo (javax.management.MBeanParameterInfo)17 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)8 MBeanServer (javax.management.MBeanServer)8 MBeanConstructorInfo (javax.management.MBeanConstructorInfo)7 MBeanNotificationInfo (javax.management.MBeanNotificationInfo)7 ReflectionException (javax.management.ReflectionException)5 Method (java.lang.reflect.Method)4 Descriptor (javax.management.Descriptor)4 TreeMap (java.util.TreeMap)3 Attribute (javax.management.Attribute)3 InstanceNotFoundException (javax.management.InstanceNotFoundException)3 IntrospectionException (javax.management.IntrospectionException)3 MalformedObjectNameException (javax.management.MalformedObjectNameException)3 OpenType (javax.management.openmbean.OpenType)3 IOException (java.io.IOException)2