/** * Configure the history length for a specific entry. If the length is 0 disable history for this * key * * @param pKey history key * @param pMaxEntries number of maximal entries. If larger than globalMaxEntries, then * globalMaxEntries is used instead. */ public synchronized void configure(HistoryKey pKey, int pMaxEntries) { int maxEntries = pMaxEntries > globalMaxEntries ? globalMaxEntries : pMaxEntries; // Remove entries if set to 0 if (pMaxEntries == 0) { removeEntries(pKey); return; } if (pKey.isMBeanPattern()) { patterns.put(pKey, maxEntries); // Trim all already stored keys for (HistoryKey key : historyStore.keySet()) { if (pKey.matches(key)) { HistoryEntry entry = historyStore.get(key); entry.setMaxEntries(maxEntries); entry.trim(); } } } else { HistoryEntry entry = historyStore.get(pKey); if (entry != null) { entry.setMaxEntries(maxEntries); entry.trim(); } else { entry = new HistoryEntry(maxEntries); historyStore.put(pKey, entry); } } }
private HistoryEntry getEntry(HistoryKey pKey, Object pValue, long pTimestamp) { HistoryEntry entry = historyStore.get(pKey); if (entry != null) { return entry; } // Now try all known patterns and add lazily the key for (HistoryKey key : patterns.keySet()) { if (key.matches(pKey)) { entry = new HistoryEntry(patterns.get(key)); entry.add(pValue, pTimestamp); historyStore.put(pKey, entry); return entry; } } return null; }
/** * Update the history store with the value of an an read, write or execute operation. Also, the * timestamp of the insertion is recorded. Also, the recorded history values are added to the * given json value. * * @param pJmxReq request for which an entry should be added in this history store * @param pJson the JSONObject to which to add the history. */ public synchronized void updateAndAdd(JmxRequest pJmxReq, JSONObject pJson) { long timestamp = System.currentTimeMillis() / 1000; pJson.put(KEY_TIMESTAMP, timestamp); JmxRequest.Type type = pJmxReq.getType(); if (type == EXEC || type == WRITE) { HistoryEntry entry = historyStore.get(new HistoryKey(pJmxReq)); if (entry != null) { synchronized (entry) { // A history data to json object for the response pJson.put(KEY_HISTORY, entry.jsonifyValues()); // Update history for next time if (type == EXEC) { entry.add(pJson.get(KEY_VALUE), timestamp); } else if (type == WRITE) { // The new value to set as string representation entry.add(pJmxReq.getValue(), timestamp); } } } } else if (type == READ) { updateReadHistory(pJmxReq, pJson, timestamp); } }
/** * Set the global maximum limit for history entries * * @param pGlobalMaxEntries limit */ public synchronized void setGlobalMaxEntries(int pGlobalMaxEntries) { globalMaxEntries = pGlobalMaxEntries; // Refresh all entries for (HistoryEntry entry : historyStore.values()) { entry.setMaxEntries(globalMaxEntries); entry.trim(); } }
// Remove entries private void removeEntries(HistoryKey pKey) { if (pKey.isMBeanPattern()) { patterns.remove(pKey); List<HistoryKey> toRemove = new ArrayList<HistoryKey>(); for (HistoryKey key : historyStore.keySet()) { if (pKey.matches(key)) { toRemove.add(key); } } // Avoid concurrent modification exceptions for (HistoryKey key : toRemove) { historyStore.remove(key); } } else { HistoryEntry entry = historyStore.get(pKey); if (entry != null) { historyStore.remove(pKey); } } }
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; }