Example usage for org.springframework.core.env ConfigurableEnvironment getPropertySources

List of usage examples for org.springframework.core.env ConfigurableEnvironment getPropertySources

Introduction

In this page you can find the example usage for org.springframework.core.env ConfigurableEnvironment getPropertySources.

Prototype

MutablePropertySources getPropertySources();

Source Link

Document

Return the PropertySources for this Environment in mutable form, allowing for manipulation of the set of PropertySource objects that should be searched when resolving properties against this Environment object.

Usage

From source file:com.home.ln_spring.ch5.env.EnvironmentSample.java

public static void main(String[] args) {
    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    ctx.refresh();//  www .  j a v a 2  s.co  m

    ConfigurableEnvironment env = ctx.getEnvironment();
    MutablePropertySources propertySources = env.getPropertySources();
    Map appMap = new HashMap();
    appMap.put("user.home", "/home/vitaliy/NetBeansProjects/Ln_Spring");
    propertySources.addFirst(new MapPropertySource("SPRING3_MAP", appMap));

    System.out.println("user.home: " + System.getProperty("user.home"));
    System.out.println("JAVA_HOME: " + System.getProperty("JAVA_HOME"));

    System.out.println("user.home: " + env.getProperty("user.home"));
    System.out.println("JAVA_HOME: " + env.getProperty("JAVA_HOME"));
}

From source file:uk.ac.kcl.Main.java

