All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cc.voox.graphql.ui.PropertyGroupReader Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package cc.voox.graphql.ui;

import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

class PropertyGroupReader {

    private Environment environment;
    private String prefix;
    private Properties props;

    PropertyGroupReader(Environment environment, String prefix) {
        this.environment = Objects.requireNonNull(environment);
        this.prefix = Optional.ofNullable(prefix).orElse("");
    }

    Properties load() {
        if (props == null) {
            props = new Properties();
            loadProps();
        }
        return props;
    }

    private void loadProps() {
        streamOfPropertySources().forEach(propertySource ->
                Arrays.stream(propertySource.getPropertyNames())
                        .filter(this::isWanted)
                        .forEach(key -> add(propertySource, key)));
    }

    private Stream streamOfPropertySources() {
        if (environment instanceof ConfigurableEnvironment) {
            Iterator> iterator = ((ConfigurableEnvironment) environment).getPropertySources().iterator();
            Iterable> iterable = () -> iterator;
            return StreamSupport.stream(iterable.spliterator(), false)
                    .filter(EnumerablePropertySource.class::isInstance)
                    .map(EnumerablePropertySource.class::cast);
        }
        return Stream.empty();
    }

    private String withoutPrefix(String key) {
        return key.replace(prefix, "");
    }

    private boolean isWanted(String key) {
        return key.startsWith(prefix);
    }

    private Object add(EnumerablePropertySource propertySource, String key) {
        return props.put(withoutPrefix(key), propertySource.getProperty(key));
    }



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy