Example usage for org.springframework.core.env MutablePropertySources remove

List of usage examples for org.springframework.core.env MutablePropertySources remove

Introduction

In this page you can find the example usage for org.springframework.core.env MutablePropertySources remove.

Prototype

@Nullable
public PropertySource<?> remove(String name) 

Source Link

Document

Remove and return the property source with the given name, null if not found.

Usage

From source file:ch.sdi.core.TestUtils.java

/**
 * Removes all key/value pairs from environment
 * @param aEnv//from  ww w .j a  v a2 s . c om
 */
public static void removeAllFromEnvironment(ConfigurableEnvironment aEnv) {
    MutablePropertySources propertySources = aEnv.getPropertySources();
    List<PropertySource<?>> toRemove = new ArrayList<PropertySource<?>>();
    propertySources.forEach(p -> toRemove.add(p));
    toRemove.forEach(p -> propertySources.remove(p.getName()));
}

From source file:nl.nn.adapterframework.configuration.IbisContext.java

/**
 * Create Spring Bean factory. Parameter 'springContext' can be null.
 *
 * Create the Spring Bean Factory using the supplied <code>springContext</code>,
 * if not <code>null</code>.
 *
 * @param springContext Spring Context to create. If <code>null</code>,
 * use the default spring context.//w  ww. ja v  a2  s.c  o  m
 * The spring context is loaded as a spring ClassPathResource from
 * the class path.
 *
 * @return The Spring XML Bean Factory.
 * @throws BeansException If the Factory can not be created.
 *
 */
private ApplicationContext createApplicationContext() throws BeansException {
    // Reading in Spring Context
    long start = System.currentTimeMillis();
    String springContext = "/springContext.xml";
    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.setConfigLocation(springContext);
    applicationContext.refresh();
    log("startup " + springContext + " in " + (System.currentTimeMillis() - start) + " ms");
    return applicationContext;
}

From source file:org.finra.dm.dao.AbstractDaoTest.java

/**
 * 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.
 *///from w  ww  .j a va 2  s. c  om
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.");
    }
}

From source file:org.finra.dm.dao.AbstractDaoTest.java

/**
 * 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.
 *//*  ww  w . j av  a 2  s  .c om*/
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;
}

From source file:org.springframework.boot.SpringApplication.java

private void removeAllPropertySources(MutablePropertySources propertySources) {
    Set<String> names = new HashSet<String>();
    for (PropertySource<?> propertySource : propertySources) {
        names.add(propertySource.getName());
    }/*  ww  w .  j  a  v  a2 s  . co m*/
    for (String name : names) {
        propertySources.remove(name);
    }
}