Search in sources :

Example 1 with ValueHint

use of org.springframework.boot.configurationmetadata.ValueHint in project cas by apereo.

the class ConfigurationMetadataGenerator method processHints.

private static Set<ConfigurationMetadataHint> processHints(final Collection<ConfigurationMetadataProperty> props, final Collection<ConfigurationMetadataProperty> groups) {
    var hints = new LinkedHashSet<ConfigurationMetadataHint>(0);
    val allValidProps = props.stream().filter(p -> p.getDeprecation() == null || !Deprecation.Level.ERROR.equals(p.getDeprecation().getLevel())).collect(Collectors.toList());
    for (val entry : allValidProps) {
        try {
            val propName = StringUtils.substringAfterLast(entry.getName(), ".");
            val groupName = StringUtils.substringBeforeLast(entry.getName(), ".");
            val grp = groups.stream().filter(g -> g.getName().equalsIgnoreCase(groupName)).findFirst().orElseThrow(() -> new IllegalArgumentException("Cant locate group " + groupName));
            val matcher = PATTERN_GENERICS.matcher(grp.getType());
            val className = matcher.find() ? matcher.group(1) : grp.getType();
            val clazz = ClassUtils.getClass(className);
            val hint = new ConfigurationMetadataHint();
            hint.setName(entry.getName());
            val annotation = Arrays.stream(clazz.getAnnotations()).filter(a -> a.annotationType().equals(RequiresModule.class)).findFirst().map(RequiresModule.class::cast).orElseThrow(() -> new RuntimeException(clazz.getCanonicalName() + " is missing @RequiresModule"));
            val valueHint = new ValueHint();
            valueHint.setValue(toJson(Map.of("module", annotation.name(), "automated", annotation.automated())));
            valueHint.setDescription(RequiresModule.class.getName());
            hint.getValues().add(valueHint);
            val grpHint = new ValueHint();
            grpHint.setValue(toJson(Map.of("owner", clazz.getCanonicalName())));
            grpHint.setDescription(PropertyOwner.class.getName());
            hint.getValues().add(grpHint);
            val names = RelaxedPropertyNames.forCamelCase(propName);
            names.getValues().forEach(Unchecked.consumer(name -> {
                val f = ReflectionUtils.findField(clazz, name);
                if (f != null && f.isAnnotationPresent(RequiredProperty.class)) {
                    val propertyHint = new ValueHint();
                    propertyHint.setValue(toJson(Map.of("owner", clazz.getName())));
                    propertyHint.setDescription(RequiredProperty.class.getName());
                    hint.getValues().add(propertyHint);
                }
                if (f != null && f.isAnnotationPresent(DurationCapable.class)) {
                    val propertyHint = new ValueHint();
                    propertyHint.setDescription(DurationCapable.class.getName());
                    propertyHint.setValue(toJson(List.of(DurationCapable.class.getName())));
                    hint.getValues().add(propertyHint);
                }
                if (f != null && f.isAnnotationPresent(ExpressionLanguageCapable.class)) {
                    val propertyHint = new ValueHint();
                    propertyHint.setDescription(ExpressionLanguageCapable.class.getName());
                    propertyHint.setValue(toJson(List.of(ExpressionLanguageCapable.class.getName())));
                    hint.getValues().add(propertyHint);
                }
            }));
            if (!hint.getValues().isEmpty()) {
                hints.add(hint);
            }
        } catch (final Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    return hints;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) lombok.val(lombok.val) RequiresModule(org.apereo.cas.configuration.support.RequiresModule) NestedConfigurationProperty(org.springframework.boot.context.properties.NestedConfigurationProperty) Arrays(java.util.Arrays) MinimalPrettyPrinter(com.fasterxml.jackson.core.util.MinimalPrettyPrinter) RequiredArgsConstructor(lombok.RequiredArgsConstructor) DurationCapable(org.apereo.cas.configuration.support.DurationCapable) StringUtils(org.apache.commons.lang3.StringUtils) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) LiteralStringValueExpr(com.github.javaparser.ast.expr.LiteralStringValueExpr) HashSet(java.util.HashSet) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) ClassUtils(org.apache.commons.lang3.ClassUtils) Map(java.util.Map) TypeReference(com.fasterxml.jackson.core.type.TypeReference) LinkedHashSet(java.util.LinkedHashSet) ValueHint(org.springframework.boot.configurationmetadata.ValueHint) Unchecked(org.jooq.lambda.Unchecked) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Deprecation(org.springframework.boot.configurationmetadata.Deprecation) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) Set(java.util.Set) RequiredProperty(org.apereo.cas.configuration.support.RequiredProperty) ConfigurationMetadataProperty(org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) ExpressionLanguageCapable(org.apereo.cas.configuration.support.ExpressionLanguageCapable) StaticJavaParser(com.github.javaparser.StaticJavaParser) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ReflectionUtils(org.springframework.util.ReflectionUtils) PropertyOwner(org.apereo.cas.configuration.support.PropertyOwner) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) Pattern(java.util.regex.Pattern) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) RelaxedPropertyNames(org.apereo.cas.configuration.support.RelaxedPropertyNames) ValueHint(org.springframework.boot.configurationmetadata.ValueHint) RequiresModule(org.apereo.cas.configuration.support.RequiresModule) PropertyOwner(org.apereo.cas.configuration.support.PropertyOwner)

