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"));
}
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());
}
}
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;
}
}
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);
}
});
}
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;
}
Aggregations