public static TabularData tableFrom(Collection<JmxProviderState> jmxProviderStates) { TabularDataSupport table = new TabularDataSupport(PROVIDER_STATE_TABLE); for (JmxProviderState entry : jmxProviderStates) { table.put(entry.asCompositeData()); } return table; }
/** Wraps the service statistics into JMX types */ private void populateServiceStatistics(boolean clearCollectedStats) { // clear existing stats in the TabularDataSupport and re-populate it with current data extracted // from service framework classes serviceInvocationStatistics.clear(); ServiceStatistics[] stats = getStats(clearCollectedStats); for (ServiceStatistics stat : stats) { Object[] statValues = new Object[attributeNames.length]; statValues[0] = stat.getServiceName(); statValues[1] = stat.getServiceVersion(); statValues[2] = stat.getStartupTimeStamp().getTime(); statValues[3] = stat.getLastCalledTimestamp() == null ? null : stat.getLastCalledTimestamp().getTime(); statValues[4] = stat.getActiveRequestsCount(); statValues[5] = stat.getTotalRequestsCount(); statValues[6] = stat.getAverageResponseTime(); statValues[7] = stat.getMinimumResponseTime(); statValues[8] = stat.getMaximumResponseTime(); statValues[9] = stat.getLastServiceRequestResponseTime(); statValues[10] = stat.getErrorRequestsCount(); statValues[11] = stat.getSuccessRequestsCount(); CompositeData compositeData; try { compositeData = new CompositeDataSupport(compositeType, attributeNames, statValues); serviceInvocationStatistics.put(compositeData); } catch (OpenDataException e) { // ideally we should not get this exception LOGGER.error( "Error constructing JMX data type from service statistics. Error is : " + e.getMessage(), e); } } }
/** * Returns a shallow clone of the information, as obtained by the {@link Object} implementation of * {@link Object#clone()}. The map is also cloned, but it still references the same objects. * * @return a shallow clone of this {@link TabularDataSupport}. */ @SuppressWarnings("unchecked") public Object clone() { TabularDataSupport clone = null; try { clone = (TabularDataSupport) super.clone(); clone.setMap((HashMap<Object, Object>) dataMap.clone()); } catch (CloneNotSupportedException e) { /* This won't happen as we implement Cloneable */ } return clone; }
@Test public void testJmxAttributeOfTypeTabularDataProviderConvertedToMap() throws Exception { // Create the CompositeType and TabularData CompositeType compositeType = new CompositeType( "typeName", "description", new String[] {"myint", "mystring", "mybool"}, // item names new String[] { "myint", "mystring", "mybool" }, // item descriptions, can't be null or empty string new OpenType<?>[] {SimpleType.INTEGER, SimpleType.STRING, SimpleType.BOOLEAN}); TabularType tt = new TabularType("typeName", "description", compositeType, new String[] {"myint"}); TabularDataSupport tds = new TabularDataSupport(tt); tds.put( new CompositeDataSupport( compositeType, new String[] {"mybool", "myint", "mystring"}, new Object[] {true, 1234, "on"})); // Create MBean GeneralisedDynamicMBean mbean = jmxService.registerMBean(ImmutableMap.of(attributeName, tds), objectName); feed = JmxFeed.builder() .entity(entity) .pollAttribute( new JmxAttributePollConfig<Map>(mapAttribute) .objectName(objectName) .attributeName(attributeName) .onSuccess((Function) JmxValueFunctions.tabularDataToMap())) .build(); // Starts with value defined when registering... assertSensorEventually( mapAttribute, ImmutableMap.of("myint", 1234, "mystring", "on", "mybool", Boolean.TRUE), TIMEOUT_MS); }
private static TabularData tabularData( String typeName, String typeDescription, String[] names, Object[] values) { if (names.length == 0) { return null; } OpenType<?>[] types = new OpenType<?>[names.length]; for (int i = 0; i < types.length; i++) { types[i] = SimpleType.STRING; } try { CompositeType ct = new CompositeType(typeName, typeDescription, names, names, types); TabularType type = new TabularType(typeName, typeDescription, ct, names); TabularDataSupport data = new TabularDataSupport(type); CompositeData line = new CompositeDataSupport(ct, names, values); data.put(line); return data; } catch (OpenDataException e) { return null; } }
/** * create result as property with name from property prefix When result is an array and * isSeparateArrayResults is true, resultproperty used as prefix (<code> * resultproperty.0-array.length</code> and store the result array length at <code> * resultproperty.length</code>. Other option is that you delimit your result with a delimiter * (java.util.StringTokenizer is used). * * @param propertyPrefix * @param result */ protected void createProperty(String propertyPrefix, Object result) { if (propertyPrefix == null) propertyPrefix = ""; if (result instanceof CompositeDataSupport) { CompositeDataSupport data = (CompositeDataSupport) result; CompositeType compositeType = data.getCompositeType(); Set<String> keys = compositeType.keySet(); for (Iterator<String> iter = keys.iterator(); iter.hasNext(); ) { String key = iter.next(); Object value = data.get(key); OpenType<?> type = compositeType.getType(key); if (type instanceof SimpleType<?>) { setProperty(propertyPrefix + "." + key, value); } else { createProperty(propertyPrefix + "." + key, value); } } } else if (result instanceof TabularDataSupport) { TabularDataSupport data = (TabularDataSupport) result; for (Iterator<Object> iter = data.keySet().iterator(); iter.hasNext(); ) { Object key = iter.next(); for (Iterator<?> iter1 = ((List<?>) key).iterator(); iter1.hasNext(); ) { Object key1 = iter1.next(); CompositeData valuedata = data.get(new Object[] {key1}); Object value = valuedata.get("value"); OpenType<?> type = valuedata.getCompositeType().getType("value"); if (type instanceof SimpleType<?>) { setProperty(propertyPrefix + "." + key1, value); } else { createProperty(propertyPrefix + "." + key1, value); } } } } else if (result.getClass().isArray()) { if (isSeparatearrayresults()) { int size = 0; for (int i = 0; i < Array.getLength(result); i++) { if (setProperty(propertyPrefix + "." + size, Array.get(result, i))) { size++; } } if (size > 0) { setProperty(propertyPrefix + ".Length", Integer.toString(size)); } } } else { String delim = getDelimiter(); if (delim != null) { StringTokenizer tokenizer = new StringTokenizer(result.toString(), delim); int size = 0; for (; tokenizer.hasMoreTokens(); ) { String token = tokenizer.nextToken(); if (setProperty(propertyPrefix + "." + size, token)) { size++; } } if (size > 0) setProperty(propertyPrefix + ".Length", Integer.toString(size)); } else { setProperty(propertyPrefix, result.toString()); } } }