@Mapping( "type" ) public ValueRepresentation getType() { return ValueRepresentation.string( attrInfo.getType() ); }
private static Type getColumnType(MBeanAttributeInfo attribute) { switch (attribute.getType()) { case "boolean": case "java.lang.Boolean": return BOOLEAN; case "byte": case "java.lang.Byte": case "short": case "java.lang.Short": case "int": case "java.lang.Integer": case "long": case "java.lang.Long": return BIGINT; case "java.lang.Number": case "float": case "java.lang.Float": case "double": case "java.lang.Double": return DOUBLE; } return createUnboundedVarcharType(); } }
private static void printJMXStats() { MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName groupMulticast = getGroupMulticastObjectName(mBeanServer); if (groupMulticast == null) { System.err.println("Unable to find the GROUP_MULTICAST protocol"); return ; } try { System.out.println("======== JMX STATS ========="); for (MBeanAttributeInfo mBeanAttributeInfo : mBeanServer.getMBeanInfo(groupMulticast).getAttributes()) { String attribute = mBeanAttributeInfo.getName(); String type = mBeanAttributeInfo.getType(); if (!type.equals("double") && !type.equals("int")) { continue; } System.out.println(attribute + "=" + mBeanServer.getAttribute(groupMulticast, attribute)); } System.out.println("======== JMX STATS ========="); } catch (Exception e) { System.err.println("Error collecting stats" + e.getLocalizedMessage()); } }
/** * 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(); }
public String get(ObjectName bean, String attr) throws Exception { MBeanInfo mbinfo = mserver.getMBeanInfo(bean); MBeanAttributeInfo ai = attrInfo(mbinfo, attr); if (ai == null) { throw new IllegalArgumentException("No such attribute '" + attr + "'"); } if (!ai.isReadable()) { throw new IllegalArgumentException("Attribute '" + attr + "' is write-only"); } Object v = mserver.getAttribute(bean, attr); String type = ai.getType(); String text = format(v, type); return text; }
public void set(ObjectName bean, String attr, String value) throws Exception { MBeanInfo mbinfo = mserver.getMBeanInfo(bean); MBeanAttributeInfo ai = attrInfo(mbinfo, attr); if (ai == null) { throw new IllegalArgumentException("No such attribute '" + attr + "'"); } if (!ai.isWritable()) { throw new IllegalArgumentException("Attribute '" + attr + "' is not writeable"); } String type = ai.getType(); Object ov = convert(value, type); mserver.setAttribute(bean, new Attribute(attr, ov)); }
for (int i = 0; i < attributes.length; i++) { result.append(' ' + attributes[i].getName() + ": " + attributes[i].getDescription() + " (type=" + attributes[i].getType() + ")"); result.append("\n");
protected static Object doAttributeOperation(MBeanServerConnection mbsc, ObjectInstance instance, String command, MBeanAttributeInfo[] infos) throws Exception { // Usually we get attributes. If an argument, then we're being asked // to set attribute. CommandParse parse = new CommandParse(command); if (parse.getArgs() == null || parse.getArgs().length == 0) { // Special-casing. If the subCommand is 'Attributes', then return // list of all attributes. if (command.equals("Attributes")) { String[] names = new String[infos.length]; for (int i = 0; i < infos.length; i++) { names[i] = infos[i].getName(); } return mbsc.getAttributes(instance.getObjectName(), names); } return mbsc.getAttribute(instance.getObjectName(), parse.getCmd()); } if (parse.getArgs().length != 1) { throw new IllegalArgumentException("One only argument setting " + "attribute values: " + parse.getArgs()); } // Get first attribute of name 'cmd'. Assumption is no method // overrides. Then, look at the attribute and use its type. MBeanAttributeInfo info = (MBeanAttributeInfo) getFeatureInfo(infos, parse.getCmd()); java.lang.reflect.Constructor c = Class.forName(info.getType()).getConstructor(new Class[] { String.class }); Attribute a = new Attribute(parse.getCmd(), c.newInstance(new Object[] { parse.getArgs()[0] })); mbsc.setAttribute(instance.getObjectName(), a); return null; }
for(MBeanAttributeInfo ai: mbinfo.getAttributes()) { sb.append(" (A) "); sb.append(ai.getName()).append(" : ").append(toPrintableType(ai.getType())).append(""); if (!ai.isReadable()) { sb.append(" - WRITEONLY");
log.info(" ** NAME: \t" + attrInfo[i].getName()); log.info(" DESCR: \t" + attrInfo[i].getDescription()); log.info(" TYPE: \t" + attrInfo[i].getType() + "\tREAD: " + attrInfo[i].isReadable() + "\tWRITE: " + attrInfo[i].isWritable());
for (MBeanAttributeInfo info : attrinuteInfos) { if ("fooMetric".equals(info.getName())) { assertEquals("java.lang.Long", info.getType()); assertTrue(info.isReadable()); assertTrue(info.isWritable());
/** * Get the list of attributes for the MBean, filtering out a few attribute * types. */ private static Set<String> getFilteredAttributes(MBeanInfo mBeanInfo) { Set<String> attributeNames = new HashSet<>(); for (MBeanAttributeInfo attributeInfo : mBeanInfo.getAttributes()) { if (!attributeInfo.getType().equals( "javax.management.openmbean.TabularData") && !attributeInfo.getType().equals( "javax.management.openmbean.CompositeData") && !attributeInfo.getType().equals( "[Ljavax.management.openmbean.CompositeData;")) { attributeNames.add(attributeInfo.getName()); } } return attributeNames; }
/** {@inheritDoc} */ @Override protected JSONObject extractData(MBeanInfo pMBeanInfo, String attribute) { JSONObject attrMap = new JSONObject(); for (MBeanAttributeInfo attrInfo : pMBeanInfo.getAttributes()) { if (attribute == null || attrInfo.getName().equals(attribute)) { JSONObject map = new JSONObject(); map.put(TYPE.getKey(), attrInfo.getType()); map.put(DESCRIPTION.getKey(), attrInfo.getDescription()); map.put(READ_WRITE.getKey(), Boolean.valueOf(attrInfo.isWritable() && attrInfo.isReadable())); attrMap.put(attrInfo.getName(), map); } } return attrMap; } }
private static void addNumericAttributes(MBeanAttributeInfo attribute, Object value, Set<String> attributeNames) { String attributeType = attribute.getType(); if (numericAttributeTypes.contains(attributeType) || attributeType.equals("java.lang.Object") && value instanceof Number) { attributeNames.add(attribute.getName()); } else if (attributeType.equals("java.lang.String") && value instanceof String) { try { Double.parseDouble((String) value); attributeNames.add(attribute.getName()); } catch (NumberFormatException e) { // log exception at debug level logger.debug(e.getMessage(), e); } } else if (attributeType.equals(CompositeData.class.getName())) { Object openType = attribute.getDescriptor().getFieldValue("openType"); CompositeType compositeType = null; if (openType instanceof CompositeType) { compositeType = (CompositeType) openType; } else if (openType == null && value instanceof CompositeDataSupport) { compositeType = ((CompositeDataSupport) value).getCompositeType(); } if (compositeType != null) { attributeNames .addAll(getCompositeTypeAttributeNames(attribute, value, compositeType)); } } }
/** {@inheritDoc} */ public void setAttribute(Attribute pAttribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { try { if (!attributeInfoMap.containsKey(pAttribute.getName())) { jolokiaMBeanServer.setAttribute(objectName, pAttribute); } else { String name = pAttribute.getName(); MBeanAttributeInfo info = attributeInfoMap.get(name); Object value; if (info instanceof OpenMBeanAttributeInfo) { value = fromJson(((OpenMBeanAttributeInfo) info).getOpenType(), (String) pAttribute.getValue()); } else { value = fromJson(info.getType(), (String) pAttribute.getValue()); } Attribute attr = new Attribute(name, value); jolokiaMBeanServer.setAttribute(objectName, attr); } } catch (InstanceNotFoundException e) { AttributeNotFoundException exp = new AttributeNotFoundException("MBean " + objectName + " not found for attribute " + pAttribute); exp.initCause(e); throw exp; } }
private MBeanAttributeInfo[] getWrappedAttributeInfo(MBeanInfo pMBeanInfo) { MBeanAttributeInfo origAttrInfo[] = pMBeanInfo.getAttributes(); MBeanAttributeInfo attrInfo[] = new MBeanAttributeInfo[origAttrInfo.length]; for (int i = 0; i < origAttrInfo.length; i++) { MBeanAttributeInfo aInfo = origAttrInfo[i]; String clazz = aInfo.getType(); String attrType; if (isDirectlySupported(clazz)) { attrType = clazz; } else { attrType = STRING_TYPE; // Remember type for later conversion when setting an attribute attributeInfoMap.put(aInfo.getName(), aInfo); } attrInfo[i] = new MBeanAttributeInfo( aInfo.getName(), attrType, aInfo.getDescription(), aInfo.isReadable(), aInfo.isWritable(), aInfo.isIs(), aInfo.getDescriptor()); } return attrInfo; }
new LinkedList<String>(), attr.getName(), attr.getType(), attr.getDescription(), attribute.getValue()
private Object setAttribute(JmxWriteRequest request, MBeanServerConnection server) throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException, IntrospectionException, InvalidAttributeValueException, IllegalAccessException, InvocationTargetException, IOException { // Old value, will throw an exception if attribute is not known. That's good. Object oldValue = server.getAttribute(request.getObjectName(), request.getAttributeName()); MBeanInfo mInfo = server.getMBeanInfo(request.getObjectName()); MBeanAttributeInfo aInfo = null; for (MBeanAttributeInfo i : mInfo.getAttributes()) { if (i.getName().equals(request.getAttributeName())) { aInfo = i; break; } } Object values[]; if (aInfo instanceof OpenMBeanAttributeInfo) { values = getValues((OpenMBeanAttributeInfo) aInfo, oldValue, request); } else { // aInfo is != null otherwise getAttribute() would have already thrown an ArgumentNotFoundException values = getValues(aInfo.getType(), oldValue, request); } Attribute attribute = new Attribute(request.getAttributeName(),values[0]); server.setAttribute(request.getObjectName(),attribute); return values[1]; }
protected boolean attributeTypeMatches(final String attributeName, final Class<?> clazz) { boolean matches = false; final MBeanAttributeInfo info = getAttributeInfo(attributeName); if (info != null) { if (clazz.getName().equals(info.getType())) { matches = true; } } return matches; }
add(new RowElement().style(Decoration.bold.fg(Color.black).bg(Color.white)).add("NAME", "TYPE", "DESCRIPTION")); for (MBeanAttributeInfo attributeInfo : info.getAttributes()) { attributes.row(attributeInfo.getName(), attributeInfo.getType(), attributeInfo.getDescription());