This repository has been archived by the owner on Aug 20, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(configuration): Configuration properties can be overriden by sys…
…tem properties or environment variables Closes gravitee-io/issues#1042
- Loading branch information
Showing
3 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
...src/main/java/io/gravitee/management/rest/spring/EnvironmentBeanFactoryPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters