Example usage for javax.management MBeanOperationInfo getSignature

List of usage examples for javax.management MBeanOperationInfo getSignature

Introduction

In this page you can find the example usage for javax.management MBeanOperationInfo getSignature.

Prototype

public MBeanParameterInfo[] getSignature() 

Source Link

Document

Returns the list of parameters for this operation.

Usage

From source file:com.clustercontrol.HinemosManagerCli.java

private void printMBeanInfo(MBeanInfo mbeanInfo) {
    MBeanAttributeInfo[] attributeInfos = mbeanInfo.getAttributes();
    System.out.println("Attributes:");
    for (MBeanAttributeInfo attributeInfo : attributeInfos) {
        System.out.println(String.format("\t%s: %s", attributeInfo.getName(), attributeInfo.getType()));
    }// www.  j  ava 2s .  c  o m

    MBeanOperationInfo[] operationInfos = mbeanInfo.getOperations();
    System.out.println("Operations:");
    for (MBeanOperationInfo operationInfo : operationInfos) {
        MBeanParameterInfo[] paramInfos = operationInfo.getSignature();

        StringBuffer paramStr = new StringBuffer();
        for (MBeanParameterInfo paramInfo : paramInfos) {
            paramStr.append(paramInfo.getType() + ",");
        }
        if (paramStr.length() != 0) {
            paramStr.append(paramStr.substring(0, paramStr.length() - 1));
        }

        System.out.println(
                String.format("\t%s %s(%s)", operationInfo.getReturnType(), operationInfo.getName(), paramStr));
    }
}

From source file:org.cloudfoundry.identity.varz.MBeanMap.java

@Override
public Set<java.util.Map.Entry<String, Object>> entrySet() {
    if (!initialized && info != null) {
        MBeanAttributeInfo[] attributes = info.getAttributes();
        for (MBeanAttributeInfo attribute : attributes) {
            String key = attribute.getName();
            try {
                Object value = server.getAttribute(name, key);
                verySafePut(map, key, value);
            } catch (Exception e) {
                logger.trace("Cannot extract attribute: " + key);
            }//from w  w w . j  a  va2 s  . co m
        }
        MBeanOperationInfo[] operations = info.getOperations();
        for (MBeanOperationInfo operation : operations) {
            String key = operation.getName();
            if (key.startsWith("get") && operation.getSignature().length == 0) {
                String attribute = VarzStringUtils.camelToUnderscore(key.substring(3));
                if (map.containsKey(attribute)) {
                    continue;
                }
                try {
                    Object value = server.invoke(name, key, null, null);
                    verySafePut(map, attribute, value);
                } catch (Exception e) {
                    logger.trace("Cannot extract operation: " + key);
                }
            }
        }
    }
    return map.entrySet();
}

From source file:org.cloudfoundry.identity.statsd.MBeanMap.java

@Override
public Set<java.util.Map.Entry<String, Object>> entrySet() {
    if (!initialized && info != null) {
        MBeanAttributeInfo[] attributes = info.getAttributes();
        for (MBeanAttributeInfo attribute : attributes) {
            String key = attribute.getName();
            try {
                Object value = server.getAttribute(name, key);
                verySafePut(map, key, value);
            } catch (Exception e) {
                logger.trace("Cannot extract attribute: " + key);
            }//from   w w w  .  ja  v a  2s .c  o m
        }
        MBeanOperationInfo[] operations = info.getOperations();
        for (MBeanOperationInfo operation : operations) {
            String key = operation.getName();
            if (key.startsWith("get") && operation.getSignature().length == 0) {
                String attribute = StringUtils.camelToUnderscore(key.substring(3));
                if (map.containsKey(attribute)) {
                    continue;
                }
                try {
                    Object value = server.invoke(name, key, null, null);
                    verySafePut(map, attribute, value);
                } catch (Exception e) {
                    logger.trace("Cannot extract operation: " + key);
                }
            }
        }
    }
    return map.entrySet();
}

