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:org.springframework.cloud.gcp.autoconfigure.core.cloudfoundry.GcpCloudFoundryEnvironmentPostProcessor.java

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    if (!StringUtils.isEmpty(environment.getProperty(VCAP_SERVICES_ENVVAR))) {
        Map<String, Object> vcapMap = this.parser.parseMap(environment.getProperty(VCAP_SERVICES_ENVVAR));

        Properties gcpCfServiceProperties = new Properties();

        Set<GcpCfService> servicesToMap = new HashSet<>(Arrays.asList(GcpCfService.values()));
        if (vcapMap.containsKey(GcpCfService.MYSQL.getCfServiceName())
                && vcapMap.containsKey(GcpCfService.POSTGRES.getCfServiceName())) {
            LOGGER.warn("Both MySQL and PostgreSQL bound to the app. " + "Not configuring Cloud SQL.");
            servicesToMap.remove(GcpCfService.MYSQL);
            servicesToMap.remove(GcpCfService.POSTGRES);
        }//from w  w  w  .  ja  va 2s  .  co  m

        servicesToMap.forEach((service) -> gcpCfServiceProperties.putAll(retrieveCfProperties(vcapMap,
                service.getGcpServiceName(), service.getCfServiceName(), service.getCfPropNameToGcp())));

        // For Cloud SQL, there are some exceptions to the rule.
        // The instance connection name must be built from three fields.
        if (gcpCfServiceProperties.containsKey("spring.cloud.gcp.sql.instance-name")) {
            String instanceConnectionName = gcpCfServiceProperties
                    .getProperty("spring.cloud.gcp.sql.project-id") + ":"
                    + gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.region") + ":"
                    + gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.instance-name");
            gcpCfServiceProperties.put("spring.cloud.gcp.sql.instance-connection-name", instanceConnectionName);
        }
        // The username and password should be in the generic DataSourceProperties.
        if (gcpCfServiceProperties.containsKey("spring.cloud.gcp.sql.username")) {
            gcpCfServiceProperties.put("spring.datasource.username",
                    gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.username"));
        }
        if (gcpCfServiceProperties.containsKey("spring.cloud.gcp.sql.password")) {
            gcpCfServiceProperties.put("spring.datasource.password",
                    gcpCfServiceProperties.getProperty("spring.cloud.gcp.sql.password"));
        }

        environment.getPropertySources()
                .addFirst(new PropertiesPropertySource("gcpCf", gcpCfServiceProperties));
    }
}

From source file:org.springframework.cloud.zookeeper.config.ZookeeperPropertySourceLocator.java

@Override
public PropertySource<?> locate(Environment environment) {
    if (environment instanceof ConfigurableEnvironment) {
        ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
        String appName = env.getProperty("spring.application.name");
        List<String> profiles = Arrays.asList(env.getActiveProfiles());

        String root = this.properties.getRoot();
        this.contexts = new ArrayList<>();

        String defaultContext = root + "/" + this.properties.getDefaultContext();
        this.contexts.add(defaultContext);
        addProfiles(this.contexts, defaultContext, profiles);

        StringBuilder baseContext = new StringBuilder(root);
        if (!appName.startsWith("/")) {
            baseContext.append("/");
        }//from   w w  w .  j  a v  a2s .  c o m
        baseContext.append(appName);
        this.contexts.add(baseContext.toString());
        addProfiles(this.contexts, baseContext.toString(), profiles);

        CompositePropertySource composite = new CompositePropertySource("zookeeper");

        Collections.reverse(this.contexts);

        for (String propertySourceContext : this.contexts) {
            try {
                PropertySource propertySource = create(propertySourceContext);
                composite.addPropertySource(propertySource);
                // TODO: howto call close when /refresh
            } catch (Exception e) {
                if (this.properties.isFailFast()) {
                    ReflectionUtils.rethrowRuntimeException(e);
                } else {
                    log.warn("Unable to load zookeeper config from " + propertySourceContext, e);
                }
            }
        }

        return composite;
    }
    return null;
}

From source file:org.springframework.xd.dirt.plugins.spark.streaming.SparkStreamingPlugin.java

