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

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

Introduction

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

Prototype

@Nullable
String getProperty(String key);

Source Link

Document

Return the property value associated with the given key, or null if the key cannot be resolved.

Usage

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

public static void main(String[] args) {
    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    ctx.refresh();/*from   w  w  w . j  a  va 2 s  .c om*/

    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: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 .  ja  v a  2 s.c o  m*/
}

From source file:org.openbaton.autoscaling.utils.Utils.java

public static void loadExternalProperties(ConfigurableEnvironment properties) {
    if (properties.containsProperty("external-properties-file")
            && properties.getProperty("external-properties-file") != null) {
        try {/*  w ww.j  a  v  a 2  s . co m*/
            InputStream is = new FileInputStream(new File(properties.getProperty("external-properties-file")));
            Properties externalProperties = new Properties();
            externalProperties.load(is);
            PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource(
                    "external-properties", externalProperties);

            MutablePropertySources propertySources = properties.getPropertySources();
            propertySources.addFirst(propertiesPropertySource);
        } catch (IOException e) {
            log.warn("Not found external-properties-file: "
                    + properties.getProperty("external-properties-file"));
        }
    }
}

From source file:ch.sdi.core.impl.data.PersonKey.java

/**
 * @param aEnv//  w  w w  .j  av a2 s.c o  m
 */
public static void initCustomKeys(ConfigurableEnvironment aEnv) {
    myCustomKeys = new ArrayList<String>();

    String configured = aEnv.getProperty(KEY_CUSTOM_KEYS);
    if (!StringUtils.hasText(configured)) {
        return;
    } // if !StringUtils.hasText( configured )

    String[] keys = configured.trim().split(",");

    for (String key : keys) {
        myCustomKeys.add(key);
    }
}

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

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication springApplication) {

    Map<String, Object> map = new LinkedHashMap<>();
    String uri = environment.getProperty("vcap.services.stats-index.credentials.uri");
    if (StringUtils.hasText(uri)) {
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(uri).build();
        String userInfo = uriComponents.getUserInfo();
        if (StringUtils.hasText(userInfo)) {
            String[] credentials = userInfo.split(":");
            map.put("initializr.stats.elastic.username", credentials[0]);
            map.put("initializr.stats.elastic.password", credentials[1]);
        }// ww w .  j a va 2s  .  c o  m
        map.put("initializr.stats.elastic.uri",
                UriComponentsBuilder.fromUriString(uri).userInfo(null).build().toString());

        addOrReplace(environment.getPropertySources(), map);
    }
}

From source file:jp.xet.uncommons.web.env.EnvironmentProfileApplicationContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    ConfigurableEnvironment environment = applicationContext.getEnvironment();

    String[] activeProfiles = environment.getActiveProfiles();
    if (ObjectUtils.isEmpty(activeProfiles)) {
        String propEnvironment = environment.getProperty("environment");
        if (Strings.isNullOrEmpty(propEnvironment)) {
            environment.setActiveProfiles("development");
            logger.warn("Spring active profiles are not specified.  Set default value [{}]", DEFAULT_PROFILE);
        } else {//from  w  w  w .j a v a2s  .co m
            environment.setActiveProfiles(propEnvironment);
            logger.info("Set Spring active profiles to [{}]", propEnvironment);
        }
    } else {
        logger.info("Spring active profiles [{}]", activeProfiles);
    }
}

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

public LavagnaEnvironment(ConfigurableEnvironment environment) {
    this.environment = environment;

    if (environment.containsProperty(LAVAGNA_CONFIG_LOCATION)
            && StringUtils.isNotBlank(environment.getProperty(LAVAGNA_CONFIG_LOCATION))) {

        String configLocation = environment.getProperty(LAVAGNA_CONFIG_LOCATION);

        LOG.info("Detected config file {}, loading it", configLocation);
        try {//  w  ww .j av  a  2  s  .com
            environment.getPropertySources()
                    .addFirst(new ResourcePropertySource(new UrlResource(configLocation)));
        } catch (IOException ioe) {
            throw new IllegalStateException(
                    "error while loading external configuration file at " + configLocation, ioe);
        }
    }

    setSystemPropertyIfNull(environment, "datasource.dialect", "HSQLDB");
    setSystemPropertyIfNull(environment, "datasource.url", "jdbc:hsqldb:mem:lavagna");
    setSystemPropertyIfNull(environment, "datasource.username", "sa");
    setSystemPropertyIfNull(environment, "datasource.password", "");
    setSystemPropertyIfNull(environment, "spring.profiles.active", "dev");

    logUse("datasource.dialect");
    logUse("datasource.url");
    logUse("datasource.username");
    logUse("spring.profiles.active");

}

