org.zodiac.sentinel.base.datasource.AbstractDataSourceAdaptor Maven / Gradle / Ivy
package org.zodiac.sentinel.base.datasource;
import javax.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.springframework.core.env.ConfigurableEnvironment;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.commons.logging.SmartSlf4jLoggerFactory;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
public abstract class AbstractDataSourceAdaptor implements DataSourceAdaptor {
protected final Logger logger = SmartSlf4jLoggerFactory.getLogger(getClass());
private final String baseConfPrefx;
private String applicationName;
public AbstractDataSourceAdaptor() {
this(null);
}
public AbstractDataSourceAdaptor(String applicationName) {
this(applicationName, null);
}
public AbstractDataSourceAdaptor(String applicationName, String baseConfPrefx) {
this.applicationName = applicationName;
// this.baseConfPrefx = StrUtil.trimTo(baseConfPrefx,
// SentinelSystemPropertiesConstants.Spring.SPRING_CLOUD_SENTINEL_DATASOURCE_PREFIX);
this.baseConfPrefx = obtainbaseConfPrefx(baseConfPrefx);
}
protected String obtainbaseConfPrefx(String baseConfPrefx) {
return baseConfPrefx;
}
@NotNull
@Override
public AbstractDataSourceOption getDataSourceOption() {
return obtainDataSourceOption();
}
@Override
public final void adapt(ConfigurableEnvironment environment) {
String appName = StrUtil.trimTo(applicationName, environment.getProperty(SystemPropertiesConstants.Spring.SPRING_APP_NAME));
AbstractDataSourceOption dataSourceOption = getDataSourceOption();
String baseConfFormat = null != getBaseConfPrefx() ? getBaseConfPrefx() + ".%s." + dataSourceOption.getDataSourceType() + ".%s" :
".%s." + dataSourceOption.getDataSourceType() + ".%s";
this.doAdapt(environment, appName, baseConfFormat);
}
protected final String getBaseConfPrefx() {
return baseConfPrefx;
}
protected final String getApplicationName() {
return applicationName;
}
protected abstract void doAdapt(ConfigurableEnvironment environment, String sentinelAppName, String sentinelConfigPropertyFormat);
@NotNull
protected abstract AbstractDataSourceOption obtainDataSourceOption();
}