// Update potentially multiple history entries for a READ request which could // return multiple values with a single request private void updateReadHistory(JmxRequest pJmxReq, JSONObject pJson, long pTimestamp) { ObjectName name = pJmxReq.getObjectName(); if (name.isPattern()) { // We have a pattern and hence a value structure // of bean -> attribute_key -> attribute_value JSONObject history = new JSONObject(); for (Map.Entry<String, Object> beanEntry : ((Map<String, Object>) pJson.get(KEY_VALUE)).entrySet()) { String beanName = beanEntry.getKey(); JSONObject beanHistory = addAttributesFromComplexValue( pJmxReq, ((Map<String, Object>) beanEntry.getValue()), beanName, pTimestamp); if (beanHistory.size() > 0) { history.put(beanName, beanHistory); } } if (history.size() > 0) { pJson.put(KEY_HISTORY, history); } } else if (!pJmxReq.isSingleAttribute() || pJmxReq.getAttributeName() == null) { // Multiple attributes, but a single bean. // Value has the following structure: // attribute_key -> attribute_value JSONObject history = addAttributesFromComplexValue( pJmxReq, ((Map<String, Object>) pJson.get(KEY_VALUE)), pJmxReq.getObjectNameAsString(), pTimestamp); if (history.size() > 0) { pJson.put(KEY_HISTORY, history); } } else { // Single attribute, single bean. Value is the attribute_value // itself. addAttributeFromSingleValue( pJson, KEY_HISTORY, new HistoryKey(pJmxReq), pJson.get(KEY_VALUE), pTimestamp); } }
private JSONObject addAttributesFromComplexValue( JmxRequest pJmxReq, Map<String, Object> pAttributesMap, String pBeanName, long pTimestamp) { JSONObject ret = new JSONObject(); for (Map.Entry<String, Object> attrEntry : pAttributesMap.entrySet()) { String attrName = attrEntry.getKey(); Object value = attrEntry.getValue(); HistoryKey key; try { key = new HistoryKey( pBeanName, attrName, null /* No path support for complex read handling */, pJmxReq.getTargetConfigUrl()); } catch (MalformedObjectNameException e) { // Shouldnt occur since we get the MBeanName from a JMX operation's result. However, // we will rethrow it throw new IllegalArgumentException("Cannot pars MBean name " + pBeanName, e); } addAttributeFromSingleValue(ret, attrName, key, value, pTimestamp); } return ret; }