Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
feat(configuration): Configuration properties can be overriden by sys…
Browse files Browse the repository at this point in the history
…tem properties or environment variables

Closes gravitee-io/issues#1042
  • Loading branch information
brasseld committed Jan 29, 2018
1 parent f977c9b commit c4325e7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
@@ -0,0 +1,82 @@
package io.gravitee.management.rest.spring;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.SystemEnvironmentPropertySource;

import java.util.HashMap;
import java.util.Map;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class EnvironmentBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

private final static String PROPERTY_PREFIX = "gravitee.";

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
StandardEnvironment environment = (StandardEnvironment) beanFactory.getBean(Environment.class);

if (environment != null) {
Map<String, Object> systemEnvironment = environment.getSystemEnvironment();
Map<String, Object> prefixlessSystemEnvironment = new HashMap<>(systemEnvironment.size());
systemEnvironment
.keySet()
.forEach(key -> {
String prefixKey = key;
if (key.startsWith(PROPERTY_PREFIX)) {
prefixKey = key.substring(PROPERTY_PREFIX.length());
}
prefixlessSystemEnvironment.put(prefixKey, systemEnvironment.get(key));
});
SystemEnvironmentPropertySource systemEnvironmentPropertySource =
new SystemEnvironmentPropertySource(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, prefixlessSystemEnvironment);

environment.getPropertySources().replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
new PrefixAwarePropertySource(systemEnvironmentPropertySource));
}
}

class PrefixAwarePropertySource extends PropertySource {


private final PropertySource propertySource;

public PrefixAwarePropertySource(PropertySource propertySource) {
super(propertySource.getName());
this.propertySource = propertySource;
}

@Override
public boolean containsProperty(String name) {
removePrefix(name);
return propertySource.containsProperty(name);
}

@Override
public Object getProperty(String name) {
removePrefix(name);
return propertySource.getProperty(name);
}

private String removePrefix(String name) {
if (name == null) {
return null;
}

if (name.startsWith(PROPERTY_PREFIX)) {
return name.substring(PROPERTY_PREFIX.length());
}

return name;
}
}
}
Expand Up @@ -26,15 +26,16 @@
import java.util.Properties;

/**
* @author David BRASSELY (brasseld at gmail.com)
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
public class PropertySourceBeanProcessor implements BeanFactoryPostProcessor, Ordered {

private Environment environment;

private Properties properties;

public PropertySourceBeanProcessor(Properties properties, Environment environment) {
PropertySourceBeanProcessor(Properties properties, Environment environment) {
this.properties = properties;
this.environment = environment;
}
Expand All @@ -46,7 +47,7 @@ public int getOrder() {

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
((ConfigurableEnvironment) environment).getPropertySources().addFirst(
((ConfigurableEnvironment) environment).getPropertySources().addLast(
new PropertiesPropertySource("graviteeConfiguration", properties));
}
}
Expand Up @@ -30,8 +30,8 @@
import java.util.Properties;

/**
*
* @author David BRASSELY (brasseld at gmail.com)
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
*/
@Configuration
@ComponentScan({"io.gravitee.management.rest.enhancer"})
Expand All @@ -50,6 +50,11 @@ public static PropertySourcesPlaceholderConfigurer properties(@Qualifier("gravit
return propertySourcesPlaceholderConfigurer;
}

@Bean
public static EnvironmentBeanFactoryPostProcessor environmentBeanFactoryPostProcessor() {
return new EnvironmentBeanFactoryPostProcessor();
}

@Bean
public static PropertySourceBeanProcessor propertySourceBeanProcessor(@Qualifier("graviteeProperties") Properties graviteeProperties,
Environment environment) {
Expand Down

0 comments on commit c4325e7

Please sign in to comment.