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

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Return the name of this PropertySource .

Usage

From source file:ch.sdi.SocialDataImporterRunner.java

/**
 * Initializes the environment./*  w w  w . j  a v a  2s .co 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:io.servicecomb.springboot.starter.configuration.ConfigurableEnvironmentConfiguration.java

private void extract(String root, Map<String, PropertySource<?>> map, PropertySource<?> source) {
    if (source instanceof CompositePropertySource) {
        for (PropertySource<?> nest : ((CompositePropertySource) source).getPropertySources()) {
            extract(source.getName() + ":", map, nest);
        }/* w w  w. j a v a  2 s  . c om*/
    } else {
        map.put(root + source.getName(), source);
    }
}

From source file:com.ericsson.eiffel.remrem.publish.config.RabbitMqPropertiesConfig.java

/***
 * Reads catalina properties to a map object.
 * //from   w w w .  j a v a 2s  .com
 * @param map
 *            RabbitMq instances map object.
 */
private void readCatalinaProperties(Map<String, Object> map) {
    String catalina_home = System.getProperty("catalina.home").replace('\\', '/');
    for (Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext();) {
        PropertySource propertySource = (PropertySource) it.next();
        if (propertySource instanceof MapPropertySource) {
            if (propertySource.getName().contains("[file:" + catalina_home + "/conf/config.properties]")) {
                map.putAll(((MapPropertySource) propertySource).getSource());
            }
        }
    }
}

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;/*from  w w  w  .  jav  a2  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:org.apache.servicecomb.config.ConfigurationSpringInitializer.java

/**
 * Get property names from {@link EnumerablePropertySource}, and get property value from {@link ConfigurableEnvironment#getProperty(String)}
 *///from  ww  w . j a  v  a2s .  c o  m
private void getProperties(ConfigurableEnvironment environment, PropertySource<?> propertySource,
        Map<String, Object> configFromSpringBoot) {
    if (propertySource instanceof CompositePropertySource) {
        // recursively get EnumerablePropertySource
        CompositePropertySource compositePropertySource = (CompositePropertySource) propertySource;
        compositePropertySource.getPropertySources()
                .forEach(ps -> getProperties(environment, ps, configFromSpringBoot));
        return;
    }
    if (propertySource instanceof EnumerablePropertySource) {
        EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) propertySource;
        for (String propertyName : enumerablePropertySource.getPropertyNames()) {
            configFromSpringBoot.put(propertyName, environment.getProperty(propertyName));
        }
        return;
    }

    LOGGER.debug("a none EnumerablePropertySource is ignored, propertySourceName = [{}]",
            propertySource.getName());
}

From source file:org.cloudfoundry.identity.uaa.config.EnvironmentMapFactoryBean.java

@Override
public Map<String, ?> getObject() {
    Map<String, Object> result = new LinkedHashMap<String, Object>();
    // The result is the default application properties overridden with
    // Spring environment values - reversing the
    // order of the placeholder configurers in the application context.
    for (Object key : defaultProperties.keySet()) {
        String name = (String) key;
        if (environment != null && environment.containsProperty(name)) {
            Object value = environment.getProperty(name, Object.class);
            logger.debug("From Environment: " + name);
            result.put(name, value);// w ww  .ja  va2 s  .co m
        } else {
            logger.debug("From Defaults: " + name);
            result.put(name, defaultProperties.get(key));
        }
    }
    // Any properties added only in the environment can be picked up here...
    if (environment instanceof ConfigurableEnvironment) {
        for (PropertySource<?> source : ((ConfigurableEnvironment) environment).getPropertySources()) {
            if (source instanceof EnumerablePropertySource
                    && !STATIC_PROPERTY_SOURCES.contains(source.getName())) {
                @SuppressWarnings("rawtypes")
                EnumerablePropertySource enumerable = (EnumerablePropertySource) source;
                for (String name : enumerable.getPropertyNames()) {
                    Object value = source.getProperty(name);
                    if (value instanceof String) {
                        // Unresolved placeholders are legal.
                        value = environment.resolvePlaceholders((String) value);
                    }
                    result.put(name, value);
                }
            }
        }
    }
    return result;
}

From source file:org.cloudfoundry.identity.uaa.config.EnvironmentPropertiesFactoryBean.java

@Override
public Map<String, ?> getObject() {
    Map<String, Object> result = new LinkedHashMap<String, Object>();
    // The result is the default application properties overridden with Spring environment values - reversing the
    // order of the placeholder configurers in the application context.
    for (Object key : defaultProperties.keySet()) {
        String name = (String) key;
        if (environment != null && environment.containsProperty(name)) {
            logger.debug("From Environment: " + name + "=" + environment.getProperty(name));
            result.put(name, environment.getProperty(name));
        } else {/*from  ww w  .  j a  v  a 2  s  . co  m*/
            logger.debug("From Defaults: " + name + "=" + defaultProperties.getProperty(name));
            result.put(name, defaultProperties.get(key));
        }
    }
    // Any properties added only in the environment can be picked up here...
    if (environment instanceof ConfigurableEnvironment) {
        for (PropertySource<?> source : ((ConfigurableEnvironment) environment).getPropertySources()) {
            if (source instanceof EnumerablePropertySource
                    && !STATIC_PROPERTY_SOURCES.contains(source.getName())) {
                @SuppressWarnings("rawtypes")
                EnumerablePropertySource enumerable = (EnumerablePropertySource) source;
                for (String name : enumerable.getPropertyNames()) {
                    result.put(name, environment.getProperty(name));
                }
            }
        }
    }
    return result;
}

From source file:org.springframework.boot.SpringApplication.java

private ConfigurableEnvironment convertToStandardEnvironment(ConfigurableEnvironment environment) {
    StandardEnvironment result = new StandardEnvironment();
    removeAllPropertySources(result.getPropertySources());
    result.setActiveProfiles(environment.getActiveProfiles());
    for (PropertySource<?> propertySource : environment.getPropertySources()) {
        if (!SERVLET_ENVIRONMENT_SOURCE_NAMES.contains(propertySource.getName())) {
            result.getPropertySources().addLast(propertySource);
        }//from w w w .  jav a 2  s.  co m
    }
    return result;
}

From source file:org.springframework.boot.SpringApplication.java

private void removeAllPropertySources(MutablePropertySources propertySources) {
    Set<String> names = new HashSet<String>();
    for (PropertySource<?> propertySource : propertySources) {
        names.add(propertySource.getName());
    }// w ww.jav  a 2  s.c o  m
    for (String name : names) {
        propertySources.remove(name);
    }
}