Search in sources :

Example 1 with BindException

use of org.springframework.boot.context.properties.bind.BindException in project spring-boot by spring-projects.

the class CacheCondition method getMatchOutcome.

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    String sourceClass = "";
    if (metadata instanceof ClassMetadata) {
        sourceClass = ((ClassMetadata) metadata).getClassName();
    }
    ConditionMessage.Builder message = ConditionMessage.forCondition("Cache", sourceClass);
    Environment environment = context.getEnvironment();
    try {
        BindResult<CacheType> specified = Binder.get(environment).bind("spring.cache.type", CacheType.class);
        if (!specified.isBound()) {
            return ConditionOutcome.match(message.because("automatic cache type"));
        }
        CacheType required = CacheConfigurations.getType(((AnnotationMetadata) metadata).getClassName());
        if (specified.get() == required) {
            return ConditionOutcome.match(message.because(specified.get() + " cache type"));
        }
    } catch (BindException ex) {
    }
    return ConditionOutcome.noMatch(message.because("unknown cache type"));
}
Also used : ClassMetadata(org.springframework.core.type.ClassMetadata) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage) Environment(org.springframework.core.env.Environment) BindException(org.springframework.boot.context.properties.bind.BindException)

Example 2 with BindException

use of org.springframework.boot.context.properties.bind.BindException in project spring-boot by spring-projects.

the class AbstractSessionCondition method getMatchOutcome.

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    ConditionMessage.Builder message = ConditionMessage.forCondition("Session Condition");
    Environment environment = context.getEnvironment();
    StoreType required = SessionStoreMappings.getType(this.webApplicationType, ((AnnotationMetadata) metadata).getClassName());
    if (!environment.containsProperty("spring.session.store-type")) {
        return ConditionOutcome.match(message.didNotFind("property", "properties").items(ConditionMessage.Style.QUOTE, "spring.session.store-type"));
    }
    try {
        Binder binder = Binder.get(environment);
        return binder.bind("spring.session.store-type", StoreType.class).map((t) -> new ConditionOutcome(t == required, message.found("spring.session.store-type property").items(t))).orElseGet(() -> ConditionOutcome.noMatch(message.didNotFind("spring.session.store-type property").atAll()));
    } catch (BindException ex) {
        return ConditionOutcome.noMatch(message.found("invalid spring.session.store-type property").atAll());
    }
}
Also used : ConditionOutcome(org.springframework.boot.autoconfigure.condition.ConditionOutcome) ConditionContext(org.springframework.context.annotation.ConditionContext) BindException(org.springframework.boot.context.properties.bind.BindException) Environment(org.springframework.core.env.Environment) WebApplicationType(org.springframework.boot.WebApplicationType) AnnotationMetadata(org.springframework.core.type.AnnotationMetadata) Binder(org.springframework.boot.context.properties.bind.Binder) AnnotatedTypeMetadata(org.springframework.core.type.AnnotatedTypeMetadata) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage) SpringBootCondition(org.springframework.boot.autoconfigure.condition.SpringBootCondition) Binder(org.springframework.boot.context.properties.bind.Binder) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage) Environment(org.springframework.core.env.Environment) BindException(org.springframework.boot.context.properties.bind.BindException) ConditionOutcome(org.springframework.boot.autoconfigure.condition.ConditionOutcome)

Example 3 with BindException

use of org.springframework.boot.context.properties.bind.BindException in project spring-boot by spring-projects.

the class ConfigDataEnvironment method withProfiles.

