Example usage for org.springframework.core.env MapPropertySource MapPropertySource

List of usage examples for org.springframework.core.env MapPropertySource MapPropertySource

Introduction

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

Prototype

public MapPropertySource(String name, Map<String, Object> source) 

Source Link

Usage

From source file:io.pivotal.spring.cloud.service.config.ConfigServerServiceConnector.java

@Override
protected PropertySource<?> toPropertySource(ConfigServerServiceInfo serviceInfo) {
    Map<String, Object> properties = new LinkedHashMap<>();
    properties.put(SPRING_CLOUD_CONFIG_URI, serviceInfo.getUri());
    properties.put(SPRING_CLOUD_CONFIG_OAUTH2_CLIENT_CLIENT_ID, serviceInfo.getClientId());
    properties.put(SPRING_CLOUD_CONFIG_OAUTH2_CLIENT_CLIENT_SECRET, serviceInfo.getClientSecret());
    properties.put(SPRING_CLOUD_CONFIG_OAUTH2_CLIENT_ACCESS_TOKEN_URI, serviceInfo.getAccessTokenUri());
    return new MapPropertySource(PROPERTY_SOURCE_NAME, properties);
}

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));
    }//  w  w w.  j a  v a 2s.  com
}

From source file:org.musa.tcpserver.Main.java

public static GenericXmlApplicationContext setupContext() {
    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

    System.out.print("Detect open server socket...");
    int availableServerSocket = SocketUtils.findAvailableServerSocket(5678);

    final Map<String, Object> sockets = new HashMap<String, Object>();
    sockets.put("availableServerSocket", availableServerSocket);

    final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);

    context.getEnvironment().getPropertySources().addLast(propertySource);

    System.out.println("using port " + context.getEnvironment().getProperty("availableServerSocket"));

    context.load("classpath:META-INF/spring/integration/serverContext.xml");

    context.registerShutdownHook();/*from  www . j  av  a  2s. com*/
    context.refresh();

    return context;
}

From source file:io.micrometer.spring.MetricsEnvironmentPostProcessor.java

private void addDefaultProperty(ConfigurableEnvironment environment, String name, String value) {
    MutablePropertySources sources = environment.getPropertySources();
    Map<String, Object> map = null;
    if (sources.contains("defaultProperties")) {
        PropertySource<?> source = sources.get("defaultProperties");
        if (source instanceof MapPropertySource) {
            map = ((MapPropertySource) source).getSource();
        }/*from  w ww.j a  va 2s . co m*/
    } else {
        map = new LinkedHashMap<>();
        sources.addLast(new MapPropertySource("defaultProperties", map));
    }
    if (map != null) {
        map.put(name, value);
    }
}

From source file:org.greencheek.utils.environment.propertyplaceholder.spring.EnvironmentalPropertySourcesPlaceholderConfigurerWithSpringValueResolution.java

@Override
public void afterPropertiesSet() {
    if (mergerBuilder == null) {
        throw new IllegalStateException("The PropertyMergerBuilder must not be null.");
    }//from w w w  .j  a  v a 2s .  c om

    Properties p = mergerBuilder.build().getMergedProperties();

    StandardEnvironment env = new StandardEnvironment();
    MutablePropertySources sources = new MutablePropertySources();

    if (isSystemPropertiesResolutionEnabled())
        sources.addLast(new MapPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME,
                env.getSystemProperties()));
    if (isEnvironmentPropertiesResolutionEnabled())
        sources.addLast(new SystemEnvironmentPropertySource(
                StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, env.getSystemEnvironment()));

    sources.addFirst(new PropertiesPropertySource(ENVIRONMENT_SPECIFIC_PROPERTIES, p));
    super.setPropertySources(sources);
}

From source file:io.spring.initializr.web.autoconfigure.CloudfoundryEnvironmentPostProcessor.java

private static void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) {
    MapPropertySource target = null;/*from ww  w. j a v a2  s.c  o m*/
    if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
        PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
        if (source instanceof MapPropertySource) {
            target = (MapPropertySource) source;
            for (String key : map.keySet()) {
                if (!target.containsProperty(key)) {
                    target.getSource().put(key, map.get(key));
                }
            }
        }
    }
    if (target == null) {
        target = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
    }
    if (!propertySources.contains(PROPERTY_SOURCE_NAME)) {
        propertySources.addLast(target);
    }
}

