use of org.springframework.core.env.CompositePropertySource in project CzechIdMng by bcvsolutions.
the class DefaultConfigurationService method getAllProperties.
private 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;
}
if (aPropSource instanceof EnumerablePropertySource<?>) {
EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource;
Arrays.asList(ps.getPropertyNames()).forEach(key -> result.put(key, ps.getProperty(key)));
return result;
}
return result;
}
use of org.springframework.core.env.CompositePropertySource in project apollo by ctripcorp.
the class ApolloApplicationContextInitializer method initialize.
/**
* Initialize Apollo Configurations Just after environment is ready.
*
* @param environment
*/
protected void initialize(ConfigurableEnvironment environment) {
if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
// already initialized, replay the logs that were printed before the logging system was initialized
DeferredLogger.replayTo();
return;
}
String namespaces = environment.getProperty(PropertySourcesConstants.APOLLO_BOOTSTRAP_NAMESPACES, ConfigConsts.NAMESPACE_APPLICATION);
logger.debug("Apollo bootstrap namespaces: {}", namespaces);
List<String> namespaceList = NAMESPACE_SPLITTER.splitToList(namespaces);
CompositePropertySource composite;
final ConfigUtil configUtil = ApolloInjector.getInstance(ConfigUtil.class);
if (configUtil.isPropertyNamesCacheEnabled()) {
composite = new CachedCompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
} else {
composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
}
for (String namespace : namespaceList) {
Config config = ConfigService.getConfig(namespace);
composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config));
}
environment.getPropertySources().addFirst(composite);
}
use of org.springframework.core.env.CompositePropertySource in project spring-cloud-netflix by spring-cloud.
the class EurekaClientConfigBeanTests method serviceUrlWithCompositePropertySource.
@Test
public void serviceUrlWithCompositePropertySource() {
CompositePropertySource source = new CompositePropertySource("composite");
this.context.getEnvironment().getPropertySources().addFirst(source);
source.addPropertySource(new MapPropertySource("config", Collections.<String, Object>singletonMap("eureka.client.serviceUrl.defaultZone", "http://example.com,http://example2.com")));
this.context.register(PropertyPlaceholderAutoConfiguration.class, TestConfiguration.class);
this.context.refresh();
assertEquals("{defaultZone=http://example.com,http://example2.com}", this.context.getBean(EurekaClientConfigBean.class).getServiceUrl().toString());
assertEquals("[http://example.com/, http://example2.com/]", getEurekaServiceUrlsForDefaultZone());
}
use of org.springframework.core.env.CompositePropertySource in project java-chassis by ServiceComb.
the class ConfigurationSpringInitializer method getProperties.
/**
* Get property names from {@link EnumerablePropertySource}, and get property value from {@link ConfigurableEnvironment#getProperty(String)}
*/
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()) {
try {
configFromSpringBoot.put(propertyName, environment.getProperty(propertyName, Object.class));
} catch (Exception e) {
throw new RuntimeException("set up spring property source failed.If you still want to start up the application and ignore errors, you can set servicecomb.config.ignoreResolveFailure to true.", e);
}
}
return;
}
LOGGER.debug("a none EnumerablePropertySource is ignored, propertySourceName = [{}]", propertySource.getName());
}
use of org.springframework.core.env.CompositePropertySource in project java-chassis by ServiceComb.
the class TestConfigurationSpringInitializer method testSetEnvironment.
@Test
public void testSetEnvironment() {
ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class);
MutablePropertySources propertySources = new MutablePropertySources();
Map<String, String> propertyMap = new HashMap<>();
final String map0Key0 = "map0-Key0";
final String map1Key0 = "map1-Key0";
final String map2Key0 = "map2-Key0";
final String map3Key0 = "map3-Key0";
propertyMap.put(map0Key0, "map0-Value0");
propertyMap.put(map1Key0, "map1-Value0");
propertyMap.put(map2Key0, "map2-Value0");
propertyMap.put(map3Key0, "map3-Value0");
/*
propertySources
|- compositePropertySource0
| |- mapPropertySource0
| | |- map0-Key0 = map0-Value0
| |- compositePropertySource1
| |- mapPropertySource1
| | |- map1-Key0 = map1-Value0
| |- mapPropertySource2
| |- map2-Key0 = map2-Value0
| |- JndiPropertySource(mocked)
|- mapPropertySource3
|- map3-Key0 = map3-Value0
*/
CompositePropertySource compositePropertySource0 = new CompositePropertySource("compositePropertySource0");
propertySources.addFirst(compositePropertySource0);
HashMap<String, Object> map0 = new HashMap<>();
map0.put(map0Key0, propertyMap.get(map0Key0));
MapPropertySource mapPropertySource0 = new MapPropertySource("mapPropertySource0", map0);
compositePropertySource0.addFirstPropertySource(mapPropertySource0);
CompositePropertySource compositePropertySource1 = new CompositePropertySource("compositePropertySource1");
compositePropertySource0.addPropertySource(compositePropertySource1);
HashMap<String, Object> map1 = new HashMap<>();
map1.put(map1Key0, propertyMap.get(map1Key0));
MapPropertySource mapPropertySource1 = new MapPropertySource("mapPropertySource1", map1);
compositePropertySource1.addPropertySource(mapPropertySource1);
HashMap<String, Object> map2 = new HashMap<>();
map2.put(map2Key0, propertyMap.get(map2Key0));
MapPropertySource mapPropertySource2 = new MapPropertySource("mapPropertySource2", map2);
compositePropertySource1.addPropertySource(mapPropertySource2);
compositePropertySource1.addPropertySource(Mockito.mock(JndiPropertySource.class));
HashMap<String, Object> map3 = new HashMap<>();
map3.put(map3Key0, propertyMap.get(map3Key0));
MapPropertySource mapPropertySource3 = new MapPropertySource("mapPropertySource3", map3);
compositePropertySource0.addPropertySource(mapPropertySource3);
Mockito.when(environment.getPropertySources()).thenReturn(propertySources);
Mockito.doAnswer((Answer<String>) invocation -> {
Object[] args = invocation.getArguments();
String propertyName = (String) args[0];
if ("spring.config.name".equals(propertyName) || "spring.application.name".equals(propertyName)) {
return null;
}
String value = propertyMap.get(propertyName);
if (null == value) {
fail("get unexpected property name: " + propertyName);
}
return value;
}).when(environment).getProperty(anyString(), Matchers.eq(Object.class));
new ConfigurationSpringInitializer().setEnvironment(environment);
Map<String, Map<String, Object>> extraLocalConfig = getExtraConfigMapFromConfigUtil();
assertFalse(extraLocalConfig.isEmpty());
Map<String, Object> extraProperties = extraLocalConfig.get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + environment.getClass().getName() + "@" + environment.hashCode());
assertNotNull(extraLocalConfig);
for (Entry<String, String> entry : propertyMap.entrySet()) {
assertEquals(entry.getValue(), extraProperties.get(entry.getKey()));
}
}
Aggregations