Java Code Examples for org.springframework.core.env.MutablePropertySources#remove()

The following examples show how to use org.springframework.core.env.MutablePropertySources#remove() . 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: TestPropertySourceUtilsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile() {
	ConfigurableEnvironment environment = new MockEnvironment();

	MutablePropertySources propertySources = environment.getPropertySources();
	propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
	assertEquals(0, propertySources.size());

	String pair = "key = value";
	ByteArrayResource resource = new ByteArrayResource(pair.getBytes(), "from inlined property: " + pair);
	ResourceLoader resourceLoader = mock(ResourceLoader.class);
	given(resourceLoader.getResource(anyString())).willReturn(resource);

	addPropertiesFilesToEnvironment(environment, resourceLoader, FOO_LOCATIONS);
	assertEquals(1, propertySources.size());
	assertEquals("value", environment.getProperty("key"));
}
 
Example 2
Source File: TestPropertySourceUtilsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void addPropertiesFilesToEnvironmentWithSinglePropertyFromVirtualFile() {
	ConfigurableEnvironment environment = new MockEnvironment();

	MutablePropertySources propertySources = environment.getPropertySources();
	propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
	assertEquals(0, propertySources.size());

	String pair = "key = value";
	ByteArrayResource resource = new ByteArrayResource(pair.getBytes(), "from inlined property: " + pair);
	ResourceLoader resourceLoader = mock(ResourceLoader.class);
	when(resourceLoader.getResource(anyString())).thenReturn(resource);

	addPropertiesFilesToEnvironment(environment, resourceLoader, FOO_LOCATIONS);
	assertEquals(1, propertySources.size());
	assertEquals("value", environment.getProperty("key"));
}
 
Example 3
Source File: ConfigurationPropertySources.java    From spring-cloud-gray with Apache License 2.0 6 votes vote down vote up
/**
 * Attach a {@link ConfigurationPropertySource} support to the specified
 * {@link Environment}. Adapts each {@link PropertySource} managed by the environment
 * to a {@link ConfigurationPropertySource} and allows classic
 * {@link PropertySourcesPropertyResolver} calls to resolve using
 * {@link ConfigurationPropertyName configuration property names}.
 * <p>
 * The attached resolver will dynamically track any additions or removals from the
 * underlying {@link Environment} property sources.
 *
 * @param environment the source environment (must be an instance of
 *                    {@link ConfigurableEnvironment})
 * @see #get(Environment)
 */
public static void attach(Environment environment) {
    Assert.isInstanceOf(ConfigurableEnvironment.class, environment);
    MutablePropertySources sources = ((ConfigurableEnvironment) environment)
            .getPropertySources();
    PropertySource<?> attached = sources.get(ATTACHED_PROPERTY_SOURCE_NAME);
    if (attached != null && attached.getSource() != sources) {
        sources.remove(ATTACHED_PROPERTY_SOURCE_NAME);
        attached = null;
    }
    if (attached == null) {
        sources.addFirst(new ConfigurationPropertySourcesPropertySource(
                ATTACHED_PROPERTY_SOURCE_NAME,
                new SpringConfigurationPropertySources(sources)));
    }
}
 
Example 4
Source File: AbstractDaoTest.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Removes the re-loadable properties source from the environment. It must not have been removed already. It can be added back using the
 * addReloadablePropertySourceToEnvironment method.
 *
 * @throws Exception if the property source couldn't be removed.
 */
protected void removeReloadablePropertySourceFromEnvironment() throws Exception
{
    // If the property source is in the holding location, then it has already been removed from the environment so throw an exception since it
    // shouldn't be removed again (i.e. it should be re-added first and then possibly removed again if needed).
    if (propertySourceHoldingLocation != null)
    {
        throw new Exception("Reloadable property source has already been removed.");
    }

    MutablePropertySources mutablePropertySources = getMutablePropertySources();
    propertySourceHoldingLocation = (ReloadablePropertySource) mutablePropertySources.remove(ReloadablePropertySource.class.getName());

    // Verify that the property source was removed and returned.
    if (propertySourceHoldingLocation == null)
    {
        throw new Exception("Property source with name \"" + ReloadablePropertySource.class.getName() +
            "\" is not configured and couldn't be removed from the environment.");
    }
}
 
Example 5
Source File: AbstractDaoTest.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Restores the re-loadable property source back into the environment. It must have first been removed using the modifyPropertySourceInEnvironment method.
 *
 * @throws Exception if the property source wasn't previously removed or couldn't be re-added.
 */
protected void restorePropertySourceInEnvironment() throws Exception
{
    // If the property source isn't in the holding area, then it hasn't yet been removed from the environment so throw an exception informing the
    // caller that it first needs to be removed before it can be added back in.
    if (propertySourceHoldingLocation == null)
    {
        throw new Exception("Reloadable property source hasn't yet been removed so it can not be re-added.");
    }

    // Remove the modified map
    MutablePropertySources mutablePropertySources = getMutablePropertySources();
    mutablePropertySources.remove(OVERRIDE_PROPERTY_SOURCE_MAP_NAME);

    // Re-add in the property source we previously removed.
    getMutablePropertySources().addLast(propertySourceHoldingLocation);

    // Remove the property source so we know it was re-added.
    propertySourceHoldingLocation = null;
}
 
