protected Result describeMbean( @Nonnull MBeanServerConnection mbeanServer, @Nonnull ObjectName objectName) throws IntrospectionException, ReflectionException, InstanceNotFoundException, IOException { MBeanInfo mbeanInfo = mbeanServer.getMBeanInfo(objectName); StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); out.println("# MBEAN"); out.println(objectName.toString()); out.println(); out.println("## OPERATIONS"); List<MBeanOperationInfo> operations = Arrays.asList(mbeanInfo.getOperations()); Collections.sort( operations, new Comparator<MBeanOperationInfo>() { @Override public int compare(MBeanOperationInfo o1, MBeanOperationInfo o2) { return o1.getName().compareTo(o1.getName()); } }); for (MBeanOperationInfo opInfo : operations) { out.print("* " + opInfo.getName() + "("); MBeanParameterInfo[] signature = opInfo.getSignature(); for (int i = 0; i < signature.length; i++) { MBeanParameterInfo paramInfo = signature[i]; out.print(paramInfo.getType() + " " + paramInfo.getName()); if (i < signature.length - 1) { out.print(", "); } } out.print("):" + opInfo.getReturnType() /* + " - " + opInfo.getDescription() */); out.println(); } out.println(); out.println("## ATTRIBUTES"); List<MBeanAttributeInfo> attributes = Arrays.asList(mbeanInfo.getAttributes()); Collections.sort( attributes, new Comparator<MBeanAttributeInfo>() { @Override public int compare(MBeanAttributeInfo o1, MBeanAttributeInfo o2) { return o1.getName().compareTo(o2.getName()); } }); for (MBeanAttributeInfo attrInfo : attributes) { out.println( "* " + attrInfo.getName() + ": " + attrInfo.getType() + " - " + (attrInfo.isReadable() ? "r" : "") + (attrInfo.isWritable() ? "w" : "") /* + " - " + attrInfo.getDescription() */); } String description = sw.getBuffer().toString(); return new Result(objectName, description, description); }
/** * Description of the specified attribute name. * * @param attr - the attribute * @return String the description */ protected String describeAttribute(MBeanAttributeInfo attr) { StringBuilder buf = new StringBuilder(); buf.append("("); if (attr.isReadable()) { buf.append("r"); } if (attr.isWritable()) { buf.append("w"); } buf.append(") ").append(attr.getType()).append(" ").append(attr.getName()); return buf.toString(); }
/** * @param mbeanServer * @param objectName * @param attributeName * @param attributeValue if <code>null</code>, this is a read access. * @return if this is a read access, the returned value (may be null); if it is a write access, * return <code>void.class</code>. * @throws IOException * @throws JMException */ public Result invokeAttribute( @Nonnull MBeanServerConnection mbeanServer, @Nonnull ObjectName objectName, @Nonnull String attributeName, @Nullable String attributeValue) throws IOException, JMException { MBeanInfo mbeanInfo = mbeanServer.getMBeanInfo(objectName); MBeanAttributeInfo attributeInfo = null; for (MBeanAttributeInfo mai : mbeanInfo.getAttributes()) { if (mai.getName().equals(attributeName)) { attributeInfo = mai; break; } } if (attributeInfo == null) { throw new IllegalArgumentException( "No attribute '" + attributeName + "' found on '" + objectName + "'. Existing attributes: " + Arrays.asList(mbeanInfo.getAttributes())); } String description; Object resultValue; if (attributeValue == null) { if (attributeInfo.isReadable()) { Object attribute = mbeanServer.getAttribute(objectName, attributeName); logger.info("get attribute value {}:{}:{}", objectName, attributeName, attribute); resultValue = attribute; description = "Get attribute value " + objectName + ":" + attributeName + ": " + resultValue; } else { throw new IllegalArgumentException( "Attribute '" + attributeName + "' is not readable on '" + objectName + "': " + attributeInfo); } } else { if (attributeInfo.isWritable()) { Object value = convertValue(attributeValue, attributeInfo.getType()); mbeanServer.setAttribute(objectName, new Attribute(attributeName, value)); logger.info("set attribute value {}:{}:{}", objectName, attributeName, value); description = "Set attribute value " + objectName + ":" + attributeName + ": " + value; resultValue = void.class; } else { throw new IllegalArgumentException( "Attribute '" + attributeName + "' is not writable on '" + objectName + "': " + attributeInfo); } } return new Result(objectName, resultValue, description); }