protected String getStringValue(MBeanServerConnection mbsc, String key) throwsException {
MonitorItem item= newMonitorItem(key);if (item.getKeyId().equals("jmx")) {if (2 !=item.getArgumentCount()) {throw newMonitorException("required key format: jmx[,]");
}
ObjectName objectName= new ObjectName(item.getArgument(1));
String attributeName= item.getArgument(2);
String realAttributeName;
String fieldNames= "";intsep;//
//Attribute name and composite data field names are separated by dots. On the other hand the//name may contain a dot too. In this case user needs to escape it with a backslash. Also the//backslash symbols in the name must be escaped. So a real separator is unescaped dot and//separatorIndex() is used to locate it.//sep=HelperFunctionChest.separatorIndex(attributeName);if (-1 !=sep) {
LOGGER.trace("'{}' contains composite data", attributeName);
realAttributeName= attributeName.substring(0, sep);
fieldNames= attributeName.substring(sep + 1);
}else{
realAttributeName=attributeName;
}//unescape possible dots or backslashes that were escaped by user
realAttributeName =HelperFunctionChest.unescapeUserInput(realAttributeName);
LOGGER.trace("attributeName:'{}'", realAttributeName);
LOGGER.trace("fieldNames:'{}'", fieldNames);returngetPrimitiveAttributeValue(mbsc.getAttribute(objectName, realAttributeName),
fieldNames);
}else if (item.getKeyId().equals("jmx.discovery")) {if (0 !=item.getArgumentCount()) {throw new MonitorException("required key format: jmx.discovery");
}
JsonArray counters= newJsonArray();for (ObjectName name : mbsc.queryNames(null, null)) {
LOGGER.trace("discovered object '{}'", name);for(MBeanAttributeInfo attrInfo : mbsc.getMBeanInfo(name).getAttributes()) {
LOGGER.trace("discovered attribute '{}'", attrInfo.getName());if (!attrInfo.isReadable()) {
LOGGER.trace("attribute not readable, skipping");continue;
}try{
LOGGER.trace("looking for attributes of primitive types");
String
descr=(attrInfo.getName().equals(attrInfo.getDescription())? null: attrInfo
.getDescription());
findPrimitiveAttributes(counters, name, descr, attrInfo.getName(),
mbsc.getAttribute(name, attrInfo.getName()));
}catch(Exception e) {
Object[] logInfo={name, attrInfo.getName(), e};
LOGGER.trace("processing '{},{}' failed", logInfo);
}
}
}
JsonObject mapping= newJsonObject();
mapping.add(MonitorConst.JSON_TAG_DATA, counters);returnmapping.toString();
}else{throw new MonitorException("key ID '%s' is not supported", item.getKeyId());
}
}private String getPrimitiveAttributeValue(Object dataObject, String fieldNames) throwsMonitorException {
LOGGER
.trace("drilling down with data object '{}' and field names '{}'", dataObject,
fieldNames);if (null ==dataObject) {throw new MonitorException("data object is null");
}if (fieldNames.equals("")) {if(isPrimitiveAttributeType(dataObject.getClass())) {returndataObject.toString();
}else{throw newMonitorException("data object type is not primitive: %s" +dataObject.getClass());
}
}if (dataObject instanceofCompositeData) {
LOGGER.trace("'{}' contains composite data", dataObject);
CompositeData comp=(CompositeData) dataObject;
String dataObjectName;
String newFieldNames= "";int sep =HelperFunctionChest.separatorIndex(fieldNames);if (-1 !=sep) {
dataObjectName= fieldNames.substring(0, sep);
newFieldNames= fieldNames.substring(sep + 1);
}else{
dataObjectName=fieldNames;
}//unescape possible dots or backslashes that were escaped by user
dataObjectName =HelperFunctionChest.unescapeUserInput(dataObjectName);returngetPrimitiveAttributeValue(comp.get(dataObjectName), newFieldNames);
}else{throw new MonitorException("unsupported data object type along the path: %s",
dataObject.getClass());
}
}private voidfindPrimitiveAttributes(JsonArray counters, ObjectName name, String descr,
String attrPath, Object attribute) {
LOGGER.trace("drilling down with attribute path '{}'", attrPath);if(isPrimitiveAttributeType(attribute.getClass())) {
LOGGER.trace("found attribute of a primitive type: {}", attribute.getClass());
JsonObject counter= newJsonObject();
counter.addProperty("{#JMXDESC}", null == descr ? name + "," +attrPath : descr);
counter.addProperty("{#JMXOBJ}", name.toString());
counter.addProperty("{#JMXATTR}", attrPath);
counter.addProperty("{#JMXTYPE}", attribute.getClass().getName());
counter.addProperty("{#JMXVALUE}", attribute.toString());
counters.add(counter);
}else if (attribute instanceofCompositeData) {
LOGGER.trace("found attribute of a composite type: {}", attribute.getClass());
CompositeData comp=(CompositeData) attribute;for(String key : comp.getCompositeType().keySet()) {
findPrimitiveAttributes(counters, name, descr, attrPath+ "." +key, comp.get(key));
}
}else if (attribute instanceof TabularDataSupport ||attribute.getClass().isArray()) {
LOGGER.trace("found attribute of a known, unsupported type: {}", attribute.getClass());
}else{
LOGGER
.trace("found attribute of an unknown, unsupported type: {}", attribute.getClass());
}
}private boolean isPrimitiveAttributeType(Class>clazz) {
Class>[]
clazzez={Boolean.class, Character.class, Byte.class, Short.class, Integer.class, Long.class,
Float.class, Double.class, String.class, java.math.BigDecimal.class,
java.math.BigInteger.class,
java.util.Date.class, ObjectName.class};returnHelperFunctionChest.arrayContains(clazzez, clazz);
}