From source file:org.cloudfoundry.identity.uaa.varz.MBeanMap.java

@Override
public Set<java.util.Map.Entry<String, Object>> entrySet() {
    if (!initialized && info != null) {
        MBeanAttributeInfo[] attributes = info.getAttributes();
        for (MBeanAttributeInfo attribute : attributes) {
            String key = attribute.getName();
            try {
                Object value = server.getAttribute(name, key);
                verySafePut(map, key, value);
            } catch (Exception e) {
                logger.trace("Cannot extract attribute: " + key);
            }/*w  ww .j  a  va 2s. c  om*/
        }
        MBeanOperationInfo[] operations = info.getOperations();
        for (MBeanOperationInfo operation : operations) {
            String key = operation.getName();
            if (key.startsWith("get") && operation.getSignature().length == 0) {
                String attribute = MBeanMap.prettify(key.substring(3));
                if (map.containsKey(attribute)) {
                    continue;
                }
                try {
                    Object value = server.invoke(name, key, null, null);
                    verySafePut(map, attribute, value);
                } catch (Exception e) {
                    logger.trace("Cannot extract operation: " + key);
                }
            }
        }
    }
    return map.entrySet();
}

From source file:org.jolokia.handler.list.OperationDataUpdater.java

/** {@inheritDoc} */
@Override//from ww w. j  a  v a 2  s  .  c o m
protected JSONObject extractData(MBeanInfo pMBeanInfo, String pOperation) {
    JSONObject opMap = new JSONObject();

    for (MBeanOperationInfo opInfo : pMBeanInfo.getOperations()) {
        if (pOperation == null || opInfo.getName().equals(pOperation)) {
            JSONObject map = new JSONObject();
            JSONArray argList = new JSONArray();
            for (MBeanParameterInfo paramInfo : opInfo.getSignature()) {
                JSONObject args = new JSONObject();
                args.put(DESCRIPTION.getKey(), paramInfo.getDescription());
                args.put(NAME.getKey(), paramInfo.getName());
                args.put(TYPE.getKey(), paramInfo.getType());
                argList.add(args);
            }
            map.put(ARGS.getKey(), argList);
            map.put(RETURN_TYPE.getKey(), opInfo.getReturnType());
            map.put(DESCRIPTION.getKey(), opInfo.getDescription());
            Object ops = opMap.get(opInfo.getName());
            if (ops != null) {
                if (ops instanceof List) {
                    // If it is already a list, simply add it to the end
                    ((List) ops).add(map);
                } else if (ops instanceof Map) {
                    // If it is a map, add a list with two elements
                    // (the old one and the new one)
                    JSONArray opList = new JSONArray();
                    opList.add(ops);
                    opList.add(map);
                    opMap.put(opInfo.getName(), opList);
                } else {
                    throw new IllegalArgumentException(
                            "Internal: list, addOperations: Expected Map or List, not " + ops.getClass());
                }
            } else {
                // No value set yet, simply add the map as plain value
                opMap.put(opInfo.getName(), map);
            }
        }
    }
    return opMap;
}

From source file:flens.query.JMXQuery.java

private Map<String, Object> getOpperations(MBeanInfo info) {
    Map<String, Object> outc = new HashMap<>();
    MBeanOperationInfo[] atts = info.getOperations();
    for (MBeanOperationInfo att : atts) {
        Map<String, Object> out = new HashMap<>();
        outc.put(att.getName(), out);/*w w w.  j  a va  2 s.  c o m*/
        out.put("description", att.getDescription());
        out.put("return-type", att.getReturnType());
        out.put("signature", getSig(att.getSignature()));

    }
    return outc;
}

From source file:com.stumbleupon.hbaseadmin.JMXQuery.java

