Example usage for org.springframework.core.env CompositePropertySource CompositePropertySource

List of usage examples for org.springframework.core.env CompositePropertySource CompositePropertySource

Introduction

In this page you can find the example usage for org.springframework.core.env CompositePropertySource CompositePropertySource.

Prototype

public CompositePropertySource(String name) 

Source Link

Document

Create a new CompositePropertySource .

Usage

From source file:org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.java

@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;//from   ww w . j  a  v  a 2 s.c  o  m
    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) {
                logger.info(String.format(
                        "Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s",
                        result.getName(),
                        result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()),
                        result.getLabel(), result.getVersion(), result.getState()));

                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;

}

From source file:org.springframework.cloud.zookeeper.config.ZookeeperPropertySourceLocator.java

@Override
public PropertySource<?> locate(Environment environment) {
    if (environment instanceof ConfigurableEnvironment) {
        ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
        String appName = env.getProperty("spring.application.name");
        List<String> profiles = Arrays.asList(env.getActiveProfiles());

        String root = this.properties.getRoot();
        this.contexts = new ArrayList<>();

        String defaultContext = root + "/" + this.properties.getDefaultContext();
        this.contexts.add(defaultContext);
        addProfiles(this.contexts, defaultContext, profiles);

        StringBuilder baseContext = new StringBuilder(root);
        if (!appName.startsWith("/")) {
            baseContext.append("/");
        }//from   w w w . j ava 2  s  . c  o  m
        baseContext.append(appName);
        this.contexts.add(baseContext.toString());
        addProfiles(this.contexts, baseContext.toString(), profiles);

        CompositePropertySource composite = new CompositePropertySource("zookeeper");

        Collections.reverse(this.contexts);

        for (String propertySourceContext : this.contexts) {
            try {
                PropertySource propertySource = create(propertySourceContext);
                composite.addPropertySource(propertySource);
                // TODO: howto call close when /refresh
            } catch (Exception e) {
                if (this.properties.isFailFast()) {
                    ReflectionUtils.rethrowRuntimeException(e);
                } else {
                    log.warn("Unable to load zookeeper config from " + propertySourceContext, e);
                }
            }
        }

        return composite;
    }
    return null;
}

From source file:org.springframework.context.annotation.ConfigurationClassParser.java

private void addPropertySource(PropertySource<?> propertySource) {
    String name = propertySource.getName();
    MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();

    if (this.propertySourceNames.contains(name)) {
        // We've already added a version, we need to extend it
        PropertySource<?> existing = propertySources.get(name);
        if (existing != null) {
            PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource
                    ? ((ResourcePropertySource) propertySource).withResourceName()
                    : propertySource);//from  w ww  .  jav  a 2  s  .  com
            if (existing instanceof CompositePropertySource) {
                ((CompositePropertySource) existing).addFirstPropertySource(newSource);
            } else {
                if (existing instanceof ResourcePropertySource) {
                    existing = ((ResourcePropertySource) existing).withResourceName();
                }
                CompositePropertySource composite = new CompositePropertySource(name);
                composite.addPropertySource(newSource);
                composite.addPropertySource(existing);
                propertySources.replace(name, composite);
            }
            return;
        }
    }

    if (this.propertySourceNames.isEmpty()) {
        propertySources.addLast(propertySource);
    } else {
        String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
        propertySources.addBefore(firstProcessed, propertySource);
    }
    this.propertySourceNames.add(name);
}