@Override
public void postProcessModule(Module module) {
    ConfigurableApplicationContext moduleContext = module.getApplicationContext();
    ConfigurableEnvironment env = moduleContext.getEnvironment();
    String transport = env.getProperty("XD_TRANSPORT");
    Properties messageBusProperties = getMessageBusProperties(module);
    Properties inboundModuleProperties = this.extractConsumerProducerProperties(module)[0];
    Properties outboundModuleProperties = this.extractConsumerProducerProperties(module)[1];
    String defaultStorageLevel = env.getProperty(SparkStreamingSupport.SPARK_STORAGE_LEVEL_PROP);
    StorageLevel configuredStorageLevel = StorageLevel
            .fromString(StringUtils.hasText(defaultStorageLevel) ? defaultStorageLevel
                    : SparkStreamingSupport.SPARK_DEFAULT_STORAGE_LEVEL);
    String storageLevelFromModule = module.getProperties()
            .getProperty(SparkStreamingSupport.SPARK_STORAGE_LEVEL_MODULE_OPTION);
    StorageLevel storageLevel = StringUtils.hasText(storageLevelFromModule)
            ? StorageLevel.fromString(storageLevelFromModule)
            : configuredStorageLevel;//from  w w w  .  ja  v  a2  s. c  om
    MessageBusReceiver receiver = null;
    if (transport.equals("local")) {
        SparkStreamingSupport processor;
        Properties sparkConfigs = null;
        try {
            processor = module.getComponent(SparkStreamingSupport.class);
            Assert.notNull(processor,
                    "Problem getting the spark streaming module. Is the module context active?");
            sparkConfigs = getSparkModuleProperties(processor);
        } catch (NoSuchBeanDefinitionException e) {
            throw new IllegalStateException("Either java or scala module should be present.");
        }
        String sparkMasterUrl = env.getProperty(SparkStreamingSupport.SPARK_MASTER_URL_PROP);
        if (sparkConfigs != null
                && StringUtils.hasText(sparkConfigs.getProperty(SparkStreamingSupport.SPARK_MASTER_URL_PROP))) {
            sparkMasterUrl = sparkConfigs.getProperty(SparkStreamingSupport.SPARK_MASTER_URL_PROP);
        }
        Assert.notNull(sparkMasterUrl, "Spark Master URL must be set.");
        if (!sparkMasterUrl.startsWith("local")) {
            throw new IllegalStateException("Spark cluster mode must be 'local' for 'local' XD transport.");
        }
        LocalMessageBusHolder messageBusHolder = new LocalMessageBusHolder();
        LocalMessageBusHolder.set(module.getComponent(MessageBus.class));
        receiver = new MessageBusReceiver(messageBusHolder, storageLevel, messageBusProperties,
                inboundModuleProperties, ModuleTypeConversionSupport.getInputMimeType(module));
        if (module.getType().equals(ModuleType.processor)) {
            MessageBusSender sender = new MessageBusSender(messageBusHolder, getOutputChannelName(module),
                    buildTapChannelName(module), messageBusProperties, outboundModuleProperties,
                    ModuleTypeConversionSupport.getOutputMimeType(module), module.getProperties());
            ConfigurableBeanFactory beanFactory = module.getApplicationContext().getBeanFactory();
            beanFactory.registerSingleton("messageBusSender", sender);
        }
    } else {
        receiver = new MessageBusReceiver(storageLevel, messageBusProperties, inboundModuleProperties,
                ModuleTypeConversionSupport.getInputMimeType(module));
        if (module.getType().equals(ModuleType.processor)) {
            ConfigurableBeanFactory beanFactory = module.getApplicationContext().getBeanFactory();
            MessageBusSender sender = new MessageBusSender(getOutputChannelName(module),
                    buildTapChannelName(module), messageBusProperties, outboundModuleProperties,
                    ModuleTypeConversionSupport.getOutputMimeType(module), module.getProperties());
            beanFactory.registerSingleton("messageBusSender", sender);
        }
    }
    registerMessageBusReceiver(receiver, module);

    // This used to be in SSDModule.start
    try {
        SparkStreamingSupport processor = module.getComponent(SparkStreamingSupport.class);
        Assert.notNull(processor, "Problem getting the spark streaming module. Is the module context active?");
        Properties sparkConfigs = getSparkModuleProperties(processor);
        startSparkStreamingContext(sparkConfigs, processor, module);
    } catch (NoSuchBeanDefinitionException e) {
        throw new IllegalStateException("Either java or scala module should be present.");
    }

}

From source file:org.springframework.xd.dirt.plugins.spark.streaming.SparkStreamingPlugin.java

/**
 * Get the configured message bus properties for the given transport.
 * @param module//from  w  w w. ja  v  a  2  s  . com
 * @return the message bus properties for the spark streaming module.
 */
private Properties getMessageBusProperties(Module module) {
    ConfigurableEnvironment env = module.getApplicationContext().getEnvironment();
    Properties busProperties = new Properties();
    busProperties.put("XD_TRANSPORT", env.getProperty("XD_TRANSPORT"));
    Iterator<PropertySource<?>> i = env.getPropertySources().iterator();
    while (i.hasNext()) {
        PropertySource<?> p = i.next();
        if (p instanceof EnumerablePropertySource) {
            for (String name : ((EnumerablePropertySource) p).getPropertyNames()) {
                if ((name.startsWith(REDIS_CONNECTION_PROPERTY_PREFIX))
                        || name.startsWith(RABBIT_CONNECTION_PROPERTY_PREFIX)
                        || name.startsWith(MESSAGE_BUS_PROPERTY_PREFIX)) {
                    busProperties.put(name, env.getProperty(name));
                }
            }
        }
    }
    return busProperties;
}