Search in sources :

Example 31 with PropertySource

use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.

the class ConfigServerHealthIndicator method doHealthCheck.

@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
    builder.up();
    List<Map<String, Object>> details = new ArrayList<>();
    for (String name : this.repositories.keySet()) {
        Repository repository = this.repositories.get(name);
        String application = (repository.getName() == null) ? name : repository.getName();
        String profiles = repository.getProfiles();
        try {
            Environment environment = this.environmentRepository.findOne(application, profiles, repository.getLabel());
            HashMap<String, Object> detail = new HashMap<>();
            detail.put("name", environment.getName());
            detail.put("label", environment.getLabel());
            if (environment.getProfiles() != null && environment.getProfiles().length > 0) {
                detail.put("profiles", Arrays.asList(environment.getProfiles()));
            }
            if (!CollectionUtils.isEmpty(environment.getPropertySources())) {
                List<String> sources = new ArrayList<>();
                for (PropertySource source : environment.getPropertySources()) {
                    sources.add(source.getName());
                }
                detail.put("sources", sources);
            }
            details.add(detail);
        } catch (Exception e) {
            HashMap<String, String> map = new HashMap<>();
            map.put("application", application);
            map.put("profiles", profiles);
            builder.withDetail("repository", map);
            builder.down(e);
            return;
        }
    }
    builder.withDetail("repositories", details);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) PropertySource(org.springframework.cloud.config.environment.PropertySource) EnvironmentRepository(org.springframework.cloud.config.server.environment.EnvironmentRepository) Environment(org.springframework.cloud.config.environment.Environment) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 32 with PropertySource

use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.

the class ConfigServicePropertySourceLocator method locate.

@Override
@Retryable(interceptor = "configServerRetryInterceptor")
public org.springframework.core.env.PropertySource<?> locate(org.springframework.core.env.Environment environment) {
    ConfigClientProperties properties = this.defaultProperties.override(environment);
    CompositePropertySource composite = new CompositePropertySource("configService");
    RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(properties) : this.restTemplate;
    Exception error = null;
    String errorBody = null;
    logger.info("Fetching config from server at: " + properties.getRawUri());
    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(restTemplate, properties, label.trim(), state);
            if (result != null) {
                log(result);
                if (result.getPropertySources() != null) {
                    // result.getPropertySources() can be null if using xml
                    for (PropertySource source : result.getPropertySources()) {
                        @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) source.getSource();
                        composite.addPropertySource(new MapPropertySource(source.getName(), map));
                    }
                }
                if (StringUtils.hasText(result.getState()) || StringUtils.hasText(result.getVersion())) {
                    HashMap<String, Object> map = new HashMap<>();
                    putValue(map, "config.client.state", result.getState());
                    putValue(map, "config.client.version", result.getVersion());
                    composite.addFirstPropertySource(new MapPropertySource("configClient", map));
                }
                return composite;
            }
        }
    } catch (HttpServerErrorException e) {
        error = e;
        if (MediaType.APPLICATION_JSON.includes(e.getResponseHeaders().getContentType())) {
            errorBody = e.getResponseBodyAsString();
        }
    } catch (Exception e) {
        error = e;
    }
    if (properties.isFailFast()) {
        throw new IllegalStateException("Could not locate PropertySource and the fail fast property is set, failing", error);
    }
    logger.warn("Could not locate PropertySource: " + (errorBody == null ? error == null ? "label not found" : error.getMessage() : errorBody));
    return null;
}
Also used : HashMap(java.util.HashMap) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) IOException(java.io.IOException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) PropertySource(org.springframework.cloud.config.environment.PropertySource) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) RestTemplate(org.springframework.web.client.RestTemplate) Environment(org.springframework.cloud.config.environment.Environment) HashMap(java.util.HashMap) Map(java.util.Map) Retryable(org.springframework.retry.annotation.Retryable)

Example 33 with PropertySource

use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.

the class EnvironmentCleaner method clean.

public Environment clean(Environment value, String workingDir, String uri) {
    Environment result = new Environment(value);
    for (PropertySource source : value.getPropertySources()) {
        String name = source.getName().replace(workingDir, "");
        name = name.replace("applicationConfig: [", "");
        name = uri + "/" + name.replace("]", "");
        result.add(new PropertySource(name, source.getSource()));
    }
    return result;
}
Also used : Environment(org.springframework.cloud.config.environment.Environment) PropertySource(org.springframework.cloud.config.environment.PropertySource)

Aggregations

PropertySource (org.springframework.cloud.config.environment.PropertySource)33 Test (org.junit.Test)20 LinkedHashMap (java.util.LinkedHashMap)19 Environment (org.springframework.cloud.config.environment.Environment)12 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 MapPropertySource (org.springframework.core.env.MapPropertySource)4 CompositePropertySource (org.springframework.core.env.CompositePropertySource)3 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)1 File (java.io.File)1 IOException (java.io.IOException)1 LinkedHashSet (java.util.LinkedHashSet)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1 YamlPropertiesFactoryBean (org.springframework.beans.factory.config.YamlPropertiesFactoryBean)1 EnvironmentRepository (org.springframework.cloud.config.server.environment.EnvironmentRepository)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 StandardEnvironment (org.springframework.core.env.StandardEnvironment)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1