protected Object doBeanOperation(MBeanServerConnection mbsc, ObjectInstance instance, String command,
        MBeanOperationInfo[] infos) throws Exception {

    final CommandParse parse = new CommandParse(command);

    final MBeanOperationInfo op = (MBeanOperationInfo) getFeatureInfo(infos, parse.getCmd());

    Object result = null;// w ww . j  av  a 2s.c  o  m

    if (op == null) {
        result = "Operation " + parse.getCmd() + " not found.";
    } else {
        final MBeanParameterInfo[] paraminfos = op.getSignature();
        final int paraminfosLength = (paraminfos == null) ? 0 : paraminfos.length;
        int objsLength = (parse.getArgs() == null) ? 0 : parse.getArgs().length;

        // FIXME: bad solution for comma containing parameter
        if (paraminfosLength == 1) {
            // concat all to one string
            String realParameter = parse.getArgs()[0];
            for (int j = 1; j < objsLength; j++) {
                realParameter = realParameter + "," + parse.getArgs()[j];
            }
            objsLength = 1;
            parse.setArgs(new String[] { realParameter });
        }

        if (paraminfosLength != objsLength) {
            result = "Passed param count does not match signature count";
        } else {
            final String[] signature = new String[paraminfosLength];
            final Object[] params = (paraminfosLength == 0) ? null : new Object[paraminfosLength];

            for (int i = 0; i < paraminfosLength; ++i) {
                final MBeanParameterInfo paraminfo = paraminfos[i];
                // System.out.println( "paraminfo.getType() = " + paraminfo.getType());
                final String classType = paraminfo.getType();
                // Constructor c = Class.forName(classType).getConstructor(new Class[] { String.class });
                final Constructor c = getResolvedClass(paraminfo.getType())
                        .getConstructor(new Class[] { String.class });

                params[i] = c.newInstance(new Object[] { parse.getArgs()[i] });
                signature[i] = classType;
            }

            result = mbsc.invoke(instance.getObjectName(), parse.getCmd(), params, signature);
        }
    }

    return result;
}

From source file:org.jumpmind.symmetric.JmxCommand.java

