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

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

Introduction

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

Prototype

public Collection<PropertySource<?>> getPropertySources() 

Source Link

Document

Return all property sources that this composite source holds.

Usage

From source file:io.gravitee.common.util.EnvironmentUtils.java

public static Map<String, Object> getAllProperties(PropertySource<?> aPropSource) {
    Map<String, Object> result = new HashMap<>();

    if (aPropSource instanceof CompositePropertySource) {
        CompositePropertySource cps = (CompositePropertySource) aPropSource;
        cps.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps)));
        return result;
    }/*from  ww  w. j a va  2  s .c o m*/

    if (aPropSource instanceof EnumerablePropertySource<?>) {
        EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource;
        Arrays.asList(ps.getPropertyNames()).forEach(key -> result.put(key, ps.getProperty(key)));
        return result;
    }

    // note: Most descendants of PropertySource are EnumerablePropertySource. There are some
    // few others like JndiPropertySource or StubPropertySource
    LOGGER.debug("Given PropertySource is instanceof " + aPropSource.getClass().getName()
            + " and cannot be iterated");

    return result;
}

From source file:ch.sdi.core.impl.cfg.ConfigUtils.java

/**
 * Returns a collection of all property names in the given PropertySource.
 * <p>/*from  www  .  j av a 2s  .com*/
 * @param aPropSource
 * @return
 */
public static Collection<String> getAllPropertyNames(PropertySource<?> aPropSource) {
    Collection<String> result = new HashSet<>();

    if (aPropSource instanceof CompositePropertySource) {
        CompositePropertySource cps = (CompositePropertySource) aPropSource;
        cps.getPropertySources().forEach(ps -> result.addAll(getAllPropertyNames(ps)));
        return result;
    }

    if (aPropSource instanceof EnumerablePropertySource<?>) {
        EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource;
        Arrays.asList(ps.getPropertyNames()).forEach(key -> result.add(key));
        return result;
    }

    // note: Most descendants of PropertySource are EnumerablePropertySource. There are some
    // few others like JndiPropertySource or StubPropertySource
    myLog.debug("Given PropertySource is instanceof " + aPropSource.getClass().getName()
            + " and cannot be iterated");

    return result;
}

From source file:io.fabric8.spring.cloud.kubernetes.reload.ConfigurationChangeDetector.java

/**
 * Finds all registered property sources of the given type.
 *///from  ww  w.  j a  v a2s. co m
protected <S extends PropertySource<?>> List<S> findPropertySources(Class<S> sourceClass) {
    List<S> managedSources = new LinkedList<>();

    LinkedList<PropertySource<?>> sources = toLinkedList(environment.getPropertySources());
    while (!sources.isEmpty()) {
        PropertySource<?> source = sources.pop();
        if (source instanceof CompositePropertySource) {
            CompositePropertySource comp = (CompositePropertySource) source;
            sources.addAll(comp.getPropertySources());
        } else if (sourceClass.isInstance(source)) {
            managedSources.add(sourceClass.cast(source));
        }
    }

    return managedSources;
}

From source file:io.gravitee.repository.jdbc.config.DataSourceFactory.java

public Map<String, Object> getAllProperties(PropertySource<?> aPropSource) {
    Map<String, Object> result = new HashMap<>();

    if (aPropSource instanceof CompositePropertySource) {
        CompositePropertySource cps = (CompositePropertySource) aPropSource;
        cps.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps)));
        return result;
    }/*  ww w  .  j av a  2  s . c  o m*/

    if (aPropSource instanceof EnumerablePropertySource<?>) {
        EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource;
        Arrays.asList(ps.getPropertyNames()).forEach(key -> result.put(key, ps.getProperty(key)));
        return result;
    }

    // note: Most descendants of PropertySource are EnumerablePropertySource. There are some
    // few others like JndiPropertySource or StubPropertySource
    logger.debug("Given PropertySource is instanceof " + aPropSource.getClass().getName()
            + " and cannot be iterated");

    return result;

}

From source file:org.apache.servicecomb.config.ConfigurationSpringInitializer.java

/**
 * Get property names from {@link EnumerablePropertySource}, and get property value from {@link ConfigurableEnvironment#getProperty(String)}
 *///  w  w w.  ja  v  a 2 s. 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());
}