From source file:com.example.journal.env.JournalEnvironmentPostProcessor.java

private void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) {
    MapPropertySource target = null;// w w w .ja  va2  s  .com
    if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
        PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
        if (source instanceof MapPropertySource) {
            target = (MapPropertySource) source;
            for (String key : map.keySet()) {
                if (!target.containsProperty(key)) {
                    target.getSource().put(key, map.get(key));
                }
            }
        }
    }
    if (target == null) {
        target = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
    }
    if (!propertySources.contains(PROPERTY_SOURCE_NAME)) {
        propertySources.addLast(target);
    }
}

From source file:com.avanza.astrix.integration.tests.GsRemotingTest.java

@Test
public void routedServiceInvocationThrowServiceUnavailableWhenProxyIsContextIsClosed() throws Exception {
    AnnotationConfigApplicationContext pingServer = autoClosables.add(new AnnotationConfigApplicationContext());
    pingServer.register(PingAppConfig.class);
    pingServer.getEnvironment().getPropertySources()
            .addFirst(new MapPropertySource("props", new HashMap<String, Object>() {
                {/*w ww  . j  a  v  a2 s .  co m*/
                    put("serviceRegistryUri", serviceRegistry.getServiceUri());
                }
            }));
    pingServer.refresh();

    AstrixContext context = autoClosables.add(new TestAstrixConfigurer().registerApiProvider(PingApi.class)
            .set(AstrixSettings.SERVICE_REGISTRY_URI, serviceRegistry.getServiceUri())
            .set(AstrixSettings.BEAN_BIND_ATTEMPT_INTERVAL, 200).configure());
    Ping ping = context.waitForBean(Ping.class, 10000);

    assertEquals("foo", ping.ping("foo"));

    context.destroy();

    assertThrows(() -> ping.ping("foo"), ServiceUnavailableException.class);
}

From source file:org.ventiv.webjars.requirejs.config.RequireJsConfigBuilderTest.java

@Test
public void overridePath() throws Exception {
    Map<String, Object> properties = new HashMap<>();
    properties.put("webjars.requirejs.newModules", "kendo-angular,kendo-colorpicker");
    properties.put("webjars.requirejs.paths.kendo-ui-core", "js/kendo.core.min");
    properties.put("webjars.requirejs.paths.kendo-angular",
            "/webjars/kendo-ui-core/2014.2.716/js/kendo.angular.min");
    properties.put("webjars.requirejs.paths.kendo-colorpicker",
            "/webjars/kendo-ui-core/2014.2.716/js/kendo.colorpicker.min");

    StandardEnvironment env = new StandardEnvironment();
    env.getPropertySources().addFirst(new MapPropertySource("props", properties));

    RequireJsConfigBuilder builder = new RequireJsConfigBuilder("/webjars/", env);
    Map<String, Object> requireJsConfig = builder.buildConfig();

    Map<String, String> paths = (Map<String, String>) requireJsConfig.get("paths");
    assertEquals(paths.size(), 170);/*from  w w  w.  j  a v a2s .co m*/
    assertEquals(paths.get("kendo-ui-core"), "/webjars/kendo-ui-core/2014.2.716/js/kendo.core.min");
    assertEquals(paths.get("kendo-angular"), "/webjars/kendo-ui-core/2014.2.716/js/kendo.angular.min");
    assertEquals(paths.get("kendo-colorpicker"), "/webjars/kendo-ui-core/2014.2.716/js/kendo.colorpicker.min");
}

From source file:com.richard.memorystore.udp.UDPServer.java

public static GenericXmlApplicationContext setupContext() {
    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

    System.out.print("Detect open server socket...");
    int availableServerSocket = SocketUtils.findAvailableServerSocket(11111);

    final Map<String, Object> sockets = new HashMap<String, Object>();
    sockets.put("availableServerSocket", availableServerSocket);

    final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);

    context.getEnvironment().getPropertySources().addLast(propertySource);

    System.out.println("using port " + context.getEnvironment().getProperty("availableServerSocket"));

    context.load("classpath:udpServer.xml");
    context.registerShutdownHook();//from w w w .j a v  a2  s. co m
    context.refresh();

    return context;
}