Search in sources :

Example 1 with UnboundConfigurationPropertiesException

use of org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException 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)

Aggregations

ArrayList (java.util.ArrayList)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 ConfigurationPropertiesBean (org.springframework.boot.context.properties.ConfigurationPropertiesBean)1 BindException (org.springframework.boot.context.properties.bind.BindException)1 Binder (org.springframework.boot.context.properties.bind.Binder)1 PropertySourcesPlaceholdersResolver (org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver)1 UnboundConfigurationPropertiesException (org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException)1 IgnoreTopLevelConverterNotFoundBindHandler (org.springframework.boot.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler)1 NoUnboundElementsBindHandler (org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler)1 ConfigurationPropertySources (org.springframework.boot.context.properties.source.ConfigurationPropertySources)1 UnboundElementsSourceFilter (org.springframework.boot.context.properties.source.UnboundElementsSourceFilter)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1