private ParameterMeta translate(final ParameterMeta config, final int replacedPrefixLen, final String newPrefix) { return new ParameterMeta(config.getSource(), config.getJavaType(), config.getType(), newPrefix + config.getPath().substring(replacedPrefixLen), config.getPath().length() == replacedPrefixLen ? newPrefix : config.getName(), config.getI18nPackages(), config .getNestedParameters() .stream() .map(it -> translate(it, replacedPrefixLen, newPrefix)) .collect(toList()), config.getProposals(), config.getMetadata(), config.isLogMissingResourceBundle()); } }
private ParameterBundle findBundle(final ParameterMeta p) { return p.findBundle(Thread.currentThread().getContextClassLoader(), locale); }
private List<ParameterMeta> translate(final List<ParameterMeta> metas, final String name) { if (metas == null) { return null; } return metas .stream() .filter(it -> it.getName().equals(name)) .flatMap(it -> it.getNestedParameters().stream()) .collect(toList()); }
private Stream<SimplePropertyDefinition> buildProperties(final List<ParameterMeta> meta, final ClassLoader loader, final Locale locale, final DefaultValueInspector.Instance rootInstance, final ParameterMeta parent) { return meta.stream().flatMap(p -> { final String path = sanitizePropertyName(p.getPath()); final String name = sanitizePropertyName(p.getName()); final String type = p.getType().name(); final boolean isEnum = p.getType() == ParameterMeta.Type.ENUM; PropertyValidation validation = propertyValidationService.map(p.getMetadata()); if (isEnum) { if (validation == null) { validation = new PropertyValidation(); validation.setEnumValues(p.getProposals()); final Map<String, String> sanitizedMetadata = ofNullable(p.getMetadata()) .map(m -> m .entrySet() .createDemoInstance( ofNullable(rootInstance).map(DefaultValueInspector.Instance::getValue).orElse(null), p); final ParameterBundle bundle = p.findBundle(loader, locale); final ParameterBundle parentBundle = parent == null ? null : parent.findBundle(loader, locale); return Stream .concat(Stream .of(new SimplePropertyDefinition(path, name, bundle.displayName(parentBundle).orElse(p.getName()), type, toDefault(instance, p), validation, rewriteMetadataForLocale(metadata, parentBundle, bundle), bundle.placeholder(parentBundle).orElse(p.getName()), !isEnum ? null : p
private Class<?> toJavaType(final ParameterMeta p) { if (p.getType().equals(OBJECT) || p.getType().equals(ENUM)) { if (Class.class.isInstance(p.getJavaType())) { return Class.class.cast(p.getJavaType()); } throw new IllegalArgumentException("Unsupported type for parameter " + p.getPath() + " (from " + p.getSource().declaringClass() + "), ensure it is a Class<?>"); } if (p.getType().equals(ARRAY) && ParameterizedType.class.isInstance(p.getJavaType())) { final ParameterizedType parameterizedType = ParameterizedType.class.cast(p.getJavaType()); final Type[] arguments = parameterizedType.getActualTypeArguments(); if (arguments.length == 1 && Class.class.isInstance(arguments[0])) { return Class.class.cast(arguments[0]); } throw new IllegalArgumentException("Unsupported type for parameter " + p.getPath() + " (from " + p.getSource().declaringClass() + "), " + "ensure it is a ParameterizedType with one argument"); } throw new IllegalStateException("Parameter '" + p.getName() + "' is not an object."); }
private void addProcessorsBuiltInParameters(final Class<?> type, final List<ParameterMeta> parameterMetas) { final ParameterMeta root = parameterMetas.stream().filter(p -> p.getName().equals(p.getPath())).findFirst().orElseGet(() -> { final ParameterMeta umbrella = new ParameterMeta(new ParameterMeta.Source() { final String layoutType = paramBuilder.getLayoutType(); if (layoutType == null) { root.getMetadata().put("tcomp::ui::gridlayout::Advanced::value", maxBatchSize.getName()); root .getMetadata() .put("tcomp::ui::gridlayout::Main::value", root .getNestedParameters() .stream() .map(ParameterMeta::getName) .collect(joining("|"))); } else if (!root.getMetadata().containsKey(layoutType)) { root .getMetadata() .put(layoutType, layoutType.contains("gridlayout") ? maxBatchSize.getName() : "true"); } else if (layoutType.contains("gridlayout")) { final String oldLayout = root.getMetadata().get(layoutType); root.getMetadata().put(layoutType, maxBatchSize.getName() + "|" + oldLayout); root.getNestedParameters().add(maxBatchSize);
final Object value = getValue(instance, param.getName()); if (value == null) { return Collections.<String, String> emptyMap(); switch (param.getType()) { case OBJECT: return map(param.getNestedParameters(), value, indexes); case ARRAY: final Collection<Object> values = Collection.class.isInstance(value) ? Collection.class.cast(value) indexes.put(arrayIndex, valuesIndex.getAndIncrement()); final Map<String, String> res = param .getNestedParameters() .stream() .filter(ConfigurationMapper::isPrimitive) .collect(toMap(p -> evaluateIndexes(p.getPath(), indexes), p -> getValue(item, p.getName()).toString())); param.getNestedParameters().stream().filter(p -> !isPrimitive(p)).collect(toList()), item, indexes)); return res; return singletonMap(evaluateIndexes(param.getPath(), indexes), value.toString());
private <C, T, A> A create(final Class<A> api, final Class<T> componentType, final C configuration) { final ComponentFamilyMeta.BaseMeta<? extends Lifecycle> meta = findMeta(componentType); return api .cast(meta .getInstantiator() .apply(configuration == null || meta.getParameterMetas().isEmpty() ? emptyMap() : configurationByExample(configuration, meta .getParameterMetas() .stream() .filter(p -> p.getName().equals(p.getPath())) .findFirst() .map(p -> p.getName() + '.') .orElseThrow(() -> new IllegalArgumentException( "Didn't find any option and therefore " + "can't convert the configuration instance to a configuration"))))); }
private String toAsciidoctor(final ParameterMeta p, final DefaultValueInspector.Instance instance, final ParameterBundle parent) { final ParameterBundle bundle = findBundle(p); return "|" + bundle.displayName(parent).orElse(p.getName()) + '|' + bundle.documentation(parent).orElseGet(() -> findDocumentation(p)) + '|' + ofNullable(findDefault(p, instance)).orElse("-") + '|' + renderConditions(p.getPath(), p.getMetadata()) + '|' + p.getPath(); }
.getMetadata() .entrySet() .stream() .getMetadata() .entrySet() .stream() .filter(fieldInLayout -> config .getValue() .getNestedParameters() .stream() .map(ParameterMeta::getName) .getNestedParameters() .stream() .filter(field -> !fieldsInGridLayout.contains(field.getName())) .map(field -> "Field '" + field.getName() + "' in " + config.getKey() + " is not declared in any layout.") .forEach(this.log::error); .filter(fieldInLayout -> config .getValue() .getNestedParameters() .stream() .map(ParameterMeta::getName) .getNestedParameters() .stream()
private void onProperty(final String contextualPrefix, final Collection<ParameterMeta> definitions, final Map<String, String> config, final JsonObjectBuilder json, final ParameterMeta definition) { final String name = definition.getName(); final String newPath = contextualPrefix + (contextualPrefix.isEmpty() ? "" : ".") + name; switch (definition.getType()) { case OBJECT: { onObject(definitions, definition, config, json, name, newPath); onArray(definition.getNestedParameters(), definition, config, newPath, json, name); break;
private Stream<ParameterMeta> getNestedConfigType(final ParameterMeta parameterMeta) { return concat( (parameterMeta.getJavaType() instanceof Class && parameterMeta .getMetadata() .keySet() .stream() .anyMatch(k -> k.startsWith("tcomp::configurationtype::"))) ? Stream.of(parameterMeta) : Stream.empty(), ofNullable(parameterMeta.getNestedParameters()) .map(Collection::stream) .orElseGet(Stream::empty) .flatMap(this::getNestedConfigType)); }
private boolean isArrayOfObject(final ParameterMeta param) { return ARRAY.equals(param.getType()) && param.getNestedParameters() != null && param .getNestedParameters() .stream() .anyMatch(p -> OBJECT.equals(p.getType()) || ENUM.equals(p.getType()) || isArrayOfObject(p)); }
public ConditionGroup build(final ParameterMeta param) { final boolean and = "AND".equalsIgnoreCase(param.getMetadata().getOrDefault("tcomp::condition::ifs::operator", "AND")); return new ConditionGroup(param .getMetadata() .entrySet() .stream() .filter(meta -> meta.getKey().startsWith("tcomp::condition::if::target")) .map(meta -> { final String[] split = meta.getKey().split("::"); final String index = split.length == 5 ? "::" + split[split.length - 1] : ""; final String valueKey = "tcomp::condition::if::value" + index; final String negateKey = "tcomp::condition::if::negate" + index; final String evaluationStrategyKey = "tcomp::condition::if::evaluationStrategy" + index; final String absoluteTargetPath = pathResolver.resolveProperty(param.getPath(), meta.getValue()); return new Condition(toPointer(absoluteTargetPath), Boolean.parseBoolean(param.getMetadata().getOrDefault(negateKey, "false")), param.getMetadata().getOrDefault(evaluationStrategyKey, "DEFAULT").toUpperCase(ROOT), param.getMetadata().getOrDefault(valueKey, "true").split(",")); }) .collect(toList()), and ? stream -> stream.allMatch(i -> i) : stream -> stream.anyMatch(i -> i)); }
private Config createConfig(final String plugin, final ComponentManager.AllServices services, final ParameterMeta config, final String familyName, final String familyIcon, final MigrationHandlerFactory migrationHandlerFactory) { final Config c = new Config(); c.setIcon(familyIcon); c.setKey(getKey(familyName, config.getMetadata())); c.setMeta(translate(config, config.getPath().length(), "configuration")); c .setId(IdGenerator .get(plugin, c.getKey().getFamily(), c.getKey().getConfigType(), c.getKey().getConfigName())); if (Class.class.isInstance(config.getJavaType())) { final Class<?> clazz = Class.class.cast(config.getJavaType()); final Version version = clazz.getAnnotation(Version.class); if (version != null) { c.setVersion(version.value()); if (version.migrationHandler() != MigrationHandler.class) { c .setMigrationHandler(migrationHandlerFactory .findMigrationHandler(singletonList(c.getMeta()), clazz, services)); } } else { c.setVersion(-1); } } if (c.getMigrationHandler() == null) { c.setMigrationHandler((v, d) -> d); } return c; }
private Stream<ParameterMeta> flatten(final ParameterMeta meta) { if (meta.getNestedParameters() == null || meta.getNestedParameters().isEmpty()) { return Stream.of(meta); } return Stream.concat(meta.getNestedParameters().stream().flatMap(this::flatten), Stream.of(meta)); }
public ParameterMeta newBulkParameter() { return new ParameterMeta(new ParameterMeta.Source() { @Override public String name() { return name; } @Override public Class<?> declaringClass() { return MaxBatchSizeParamBuilder.class; } }, Integer.class, ParameterMeta.Type.NUMBER, root.getPath() + "." + name, name, concat(Stream.of(MaxBatchSizeParamBuilder.class.getPackage().getName()), Stream.of(ofNullable(root.getI18nPackages()).orElse(new String[0]))).toArray(String[]::new), emptyList(), emptyList(), new HashMap<String, String>() { { put("tcomp::ui::defaultvalue::value", String.valueOf(defaultValue)); put("tcomp::validation::min", "1"); } }, true); }
private String findDocumentation(final ParameterMeta p) { final String inline = p.getMetadata().get("documentation"); if (inline != null) { if (inline.startsWith("resource:")) { final InputStream stream = Thread .currentThread() .getContextClassLoader() .getResourceAsStream(inline.substring("resource:".length())); if (stream != null) { try (final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) { return reader.lines().collect(joining("\n")); } catch (final IOException e) { throw new IllegalArgumentException("Bad resource: '" + inline + "'", e); } } else { throw new IllegalArgumentException("No resource: '" + inline + "'"); } } return inline; } return p.getName() + " configuration"; }
.map(p -> createConfig(familyMeta.getPlugin(), services, p, familyMeta.getName(), familyMeta.getIcon(), migrationHandlerFactory)) .collect(toMap(c -> c.getMeta().getJavaType(), identity(), (config1, config2) -> config1, LinkedHashMap::new)) .values() .getConfigs() .stream() .filter(c -> toParamStream(item.getMeta().getNestedParameters()) .anyMatch(p -> p.getJavaType() == c.getMeta().getJavaType())) .findFirst() .map(Config::getChildConfigs) if (configs .stream() .noneMatch(c -> c.getMeta().getJavaType() == item.getMeta().getJavaType())) { configs.add(item);