From source file:org.jrecruiter.web.config.DefaultApplicationContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {

    final String profile = System.getProperty("jrecruiter-spring-profile");

    if (profile == null) {
        LOGGER.info("System property 'jrecruiter-spring-profile' not set. Setting active profile to '{}'.",
                SpringContextMode.DemoContextConfiguration.getCode());
        applicationContext.getEnvironment()
                .setActiveProfiles(SpringContextMode.DemoContextConfiguration.getCode());
    } else {/*from w  w  w.j a  va  2s. co m*/
        applicationContext.getEnvironment().setActiveProfiles(profile);
    }

    final ConfigurableEnvironment environment = applicationContext.getEnvironment();

    if (environment.acceptsProfiles("standalone")) {
        String tingHome = environment.getProperty(Apphome.APP_HOME_DIRECTORY);
        try {
            ResourcePropertySource source = new ResourcePropertySource(
                    "file:" + tingHome + File.separator + SystemInformationUtils.JRECRUITER_CONFIG_FILENAME);
            environment.getPropertySources().addFirst(source);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Properties for standalone mode loaded");
    } else {
        try {
            environment.getPropertySources()
                    .addFirst(new ResourcePropertySource("classpath:jrecruiter-demo.properties"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Properties for demo mode loaded");
    }

    final Boolean twitterEnabled = environment.getProperty("twitter.enabled", Boolean.class, Boolean.FALSE);

    if (twitterEnabled) {
        applicationContext.getEnvironment().addActiveProfile("twitter-enabled");
    }
    System.out.println("::Twitter enabled: " + twitterEnabled);

    final Boolean mailEnabled = environment.getProperty("mail.enabled", Boolean.class, Boolean.FALSE);

    if (mailEnabled) {
        applicationContext.getEnvironment().addActiveProfile("mail-enabled");
    }
    System.out.println("::Mail enabled: " + mailEnabled);

    final Boolean bitlyEnabled = environment.getProperty("mail.enabled", Boolean.class, Boolean.FALSE);

    if (bitlyEnabled) {
        applicationContext.getEnvironment().addActiveProfile("bitly-enabled");
    }
    System.out.println("::Bitly enabled: " + bitlyEnabled);
}

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

private void applySpringProfiles(ConfigurableEnvironment environment, ServletContext servletContext) {
    if (environment.containsProperty("spring_profiles")) {
        String profiles = (String) environment.getProperty("spring_profiles");
        servletContext.log("Setting active profiles: " + profiles);
        environment.setActiveProfiles(StringUtils.commaDelimitedListToStringArray(profiles));
    }//w w w . j a v a 2  s  .c  om
}

From source file:gov.uscis.vis.api.config.SwaggerPropertiesLoader.java

private Properties createSwaggerProperties(ConfigurableEnvironment env) {
    Properties properties = new Properties();
    properties.setProperty("swagger.title", "Jira Metrics API");
    properties.setProperty("swagger.description", "Jira Metrics API Documentation");
    properties.setProperty("swagger.version", env.getProperty("info.build.version"));
    properties.setProperty("swagger.base-path", "/jira-metrics-api");
    properties.setProperty("swagger.pretty-print", "true");
    properties.setProperty("swagger.scan", "true");
    properties.setProperty("swagger.contact", "");
    properties.setProperty("swagger.license", "");
    properties.setProperty("swagger.licenseUrl", "");

    // Packages that should be added as resources visible in swagger
    List<String> packagesToAdd = new ArrayList<>();
    packagesToAdd.add("gov.uscis.vis.api");
    //        packagesToAdd.add("gov.uscis.visapi.common.batch.resource");
    // Add scan packages
    properties.setProperty("swagger.resourcePackage", StringUtils.join(packagesToAdd, ","));

    return properties;
}