List of usage examples for org.springframework.core.env CompositePropertySource addFirstPropertySource
public void addFirstPropertySource(PropertySource<?> propertySource)
From source file:org.apache.servicecomb.config.TestConfigurationSpringInitializer.java
@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"); /*//w w w.j a v a2 s .c o m 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()); 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())); } }
From source file:org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.java
@Override @Retryable(interceptor = "configServerRetryInterceptor") public org.springframework.core.env.PropertySource<?> locate( org.springframework.core.env.Environment environment) { ConfigClientProperties properties = this.defaultProperties.override(environment); CompositePropertySource composite = new CompositePropertySource("configService"); RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(properties) : this.restTemplate; Exception error = null;/*from ww w. j av a2 s.co m*/ String errorBody = null; logger.info("Fetching config from server at: " + properties.getRawUri()); try { String[] labels = new String[] { "" }; if (StringUtils.hasText(properties.getLabel())) { labels = StringUtils.commaDelimitedListToStringArray(properties.getLabel()); } String state = ConfigClientStateHolder.getState(); // Try all the labels until one works for (String label : labels) { Environment result = getRemoteEnvironment(restTemplate, properties, label.trim(), state); if (result != null) { logger.info(String.format( "Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s", result.getName(), result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()), result.getLabel(), result.getVersion(), result.getState())); for (PropertySource source : result.getPropertySources()) { @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) source.getSource(); composite.addPropertySource(new MapPropertySource(source.getName(), map)); } if (StringUtils.hasText(result.getState()) || StringUtils.hasText(result.getVersion())) { HashMap<String, Object> map = new HashMap<>(); putValue(map, "config.client.state", result.getState()); putValue(map, "config.client.version", result.getVersion()); composite.addFirstPropertySource(new MapPropertySource("configClient", map)); } return composite; } } } catch (HttpServerErrorException e) { error = e; if (MediaType.APPLICATION_JSON.includes(e.getResponseHeaders().getContentType())) { errorBody = e.getResponseBodyAsString(); } } catch (Exception e) { error = e; } if (properties.isFailFast()) { throw new IllegalStateException( "Could not locate PropertySource and the fail fast property is set, failing", error); } logger.warn("Could not locate PropertySource: " + (errorBody == null ? error == null ? "label not found" : error.getMessage() : errorBody)); return null; }