@Override
protected boolean executeWithOptions(final CommandLine line) throws Exception {
    if (line.hasOption(OPTION_LISTBEANS)) {
        execute(new IJmxTemplate<Object>() {
            @Override//from   w w w  .  j  av  a2  s  .  com
            public Object execute(String engineName, MBeanServerConnection mbeanConn) throws Exception {
                Set<ObjectName> beanSet = mbeanConn.queryNames(null, null);
                for (ObjectName objectName : beanSet) {
                    if (objectName.getDomain().startsWith("org.jumpmind.symmetric." + engineName)) {
                        System.out.println(objectName.toString());
                    }
                }
                return null;
            }
        });
    } else if (line.hasOption(OPTION_LISTMETHODS) || line.hasOption(OPTION_METHOD)) {
        if (line.hasOption(OPTION_BEAN)) {

            execute(new IJmxTemplate<Object>() {
                @Override
                public Object execute(String engineName, MBeanServerConnection mbeanConn) throws Exception {
                    String beanName = line.getOptionValue(OPTION_BEAN);
                    MBeanInfo info = mbeanConn.getMBeanInfo(new ObjectName(beanName));
                    if (info != null) {
                        if (line.hasOption(OPTION_LISTMETHODS)) {
                            MBeanOperationInfo[] operations = info.getOperations();
                            Map<String, MBeanOperationInfo> orderedMap = new TreeMap<String, MBeanOperationInfo>();
                            for (MBeanOperationInfo methodInfo : operations) {
                                orderedMap.put(methodInfo.getName(), methodInfo);
                            }
                            for (MBeanOperationInfo methodInfo : orderedMap.values()) {
                                System.out.print(methodInfo.getName() + "(");
                                MBeanParameterInfo[] params = methodInfo.getSignature();
                                int index = 0;
                                for (MBeanParameterInfo p : params) {
                                    if (index > 0) {
                                        System.out.print(", ");
                                    }
                                    System.out.print(p.getType() + " " + p.getName());
                                    index++;
                                }
                                System.out.print(")");
                                if (methodInfo.getReturnType() != null
                                        && !methodInfo.getReturnType().equals("void")) {
                                    System.out.print(" : " + methodInfo.getReturnType());
                                }
                                System.out.println();
                            }
                        } else if (line.hasOption(OPTION_METHOD)) {
                            String argsDelimiter = line.getOptionValue(OPTION_ARGS_DELIM);
                            if (isBlank(argsDelimiter)) {
                                argsDelimiter = ",";
                            } else {
                                argsDelimiter = argsDelimiter.trim();
                            }
                            String methodName = line.getOptionValue(OPTION_METHOD);
                            String[] args = null;
                            if (line.hasOption(OPTION_ARGS)) {
                                String argLine = line.getOptionValue(OPTION_ARGS);
                                args = argsDelimiter == "," ? CsvUtils.tokenizeCsvData(argLine)
                                        : argLine.split(argsDelimiter);
                                ;
                            } else {
                                args = new String[0];
                            }

                            MBeanOperationInfo[] operations = info.getOperations();
                            for (MBeanOperationInfo methodInfo : operations) {
                                MBeanParameterInfo[] paramInfos = methodInfo.getSignature();
                                if (methodInfo.getName().equals(methodName)
                                        && paramInfos.length == args.length) {
                                    String[] signature = new String[args.length];
                                    Object[] objArgs = new Object[args.length];
                                    int index = 0;
                                    for (MBeanParameterInfo paramInfo : paramInfos) {
                                        signature[index] = paramInfo.getType();
                                        if (!paramInfo.getType().equals(String.class.getName())) {
                                            Class<?> clazz = Class.forName(paramInfo.getType());
                                            Constructor<?> constructor = clazz.getConstructor(String.class);
                                            objArgs[index] = constructor.newInstance(args[index]);
                                        } else {
                                            objArgs[index] = args[index];
                                        }
                                        index++;
                                    }
                                    Object returnValue = mbeanConn.invoke(new ObjectName(beanName), methodName,
                                            objArgs, signature);
                                    if (methodInfo.getReturnType() != null
                                            && !methodInfo.getReturnType().equals("void")) {
                                        System.out.println(returnValue);
                                    }
                                    System.exit(0);
                                }
                            }

                            System.out.println("ERROR: Could not locate a JMX method named: " + methodName
                                    + " with " + args.length + " arguments on bean: " + beanName);
                            System.exit(1);

                            return null;

                        }
                    } else {
                        System.out.println("ERROR: Could not locate a JMX bean with the name of: " + beanName);
                        System.exit(1);
                    }
                    return null;
                }
            });
        } else {
            System.out.println("ERROR: Must specifiy the --bean option.");
            System.exit(1);
        }
    } else {
        return false;
    }

    return true;
}

From source file:com.springsource.hq.plugin.tcserver.plugin.appmgmt.TomcatJmxScriptingApplicationManager.java

private boolean isTcRuntime250OrLater(String objectName, String operationName,
        int expected25OrLaterArgumentCount, ConfigResponse config) throws JMException, IOException {
    MBeanServerConnection mBeanServer = mxUtil.getMBeanServer(config.toProperties());
    MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(new ObjectName(objectName));
    for (MBeanOperationInfo operationInfo : mBeanInfo.getOperations()) {
        if (operationInfo.getName().equals(operationName)
                && (expected25OrLaterArgumentCount == operationInfo.getSignature().length)) {
            return true;
        }/*from  w w w .ja  v  a 2  s  .c o m*/
    }
    return false;
}

From source file:org.apache.openejb.server.cli.command.LocalJMXCommand.java

private void invoke(final String value) {
    if (!value.contains("(") || !value.contains(")")) {
        streamManager.writeErr("method should follow the format: <methoName>(<arg1>,<arg2>,...)");
        return;/*from  ww  w  . j a v  a2  s.com*/
    }

    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;
    }
}