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

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

Introduction

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

Prototype

StandardEnvironment

Source Link

Usage

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  w w  .ja v a2 s . co 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:piecework.client.LoadTester.java

public static final void main(String[] args) throws Exception {
    String profile = args.length > 0 ? args[0] : "workstation";

    StandardEnvironment environment = new StandardEnvironment();
    environment.setActiveProfiles(profile);
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.setEnvironment(environment);//from   ww w  .j  a v a2s  .  c  o  m
    ctx.register(ClientConfiguration.class);
    ctx.refresh();

    KeyManagerCabinet cabinet = ctx.getBean(KeyManagerCabinet.class);
    SecuritySettings securitySettings = ctx.getBean(SecuritySettings.class);

    LoadTester loadTester = new LoadTester(cabinet.getKeystore(), securitySettings);
    loadTester.retrieveAllTasks();
}

From source file:org.wallride.autoconfigure.WallRideInitializer.java

public static ConfigurableEnvironment createEnvironment(ApplicationStartedEvent event) {
    StandardEnvironment environment = new StandardEnvironment();

    String home = environment.getProperty(WallRideProperties.HOME_PROPERTY);
    if (!StringUtils.hasText(home)) {
        throw new IllegalStateException(WallRideProperties.HOME_PROPERTY + " is empty");
    }//from w  ww. j a v  a  2s  . com
    if (!home.endsWith("/")) {
        home = home + "/";
    }

    String config = home + WallRideProperties.DEFAULT_CONFIG_PATH_NAME;
    String media = home + WallRideProperties.DEFAULT_MEDIA_PATH_NAME;

    System.setProperty(WallRideProperties.CONFIG_LOCATION_PROPERTY, config);
    System.setProperty(WallRideProperties.MEDIA_LOCATION_PROPERTY, media);

    event.getSpringApplication().getListeners().stream()
            .filter(listener -> listener.getClass().isAssignableFrom(ConfigFileApplicationListener.class))
            .map(listener -> (ConfigFileApplicationListener) listener)
            .forEach(listener -> listener.setSearchLocations(DEFAULT_CONFIG_SEARCH_LOCATIONS + "," + config));

    return environment;
}

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());//from  w  w w  . j a v a 2  s  . 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()/* w  w  w  .j a  va2  s  .co m*/
            .addFirst(new MapPropertySource("foo", Collections.singletonMap("foo", null)));
    factory.setEnvironment(environment);
    Properties properties = factory.getObject();
    assertEquals("", properties.get("foo"));
}

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

public static PropertySource getSystemEnvironmentProperties() {
    StandardEnvironment env = new StandardEnvironment();
    return new SystemEnvironmentPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
            env.getSystemEnvironment());
}

From source file:com.springsource.greenhouse.database.GreenhouseDatabaseInstallerTest.java

@Test
public void runUpgrader() {
    EmbeddedDatabaseFactory factory = new EmbeddedDatabaseFactory();
    factory.setDatabaseType(EmbeddedDatabaseType.H2);
    EmbeddedDatabase db = factory.getDatabase();
    System.setProperty("security.encryptPassword", "foo");
    System.setProperty("security.encryptSalt",
            new String(Hex.encode(KeyGenerators.secureRandom().generateKey())));
    DatabaseUpgrader installer = new DatabaseUpgrader(db, new StandardEnvironment(), Encryptors.noOpText());
    installer.run();//w  ww .ja v  a  2 s . c  om
    installer.run();
    DatabaseUpgrader installer2 = new DatabaseUpgrader(db, new StandardEnvironment(), Encryptors.noOpText());
    installer2.run();
}

From source file:com.kixeye.chassis.support.test.eureka.metadata.MetadataCollectorTest.java

@Before
public void setup() {
    ConfigurationManager.getConfigInstance().setProperty("chassis.eureka.disable", "true");
    ConfigurationManager.getConfigInstance().setProperty("eureka.metadata.prop1", "propValue");
    ConfigurationManager.getConfigInstance().setProperty("eureka.datacenter", "default");

    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new ArchaiusSpringPropertySource());
    context = new AnnotationConfigApplicationContext();
    context.setEnvironment(environment);
    context.register(MetadataCollectorConfiguration.class);
    context.refresh();/*from   ww  w .j  av a 2 s .c o  m*/
    context.start();
}

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

@Test
public void testPlaceholderProperties() throws Exception {
    EnvironmentMapFactoryBean factory = new EnvironmentMapFactoryBean();
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources()//from w w w  .ja v  a2  s  .c  o  m
            .addLast(new NestedMapPropertySource("override", getProperties("bar=${spam}")));
    factory.setEnvironment(environment);
    factory.setDefaultProperties(getProperties("foo=baz"));
    Map<String, ?> properties = factory.getObject();
    assertEquals("baz", properties.get("foo"));
    assertEquals("${spam}", properties.get("bar"));
}

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   www.  j a v a2s. c  o m

    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);
}