com.nepxion.discovery.plugin.configcenter.ConfigInitializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of discovery-plugin-config-center Show documentation
Show all versions of discovery-plugin-config-center Show documentation
Nepxion Discovery is an enhancement for Spring Cloud Discovery
package com.nepxion.discovery.plugin.configcenter;
/**
* Title: Nepxion Discovery
* Description: Nepxion Discovery
* Copyright: Copyright (c) 2017-2050
* Company: Nepxion
* @author Haojun Ren
* @version 1.0
*/
import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.common.entity.RuleEntity;
import com.nepxion.discovery.plugin.configcenter.loader.LocalConfigLoader;
import com.nepxion.discovery.plugin.configcenter.loader.RemoteConfigLoader;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.framework.config.PluginConfigParser;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
public class ConfigInitializer {
private static final Logger LOG = LoggerFactory.getLogger(ConfigInitializer.class);
@Autowired
private PluginContextAware pluginContextAware;
@Autowired
private PluginAdapter pluginAdapter;
@Autowired
private PluginConfigParser pluginConfigParser;
@Autowired
private LocalConfigLoader localConfigLoader;
@Autowired(required = false)
private RemoteConfigLoader remoteConfigLoader;
@PostConstruct
public void initialize() {
Boolean registerControlEnabled = pluginContextAware.isRegisterControlEnabled();
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!registerControlEnabled && !discoveryControlEnabled) {
LOG.info("Register and Discovery controls are all disabled, ignore to initialize");
return;
}
LOG.info("Rule starts to initialize...");
String remoteConfig = getRemoteConfig();
if (StringUtils.isNotEmpty(remoteConfig)) {
try {
RuleEntity ruleEntity = pluginConfigParser.parse(remoteConfig);
pluginAdapter.setDynamicRule(ruleEntity);
} catch (Exception e) {
LOG.error("Parse rule xml failed", e);
}
}
String localConfig = getLocalConfig();
if (StringUtils.isNotEmpty(localConfig)) {
try {
RuleEntity ruleEntity = pluginConfigParser.parse(localConfig);
pluginAdapter.setLocalRule(ruleEntity);
} catch (Exception e) {
LOG.error("Parse rule xml failed", e);
}
}
if (StringUtils.isEmpty(remoteConfig) && StringUtils.isEmpty(localConfig)) {
LOG.info("No config is retrieved");
}
}
private String getRemoteConfig() {
if (remoteConfigLoader != null) {
String config = null;
try {
config = remoteConfigLoader.getConfig();
} catch (Exception e) {
LOG.warn("Get remote config failed", e);
}
if (StringUtils.isNotEmpty(config)) {
LOG.info("Remote config is retrieved");
return config;
} else {
LOG.info("Remote config isn't retrieved");
}
} else {
LOG.info("Remote config loader isn't provided");
}
return null;
}
private String getLocalConfig() {
String config = null;
try {
config = localConfigLoader.getConfig();
} catch (Exception e) {
LOG.warn("Get local config failed", e);
}
if (StringUtils.isNotEmpty(config)) {
LOG.info("Local config is retrieved");
return config;
} else {
LOG.info("Local config isn't retrieved");
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy