Search in sources :

Example 1 with Option

use of org.springframework.boot.context.config.ConfigData.Option in project spring-boot by spring-projects.

the class ConfigDataTests method optionsOfUsesCopyOfOptions.

@Test
void optionsOfUsesCopyOfOptions() {
    Option[] array = { Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES };
    Options options = Options.of(array);
    array[0] = Option.PROFILE_SPECIFIC;
    assertThat(options.asSet()).containsExactly(Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);
}
Also used : PropertySourceOptions(org.springframework.boot.context.config.ConfigData.PropertySourceOptions) Options(org.springframework.boot.context.config.ConfigData.Options) Option(org.springframework.boot.context.config.ConfigData.Option) Test(org.junit.jupiter.api.Test)

Example 2 with Option

use of org.springframework.boot.context.config.ConfigData.Option in project spring-cloud-consul by spring-cloud.

the class ConsulConfigDataLoader method doLoad.

public ConfigData doLoad(ConfigDataLoaderContext context, ConsulConfigDataResource resource) {
    try {
        ConsulClient consul = getBean(context, ConsulClient.class);
        ConsulConfigIndexes indexes = getBean(context, ConsulConfigIndexes.class);
        ConsulPropertySource propertySource = resource.getConsulPropertySources().createPropertySource(resource.getContext(), consul, indexes.getIndexes()::put);
        if (propertySource == null) {
            return null;
        }
        List<ConsulPropertySource> propertySources = Collections.singletonList(propertySource);
        if (ALL_OPTIONS.size() == 1) {
            // boot 2.4.2 and prior
            return new ConfigData(propertySources);
        } else if (ALL_OPTIONS.size() == 2) {
            // boot 2.4.3 and 2.4.4
            return new ConfigData(propertySources, Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);
        } else if (ALL_OPTIONS.size() > 2) {
            // boot 2.4.5+
            return new ConfigData(propertySources, source -> {
                List<Option> options = new ArrayList<>();
                options.add(Option.IGNORE_IMPORTS);
                options.add(Option.IGNORE_PROFILES);
                if (StringUtils.hasText(resource.getProfile())) {
                    options.add(Option.PROFILE_SPECIFIC);
                }
                return Options.of(options.toArray(new Option[0]));
            });
        }
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("Error getting properties from consul: " + resource, e);
        }
        throw new ConfigDataResourceNotFoundException(resource, e);
    }
    return null;
}
Also used : ConsulClient(com.ecwid.consul.v1.ConsulClient) ConfigData(org.springframework.boot.context.config.ConfigData) ArrayList(java.util.ArrayList) ConfigDataResourceNotFoundException(org.springframework.boot.context.config.ConfigDataResourceNotFoundException) Option(org.springframework.boot.context.config.ConfigData.Option) ConfigDataResourceNotFoundException(org.springframework.boot.context.config.ConfigDataResourceNotFoundException)

Example 3 with Option

use of org.springframework.boot.context.config.ConfigData.Option in project spring-cloud-config by spring-cloud.

the class ConfigServerConfigDataLoader method doLoad.

public ConfigData doLoad(ConfigDataLoaderContext context, ConfigServerConfigDataResource resource) {
    ConfigClientProperties properties = resource.getProperties();
    List<PropertySource<?>> propertySources = new ArrayList<>();
    Exception error = null;
    String errorBody = null;
    try {
        String[] labels = new String[] { "" };
        if (StringUtils.hasText(properties.getLabel())) {
            labels = StringUtils.commaDelimitedListToStringArray(properties.getLabel());
        }
        String state = ConfigClientStateHolder.getState();
        // Try all the labels until one works
        for (String label : labels) {
            Environment result = getRemoteEnvironment(context, resource, label.trim(), state);
            if (result != null) {
                log(result);
                // result.getPropertySources() can be null if using xml
                if (result.getPropertySources() != null) {
                    for (org.springframework.cloud.config.environment.PropertySource source : result.getPropertySources()) {
                        @SuppressWarnings("unchecked") Map<String, Object> map = translateOrigins(source.getName(), (Map<String, Object>) source.getSource());
                        propertySources.add(0, new OriginTrackedMapPropertySource("configserver:" + source.getName(), map));
                    }
                }
                HashMap<String, Object> map = new HashMap<>();
                if (StringUtils.hasText(result.getState())) {
                    putValue(map, "config.client.state", result.getState());
                }
                if (StringUtils.hasText(result.getVersion())) {
                    putValue(map, "config.client.version", result.getVersion());
                }
                // the existence of this property source confirms a successful
                // response from config server
                propertySources.add(0, new MapPropertySource(CONFIG_CLIENT_PROPERTYSOURCE_NAME, map));
                if (ALL_OPTIONS.size() == 1) {
                    // boot 2.4.2 and prior
                    return new ConfigData(propertySources);
                } else if (ALL_OPTIONS.size() == 2) {
                    // boot 2.4.3 and 2.4.4
                    return new ConfigData(propertySources, Option.IGNORE_IMPORTS, Option.IGNORE_PROFILES);
                } else if (ALL_OPTIONS.size() > 2) {
                    // boot 2.4.5+
                    return new ConfigData(propertySources, propertySource -> {
                        String propertySourceName = propertySource.getName();
                        List<Option> options = new ArrayList<>();
                        options.add(Option.IGNORE_IMPORTS);
                        options.add(Option.IGNORE_PROFILES);
                        // https://github.com/spring-cloud/spring-cloud-config/issues/1874
                        for (String profile : resource.getAcceptedProfiles()) {
                            // - is the default profile-separator for property sources
                            if (propertySourceName.matches(".*[-,]" + profile + ".*")) {
                                // // TODO: switch to Options.with() when implemented
                                options.add(Option.PROFILE_SPECIFIC);
                            }
                        }
                        return Options.of(options.toArray(new Option[0]));
                    });
                }
            }
        }
        errorBody = String.format("None of labels %s found", Arrays.toString(labels));
    } catch (HttpServerErrorException e) {
        error = e;
        if (MediaType.APPLICATION_JSON.includes(e.getResponseHeaders().getContentType())) {
            errorBody = e.getResponseBodyAsString();
        }
    } catch (Exception e) {
        error = e;
    }
    if (properties.isFailFast() || !resource.isOptional()) {
        String reason;
        if (properties.isFailFast()) {
            reason = "the fail fast property is set";
        } else {
            reason = "the resource is not optional";
        }
        throw new ConfigClientFailFastException("Could not locate PropertySource and " + reason + ", failing" + (errorBody == null ? "" : ": " + errorBody), error);
    }
    logger.warn("Could not locate PropertySource (" + resource + "): " + (error != null ? error.getMessage() : errorBody));
    return null;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) ResourceAccessException(org.springframework.web.client.ResourceAccessException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) PropertySource(org.springframework.core.env.PropertySource) OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) ConfigData(org.springframework.boot.context.config.ConfigData) Environment(org.springframework.cloud.config.environment.Environment) Option(org.springframework.boot.context.config.ConfigData.Option)

Aggregations

Option (org.springframework.boot.context.config.ConfigData.Option)3 ArrayList (java.util.ArrayList)2 ConfigData (org.springframework.boot.context.config.ConfigData)2 ConsulClient (com.ecwid.consul.v1.ConsulClient)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.jupiter.api.Test)1 Options (org.springframework.boot.context.config.ConfigData.Options)1 PropertySourceOptions (org.springframework.boot.context.config.ConfigData.PropertySourceOptions)1 ConfigDataResourceNotFoundException (org.springframework.boot.context.config.ConfigDataResourceNotFoundException)1 OriginTrackedMapPropertySource (org.springframework.boot.env.OriginTrackedMapPropertySource)1 Environment (org.springframework.cloud.config.environment.Environment)1 MapPropertySource (org.springframework.core.env.MapPropertySource)1 PropertySource (org.springframework.core.env.PropertySource)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)1 ResourceAccessException (org.springframework.web.client.ResourceAccessException)1