List of usage examples for javax.management MBeanInfo getOperations
public MBeanOperationInfo[] getOperations()
From source file:cc.kune.kunecli.JMXUtils.java
public static Object doOperation(final String objectName, final String operation) { final List<VirtualMachineDescriptor> vms = VirtualMachine.list(); try {/*from ww w . j a va 2 s . com*/ for (final VirtualMachineDescriptor vmd : vms) { final String id = vmd.id(); LOG.debug("VM id: " + id); final JMXServiceURL url = getURLForPid(id.trim()); final JMXConnector jmxc = JMXConnectorFactory.connect(url, null); final MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); final ObjectName mbeanName = new ObjectName(objectName); MBeanInfo beanInfo; try { beanInfo = mbsc.getMBeanInfo(mbeanName); for (final MBeanOperationInfo currentOp : beanInfo.getOperations()) { if (currentOp.getName().equals(operation)) { LOG.debug("Doing operation '" + operation + "' over mbean: '" + objectName + "' with id: '" + id + "'."); final Object invoke = mbsc.invoke(mbeanName, currentOp.getName(), new Object[] {}, new String[] {}); return invoke; } } } catch (final InstanceNotFoundException e) { // Ok, operation not found in this VM or domain } } throw new RuntimeException("JMX operation not found"); } catch (final Exception e) { LOG.error("Error in jmx connection", e); } throw new RuntimeException("JMX operation failed"); }
From source file:org.hyperic.hq.plugin.jboss.MBeanUtil.java
public static OperationParams getOperationParams(MBeanInfo info, String method, Object args[]) throws PluginException { boolean isDebug = log.isDebugEnabled(); MBeanOperationInfo[] ops = info.getOperations(); MBeanParameterInfo[] pinfo = null; HashMap sigs = new HashMap(); if (isDebug) { log.debug("Converting params for: " + method + Arrays.asList(args)); }/*from www.ja v a 2 s .com*/ for (int i = 0; i < ops.length; i++) { if (ops[i].getName().equals(method)) { pinfo = ops[i].getSignature(); sigs.put(new Integer(pinfo.length), pinfo); //XXX might have more than 1 method w/ same //number of args but different signature } } if (sigs.size() == 0) { OperationParams op = getAttributeParams(info, method, args); if (op != null) { return op; } String msg = "No MBean Operation or Attribute Info found for: " + method; throw new PluginException(msg); } else if (sigs.size() > 1) { //try exact match, else last one wins. Object o = sigs.get(new Integer(args.length)); if (o != null) { pinfo = (MBeanParameterInfo[]) o; } } int len = pinfo.length; int nargs = args.length; String[] signature = new String[len]; List arguments = new ArrayList(); for (int i = 0; i < len; i++) { String sig = pinfo[i].getType(); signature[i] = sig; if (!hasConverter(sig)) { String msg = "Cannot convert String argument to " + sig; throw new PluginException(msg); } if (i >= args.length) { throw invalidParams(method, sigs, args); } if (isListType(sig)) { String[] listArgs; if (i != 0) { int diff = args.length - i; listArgs = new String[diff]; System.arraycopy(args, i, listArgs, 0, diff); } else { listArgs = (String[]) args; } nargs -= listArgs.length; arguments.add(convert(sig, listArgs)); if (i != (len - 1)) { String msg = sig + " must be the last argument"; throw new PluginException(msg); } } else { nargs--; try { arguments.add(convert(sig, (String) args[i])); } catch (Exception e) { String msg = "Exception converting param[" + i + "] '" + args[i] + "' to type '" + sig + "'"; throw new PluginException(msg + ": " + e); } } if (isDebug) { log.debug(method + "() arg " + i + "=" + arguments.get(i) + ", type=" + sig); } } if (nargs != 0) { throw invalidParams(method, sigs, args); } OperationParams params = new OperationParams(); params.signature = signature; params.arguments = arguments.toArray(); return params; }
From source file:org.hyperic.hq.product.jmx.MBeanUtil.java
public static OperationParams getOperationParams(MBeanInfo info, String method, Object args[]) throws PluginException { boolean isDebug = log.isDebugEnabled(); MBeanOperationInfo[] ops = info.getOperations(); MBeanParameterInfo[] pinfo = null; HashMap sigs = new HashMap(); String methodSignature = null; if (args.length != 0) { String arg = (String) args[0]; if (arg.startsWith("@(") && arg.endsWith(")")) { methodSignature = arg.substring(1); String[] dst = new String[args.length - 1]; System.arraycopy(args, 1, dst, 0, dst.length); args = dst;/* ww w . java2 s .c o m*/ } } if (isDebug) { String msg = "Converting params for: " + method + Arrays.asList(args); if (methodSignature != null) { msg += ", using provided signature: " + methodSignature; } log.debug(msg); } for (int i = 0; i < ops.length; i++) { if (ops[i].getName().equals(method)) { pinfo = ops[i].getSignature(); StringBuffer sig = new StringBuffer(); sig.append("("); for (int j = 0; j < pinfo.length; j++) { sig.append(pinfo[j].getType()); if (j + 1 != pinfo.length) { sig.append(';'); } } sig.append(')'); log.debug("Found operation: " + method + sig); sigs.put(sig.toString(), pinfo); sigs.put(new Integer(pinfo.length), pinfo); //XXX might have more than 1 method w/ same //number of args but different signature } } if (sigs.size() == 0) { OperationParams op = getAttributeParams(info, method, args); if (op != null) { return op; } String msg = "No MBean Operation or Attribute Info found for: " + method; throw new PluginException(msg); } else if (sigs.size() > 1) { if (methodSignature == null) { //try exact match, else last one wins. Object o = sigs.get(new Integer(args.length)); if (o != null) { pinfo = (MBeanParameterInfo[]) o; if (log.isDebugEnabled()) { log.debug("Using default sig: " + toString(pinfo)); } } } else { pinfo = (MBeanParameterInfo[]) sigs.get(methodSignature); if (pinfo == null) { String msg = "No matching Operation signature found for: " + method + methodSignature; throw new PluginException(msg); } else if (log.isDebugEnabled()) { log.debug("Using matched sig: " + toString(pinfo)); } } } int len = pinfo.length; int nargs = args.length; int consumed; String[] signature = new String[len]; List arguments = new ArrayList(); for (int i = 0, j = 0; i < len; i++, j += consumed) { consumed = 1; String sig = pinfo[i].getType(); signature[i] = sig; if (!hasConverter(sig)) { String msg = "Cannot convert String argument to " + sig; throw new PluginException(msg); } if (j >= args.length) { throw invalidParams(method, sigs, args); } if (isListType(sig)) { String[] listArgs; if (len == 1) { listArgs = (String[]) args; } else { int remain = (len - 1) - j; consumed = args.length - j - remain; listArgs = new String[consumed]; System.arraycopy(args, j, listArgs, 0, consumed); } nargs -= listArgs.length; try { arguments.add(convert(sig, listArgs)); } catch (Exception e) { String msg = "Exception converting " + Arrays.asList(listArgs) + "' to type '" + sig + "'"; throw new PluginException(msg + ": " + e); } } else { nargs--; try { arguments.add(convert(sig, (String) args[j])); } catch (Exception e) { String msg = "Exception converting param[" + j + "] '" + args[j] + "' to type '" + sig + "'"; throw new PluginException(msg + ": " + e); } } if (isDebug) { Object arg = arguments.get(i); if (arg.getClass().isArray()) { if ((arg = asObjectArray(arg)) == null) { arg = arguments.get(i).toString(); } else { arg = Arrays.asList((Object[]) arg).toString(); } } log.debug(method + "() arg " + i + "=" + arg + ", type=" + sig); } } if (nargs != 0) { throw invalidParams(method, sigs, args); } OperationParams params = new OperationParams(); params.signature = signature; params.arguments = arguments.toArray(); return params; }
From source file:org.jolokia.handler.list.OperationDataUpdater.java
/** {@inheritDoc} */ @Override/* w w w .j av 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:org.rhq.plugins.jslee.ServiceSbbUsageParameterSetComponent.java
@Override public Configuration loadResourceConfiguration() throws Exception { try {// w w w . j ava 2 s.c o m Configuration config = new Configuration(); MBeanServerConnection connection = this.mbeanUtils.getConnection(); this.mbeanUtils.login(); // As an example, if a particular service has the name FooService, vendor FooCompany, and version 1.0, // then the Object Name of a ServiceUsageMBean for that service would be: // javax.slee.management.usage:type=ServiceUsage,serviceName="FooService", serviceVendor="FooCompany",serviceVersion="1.0" ObjectName serviceUsageON = new ObjectName(ServiceUsageMBean.BASE_OBJECT_NAME + ',' + ServiceUsageMBean.SERVICE_NAME_KEY + '=' + ObjectName.quote(serviceId.getName()) + ',' + ServiceUsageMBean.SERVICE_VENDOR_KEY + '=' + ObjectName.quote(serviceId.getVendor()) + ',' + ServiceUsageMBean.SERVICE_VERSION_KEY + '=' + ObjectName.quote(serviceId.getVersion())); ServiceUsageMBean serviceUsageMBean = (ServiceUsageMBean) MBeanServerInvocationHandler.newProxyInstance( connection, serviceUsageON, javax.slee.management.ServiceUsageMBean.class, false); PropertyList columnList = new PropertyList("usageParameter"); ObjectName sbbUsageON = null; if (usageParameterSetName != null && !usageParameterSetName.equals("<default>")) { sbbUsageON = serviceUsageMBean.getSbbUsageMBean(sbbId, usageParameterSetName); } else { sbbUsageON = serviceUsageMBean.getSbbUsageMBean(sbbId); } MBeanInfo usageInfo = connection.getMBeanInfo(sbbUsageON); for (MBeanOperationInfo operation : usageInfo.getOperations()) { String opName = operation.getName(); if (opName.startsWith("get")) { PropertyMap col = new PropertyMap("usageParameterDefinition"); col.put(new PropertySimple("usageParameterName", opName.replaceFirst("get", ""))); boolean isSampleType = operation.getReturnType().equals("javax.slee.usage.SampleStatistics"); col.put(new PropertySimple("usageParameterType", isSampleType ? "Sample" : "Counter")); Object value = connection.invoke(sbbUsageON, opName, new Object[] { false }, new String[] { "boolean" }); col.put(new PropertySimple("usageParameterValue", value)); columnList.add(col); } } config.put(columnList); return config; } finally { try { this.mbeanUtils.logout(); } catch (LoginException e) { if (log.isDebugEnabled()) { log.debug("Failed to logout from secured JMX", e); } } } }
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 w w w. j a v a 2 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; } }
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);//from ww w . ja v a 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.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())); }//from w w w.j ava 2 s. c om 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.jumpmind.symmetric.JmxCommand.java
@Override protected boolean executeWithOptions(final CommandLine line) throws Exception { if (line.hasOption(OPTION_LISTBEANS)) { execute(new IJmxTemplate<Object>() { @Override/*from ww w . j ava 2s .co m*/ 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:net.sbbi.upnp.jmx.UPNPMBeanService.java
private String getDeviceSSDP(MBeanInfo info) throws IllegalArgumentException { MBeanOperationInfo[] ops = info.getOperations(); MBeanAttributeInfo[] atts = info.getAttributes(); if ((ops == null || ops.length == 0) && (atts == null || atts.length == 0)) { throw new IllegalArgumentException( "MBean has no operation and no attribute and cannot be exposed as an UPNP device, provide at least one attribute"); }/*w w w. j a v a 2s . c o m*/ Set deployedActionNames = new HashSet(); operationsStateVariables = new HashMap(); StringBuffer rtrVal = new StringBuffer(); rtrVal.append("<?xml version=\"1.0\" ?>\r\n"); rtrVal.append("<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">\r\n"); rtrVal.append("<specVersion><major>1</major><minor>0</minor></specVersion>\r\n"); if (ops != null && ops.length > 0) { rtrVal.append("<actionList>\r\n"); for (int i = 0; i < ops.length; i++) { MBeanOperationInfo op = ops[i]; StringBuffer action = new StringBuffer(); if (deployedActionNames.contains(op.getName())) { log.debug("The " + op.getName() + " is allready deplyoed and cannot be reused, skipping operation deployment"); continue; } action.append("<action>\r\n"); action.append("<name>"); action.append(op.getName()); action.append("</name>\r\n"); action.append("<argumentList>\r\n"); // output argument action.append("<argument>\r\n"); action.append("<name>"); // TODO handle specific output vars String outVarName = op.getName() + "_out"; String actionOutDataType = ServiceStateVariable.getUPNPDataTypeMapping(op.getReturnType()); if (actionOutDataType == null) actionOutDataType = ServiceStateVariableTypes.STRING; action.append(outVarName); action.append("</name>\r\n"); action.append("<direction>out</direction>\r\n"); action.append("<relatedStateVariable>"); action.append(outVarName); action.append("</relatedStateVariable>\r\n"); action.append("</argument>\r\n"); // handle now for all input argument boolean nonPrimitiveInputType = false; boolean duplicatedInputVarname = false; Map operationsInputStateVariables = new HashMap(); if (op.getSignature() != null) { for (int z = 0; z < op.getSignature().length; z++) { MBeanParameterInfo param = op.getSignature()[z]; // do some sanity checks String actionInDataType = ServiceStateVariable.getUPNPDataTypeMapping(param.getType()); if (actionInDataType == null) { nonPrimitiveInputType = true; log.debug("The " + param.getType() + " type is not an UPNP compatible data type, use only primitives"); break; } String inVarName = param.getName(); // check that if the name does allready exists it // has the same type String existing = (String) operationsStateVariables.get(inVarName); if (existing != null && !existing.equals(actionInDataType)) { String msg = "The operation " + op.getName() + " " + inVarName + " parameter already exists for another method with another data type (" + existing + ") either match the data type or change the parameter name" + " in you MBeanParameterInfo object for this operation"; duplicatedInputVarname = true; log.debug(msg); break; } action.append("<argument>\r\n"); action.append("<name>"); operationsInputStateVariables.put(inVarName, actionInDataType); action.append(inVarName); action.append("</name>\r\n"); action.append("<direction>in</direction>\r\n"); action.append("<relatedStateVariable>"); action.append(inVarName); action.append("</relatedStateVariable>\r\n"); action.append("</argument>\r\n"); } } action.append("</argumentList>\r\n"); action.append("</action>\r\n"); // finally the action is only added to the UPNP SSDP if no problems have been detected // with the input parameters type and names. if (!nonPrimitiveInputType && !duplicatedInputVarname) { operationsStateVariables.putAll(operationsInputStateVariables); operationsStateVariables.put(outVarName, actionOutDataType); rtrVal.append(action.toString()); deployedActionNames.add(op.getName()); } } rtrVal.append("</actionList>\r\n"); } else { rtrVal.append("<actionList/>\r\n"); } // now append the operation created state vars rtrVal.append("<serviceStateTable>\r\n"); for (Iterator i = operationsStateVariables.keySet().iterator(); i.hasNext();) { String name = (String) i.next(); String type = (String) operationsStateVariables.get(name); // TODO handle sendevents with mbean notifications ??? // TODO handle defaultValue and allowedValueList values rtrVal.append("<stateVariable sendEvents=\"no\">\r\n"); rtrVal.append("<name>"); rtrVal.append(name); rtrVal.append("</name>\r\n"); rtrVal.append("<dataType>"); rtrVal.append(type); rtrVal.append("</dataType>\r\n"); rtrVal.append("</stateVariable>\r\n"); } if (atts != null && atts.length > 0) { for (int i = 0; i < atts.length; i++) { MBeanAttributeInfo att = atts[i]; if (att.isReadable()) { rtrVal.append("<stateVariable sendEvents=\"no\">\r\n"); rtrVal.append("<name>"); rtrVal.append(att.getName()); rtrVal.append("</name>\r\n"); rtrVal.append("<dataType>"); // TODO check if this works String stateVarType = ServiceStateVariable.getUPNPDataTypeMapping(att.getType()); if (stateVarType == null) stateVarType = ServiceStateVariableTypes.STRING; rtrVal.append(stateVarType); rtrVal.append("</dataType>\r\n"); rtrVal.append("</stateVariable>\r\n"); } } } rtrVal.append("</serviceStateTable>\r\n"); rtrVal.append("</scpd>"); return rtrVal.toString(); }