|
| 1 | +package com.baeldung.environmentpostprocessor; |
| 2 | + |
| 3 | +import java.util.LinkedHashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | +import org.springframework.boot.SpringApplication; |
| 9 | +import org.springframework.boot.env.EnvironmentPostProcessor; |
| 10 | +import org.springframework.core.Ordered; |
| 11 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 12 | +import org.springframework.core.env.MapPropertySource; |
| 13 | +import org.springframework.core.env.PropertySource; |
| 14 | +import org.springframework.core.env.StandardEnvironment; |
| 15 | + |
| 16 | +public class PriceCalculationEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { |
| 17 | + |
| 18 | + private static final Logger logger = LoggerFactory.getLogger(PriceCalculationEnvironmentPostProcessor.class); |
| 19 | + |
| 20 | + public static final int DEFAULT_ORDER = Ordered.LOWEST_PRECEDENCE; |
| 21 | + private int order = DEFAULT_ORDER; |
| 22 | + private static final String PROPERTY_PREFIX = "com.baeldung.environmentpostprocessor."; |
| 23 | + private static final String OS_ENV_PROPERTY_CALCUATION_MODE = "calculation_mode"; |
| 24 | + private static final String OS_ENV_PROPERTY_GROSS_CALCULATION_TAX_RATE = "gross_calculation_tax_rate"; |
| 25 | + private static final String OS_ENV_PROPERTY_CALCUATION_MODE_DEFAULT_VALUE = "NET"; |
| 26 | + private static final double OS_ENV_PROPERTY_GROSS_CALCULATION_TAX_RATE_DEFAULT_VALUE = 0; |
| 27 | + |
| 28 | + @Override |
| 29 | + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { |
| 30 | + |
| 31 | + PropertySource<?> systemEnvironmentPropertySource = environment.getPropertySources() |
| 32 | + .get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); |
| 33 | + |
| 34 | + Map<String, Object> priceCalculationConfiguration = new LinkedHashMap<>(); |
| 35 | + if (isActive(systemEnvironmentPropertySource)) { |
| 36 | + priceCalculationConfiguration.put(key(OS_ENV_PROPERTY_CALCUATION_MODE), systemEnvironmentPropertySource.getProperty(OS_ENV_PROPERTY_CALCUATION_MODE)); |
| 37 | + priceCalculationConfiguration.put(key(OS_ENV_PROPERTY_GROSS_CALCULATION_TAX_RATE), systemEnvironmentPropertySource.getProperty(OS_ENV_PROPERTY_GROSS_CALCULATION_TAX_RATE)); |
| 38 | + } else { |
| 39 | + logger.warn("System environment variables [calculation_mode,gross_calculation_tax_rate] not detected, fallback to default value [calcuation_mode={},gross_calcuation_tax_rate={}]", OS_ENV_PROPERTY_CALCUATION_MODE_DEFAULT_VALUE, |
| 40 | + OS_ENV_PROPERTY_GROSS_CALCULATION_TAX_RATE_DEFAULT_VALUE); |
| 41 | + priceCalculationConfiguration.put(key(OS_ENV_PROPERTY_CALCUATION_MODE), OS_ENV_PROPERTY_CALCUATION_MODE_DEFAULT_VALUE); |
| 42 | + priceCalculationConfiguration.put(key(OS_ENV_PROPERTY_GROSS_CALCULATION_TAX_RATE), OS_ENV_PROPERTY_GROSS_CALCULATION_TAX_RATE_DEFAULT_VALUE); |
| 43 | + } |
| 44 | + |
| 45 | + PropertySource<?> priceCalcuationPropertySource = new MapPropertySource("priceCalcuationPS", priceCalculationConfiguration); |
| 46 | + environment.getPropertySources() |
| 47 | + .addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, priceCalcuationPropertySource); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + private String key(String key) { |
| 52 | + return PROPERTY_PREFIX + key.replaceAll("\\_", "."); |
| 53 | + } |
| 54 | + |
| 55 | + private boolean isActive(PropertySource<?> systemEnvpropertySource) { |
| 56 | + if (systemEnvpropertySource.containsProperty(OS_ENV_PROPERTY_CALCUATION_MODE) && systemEnvpropertySource.containsProperty(OS_ENV_PROPERTY_GROSS_CALCULATION_TAX_RATE)) { |
| 57 | + return true; |
| 58 | + } else |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + public void setOrder(int order) { |
| 63 | + this.order = order; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public int getOrder() { |
| 68 | + return order; |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments