Search in sources :

Example 1 with BindHandler

use of cn.taketoday.context.properties.bind.BindHandler in project today-infrastructure by TAKETODAY.

the class ConfigDataEnvironmentContributors method getBinder.

private Binder getBinder(@Nullable ConfigDataActivationContext activationContext, Predicate<ConfigDataEnvironmentContributor> filter, Set<BinderOption> options) {
    boolean failOnInactiveSource = options.contains(BinderOption.FAIL_ON_BIND_TO_INACTIVE_SOURCE);
    Iterable<ConfigurationPropertySource> sources = () -> getBinderSources(filter.and((contributor) -> failOnInactiveSource || contributor.isActive(activationContext)));
    var placeholdersResolver = new ConfigDataEnvironmentContributorPlaceholdersResolver(this.root, activationContext, null, failOnInactiveSource);
    BindHandler bindHandler = !failOnInactiveSource ? null : new InactiveSourceChecker(activationContext);
    return new Binder(sources, placeholdersResolver, null, null, bindHandler);
}
Also used : Arrays(java.util.Arrays) Iterator(java.util.Iterator) Bindable(cn.taketoday.context.properties.bind.Bindable) Predicate(java.util.function.Predicate) Set(java.util.Set) BindHandler(cn.taketoday.context.properties.bind.BindHandler) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) LoggerFactory(cn.taketoday.logging.LoggerFactory) List(java.util.List) ConfigurationPropertySource(cn.taketoday.context.properties.source.ConfigurationPropertySource) ObjectUtils(cn.taketoday.util.ObjectUtils) ConfigurationPropertyName(cn.taketoday.context.properties.source.ConfigurationPropertyName) Nullable(cn.taketoday.lang.Nullable) Map(java.util.Map) Logger(cn.taketoday.logging.Logger) Kind(cn.taketoday.framework.context.config.ConfigDataEnvironmentContributor.Kind) Binder(cn.taketoday.context.properties.bind.Binder) ConfigurableBootstrapContext(cn.taketoday.framework.ConfigurableBootstrapContext) Collections(java.util.Collections) ImportPhase(cn.taketoday.framework.context.config.ConfigDataEnvironmentContributor.ImportPhase) EnumSet(java.util.EnumSet) BindContext(cn.taketoday.context.properties.bind.BindContext) Binder(cn.taketoday.context.properties.bind.Binder) ConfigurationPropertySource(cn.taketoday.context.properties.source.ConfigurationPropertySource) BindHandler(cn.taketoday.context.properties.bind.BindHandler)

Example 2 with BindHandler

use of cn.taketoday.context.properties.bind.BindHandler in project today-framework by TAKETODAY.

the class ConfigurationPropertiesBinder method getBindHandler.

private <T> BindHandler getBindHandler(Bindable<T> target, ConfigurationProperties annotation) {
    List<Validator> validators = getValidators(target);
    BindHandler handler = getHandler();
    handler = new ConfigurationPropertiesBindHandler(handler);
    if (annotation.ignoreInvalidFields()) {
        handler = new IgnoreErrorsBindHandler(handler);
    }
    if (!annotation.ignoreUnknownFields()) {
        UnboundElementsSourceFilter filter = new UnboundElementsSourceFilter();
        handler = new NoUnboundElementsBindHandler(handler, filter);
    }
    if (!validators.isEmpty()) {
        handler = new ValidationBindHandler(handler, validators.toArray(new Validator[0]));
    }
    for (ConfigurationPropertiesBindHandlerAdvisor advisor : getBindHandlerAdvisors()) {
        handler = advisor.apply(handler);
    }
    return handler;
}
Also used : IgnoreErrorsBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler) NoUnboundElementsBindHandler(cn.taketoday.context.properties.bind.handler.NoUnboundElementsBindHandler) UnboundElementsSourceFilter(cn.taketoday.context.properties.source.UnboundElementsSourceFilter) ValidationBindHandler(cn.taketoday.context.properties.bind.validation.ValidationBindHandler) BoundPropertiesTrackingBindHandler(cn.taketoday.context.properties.bind.BoundPropertiesTrackingBindHandler) BindHandler(cn.taketoday.context.properties.bind.BindHandler) IgnoreErrorsBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler) IgnoreTopLevelConverterNotFoundBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler) AbstractBindHandler(cn.taketoday.context.properties.bind.AbstractBindHandler) NoUnboundElementsBindHandler(cn.taketoday.context.properties.bind.handler.NoUnboundElementsBindHandler) Validator(cn.taketoday.validation.Validator) ValidationBindHandler(cn.taketoday.context.properties.bind.validation.ValidationBindHandler)

Example 3 with BindHandler

use of cn.taketoday.context.properties.bind.BindHandler in project today-framework by TAKETODAY.

the class ConfigurationPropertiesBinder method bindOrCreate.

