Example usage for org.springframework.core.env EnumerablePropertySource getPropertyNames

List of usage examples for org.springframework.core.env EnumerablePropertySource getPropertyNames

Introduction

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

Prototype

public abstract String[] getPropertyNames();

Source Link

Document

Return the names of all properties contained by the #getSource() source object (never null ).

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;
    }//www .j a va 2s.  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:zipkin.sparkstreaming.autoconfigure.consumer.storage.ZipkinStorageConsumerAutoConfiguration.java

static Properties extractZipkinProperties(ConfigurableEnvironment env) {
    Properties properties = new Properties();
    Iterator<PropertySource<?>> it = env.getPropertySources().iterator();
    while (it.hasNext()) {
        PropertySource<?> next = it.next();
        if (!(next instanceof EnumerablePropertySource))
            continue;
        EnumerablePropertySource source = (EnumerablePropertySource) next;
        for (String name : source.getPropertyNames()) {
            if (name.startsWith("zipkin"))
                properties.put(name, source.getProperty(name));
        }/*  ww w .j  a v a2  s .co m*/
    }
    return properties;
}

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

/**
 * Returns a collection of all property names in the given PropertySource.
 * <p>/*from  w w w  .  j av a 2s.  c  om*/
 * @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:com.netflix.spinnaker.kork.archaius.SpringEnvironmentPolledConfigurationSource.java

@Override
public PollResult poll(boolean initial, Object checkPoint) throws Exception {
    Map<String, Object> result = new HashMap<>();
    environment.getPropertySources().iterator().forEachRemaining(source -> {
        if (source instanceof EnumerablePropertySource) {
            EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
            for (String key : enumerable.getPropertyNames()) {
                result.putIfAbsent(key, enumerable.getProperty(key));
            }/* w ww. j a v  a  2 s .  c  o m*/
        }
    });
    return PollResult.createFull(result);
}

From source file:io.servicecomb.springboot.starter.configuration.ConfigurableEnvironmentConfiguration.java

@Override
public Iterator<String> getKeys() {
    List<String> result = new ArrayList<>();
    for (Map.Entry<String, PropertySource<?>> entry : getPropertySources().entrySet()) {
        PropertySource<?> source = entry.getValue();
        if (source instanceof EnumerablePropertySource) {
            EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
            for (String name : enumerable.getPropertyNames()) {
                result.add(name);//from   w  w w  .  j  av a2 s  .  c o  m
            }
        }
    }
    return result.iterator();
}

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;
    }/*from   ww w . ja  va2s .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)}
 *///from   w w w  .  j a v a 2s  . 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);/*from  w  w  w  .ja va  2s.  c  o  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   w w  w.  ja v a 2s. c om*/
            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;
}