Search in sources :

Example 1 with RequiresModule

use of org.apereo.cas.configuration.support.RequiresModule 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)

Aggregations

JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 MinimalPrettyPrinter (com.fasterxml.jackson.core.util.MinimalPrettyPrinter)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 MapperFeature (com.fasterxml.jackson.databind.MapperFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 StaticJavaParser (com.github.javaparser.StaticJavaParser)1 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)1 BooleanLiteralExpr (com.github.javaparser.ast.expr.BooleanLiteralExpr)1 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)1 LiteralStringValueExpr (com.github.javaparser.ast.expr.LiteralStringValueExpr)1 File (java.io.File)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1