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:com.home.ln_spring.ch5.env.EnvironmentSample.java

public static void main(String[] args) {
    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    ctx.refresh();// ww w .j a  v  a 2s .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")));
                    }//from w  ww.  j a v a2s. 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:com.hillert.botanic.MainApp.java

/**
 * Main class initializes the Spring Application Context and populates seed
 * data using {@link SeedDataService}./*from www .  j a va  2 s  . com*/
 *
 * @param args Not used.
 */
public static void main(String[] args) {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put("redisPort", SocketUtils.findAvailableTcpPort());

    SpringApplication app = new SpringApplication(MainApp.class);
    app.setDefaultProperties(props);

    ConfigurableApplicationContext context = app.run(args);

    MapPropertySource propertySource = new MapPropertySource("ports", props);

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

    SeedDataService seedDataService = context.getBean(SeedDataService.class);
    seedDataService.populateSeedData();
}

From source file:com.cloudera.director.aws.common.PropertyResolvers.java

/**
 * Creates a new property resolver that gets properties from the given map.
 *
 * @param m property map//w w w.j  ava 2s. co m
 * @return new property resolver
 * @throws NullPointerException if the map is null
 */
public static PropertyResolver newMapPropertyResolver(Map<String, String> m) {
    checkNotNull(m, "map is null");
    MutablePropertySources sources = new MutablePropertySources();
    sources.addFirst(new MapPropertySource("map", ImmutableMap.<String, Object>copyOf(m)));
    return new PropertySourcesPropertyResolver(sources);
}

From source file:com.example.autoconfigure.listener.ExampleApplicationListener.java

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    System.err.println("Adding extra properties");
    PropertySource<?> propertySource = new MapPropertySource("example",
            Collections.singletonMap("example.secret", "the-secret-key"));
    event.getEnvironment().getPropertySources().addFirst(propertySource);
}

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

public static PropertySource getSystemProperties() {
    StandardEnvironment env = new StandardEnvironment();
    return new MapPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME,
            env.getSystemProperties());/*  w  ww.  ja  v  a2s.  co  m*/
}

From source file:org.cloudfoundry.identity.uaa.config.EnvironmentPropertiesFactoryBeanTests.java

@Test
public void testNullProperties() throws Exception {
    EnvironmentPropertiesFactoryBean factory = new EnvironmentPropertiesFactoryBean();
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources()//from   w  w w  .  j  a v  a 2s  .  c o  m
            .addFirst(new MapPropertySource("foo", Collections.singletonMap("foo", null)));
    factory.setEnvironment(environment);
    Properties properties = factory.getObject();
    assertEquals("", properties.get("foo"));
}

From source file:com.tcp.client.Main.java

public static GenericXmlApplicationContext setupContext() throws InterruptedException, IOException {

    int availableServerSocket = 5682;
    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

    System.out.println("===== Player 2 =====");

    while (true) {
        try {/*from  w  w  w.  ja  va2 s .co  m*/
            ServerSocket s = new ServerSocket(availableServerSocket);
            s.close();
            System.out.println("Player 1 is unavailable.. trying again!!");
            Thread.sleep(2000);

        } catch (IOException e) {
            System.out.println("Found player 1!!");
            break;
        }
    }

    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);
    context.load("classpath:META-INF/context.xml");
    context.registerShutdownHook();
    context.refresh();

    return context;
}

From source file:ReproTests.java

/**
 * Work around the lifecycle issues above by introducing a PropertySource containing
 * the value for the "resourceDirPlaceHolder"
 *//*from w ww.  j  ava2  s  .co m*/
@Test
public void workaround() {
    Map<String, Object> localProps = new HashMap<String, Object>();
    localProps.put("resourceDirPlaceHolder", "myResourceDir");

    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    ctx.getEnvironment().getPropertySources().addFirst(new MapPropertySource("localProps", localProps));
    ctx.load("ReproTests-workaround.xml");
    ctx.refresh();
}

From source file:io.pivotal.spring.cloud.service.eureka.EurekaServiceConnector.java

@Override
protected PropertySource<?> toPropertySource(EurekaServiceInfo eurekaServiceInfo) {
    Map<String, Object> map = new LinkedHashMap<>();
    map.put(EUREKA_CLIENT + "serviceUrl.defaultZone", eurekaServiceInfo.getUri() + EUREKA_API_PREFIX);
    map.put(EUREKA_CLIENT + "region", DEFAULT_REGION);
    map.put(EUREKA_CLIENT_OAUTH2 + "clientId", eurekaServiceInfo.getClientId());
    map.put(EUREKA_CLIENT_OAUTH2 + "clientSecret", eurekaServiceInfo.getClientSecret());
    map.put(EUREKA_CLIENT_OAUTH2 + "accessTokenUri", eurekaServiceInfo.getAccessTokenUri());
    return new MapPropertySource(PROPERTY_SOURCE_NAME, map);
}