Object bindOrCreate(ConfigurationPropertiesBean propertiesBean) {
    Bindable<?> target = propertiesBean.asBindTarget();
    ConfigurationProperties annotation = propertiesBean.getAnnotation();
    BindHandler bindHandler = getBindHandler(target, annotation);
    return getBinder().bindOrCreate(annotation.prefix(), target, bindHandler);
}
Also used : ValidationBindHandler(cn.taketoday.context.properties.bind.validation.ValidationBindHandler) BoundPropertiesTrackingBindHandler(cn.taketoday.context.properties.bind.BoundPropertiesTrackingBindHandler) BindHandler(cn.taketoday.context.properties.bind.BindHandler) IgnoreErrorsBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler) IgnoreTopLevelConverterNotFoundBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler) AbstractBindHandler(cn.taketoday.context.properties.bind.AbstractBindHandler) NoUnboundElementsBindHandler(cn.taketoday.context.properties.bind.handler.NoUnboundElementsBindHandler)

Example 4 with BindHandler

use of cn.taketoday.context.properties.bind.BindHandler in project today-framework by TAKETODAY.

the class ConfigurationPropertiesBinder method bind.

BindResult<?> bind(ConfigurationPropertiesBean propertiesBean) {
    Bindable<?> target = propertiesBean.asBindTarget();
    ConfigurationProperties annotation = propertiesBean.getAnnotation();
    BindHandler bindHandler = getBindHandler(target, annotation);
    return getBinder().bind(annotation.prefix(), target, bindHandler);
}
Also used : ValidationBindHandler(cn.taketoday.context.properties.bind.validation.ValidationBindHandler) BoundPropertiesTrackingBindHandler(cn.taketoday.context.properties.bind.BoundPropertiesTrackingBindHandler) BindHandler(cn.taketoday.context.properties.bind.BindHandler) IgnoreErrorsBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler) IgnoreTopLevelConverterNotFoundBindHandler(cn.taketoday.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler) AbstractBindHandler(cn.taketoday.context.properties.bind.AbstractBindHandler) NoUnboundElementsBindHandler(cn.taketoday.context.properties.bind.handler.NoUnboundElementsBindHandler)

Example 5 with BindHandler

use of cn.taketoday.context.properties.bind.BindHandler in project today-framework by TAKETODAY.

the class ConfigDataEnvironmentPostProcessorIntegrationTests method runWhenImportingIncludesParentOrigin.

@Test
void runWhenImportingIncludesParentOrigin() {
    ConfigurableApplicationContext context = this.application.run("--context.config.location=classpath:application-import-with-placeholder.properties");
    Binder binder = Binder.get(context.getEnvironment());
    List<ConfigurationProperty> properties = new ArrayList<>();
    BindHandler bindHandler = new BindHandler() {

        @Override
        public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result) {
            properties.add(context.getConfigurationProperty());
            return result;
        }
    };
    binder.bind("my.value", Bindable.of(String.class), bindHandler);
    assertThat(properties).hasSize(1);
    Origin origin = properties.get(0).getOrigin();
    assertThat(origin.toString()).contains("application-import-with-placeholder-imported");
    assertThat(origin.getParent().toString()).contains("application-import-with-placeholder");
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) ConfigurationProperty(cn.taketoday.context.properties.source.ConfigurationProperty) BindContext(cn.taketoday.context.properties.bind.BindContext) Origin(cn.taketoday.origin.Origin) Binder(cn.taketoday.context.properties.bind.Binder) ConfigurationPropertyName(cn.taketoday.context.properties.source.ConfigurationPropertyName) ArrayList(java.util.ArrayList) Bindable(cn.taketoday.context.properties.bind.Bindable) BindHandler(cn.taketoday.context.properties.bind.BindHandler) Test(org.junit.jupiter.api.Test)

Aggregations

BindHandler (cn.taketoday.context.properties.bind.BindHandler)7 BindContext (cn.taketoday.context.properties.bind.BindContext)4 Bindable (cn.taketoday.context.properties.bind.Bindable)4 Binder (cn.taketoday.context.properties.bind.Binder)4 ConfigurationPropertyName (cn.taketoday.context.properties.source.ConfigurationPropertyName)4 ArrayList (java.util.ArrayList)4 AbstractBindHandler (cn.taketoday.context.properties.bind.AbstractBindHandler)3 BoundPropertiesTrackingBindHandler (cn.taketoday.context.properties.bind.BoundPropertiesTrackingBindHandler)3 IgnoreErrorsBindHandler (cn.taketoday.context.properties.bind.handler.IgnoreErrorsBindHandler)3 IgnoreTopLevelConverterNotFoundBindHandler (cn.taketoday.context.properties.bind.handler.IgnoreTopLevelConverterNotFoundBindHandler)3 NoUnboundElementsBindHandler (cn.taketoday.context.properties.bind.handler.NoUnboundElementsBindHandler)3 ValidationBindHandler (cn.taketoday.context.properties.bind.validation.ValidationBindHandler)3 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)2 ConfigurationProperty (cn.taketoday.context.properties.source.ConfigurationProperty)2 ConfigurationPropertySource (cn.taketoday.context.properties.source.ConfigurationPropertySource)2 ConfigurableBootstrapContext (cn.taketoday.framework.ConfigurableBootstrapContext)2 ImportPhase (cn.taketoday.framework.context.config.ConfigDataEnvironmentContributor.ImportPhase)2 Kind (cn.taketoday.framework.context.config.ConfigDataEnvironmentContributor.Kind)2 Nullable (cn.taketoday.lang.Nullable)2 Logger (cn.taketoday.logging.Logger)2