Search in sources :

Example 16 with Origin

use of cn.taketoday.origin.Origin in project today-framework by TAKETODAY.

the class ConfigDataEnvironmentContributorPlaceholdersResolver method resolvePlaceholder.

@Nullable
private String resolvePlaceholder(String placeholder) {
    Object result = null;
    for (ConfigDataEnvironmentContributor contributor : this.contributors) {
        PropertySource<?> propertySource = contributor.getPropertySource();
        Object value = (propertySource != null) ? propertySource.getProperty(placeholder) : null;
        if (value != null && !isActive(contributor)) {
            if (this.failOnResolveFromInactiveContributor) {
                ConfigDataResource resource = contributor.getResource();
                Origin origin = OriginLookup.getOrigin(propertySource, placeholder);
                throw new InactiveConfigDataAccessException(propertySource, resource, placeholder, origin);
            }
            value = null;
        }
        result = (result != null) ? result : value;
    }
    return (result != null) ? String.valueOf(result) : null;
}
Also used : Origin(cn.taketoday.origin.Origin) Nullable(cn.taketoday.lang.Nullable)

Example 17 with Origin

use of cn.taketoday.origin.Origin in project today-framework by TAKETODAY.

the class ConfigDataNotFoundFailureAnalyzer method analyze.

@Override
protected FailureAnalysis analyze(Throwable rootFailure, ConfigDataNotFoundException cause) {
    ConfigDataLocation location = getLocation(cause);
    Origin origin = Origin.from(location);
    String message = String.format("Config data %s does not exist", cause.getReferenceDescription());
    StringBuilder action = new StringBuilder("Check that the value ");
    if (location != null) {
        action.append(String.format("'%s' ", location));
    }
    if (origin != null) {
        action.append(String.format("at %s ", origin));
    }
    action.append("is correct");
    if (location != null && !location.isOptional()) {
        action.append(String.format(", or prefix it with '%s'", ConfigDataLocation.OPTIONAL_PREFIX));
    }
    return new FailureAnalysis(message, action.toString(), cause);
}
Also used : Origin(cn.taketoday.origin.Origin) FailureAnalysis(cn.taketoday.framework.diagnostics.FailureAnalysis)

Example 18 with Origin

use of cn.taketoday.origin.Origin in project today-infrastructure by TAKETODAY.

the class ConfigDataEnvironmentContributorPlaceholdersResolver method resolvePlaceholder.

@Nullable
private String resolvePlaceholder(String placeholder) {
    Object result = null;
    for (ConfigDataEnvironmentContributor contributor : this.contributors) {
        PropertySource<?> propertySource = contributor.getPropertySource();
        Object value = (propertySource != null) ? propertySource.getProperty(placeholder) : null;
        if (value != null && !isActive(contributor)) {
            if (this.failOnResolveFromInactiveContributor) {
                ConfigDataResource resource = contributor.getResource();
                Origin origin = OriginLookup.getOrigin(propertySource, placeholder);
                throw new InactiveConfigDataAccessException(propertySource, resource, placeholder, origin);
            }
            value = null;
        }
        result = (result != null) ? result : value;
    }
    return (result != null) ? String.valueOf(result) : null;
}
Also used : Origin(cn.taketoday.origin.Origin) Nullable(cn.taketoday.lang.Nullable)

Example 19 with Origin

use of cn.taketoday.origin.Origin in project today-infrastructure 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)

Example 20 with Origin

use of cn.taketoday.origin.Origin in project today-infrastructure by TAKETODAY.

the class ConfigDataLocationTests method withOriginSetsOrigin.

@Test
void withOriginSetsOrigin() {
    Origin origin = mock(Origin.class);
    ConfigDataLocation location = ConfigDataLocation.valueOf("test").withOrigin(origin);
    assertThat(location.getOrigin()).isSameAs(origin);
}
Also used : Origin(cn.taketoday.origin.Origin) Test(org.junit.jupiter.api.Test)

Aggregations

Origin (cn.taketoday.origin.Origin)22 Test (org.junit.jupiter.api.Test)14 ConfigurationProperty (cn.taketoday.context.properties.source.ConfigurationProperty)4 ConfigurationPropertyName (cn.taketoday.context.properties.source.ConfigurationPropertyName)4 ArrayList (java.util.ArrayList)4 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)2 BindContext (cn.taketoday.context.properties.bind.BindContext)2 BindHandler (cn.taketoday.context.properties.bind.BindHandler)2 Bindable (cn.taketoday.context.properties.bind.Bindable)2 Binder (cn.taketoday.context.properties.bind.Binder)2 FailureAnalysis (cn.taketoday.framework.diagnostics.FailureAnalysis)2 Nullable (cn.taketoday.lang.Nullable)2 MockOrigin (cn.taketoday.origin.MockOrigin)2 OriginProvider (cn.taketoday.origin.OriginProvider)2 TextResourceOrigin (cn.taketoday.origin.TextResourceOrigin)2 Location (cn.taketoday.origin.TextResourceOrigin.Location)2 FieldError (cn.taketoday.validation.FieldError)2 ObjectError (cn.taketoday.validation.ObjectError)2 LinkedHashSet (java.util.LinkedHashSet)2