Search in sources :

Example 1 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project spring-cloud-alibaba by alibaba.

the class NacosJsonPropertySourceLoader method doLoad.

/**
 * Load the resource into one or more property sources. Implementations may either
 * return a list containing a single source, or in the case of a multi-document format
 * such as yaml a source for each document in the resource.
 * @param name the root name of the property source. If multiple documents are loaded
 * an additional suffix should be added to the name for each source loaded.
 * @param resource the resource to load
 * @return a list property sources
 * @throws IOException if the source cannot be loaded
 */
@Override
protected List<PropertySource<?>> doLoad(String name, Resource resource) throws IOException {
    Map<String, Object> result = new LinkedHashMap<>(32);
    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> nacosDataMap = mapper.readValue(resource.getInputStream(), LinkedHashMap.class);
    flattenedMap(result, nacosDataMap, null);
    return Collections.singletonList(new OriginTrackedMapPropertySource(name, this.reloadMap(result), true));
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project spring-boot-web by wangchengming666.

the class ApplicationEnvironmentPreparedListener method handleEnvironment.

public void handleEnvironment() {
    Objects.requireNonNull(env);
    initFrameworkConstants();
    initKeysConstants();
    initActiveProfilesConstants();
    MutablePropertySources propSources = env.getPropertySources();
    StreamSupport.stream(propSources.spliterator(), false).forEach(p -> {
        if (p instanceof OriginTrackedMapPropertySource) {
            OriginTrackedMapPropertySource o = (OriginTrackedMapPropertySource) p;
            initEncryptorConstants(p.getName(), o);
        }
    });
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 3 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project mmqtt by MrHKing.

the class StartingApplicationListener method loadPreProperties.

private void loadPreProperties(ConfigurableEnvironment environment) {
    try {
        SOURCES.putAll(EnvUtil.loadProperties(EnvUtil.getApplicationConfFileResource()));
        environment.getPropertySources().addLast(new OriginTrackedMapPropertySource(MMQ_APPLICATION_CONF, SOURCES));
        registerWatcher();
    } catch (Exception e) {
        throw new MmqRuntimeException(MmqException.SERVER_ERROR, e);
    }
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) MmqRuntimeException(org.monkey.mmq.core.exception.runtime.MmqRuntimeException) MmqException(org.monkey.mmq.core.exception.MmqException) IOException(java.io.IOException) MmqRuntimeException(org.monkey.mmq.core.exception.runtime.MmqRuntimeException)

Example 4 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project mdx-kylin by Kyligence.

the class MdxPropertySourceLoader method load.

@SuppressWarnings("unchecked")
@Override
public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
    String filename = resource.getFilename();
    List<PropertySource<?>> propertySources;
    if (filename == null || filename.endsWith(".properties") || filename.endsWith(".xml")) {
        propertySources = propLoader.load(name, resource);
    } else {
        propertySources = yamlLoader.load(name, resource);
    }
    List<PropertySource<?>> newSources = new ArrayList<>();
    for (PropertySource propertySource : propertySources) {
        Map<String, ?> properties = (Map<String, ?>) propertySource.getSource();
        Map<String, Object> newProperties = new LinkedHashMap<>();
        for (String propertyKey : properties.keySet()) {
            Object propertyValue = properties.get(propertyKey);
            newProperties.put(propertyKey, handle(propertyKey, propertyValue));
        }
        newSources.add(new OriginTrackedMapPropertySource(name, Collections.unmodifiableMap(newProperties), true));
    }
    try {
        handleAad();
    } catch (Exception e) {
        // Nothing to do
        log.warn("Failed to handle aad", e);
    }
    return newSources;
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) ArrayList(java.util.ArrayList) JSONObject(com.alibaba.fastjson.JSONObject) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) PwdDecryptException(io.kylin.mdx.insight.common.PwdDecryptException) IOException(java.io.IOException) PropertySource(org.springframework.core.env.PropertySource) OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project xuxiaowei-cloud by xuxiaowei-cloud.

the class EnvironmentTests method getEnvironment.

/**
 * 获取环境变量
 */
@Test
void getEnvironment() {
    Environment environment = nacosConfigProperties.getEnvironment();
    MutablePropertySources mutablePropertySources = configurableEnvironment.getPropertySources();
    mutablePropertySources.forEach(propertySource -> {
        if (propertySource instanceof OriginTrackedMapPropertySource) {
            OriginTrackedMapPropertySource originTrackedMapPropertySource = (OriginTrackedMapPropertySource) propertySource;
            String name = originTrackedMapPropertySource.getName();
            Map<String, Object> source = originTrackedMapPropertySource.getSource();
            System.out.println("# 配置文件:" + name);
            for (String key : source.keySet()) {
                System.out.println(key + "= " + source.get(key));
            }
            System.out.println();
        } else if (propertySource instanceof MapPropertySource) {
            MapPropertySource mapPropertySource = (MapPropertySource) propertySource;
            String name = mapPropertySource.getName();
            Object property = mapPropertySource.getProperty(name);
        } else if (propertySource instanceof BootstrapPropertySource) {
            PropertySource<?> delegate = ((BootstrapPropertySource<?>) propertySource).getDelegate();
            if (delegate instanceof NacosPropertySource) {
                String name = delegate.getName();
                if (environment instanceof StandardServletEnvironment) {
                    StandardServletEnvironment standardServletEnvironment = (StandardServletEnvironment) environment;
                    MutablePropertySources propertySources = standardServletEnvironment.getPropertySources();
                    PropertySource<?> bootstrapProperties = propertySources.get("bootstrapProperties-" + name);
                    assert bootstrapProperties != null;
                    Object source = bootstrapProperties.getSource();
                    if (source instanceof LinkedHashMap) {
                        LinkedHashMap<?, ?> linkedHashMap = (LinkedHashMap<?, ?>) source;
                        System.out.println("# 配置文件:" + name);
                        for (Object key : linkedHashMap.keySet()) {
                            System.out.println(key + "= " + linkedHashMap.get(key));
                        }
                        System.out.println();
                    }
                }
            }
        }
    });
}
Also used : BootstrapPropertySource(org.springframework.cloud.bootstrap.config.BootstrapPropertySource) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) LinkedHashMap(java.util.LinkedHashMap) OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) NacosPropertySource(com.alibaba.cloud.nacos.client.NacosPropertySource) OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

OriginTrackedMapPropertySource (org.springframework.boot.env.OriginTrackedMapPropertySource)20 IOException (java.io.IOException)8 LinkedHashMap (java.util.LinkedHashMap)8 Map (java.util.Map)4 PropertySource (org.springframework.core.env.PropertySource)4 HashMap (java.util.HashMap)3 PropertySourceLoader (org.springframework.boot.env.PropertySourceLoader)3 Environment (org.springframework.cloud.config.environment.Environment)3 MutablePropertySources (org.springframework.core.env.MutablePropertySources)3 NacosException (com.alibaba.nacos.api.exception.NacosException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 PropertiesPropertySourceLoader (org.springframework.boot.env.PropertiesPropertySourceLoader)2 YamlPropertySourceLoader (org.springframework.boot.env.YamlPropertySourceLoader)2 OriginTrackedValue (org.springframework.boot.origin.OriginTrackedValue)2 PropertySource (org.springframework.cloud.config.environment.PropertySource)2 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)2 MapPropertySource (org.springframework.core.env.MapPropertySource)2 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)2 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)2