All Downloads are FREE. Search and download functionalities are using the official Maven repository.

spring.turbo.bean.condition.ConditionalOnResourceOptionCondition Maven / Gradle / Ivy

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *    ____             _            _____           _
 *   / ___| _ __  _ __(_)_ __   __ |_   _|   _ _ __| |__   ___
 *   \___ \| '_ \| '__| | '_ \ / _` || || | | | '__| '_ \ / _ \
 *    ___) | |_) | |  | | | | | (_| || || |_| | |  | |_) | (_) |
 *   |____/| .__/|_|  |_|_| |_|\__, ||_| \__,_|_|  |_.__/ \___/
 *         |_|                 |___/   https://github.com/yingzhuo/spring-turbo
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package spring.turbo.bean.condition;

import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotatedTypeMetadata;
import spring.turbo.io.RichResource;
import spring.turbo.util.InstanceUtils;

import java.util.function.Predicate;

/**
 * @author 应卓
 * @since 2.0.1
 */
@SuppressWarnings("unchecked")
public final class ConditionalOnResourceOptionCondition extends SpringBootCondition {

    @Override
    public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
        var attributes =
                AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(ConditionalOnResourceOption.class.getName()));

        if (attributes == null) {
            return ConditionOutcome.noMatch("resources absent");
        }

        var resources = attributes.getStringArray("value");
        var discriminatorType = (Class>) attributes.getClass("discriminator");

        if (resources.length == 0) {
            return ConditionOutcome.noMatch("resources absent");
        }

        var match = RichResource.builder()
                .blankSafeAddLocations(resources)
                .discriminator(InstanceUtils.newInstanceElseThrow(discriminatorType))
                .build()
                .isPresent();

        if (match) {
            return ConditionOutcome.match();
        } else {
            return ConditionOutcome.noMatch("resources absent");
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy