private static ConfigurationMetadataSource getSource(final RawConfigurationMetadata metadata, final ConfigurationMetadataItem item) { if (item.getSourceType() == null) { return null; } val idx = item.getId().lastIndexOf('.'); val name = idx > 0 ? item.getId().substring(0, idx) : StringUtils.EMPTY; return metadata.getSources().stream() .filter(source -> source.getType().equals(item.getSourceType()) && name.equals(source.getGroupId())) .findFirst() .orElse(null); }
private ConfigurationMetadataSource getSource(RawConfigurationMetadata metadata, ConfigurationMetadataItem item) { if (item.getSourceType() != null) { return metadata.getSource(item.getSourceType()); } return null; }
RawConfigurationMetadata(List<ConfigurationMetadataSource> sources, List<ConfigurationMetadataItem> items, List<ConfigurationMetadataHint> hints) { this.sources = new ArrayList<>(sources); this.items = new ArrayList<>(items); this.hints = new ArrayList<>(hints); for (ConfigurationMetadataItem item : this.items) { resolveName(item); } }
private static SimpleConfigurationMetadataRepository create(final RawConfigurationMetadata metadata) { val repository = new SimpleConfigurationMetadataRepository(); repository.add(metadata.getSources()); for (val item : metadata.getItems()) { val source = getSource(metadata, item); repository.add(item, source); } val allProperties = repository.getAllProperties(); for (val hint : metadata.getHints()) { var property = allProperties.get(hint.getId()); if (property != null) { addValueHints(property, hint); } else { val id = hint.resolveId(); property = allProperties.get(id); if (property != null) { if (hint.isMapKeyHints()) { addMapHints(property, hint); } else { addValueHints(property, hint); } } } } return repository; }
/** * Resolve the name of an item against this instance. * @param item the item to resolve * @see ConfigurationMetadataProperty#setName(String) */ private void resolveName(ConfigurationMetadataItem item) { item.setName(item.getId()); // fallback if (item.getSourceType() == null) { return; } ConfigurationMetadataSource source = getSource(item.getSourceType()); if (source != null) { String groupId = source.getGroupId(); String dottedPrefix = groupId + "."; String id = item.getId(); if (hasLength(groupId) && id.startsWith(dottedPrefix)) { String name = id.substring(dottedPrefix.length()); item.setName(name); } } }
public RawConfigurationMetadata read(InputStream in, Charset charset) throws IOException { try { JSONObject json = readJson(in, charset); List<ConfigurationMetadataSource> groups = parseAllSources(json); List<ConfigurationMetadataItem> items = parseAllItems(json); List<ConfigurationMetadataHint> hints = parseAllHints(json); return new RawConfigurationMetadata(groups, items, hints); } catch (Exception ex) { if (ex instanceof IOException) { throw (IOException) ex; } if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } throw new IllegalStateException(ex); } }
private SimpleConfigurationMetadataRepository create( RawConfigurationMetadata metadata) { SimpleConfigurationMetadataRepository repository = new SimpleConfigurationMetadataRepository(); repository.add(metadata.getSources()); for (ConfigurationMetadataItem item : metadata.getItems()) { ConfigurationMetadataSource source = getSource(metadata, item); repository.add(item, source); } Map<String, ConfigurationMetadataProperty> allProperties = repository .getAllProperties(); for (ConfigurationMetadataHint hint : metadata.getHints()) { ConfigurationMetadataProperty property = allProperties.get(hint.getId()); if (property != null) { addValueHints(property, hint); } else { String id = hint.resolveId(); property = allProperties.get(id); if (property != null) { if (hint.isMapKeyHints()) { addMapHints(property, hint); } else { addValueHints(property, hint); } } } } return repository; }