Skip to content

Instantly share code, notes, and snippets.

@circlee
Created February 11, 2019 14:34
Show Gist options
  • Save circlee/dc4664bdc7db698b3e191bc4c48f7896 to your computer and use it in GitHub Desktop.
Save circlee/dc4664bdc7db698b3e191bc4c48f7896 to your computer and use it in GitHub Desktop.
@Configuration
public class EnvConfig {
Logger logger = LoggerFactory.getLogger(EnvConfig.class);
@Autowired
private EnvRepository envRepository;
@Bean
public EnvironmentRepository getEnvironmentRepository(){
return new EnvironmentRepository() {
@Override
public Environment findOne(String appName, String profile, String label) {
logger.info("appName >>> {}" , appName);
logger.info("profile >>> {}" , profile);
logger.info("label >>> {}" , label);
Map<String, String> property = envRepository.getProp(appName, profile, label);
PropertySource propertySource = new PropertySource("testProperty", property);
Environment environment = new Environment(appName, profile);
environment.add(propertySource);
return environment;
}
};
}
}
@Repository
public class EnvRepository {
Map<String, Map<String, String>> envMap = new HashMap<>();
@PostConstruct
public void init(){
Map<String, String> property = new HashMap<>();
property.put("central.test", "test");
property.put("central.test1", "test1");
property.put("central.test2", "test2");
putProp("cofigClient", property);
}
public void putProp(String app, Map<String, String> prop) {
putProp(app, "default", "null", prop);
}
public void putProp(String app, String profile, Map<String, String> prop) {
putProp(app, profile, "null", prop);
}
public void putProp(String app, String profile, String label, Map<String, String> prop) {
String mapKey = app.join("-").join(profile).join("-").join(label == null ? "null" : label);
envMap.put(mapKey, prop);
}
public Map<String, String> getProp(String app, String profile) {
return getProp(app, profile, "null");
}
public Map<String, String> getProp(String app, String profile, String label) {
String mapKey = app.join("-").join(profile).join("-").join(label == null ? "null" : label);
return envMap.get(mapKey);
}
public void setPropKey(String app, String key, String value) {
setPropKey(app, "default", "null", key , value);
}
public void setPropKey(String app, String profile, String key, String value) {
setPropKey(app, profile, "null", key , value);
}
public void setPropKey(String app, String profile, String label, String key, String value) {
Map<String, String> propMap = getProp(app, profile, label == null ? "null" : label);
if(propMap == null) {
propMap = new HashMap<>();
}
propMap.put(key, value);
putProp(app, profile, label, propMap);
}
public Map<String, Map<String, String>> getEnvMap() {
return envMap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment