public static void appendTabularMap(Map map, CompositeData cdata, String fieldName) { Object tabularObject = cdata.get(fieldName); if (tabularObject instanceof TabularData) { TabularData tabularData = (TabularData) tabularObject; Collection<CompositeData> values = (Collection<CompositeData>) tabularData.values(); for (CompositeData compositeData : values) { Object key = compositeData.get("key"); Object value = compositeData.get("value"); map.put(key, value); } } }
private Map<String, Object> toNeo4jValue( CompositeData composite ) { HashMap<String,Object> properties = new HashMap<>(); for ( String key : composite.getCompositeType().keySet() ) { properties.put( key, toNeo4jValue(composite.get( key )) ); } return map( "description", composite.getCompositeType().getDescription(), "properties", properties ); }
@Mapping( "value" ) public ListRepresentation getValue() { ArrayList<Representation> values = new ArrayList<>(); for ( Object key : data.getCompositeType().keySet() ) { String name = key.toString(); String description = data.getCompositeType().getDescription( name ); Representation value = REPRESENTATION_DISPATCHER.dispatch( data.get( name ), "" ); values.add( new NameDescriptionValueRepresentation( name, description, value ) ); } return new ListRepresentation( "value", values ); } }
public static JobDataMap newJobDataMap(TabularData tabularData) { JobDataMap jobDataMap = new JobDataMap(); if(tabularData != null) { for (final Iterator<?> pos = tabularData.values().iterator(); pos.hasNext();) { CompositeData cData = (CompositeData) pos.next(); jobDataMap.put((String) cData.get("key"), (String) cData.get("value")); } } return jobDataMap; }
private String[] formatRow(CompositeData row, List<String> header) { String[] text = new String[header.size()]; for(int i = 0; i != text.length; ++i) { String attr = header.get(i); text[i] = formatLine(row.get(attr), row.getCompositeType().getType(attr).getClassName()); } return text; }
public static long getLastGcDuration(ObjectName gcCollector) { long lastGcDuration = 0; Object lastGcInfo = getValueFromMBean(gcCollector, "LastGcInfo"); if (lastGcInfo != null && lastGcInfo instanceof CompositeData) { CompositeData cds = (CompositeData)lastGcInfo; lastGcDuration = (long) cds.get("duration"); } return lastGcDuration; }
/** * Recursively extracts simple numeric values from composite data objects. * The map {@code values} will be populated with a path to the value as * the key and the simple object as the value. */ private void extractValues(String path, Map<String, Object> values, CompositeData obj) { for (String key : obj.getCompositeType().keySet()) { String newPath = (path == null) ? key : path + "." + key; Object value = obj.get(key); if (value instanceof CompositeData) { extractValues(newPath, values, (CompositeData) value); } else if (value != null) { values.put(newPath, value); } } }
public static OperableTrigger newTrigger(CompositeData cData) throws ParseException { SimpleTriggerImpl result = new SimpleTriggerImpl(); result.setRepeatCount(((Integer) cData.get("repeatCount")).intValue()); result.setRepeatInterval(((Long) cData.get("repeatInterval")).longValue()); result.setTimesTriggered(((Integer) cData.get("timesTriggered")).intValue()); TriggerSupport.initializeTrigger(result, cData); return result; }
protected static StringBuffer recurseCompositeData(StringBuffer buffer, String indent, String name, CompositeData data) { indent = addNameToBuffer(buffer, indent, name); for (Iterator i = data.getCompositeType().keySet().iterator(); i.hasNext();) { String key = (String) i.next(); Object o = data.get(key); if (o instanceof CompositeData) { recurseCompositeData(buffer, indent + " ", key, (CompositeData) o); } else if (o instanceof TabularData) { recurseTabularData(buffer, indent, key, (TabularData) o); } else { buffer.append(indent); buffer.append(key); buffer.append(": "); buffer.append(o); buffer.append("\n"); } } return buffer; }
/** * @param cData * @return JobDetail */ public static JobDetail newJobDetail(CompositeData cData) throws ClassNotFoundException { JobDetailImpl jobDetail = new JobDetailImpl(); int i = 0; jobDetail.setName((String) cData.get(ITEM_NAMES[i++])); jobDetail.setGroup((String) cData.get(ITEM_NAMES[i++])); jobDetail.setDescription((String) cData.get(ITEM_NAMES[i++])); Class<?> jobClass = Class.forName((String) cData.get(ITEM_NAMES[i++])); @SuppressWarnings("unchecked") Class<? extends Job> jobClassTyped = (Class<? extends Job>)jobClass; jobDetail.setJobClass(jobClassTyped); jobDetail.setJobDataMap(JobDataMapSupport.newJobDataMap((TabularData) cData.get(ITEM_NAMES[i++]))); jobDetail.setDurability((Boolean) cData.get(ITEM_NAMES[i++])); jobDetail.setRequestsRecovery((Boolean) cData.get(ITEM_NAMES[i++])); return jobDetail; }
public void writeCompositeData(CompositeData compositeData) { if (compositeData == null) { writeNull(); return; } int entryIndex = 0; write('{'); for (Object key : compositeData.getCompositeType().keySet()) { if (entryIndex != 0) { write(','); } writeString((String) key); write(':'); Object value = compositeData.get((String) key); writeObject(value); entryIndex++; } write('}'); }
public synchronized void capture() throws JMException, IOException { CompositeData lastGc = getLastGcInfo(); if (lastGc != null) { long id = along(lastGc.get("id")); long ts = along(lastGc.get("startTime")); // ignoring initial (t==0) pseudo event if (ts > 0 && id != lastReportedEvent) { processGcEvent(lastGc); } } }
/** * Add results from a value of type composite data. * This is a recursive function. */ private void add(String attributeName, ImmutableList.Builder<String> valuePath, CompositeData cds) { CompositeType t = cds.getCompositeType(); Set<String> keys = t.keySet(); for (String key : keys) { if (!query.getKeys().isEmpty() && !query.getKeys().contains(key)) { continue; } Object value = cds.get(key); add(attributeName, newValuePath(valuePath, key), value); } }
public static OperableTrigger newTrigger(CompositeData cData) throws ParseException { CronTriggerImpl result = new CronTriggerImpl(); result.setCronExpression((String) cData.get("cronExpression")); if(cData.containsKey("timeZone")) { result.setTimeZone(TimeZone.getTimeZone((String)cData.get("timeZone"))); } TriggerSupport.initializeTrigger(result, cData); return result; }
@Override public void handleNotification(Notification notification, Object handback) { try { GcTracker tracker = (GcTracker) handback; CompositeData cdata = (CompositeData) notification.getUserData(); CompositeData gcInfo = (CompositeData) cdata.get("gcInfo"); tracker.processGcEvent(gcInfo); } catch (JMException e) { // ignore } catch (IOException e) { // ignore } } }
private void addTabularMetrics(MetricFilter filter, List<Metric> metrics, TagList tags, String attrName, CompositeData obj) { Map<String, Object> values = new HashMap<>(); // tabular composite data has a value called key and one called value values.put(obj.get("key").toString(), obj.get("value")); for (Map.Entry<String, Object> e : values.entrySet()) { final Tag compositeTag = Tags.newTag(COMPOSITE_PATH_KEY, e.getKey()); final TagList newTags = getTagListWithAdditionalTag(tags, compositeTag); if (filter.matches(MonitorConfig.builder(attrName).withTags(newTags).build())) { addMetric(metrics, attrName, newTags, e.getValue()); } } }
@Override public Object fromNonNullOpenValue(Object openValue) throws InvalidObjectException { final TabularData table = (TabularData) openValue; final Collection<CompositeData> rows = (Collection<CompositeData>) table.values(); final Map<Object, Object> valueMap = sortedMap ? OpenTypeUtil.newSortedMap() : OpenTypeUtil.newMap(); for (CompositeData row : rows) { final Object key = keyConverter.fromOpenValue(row.get("key")); final Object value = valueConverter.fromOpenValue(row.get("value")); if (valueMap.put(key, value) != null) { final String msg = "Duplicate entry in TabularData: key=" + key; throw new InvalidObjectException(msg); } } return valueMap; }
/** * Allows for reconstruction via CompositeData. * * @param cd * composite data * @return Context class instance */ public static Context from(CompositeData cd) { Context instance = new Context(); if (cd.containsKey("context") && cd.containsKey("contextPath")) { Object context = cd.get("context"); Object contextPath = cd.get("contextPath"); if (context != null && contextPath != null) { instance = new Context((ApplicationContext) context, (String) contextPath); } } return instance; }
@Override Object fromCompositeData(CompositeData cd, String[] itemNames, OpenTypeConverter[] converters) throws InvalidObjectException { Object o; try { o = getTargetClass().newInstance(); for (int i = 0; i < itemNames.length; i++) { if (cd.containsKey(itemNames[i])) { Object openItem = cd.get(itemNames[i]); Object javaItem = converters[i].fromOpenValue(openItem); setters[i].invoke(o, javaItem); } } } catch (Exception e) { throw invalidObjectException(e); } return o; }
@Test public void testGetLastGCInfo() { List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); for(GarbageCollectorMXBean bean:gcBeans) { ObjectName on = bean.getObjectName(); Object value = JSONMetricUtil.getValueFromMBean(on, "LastGcInfo"); LOG.info("Collector Info: "+ value); if (value != null && value instanceof CompositeData) { CompositeData cds = (CompositeData)value; assertNotNull(cds.get("duration")); } } } }