Example 6
Source File: BootstrapApplicationListener.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
private void mergeAdditionalPropertySources(MutablePropertySources environment,
		MutablePropertySources bootstrap) {
	PropertySource<?> defaultProperties = environment.get(DEFAULT_PROPERTIES);
	ExtendedDefaultPropertySource result = defaultProperties instanceof ExtendedDefaultPropertySource
			? (ExtendedDefaultPropertySource) defaultProperties
			: new ExtendedDefaultPropertySource(DEFAULT_PROPERTIES,
					defaultProperties);
	for (PropertySource<?> source : bootstrap) {
		if (!environment.contains(source.getName())) {
			result.add(source);
		}
	}
	for (String name : result.getPropertySourceNames()) {
		bootstrap.remove(name);
	}
	addOrReplace(environment, result);
	addOrReplace(bootstrap, result);
}
 
Example 7
Source File: IbisApplicationInitializer.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Override
protected WebApplicationContext createWebApplicationContext(ServletContext servletContext) {
	System.setProperty(EndpointImpl.CHECK_PUBLISH_ENDPOINT_PERMISSON_PROPERTY_WITH_SECURITY_MANAGER, "false");
	servletContext.log("Starting IBIS WebApplicationInitializer");

	XmlWebApplicationContext applicationContext = new XmlWebApplicationContext();
	applicationContext.setConfigLocation(XmlWebApplicationContext.CLASSPATH_URL_PREFIX + "/webApplicationContext.xml");
	applicationContext.setDisplayName("IbisApplicationInitializer");

	MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
	propertySources.remove(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
	propertySources.remove(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
	propertySources.addFirst(new PropertiesPropertySource("ibis", AppConstants.getInstance()));

	return applicationContext;
}
 
Example 8
Source File: TestPropertySourceUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void addInlinedPropertiesToEnvironmentWithEmptyProperty() {
	ConfigurableEnvironment environment = new MockEnvironment();
	MutablePropertySources propertySources = environment.getPropertySources();
	propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
	assertEquals(0, propertySources.size());
	addInlinedPropertiesToEnvironment(environment, asArray("  "));
	assertEquals(1, propertySources.size());
	assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size());
}
 
Example 9
Source File: TestPropertySourceUtilsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void addInlinedPropertiesToEnvironmentWithEmptyProperty() {
	ConfigurableEnvironment environment = new MockEnvironment();
	MutablePropertySources propertySources = environment.getPropertySources();
	propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
	assertEquals(0, propertySources.size());
	addInlinedPropertiesToEnvironment(environment, asArray("  "));
	assertEquals(1, propertySources.size());
	assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size());
}
 
Example 10
Source File: PropertySourcesProcessor.java    From apollo with Apache License 2.0 5 votes vote down vote up
private void ensureBootstrapPropertyPrecedence(ConfigurableEnvironment environment) {
  MutablePropertySources propertySources = environment.getPropertySources();

  PropertySource<?> bootstrapPropertySource = propertySources
      .get(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);

  // not exists or already in the first place
  if (bootstrapPropertySource == null || propertySources.precedenceOf(bootstrapPropertySource) == 0) {
    return;
  }

  propertySources.remove(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
  propertySources.addFirst(bootstrapPropertySource);
}
 
Example 11
Source File: TestPropertySourceUtilsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * @since 4.1.5
 */
@Test
@SuppressWarnings("rawtypes")
public void addInlinedPropertiesToEnvironmentWithEmptyProperty() {
	ConfigurableEnvironment environment = new MockEnvironment();
	MutablePropertySources propertySources = environment.getPropertySources();
	propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
	assertEquals(0, propertySources.size());
	addInlinedPropertiesToEnvironment(environment, new String[] { "  " });
	assertEquals(1, propertySources.size());
	assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size());
}
 
Example 12
Source File: WireMockApplicationListener.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
private void addPropertySource(MutablePropertySources propertySources) {
	if (!propertySources.contains("wiremock")) {
		propertySources.addFirst(
				new MapPropertySource("wiremock", new HashMap<String, Object>()));
	}
	else {
		// Move it up into first place
		PropertySource<?> wiremock = propertySources.remove("wiremock");
		propertySources.addFirst(wiremock);
	}
}
 
Example 13
Source File: IbisApplicationContext.java    From iaf with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the Spring Application Context when ran from the command line.
 * @throws BeansException when the Context fails to initialize
 */
private ClassPathXmlApplicationContext createClassPathApplicationContext() throws BeansException {
	ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext();

	MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
	propertySources.remove(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
	propertySources.remove(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
	propertySources.addFirst(new PropertiesPropertySource("ibis", APP_CONSTANTS));
	applicationContext.setConfigLocations(getSpringConfigurationFiles(applicationContext.getClassLoader()));

	return applicationContext;
}