Java Code Examples for org.springframework.core.env.CompositePropertySource#addFirstPropertySource()

The following examples show how to use org.springframework.core.env.CompositePropertySource#addFirstPropertySource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BootstrapConfigurationTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Override
public PropertySource<?> locate(Environment environment) {
	if (this.name != null) {
		then(this.name)
				.isEqualTo(environment.getProperty("spring.application.name"));
	}
	if (this.fail) {
		throw new RuntimeException("Planned");
	}
	CompositePropertySource compositePropertySource = new CompositePropertySource(
			"listTestBootstrap");
	compositePropertySource.addFirstPropertySource(
			new MapPropertySource("testBootstrap1", MAP1));
	compositePropertySource.addFirstPropertySource(
			new MapPropertySource("testBootstrap2", MAP2));
	return compositePropertySource;
}
 
Example 2
Source File: ReleaseMessage.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
public CompositePropertySource convertCompositePropertySource(String sourceName) {
	CompositePropertySource composite = new CompositePropertySource(sourceName);
	for (ReleasePropertySource ps : getPropertySources()) {
		// See:org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration
		composite.addFirstPropertySource(ps.convertMapPropertySource());
	}
	return composite;
}
 
Example 3
Source File: NacosPropertySourceLocator.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
/**
 * Add the nacos configuration to the first place and maybe ignore the empty
 * configuration.
 */
private void addFirstPropertySource(final CompositePropertySource composite,
		NacosPropertySource nacosPropertySource, boolean ignoreEmpty) {
	if (null == nacosPropertySource || null == composite) {
		return;
	}
	if (ignoreEmpty && nacosPropertySource.getSource().isEmpty()) {
		return;
	}
	composite.addFirstPropertySource(nacosPropertySource);
}
 
Example 4
Source File: ConfigMapPropertySourceLocator.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
private void addPropertySourceIfNeeded(
		Function<String, Map<String, Object>> contentToMapFunction, String content,
		String name, CompositePropertySource composite) {

	Map<String, Object> map = new HashMap<>();
	map.putAll(contentToMapFunction.apply(content));
	if (map.isEmpty()) {
		LOG.warn("Property source: " + name
				+ "will be ignored because no properties could be found");
	}
	else {
		composite.addFirstPropertySource(new MapPropertySource(name, map));
	}
}
 
Example 5
Source File: SecretsPropertySourceLocator.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
private void readFile(Path path, CompositePropertySource composite) {
	try {
		Map<String, Object> result = new HashMap<>();
		result.put(path.getFileName().toString(),
				new String(Files.readAllBytes(path)).trim());
		if (!result.isEmpty()) {
			composite.addFirstPropertySource(new MapPropertySource(
					path.getFileName().toString().toLowerCase(), result));
		}
	}
	catch (IOException e) {
		LOG.warn("Error reading properties file", e);
	}
}
 
Example 6
Source File: EnvironmentUtilsTest.java    From dubbo-spring-boot-project with Apache License 2.0 4 votes vote down vote up
@Test
public void testExtraProperties() {

    System.setProperty("user.name", "mercyblitz");

    StandardEnvironment environment = new StandardEnvironment();

    Map<String, Object> map = new HashMap<>();

    map.put("user.name", "Mercy");

    MapPropertySource propertySource = new MapPropertySource("first", map);

    CompositePropertySource compositePropertySource = new CompositePropertySource("comp");

    compositePropertySource.addFirstPropertySource(propertySource);

    MutablePropertySources propertySources = environment.getPropertySources();

    propertySources.addFirst(compositePropertySource);

    Map<String, Object> properties = EnvironmentUtils.extractProperties(environment);

    Assert.assertEquals("Mercy", properties.get("user.name"));

}
 
Example 7
Source File: EnvironmentUtilsTest.java    From dubbo-spring-boot-project with Apache License 2.0 4 votes vote down vote up
@Test
public void testExtraProperties() {

    System.setProperty("user.name", "mercyblitz");

    StandardEnvironment environment = new StandardEnvironment();

    Map<String, Object> map = new HashMap<>();

    map.put("user.name", "Mercy");

    MapPropertySource propertySource = new MapPropertySource("first", map);

    CompositePropertySource compositePropertySource = new CompositePropertySource("comp");

    compositePropertySource.addFirstPropertySource(propertySource);

    MutablePropertySources propertySources = environment.getPropertySources();

    propertySources.addFirst(compositePropertySource);

    Map<String, Object> properties = EnvironmentUtils.extractProperties(environment);

    Assert.assertEquals("Mercy", properties.get("user.name"));

}
 
Example 8
Source File: TestConfigurationSpringInitializer.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@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()));
  }
}