Example 2 with ValueHint

use of org.springframework.boot.configurationmetadata.ValueHint in project cas by apereo.

the class CasConfigurationMetadataCatalog method doesPropertyBelongToModule.

private static boolean doesPropertyBelongToModule(final ConfigurationMetadataProperty property, final ConfigurationMetadataCatalogQuery query) {
    if (query.getModules().isEmpty()) {
        return true;
    }
    val valueHints = property.getHints().getValueHints();
    return valueHints.stream().filter(hint -> StringUtils.isNotBlank(hint.getDescription())).filter(hint -> hint.getDescription().equals(RequiresModule.class.getName())).anyMatch(hint -> {
        val valueHint = ValueHint.class.cast(hint);
        val results = reasonJsonValueAsMap(valueHint.getValue().toString());
        val module = results.get("module").toString();
        return query.getModules().contains(module);
    });
}
Also used : lombok.val(lombok.val) RequiresModule(org.apereo.cas.configuration.support.RequiresModule) SneakyThrows(lombok.SneakyThrows) RequiredArgsConstructor(lombok.RequiredArgsConstructor) DurationCapable(org.apereo.cas.configuration.support.DurationCapable) StringUtils(org.apache.commons.lang3.StringUtils) TreeSet(java.util.TreeSet) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Map(java.util.Map) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) ConfigurationMetadataRepository(org.springframework.boot.configurationmetadata.ConfigurationMetadataRepository) ValueHint(org.springframework.boot.configurationmetadata.ValueHint) Unchecked(org.jooq.lambda.Unchecked) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) lombok.val(lombok.val) RequiredProperty(org.apereo.cas.configuration.support.RequiredProperty) ConfigurationMetadataProperty(org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty) Collectors(java.util.stream.Collectors) File(java.io.File) Objects(java.util.Objects) ExpressionLanguageCapable(org.apereo.cas.configuration.support.ExpressionLanguageCapable) ReflectionUtils(org.springframework.util.ReflectionUtils) JacksonObjectMapperFactory(org.apereo.cas.util.serialization.JacksonObjectMapperFactory) PropertyOwner(org.apereo.cas.configuration.support.PropertyOwner) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Comparator(java.util.Comparator) RequiresModule(org.apereo.cas.configuration.support.RequiresModule)

Aggregations

JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 File (java.io.File)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 RequiredArgsConstructor (lombok.RequiredArgsConstructor)2 lombok.val (lombok.val)2 StringUtils (org.apache.commons.lang3.StringUtils)2 DurationCapable (org.apereo.cas.configuration.support.DurationCapable)2 ExpressionLanguageCapable (org.apereo.cas.configuration.support.ExpressionLanguageCapable)2 PropertyOwner (org.apereo.cas.configuration.support.PropertyOwner)2 RequiredProperty (org.apereo.cas.configuration.support.RequiredProperty)2 RequiresModule (org.apereo.cas.configuration.support.RequiresModule)2 Unchecked (org.jooq.lambda.Unchecked)2 ConfigurationMetadataProperty (org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty)2 ValueHint (org.springframework.boot.configurationmetadata.ValueHint)2 ReflectionUtils (org.springframework.util.ReflectionUtils)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 MinimalPrettyPrinter (com.fasterxml.jackson.core.util.MinimalPrettyPrinter)1