Search in sources :

Example 11 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project dynamic-tp by dromara.

the class ZkConfigEnvironmentProcessor method createZkPropertySource.

private void createZkPropertySource(ConfigurableEnvironment environment, Map<Object, Object> properties) {
    MutablePropertySources propertySources = environment.getPropertySources();
    OriginTrackedMapPropertySource zkSource = new OriginTrackedMapPropertySource(ZK_PROPERTY_SOURCE_NAME, properties);
    propertySources.addLast(zkSource);
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 12 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project nacos by alibaba.

the class NacosAutoRefreshPropertySourceLoader method load.

@Override
public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
    holder = resource;
    Map<String, ?> tmp = loadProperties(resource);
    properties.putAll(tmp);
    try {
        WatchFileCenter.registerWatcher(EnvUtil.getConfPath(), new FileWatcher() {

            @Override
            public void onChange(FileChangeEvent event) {
                try {
                    Map<String, ?> tmp1 = loadProperties(holder);
                    properties.putAll(tmp1);
                } catch (IOException ignore) {
                }
            }

            @Override
            public boolean interest(String context) {
                return StringUtils.contains(context, "application.properties");
            }
        });
    } catch (NacosException ignore) {
    }
    if (properties.isEmpty()) {
        return Collections.emptyList();
    }
    return Collections.singletonList(new OriginTrackedMapPropertySource("nacos_application_conf", properties));
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) FileWatcher(com.alibaba.nacos.sys.file.FileWatcher) IOException(java.io.IOException) FileChangeEvent(com.alibaba.nacos.sys.file.FileChangeEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 13 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project nacos by alibaba.

the class StartingApplicationListener method loadPreProperties.

private void loadPreProperties(ConfigurableEnvironment environment) {
    try {
        SOURCES.putAll(EnvUtil.loadProperties(EnvUtil.getApplicationConfFileResource()));
        environment.getPropertySources().addLast(new OriginTrackedMapPropertySource(NACOS_APPLICATION_CONF, SOURCES));
        registerWatcher();
    } catch (Exception e) {
        throw new NacosRuntimeException(NacosException.SERVER_ERROR, e);
    }
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) IOException(java.io.IOException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)

Example 14 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project byzer-notebook by byzer-org.

the class EnvironmentPropertyProcessor method postProcessEnvironment.

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    if (!preInitializationStarted.compareAndSet(false, true)) {
        return;
    }
    for (PropertySource propertySource : environment.getPropertySources()) {
        if (propertySource instanceof OriginTrackedMapPropertySource) {
            OriginTrackedMapPropertySource originPropertySource = (OriginTrackedMapPropertySource) propertySource;
            Map<String, Object> source = originPropertySource.getSource();
            source.forEach((k, v) -> {
                if (v instanceof OriginTrackedValue) {
                    Object value = ((OriginTrackedValue) v).getValue();
                    if (value instanceof String) {
                        Origin origin = ((OriginTrackedValue) v).getOrigin();
                        String trimVal = ((String) value).trim();
                        try {
                            Class<?> aClass = Class.forName(SPRING_ORIGIN_VALUE_CLASS);
                            Constructor<?> constructor = aClass.getDeclaredConstructor(CharSequence.class, Origin.class);
                            constructor.setAccessible(true);
                            // TODO decrypt password
                            Object o = constructor.newInstance(trimVal, origin);
                            source.put(k, o);
                        } catch (Exception e) {
                            log.error("Java reflection error", e);
                        }
                    }
                }
            });
        }
    }
}
Also used : Origin(org.springframework.boot.origin.Origin) OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) OriginTrackedValue(org.springframework.boot.origin.OriginTrackedValue) PropertySource(org.springframework.core.env.PropertySource) OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource)

Example 15 with OriginTrackedMapPropertySource

use of org.springframework.boot.env.OriginTrackedMapPropertySource in project fizz-gateway-community by wehotel.

the class DedicatedLineServiceRegistration method onApplicationEvent.

@SneakyThrows
@Override
public void onApplicationEvent(DedicatedLineWebServerInitializedEvent event) {
    ReactiveWebServerApplicationContext applicationContext = event.getApplicationContext();
    ConfigurableEnvironment env = applicationContext.getEnvironment();
    String prefix = SystemConfig.FIZZ_DEDICATED_LINE_CLIENT_PREFIX + ".service-registration";
    boolean eureka = env.containsProperty((prefix + ".eureka.instance.appname"));
    boolean nacos = env.containsProperty((prefix + ".nacos.discovery.server-addr"));
    if (eureka || nacos) {
        if (eureka) {
            Properties eurekaProperties = new Properties();
            boolean find = false;
            for (PropertySource<?> propertySource : env.getPropertySources()) {
                if (propertySource instanceof OriginTrackedMapPropertySource) {
                    OriginTrackedMapPropertySource originTrackedMapPropertySource = (OriginTrackedMapPropertySource) propertySource;
                    String[] propertyNames = originTrackedMapPropertySource.getPropertyNames();
                    for (String propertyName : propertyNames) {
                        if (propertyName.length() > 55) {
                            int eurekaPos = propertyName.indexOf("eureka");
                            if (eurekaPos > -1) {
                                eurekaProperties.setProperty(propertyName.substring(eurekaPos), originTrackedMapPropertySource.getProperty(propertyName).toString());
                                find = true;
                            }
                        }
                    }
                    if (find) {
                        break;
                    }
                }
            }
            if (!find) {
                log.error("no eureka config");
                return;
            }
            fizzServiceRegistration = FizzEurekaHelper.getServiceRegistration(applicationContext, eurekaProperties);
        }
        if (nacos) {
            Properties nacosProperties = new Properties();
            boolean find = false;
            for (PropertySource<?> propertySource : env.getPropertySources()) {
                if (propertySource instanceof OriginTrackedMapPropertySource) {
                    OriginTrackedMapPropertySource originTrackedMapPropertySource = (OriginTrackedMapPropertySource) propertySource;
                    String[] propertyNames = originTrackedMapPropertySource.getPropertyNames();
                    for (String propertyName : propertyNames) {
                        if (propertyName.length() > 64) {
                            int naocsPos = propertyName.indexOf("nacos");
                            if (naocsPos > -1) {
                                nacosProperties.setProperty(propertyName.substring(naocsPos), originTrackedMapPropertySource.getProperty(propertyName).toString());
                                find = true;
                            }
                        }
                    }
                    if (find) {
                        break;
                    }
                }
            }
            if (!find) {
                log.error("no nacos config");
                return;
            }
            fizzServiceRegistration = FizzNacosHelper.getServiceRegistration(applicationContext, nacosProperties);
        }
        fizzServiceRegistration.register();
    }
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) ReactiveWebServerApplicationContext(org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext) Properties(java.util.Properties) SneakyThrows(lombok.SneakyThrows)

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