cn.majingjing.config.client.ConfigClient Maven / Gradle / Ivy
package cn.majingjing.config.client;
import cn.majingjing.config.client.exception.ConfigClientException;
import cn.majingjing.config.client.listener.ConfigChangeListener;
import cn.majingjing.config.client.provider.ConfigProvider;
import java.util.Objects;
/**
* Config Client
*
* @author JingjingMa
* @date 2019/7/25 10:50
*/
public final class ConfigClient {
private ConfigClient() {
}
private static volatile ConfigProvider provider;
private static ConfigProvider getProvider() {
if (Objects.isNull(ConfigClient.provider)) {
synchronized (ConfigClient.class) {
if (Objects.isNull(ConfigClient.provider)) {
Class> aClass = null;
try {
aClass = Class.forName(System.getProperty("majj.config.client.provider"));
} catch (Exception e) {
throw new ConfigClientException("ConfigClient config error , not found majj.config.client.provider, "+e.getMessage(),e);
}
try {
ConfigClient.provider = (ConfigProvider) aClass.newInstance();
} catch (Exception e) {
throw new ConfigClientException("ConfigClient config error , can not create ConfigProvider , "+e.getMessage(),e);
}
}
}
}
return ConfigClient.provider;
}
public static ConfigValue getApplicationProp(String key) {
return getProp("application", key);
}
public static ConfigValue getProp(String namespace, String key) {
String val = ConfigClient.getProvider().getProp(namespace, key);
return new ConfigValue(namespace, key, val);
}
public static ConfigValue getFile(String namespace) {
String val = ConfigClient.getProvider().getFile(namespace);
return new ConfigValue(namespace, "", val);
}
public static void addListener(String namespace, ConfigChangeListener listener) {
ConfigClient.getProvider().addListener(namespace, listener);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy