Example usage for org.springframework.core.env PropertySource getSource

List of usage examples for org.springframework.core.env PropertySource getSource

Introduction

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

Prototype

public T getSource() 

Source Link

Document

Return the underlying source object for this PropertySource .

Usage

From source file:com.bose.aem.spring.config.ConfigServicePropertySourceLocator.java

@Override
public org.springframework.core.env.PropertySource<?> locate(
        org.springframework.core.env.Environment environment) {
    ConfigClientProperties client = this.defaults.override(environment);
    CompositePropertySource composite = new CompositePropertySource("configService");
    RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(client) : this.restTemplate;
    Exception error = null;//w ww  .ja v a 2 s.  c  o  m
    String errorBody = null;
    //logger.info("Fetching config from server at: " + client.getRawUri());
    try {
        String[] labels = new String[] { "" };
        if (StringUtils.hasText(client.getLabel())) {
            labels = StringUtils.commaDelimitedListToStringArray(client.getLabel());
        }
        // Try all the labels until one works
        for (String label : labels) {
            Environment result = getRemoteEnvironment(restTemplate, client.getRawUri(), client.getName(),
                    client.getProfile(), label.trim());
            if (result != null) {
                //                    logger.info(String.format("Located environment: name=%s, profiles=%s, label=%s, version=%s",
                //                            result.getName(),
                //                            result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()),
                //                            result.getLabel(), result.getVersion()));

                for (PropertySource source : result.getPropertySources()) {
                    @SuppressWarnings("unchecked")
                    Map<String, Object> map = (Map<String, Object>) source.getSource();
                    composite.addPropertySource(new MapPropertySource(source.getName(), 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 (client != null && client.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:ch.sdi.SocialDataImporterRunner.java

/**
 * Initializes the environment.//from w  w  w  . j  av  a2s  .  c  o m
 * <p>
 * @param aArgs the command line arguments
 * @throws SdiException on any problems
 */
private void initialize(String[] aArgs) throws SdiException {
    if (myConversionService == null) {
        throw new SdiException("ConversionService not injected", SdiException.EXIT_CODE_CONFIG_ERROR);
    } // if myConversionService == null

    ConfigUtils.setConversionService(myConversionService);

    myUserPropertyOverloader.overrideByUserProperties();

    String s = Arrays.toString(aArgs);
    myLog.debug("adding command line arguments to the environment: " + s);

    MutablePropertySources propertySources = myEnv.getPropertySources();
    propertySources.addFirst(new SimpleCommandLinePropertySource(ConfigUtils.PROP_SOURCE_NAME_CMD_LINE, aArgs));

    if (myLog.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder(
                "After self-configuration, and after user property " + "override. ");
        sb.append(myLog.isTraceEnabled() ? "Loaded properties: " : "Loaded property sources: ");

        MutablePropertySources mps = myEnv.getPropertySources();
        for (PropertySource<?> propertySource : mps) {
            sb.append("\n    PropertySource: ").append(propertySource.getName());

            if (myLog.isTraceEnabled()) {
                sb.append("\n        ").append("" + propertySource.getSource());
            } // if myLog.isTraceEnabled()
        }

        if (myLog.isTraceEnabled()) {
            myLog.trace(sb.toString());
        } // if myLog.isTraceEnabled()
        else if (myLog.isDebugEnabled()) {
            myLog.debug(sb.toString());
        }
    } // if myLog.isDebugEnabled()

    myLog.trace(
            "inputcollector.thing.alternateName: " + myEnv.getProperty("inputcollector.thing.alternateName"));
}

From source file:net.opentsdb.contrib.tsquare.support.TsdbFactoryBean.java

@Override
public TSDB getObject() throws Exception {
    if (instance == null) {
        @SuppressWarnings("unchecked")
        PropertySource<Config> tsdbPropertySource = (PropertySource<Config>) springEnvironment
                .getPropertySources().get(TsWebApplicationContextInitializer.PROPERTY_SOURCE_NAME);
        Preconditions.checkNotNull(tsdbPropertySource, "Unable to load TSDB property source");
        instance = new TSDB(tsdbPropertySource.getSource());
    }/* www .  j av  a  2  s. co m*/

    return instance;
}

From source file:com.github.eddumelendez.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration.java

private Map<String, Object> getLdapPorts(MutablePropertySources sources) {
    PropertySource<?> propertySource = sources.get("ldap.ports");
    if (propertySource == null) {
        propertySource = new MapPropertySource("ldap.ports", new HashMap<String, Object>());
        sources.addFirst(propertySource);
    }/*from w w w. j ava2  s  .  c o  m*/
    return (Map<String, Object>) propertySource.getSource();
}

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

CompositePropertySource exchange(ConfigClientProperties client, CompositePropertySource composite) {
    RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(client) : this.restTemplate;
    logger.info("Attempting to load environment from source: " + client.getRawUri());
    org.springframework.cloud.config.Environment result = restTemplate
            .exchange(client.getRawUri() + "/{name}/{profile}/{label}", HttpMethod.GET,
                    new HttpEntity<Void>((Void) null), org.springframework.cloud.config.Environment.class,
                    client.getName(), client.getProfile(), client.getLabel())
            .getBody();// w  w  w.  j a  va  2  s  . c  o  m
    for (org.springframework.cloud.config.PropertySource source : result.getPropertySources()) {
        @SuppressWarnings("unchecked")
        Map<String, Object> map = (Map<String, Object>) source.getSource();
        composite.addPropertySource(new MapPropertySource(source.getName(), map));
    }
    return composite;
}

From source file:org.springframework.cloud.dataflow.server.config.DefaultEnvironmentPostProcessor.java

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    Map<String, Object> internalDefaults = new HashMap<>();
    Map<String, Object> defaults = new HashMap<>();
    MutablePropertySources existingPropertySources = environment.getPropertySources();

    contributeDefaults(internalDefaults, serverDefaultsResource);
    contributeDefaults(defaults, serverResource);

    String defaultPropertiesKey = "defaultProperties";

    if (!existingPropertySources.contains(defaultPropertiesKey)
            || existingPropertySources.get(defaultPropertiesKey) == null) {
        existingPropertySources.addLast(new MapPropertySource(defaultPropertiesKey, internalDefaults));
        existingPropertySources.addLast(new MapPropertySource(defaultPropertiesKey, defaults));
    } else {/*from  www. j a  v  a 2 s.  c om*/
        PropertySource<?> propertySource = existingPropertySources.get(defaultPropertiesKey);
        @SuppressWarnings("unchecked")
        Map<String, Object> mapOfProperties = Map.class.cast(propertySource.getSource());
        for (String k : internalDefaults.keySet()) {
            Set<String> setOfPropertyKeys = mapOfProperties.keySet();
            if (!setOfPropertyKeys.contains(k)) {
                mapOfProperties.put(k, internalDefaults.get(k));
                logger.debug(k + '=' + internalDefaults.get(k));
            }
        }
        for (String k : defaults.keySet()) {
            Set<String> setOfPropertyKeys = mapOfProperties.keySet();
            if (!setOfPropertyKeys.contains(k)) {
                mapOfProperties.put(k, defaults.get(k));
                logger.debug(k + '=' + defaults.get(k));
            }
        }
    }

}