public static void main(String[] args) {
    File folder = new File(args[0]);
    File[] listOfFiles = folder.listFiles();
    assert listOfFiles != null;
    for (File listOfFile : listOfFiles) {
        if (listOfFile.isFile()) {
            if (listOfFile.getName().endsWith(".properties")) {
                System.out.println("Properties sile found:" + listOfFile.getName()
                        + ". Attempting to launch application context");
                Properties properties = new Properties();
                InputStream input;
                try {
                    input = new FileInputStream(listOfFile);
                    properties.load(input);
                    if (properties.getProperty("globalSocketTimeout") != null) {
                        TcpHelper.setSocketTimeout(
                                Integer.valueOf(properties.getProperty("globalSocketTimeout")));
                    }/* w ww  .  j  ava 2 s .c o m*/
                    Map<String, Object> map = new HashMap<>();
                    properties.forEach((k, v) -> {
                        map.put(k.toString(), v);
                    });
                    ConfigurableEnvironment environment = new StandardEnvironment();
                    MutablePropertySources propertySources = environment.getPropertySources();
                    propertySources.addFirst(new MapPropertySource(listOfFile.getName(), map));
                    @SuppressWarnings("resource")
                    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
                    ctx.registerShutdownHook();
                    ctx.setEnvironment(environment);
                    String scheduling;
                    try {
                        scheduling = properties.getProperty("scheduler.useScheduling");
                        if (scheduling.equalsIgnoreCase("true")) {
                            ctx.register(ScheduledJobLauncher.class);
                            ctx.refresh();
                        } else if (scheduling.equalsIgnoreCase("false")) {
                            ctx.register(SingleJobLauncher.class);
                            ctx.refresh();
                            SingleJobLauncher launcher = ctx.getBean(SingleJobLauncher.class);
                            launcher.launchJob();
                        } else if (scheduling.equalsIgnoreCase("slave")) {
                            ctx.register(JobConfiguration.class);
                            ctx.refresh();
                        } else {
                            throw new RuntimeException(
                                    "useScheduling not configured. Must be true, false or slave");
                        }
                    } catch (NullPointerException ex) {
                        throw new RuntimeException(
                                "useScheduling not configured. Must be true, false or slave");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

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

/**
 * Removes all key/value pairs from environment
 * @param aEnv//from  ww  w. j  av a  2s  .  c  o m
 */
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:ch.sdi.core.TestUtils.java

/**
 * Outputs the environment to log output
 * @param aEnv//from ww  w  .j a  v a  2s .co m
 */
public static void debugPropertySources(ConfigurableEnvironment aEnv) {
    MutablePropertySources propertySources = aEnv.getPropertySources();
    for (PropertySource<?> propertySource : propertySources) {
        myLog.debug("PropertySource: " + propertySource);
    }
}

From source file:io.gravitee.common.util.EnvironmentUtils.java

public static Map<String, Object> getAllProperties(ConfigurableEnvironment aEnv) {
    Map<String, Object> result = new HashMap<>();
    aEnv.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps)));
    return result;
}

From source file:zipkin.sparkstreaming.autoconfigure.consumer.storage.ZipkinStorageConsumerAutoConfiguration.java

static Properties extractZipkinProperties(ConfigurableEnvironment env) {
    Properties properties = new Properties();
    Iterator<PropertySource<?>> it = env.getPropertySources().iterator();
    while (it.hasNext()) {
        PropertySource<?> next = it.next();
        if (!(next instanceof EnumerablePropertySource))
            continue;
        EnumerablePropertySource source = (EnumerablePropertySource) next;
        for (String name : source.getPropertyNames()) {
            if (name.startsWith("zipkin"))
                properties.put(name, source.getProperty(name));
        }//from  w  w w . j  a v a 2s  .c om
    }
    return properties;
}

From source file:com.teradata.benchto.driver.utils.PropertiesUtils.java

public static <T> T resolveEnvironmentProperties(ConfigurableEnvironment environment, Class<T> clazz,
        String prefix) {//from   w  w w .  j a  v a  2s .  c  o m
    try {
        T properties = BeanUtils.instantiate(clazz);
        PropertiesConfigurationFactory<T> factory = new PropertiesConfigurationFactory<T>(properties);
        factory.setTargetName(prefix);
        factory.setPropertySources(environment.getPropertySources());
        factory.setConversionService(environment.getConversionService());
        factory.bindPropertiesToTarget();
        return properties;
    } catch (BindException ex) {
        throw new FatalBeanException("Could not bind " + clazz + " properties", ex);
    }
}

From source file:ch.sdi.core.impl.cfg.ConfigUtils.java

/**
 * Returns a collection of all property names in the environment.
 * <p>//from  ww  w .  j a va2s .  c  o m
 * @param aEnv
 * @return
 */
public static Collection<String> getAllPropertyNames(ConfigurableEnvironment aEnv) {
    Collection<String> result = new HashSet<>();
    aEnv.getPropertySources().forEach(ps -> result.addAll(getAllPropertyNames(ps)));
    return result;
}

From source file:io.lavagna.common.LavagnaEnvironment.java

private static void setSystemPropertyIfNull(ConfigurableEnvironment env, String name, String value) {
    if (!env.containsProperty(name) || StringUtils.isBlank(env.getProperty(name))) {
        LOG.warn("Property {} is not set, using default value: {}", name, value);
        Map<String, Object> source = Collections.singletonMap(name, (Object) value);
        env.getPropertySources().addFirst(new MapPropertySource(name, source));
    }//from   w  w w  .j a  va2 s .com
}

From source file:ch.sdi.core.impl.cfg.ConfigUtils.java

/**
 * Tries to retrieve the named PropertySource from the environment. If not found it creates a
 * new one./*from  w ww  .j a v a  2 s. c o  m*/
 * <p>
 *
 * @param aEnv
 * @param aPropertySourceName
 * @return the embedded property map
 * @throws SdiException if the found property source is not instance of PropertiesPropertySource
 */
public static Map<String, Object> getOrCreatePropertySource(ConfigurableEnvironment aEnv,
        String aPropertySourceName) throws SdiException {
    MutablePropertySources mps = aEnv.getPropertySources();
    PropertySource<?> ps = mps.get(aPropertySourceName);
    PropertiesPropertySource pps = null;

    if (ps == null) {
        Properties props = new Properties();
        pps = new PropertiesPropertySource(aPropertySourceName, props);
        mps.addFirst(pps);
    } else {
        if (!(ps instanceof PropertiesPropertySource)) {
            throw new SdiException("Found property source is not instance of PropertiesPropertySource "
                    + " but: " + ps.getClass().getName(), SdiException.EXIT_CODE_CONFIG_ERROR);
        } // if !( ps instanceof PropertiesPropertySource )

        pps = (PropertiesPropertySource) ps;
    }

    Map<String, Object> map = pps.getSource();
    return map;
}