private ConfigDataActivationContext withProfiles(ConfigDataEnvironmentContributors contributors, ConfigDataActivationContext activationContext) {
    this.logger.trace("Deducing profiles from current config data environment contributors");
    Binder binder = contributors.getBinder(activationContext, (contributor) -> !contributor.hasConfigDataOption(ConfigData.Option.IGNORE_PROFILES), BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE);
    try {
        Set<String> additionalProfiles = new LinkedHashSet<>(this.additionalProfiles);
        additionalProfiles.addAll(getIncludedProfiles(contributors, activationContext));
        Profiles profiles = new Profiles(this.environment, binder, additionalProfiles);
        return activationContext.withProfiles(profiles);
    } catch (BindException ex) {
        if (ex.getCause() instanceof InactiveConfigDataAccessException) {
            throw (InactiveConfigDataAccessException) ex.getCause();
        }
        throw ex;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Binder(org.springframework.boot.context.properties.bind.Binder) BindException(org.springframework.boot.context.properties.bind.BindException)

Example 4 with BindException

use of org.springframework.boot.context.properties.bind.BindException in project cas by apereo.

the class CasConfigurationPropertiesValidator method validateConfiguration.

private void validateConfiguration(final Class clazz, final List<String> validationResults) {
    val beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext.getBeanFactory(), clazz);
    beans.values().forEach(bean -> {
        val configBean = ConfigurationPropertiesBean.get(this.applicationContext, bean, UUID.randomUUID().toString());
        val target = configBean.asBindTarget();
        val annotation = configBean.getAnnotation();
        val handler = new NoUnboundElementsBindHandler(new IgnoreTopLevelConverterNotFoundBindHandler(), new UnboundElementsSourceFilter());
        val configBinder = new Binder(ConfigurationPropertySources.from(applicationContext.getEnvironment().getPropertySources()), new PropertySourcesPlaceholdersResolver(applicationContext.getEnvironment().getPropertySources()), applicationContext.getEnvironment().getConversionService(), null, null, null);
        try {
            configBinder.bind(annotation.prefix(), target, handler);
        } catch (final BindException e) {
            var message = "\n".concat(e.getMessage()).concat("\n");
            if (e.getCause() != null) {
                val cause = (UnboundConfigurationPropertiesException) e.getCause();
                if (cause != null) {
                    message += cause.getUnboundProperties().stream().map(property -> String.format("%n\t%s = %s (Origin: %s)", property.getName(), property.getValue(), property.getOrigin())).collect(Collectors.joining("\n"));
                }
            }
            validationResults.add(message);
        }
    });
}
Also used : lombok.val(lombok.val) NoUnboundElementsBindHandler(org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler) IgnoreTopLevelConverterNotFoundBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler) UnboundConfigurationPropertiesException(org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) lombok.val(lombok.val) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) UUID(java.util.UUID) NoUnboundElementsBindHandler(org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler) Collectors(java.util.stream.Collectors) ConfigurationPropertiesBean(org.springframework.boot.context.properties.ConfigurationPropertiesBean) ArrayList(java.util.ArrayList) PropertySourcesPlaceholdersResolver(org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver) LoggingUtils(org.apereo.cas.util.LoggingUtils) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) CasVersion(org.apereo.cas.util.CasVersion) BindException(org.springframework.boot.context.properties.bind.BindException) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) UnboundElementsSourceFilter(org.springframework.boot.context.properties.source.UnboundElementsSourceFilter) Binder(org.springframework.boot.context.properties.bind.Binder) ConfigurationPropertySources(org.springframework.boot.context.properties.source.ConfigurationPropertySources) Binder(org.springframework.boot.context.properties.bind.Binder) UnboundElementsSourceFilter(org.springframework.boot.context.properties.source.UnboundElementsSourceFilter) BindException(org.springframework.boot.context.properties.bind.BindException) PropertySourcesPlaceholdersResolver(org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver) IgnoreTopLevelConverterNotFoundBindHandler(org.springframework.boot.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler)

Example 5 with BindException

use of org.springframework.boot.context.properties.bind.BindException in project spring-boot by spring-projects.

the class BindValidationFailureAnalyzer method getBindValidationExceptionDetails.

private ExceptionDetails getBindValidationExceptionDetails(Throwable rootFailure) {
    BindValidationException validationException = findCause(rootFailure, BindValidationException.class);
    if (validationException != null) {
        BindException target = findCause(rootFailure, BindException.class);
        List<ObjectError> errors = validationException.getValidationErrors().getAllErrors();
        return new ExceptionDetails(errors, target, validationException);
    }
    org.springframework.validation.BindException bindException = findCause(rootFailure, org.springframework.validation.BindException.class);
    if (bindException != null) {
        List<ObjectError> errors = bindException.getAllErrors();
        return new ExceptionDetails(errors, bindException.getTarget(), bindException);
    }
    return null;
}
Also used : BindValidationException(org.springframework.boot.context.properties.bind.validation.BindValidationException) ObjectError(org.springframework.validation.ObjectError) BindException(org.springframework.boot.context.properties.bind.BindException)

Aggregations

BindException (org.springframework.boot.context.properties.bind.BindException)5 Binder (org.springframework.boot.context.properties.bind.Binder)3 ConditionMessage (org.springframework.boot.autoconfigure.condition.ConditionMessage)2 Environment (org.springframework.core.env.Environment)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 lombok.val (lombok.val)1 CasVersion (org.apereo.cas.util.CasVersion)1 LoggingUtils (org.apereo.cas.util.LoggingUtils)1 BeanFactoryUtils (org.springframework.beans.factory.BeanFactoryUtils)1 WebApplicationType (org.springframework.boot.WebApplicationType)1 ConditionOutcome (org.springframework.boot.autoconfigure.condition.ConditionOutcome)1 SpringBootCondition (org.springframework.boot.autoconfigure.condition.SpringBootCondition)1 ConfigurationPropertiesBean (org.springframework.boot.context.properties.ConfigurationPropertiesBean)1 PropertySourcesPlaceholdersResolver (org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver)1