use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class DataSourceWrapUtil method generate.
public static BasicDataSource generate(BasicDataSource sourceDatasource) {
Map<String, ShadowDatabaseConfig> conf = GlobalConfig.getInstance().getShadowDatasourceConfigs();
if (conf == null) {
return null;
}
ShadowDatabaseConfig ptDataSourceConf = selectMatchPtDataSourceConfiguration(sourceDatasource, conf);
if (ptDataSourceConf == null) {
return null;
}
String url = ptDataSourceConf.getShadowUrl();
String username = ptDataSourceConf.getShadowUsername();
String password = ptDataSourceConf.getShadowPassword();
if (StringUtils.isBlank(url) || StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
return null;
}
String driverClassName = ptDataSourceConf.getShadowDriverClassName();
if (StringUtils.isBlank(driverClassName)) {
driverClassName = sourceDatasource.getDriverClassName();
}
BasicDataSource target = new BasicDataSource();
target.setUrl(url);
target.setUsername(username);
target.setPassword(password);
target.setDriverClassName(driverClassName);
Boolean defaultAutoCommit = ptDataSourceConf.getBooleanProperty("defaultAutoCommit");
if (defaultAutoCommit != null) {
target.setDefaultAutoCommit(defaultAutoCommit);
} else {
target.setDefaultAutoCommit(sourceDatasource.getDefaultAutoCommit());
}
Boolean defaultReadOnly = ptDataSourceConf.getBooleanProperty("defaultReadOnly");
if (defaultReadOnly != null) {
target.setDefaultReadOnly(defaultReadOnly);
} else {
target.setDefaultReadOnly(sourceDatasource.getDefaultReadOnly());
}
Integer defaultTransactionIsolation = ptDataSourceConf.getIntProperty("defaultTransactionIsolation");
if (defaultTransactionIsolation != null) {
target.setDefaultTransactionIsolation(defaultTransactionIsolation);
} else {
target.setDefaultTransactionIsolation(sourceDatasource.getDefaultTransactionIsolation());
}
String defaultCatalog = ptDataSourceConf.getProperty("defaultCatalog");
if (StringUtils.isNotBlank(defaultCatalog)) {
target.setDefaultCatalog(defaultCatalog);
} else {
target.setDefaultCatalog(sourceDatasource.getDefaultCatalog());
}
Integer maxIdle = ptDataSourceConf.getIntProperty("maxIdle");
if (maxIdle != null) {
target.setMaxIdle(maxIdle);
} else {
target.setMaxIdle(sourceDatasource.getMaxIdle());
}
Integer minIdle = ptDataSourceConf.getIntProperty("minIdle");
if (minIdle != null) {
target.setMinIdle(minIdle);
} else {
target.setMinIdle(sourceDatasource.getMinIdle());
}
Integer initialSize = ptDataSourceConf.getIntProperty("initialSize");
if (initialSize != null) {
target.setInitialSize(initialSize);
} else {
target.setInitialSize(sourceDatasource.getInitialSize());
}
Boolean poolPreparedStatements = ptDataSourceConf.getBooleanProperty("poolPreparedStatements");
if (poolPreparedStatements != null) {
target.setPoolPreparedStatements(poolPreparedStatements);
} else {
target.setPoolPreparedStatements(sourceDatasource.isPoolPreparedStatements());
}
Integer maxOpenPreparedStatements = ptDataSourceConf.getIntProperty("maxOpenPreparedStatements");
if (maxOpenPreparedStatements != null) {
target.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
} else {
target.setMaxOpenPreparedStatements(sourceDatasource.getMaxOpenPreparedStatements());
}
Boolean testOnBorrow = ptDataSourceConf.getBooleanProperty("testOnBorrow");
if (testOnBorrow != null) {
target.setTestOnBorrow(testOnBorrow);
} else {
target.setTestOnBorrow(sourceDatasource.getTestOnBorrow());
}
Integer maxTotal = ptDataSourceConf.getIntProperty("maxTotal");
if (maxTotal != null) {
target.setMaxActive(maxTotal);
} else {
target.setMaxActive(sourceDatasource.getMaxActive());
}
Boolean testOnReturn = ptDataSourceConf.getBooleanProperty("testOnReturn");
if (testOnReturn != null) {
target.setTestOnReturn(testOnReturn);
} else {
target.setTestOnReturn(sourceDatasource.getTestOnReturn());
}
Long timeBetweenEvictionRunsMillis = ptDataSourceConf.getLongProperty("timeBetweenEvictionRunsMillis");
if (timeBetweenEvictionRunsMillis != null) {
target.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
} else {
target.setTimeBetweenEvictionRunsMillis(sourceDatasource.getTimeBetweenEvictionRunsMillis());
}
Integer numTestsPerEvictionRun = ptDataSourceConf.getIntProperty("numTestsPerEvictionRun");
if (numTestsPerEvictionRun != null) {
target.setNumTestsPerEvictionRun(Integer.valueOf(numTestsPerEvictionRun));
} else {
target.setNumTestsPerEvictionRun(sourceDatasource.getNumTestsPerEvictionRun());
}
Long minEvictableIdleTimeMillis = ptDataSourceConf.getLongProperty("minEvictableIdleTimeMillis");
if (minEvictableIdleTimeMillis != null) {
target.setMinEvictableIdleTimeMillis(Long.valueOf(minEvictableIdleTimeMillis));
} else {
target.setMinEvictableIdleTimeMillis(sourceDatasource.getMinEvictableIdleTimeMillis());
}
Boolean testWhileIdle = ptDataSourceConf.getBooleanProperty("testWhileIdle");
if (testWhileIdle != null) {
target.setTestWhileIdle(testWhileIdle);
} else {
target.setTestWhileIdle(sourceDatasource.getTestWhileIdle());
}
String validationQuery = ptDataSourceConf.getProperty("validationQuery");
if (StringUtils.isNotBlank(validationQuery)) {
target.setValidationQuery(validationQuery);
} else {
target.setValidationQuery(sourceDatasource.getValidationQuery());
}
Integer validationQueryTimeout = ptDataSourceConf.getIntProperty("validationQueryTimeout");
if (validationQueryTimeout != null) {
target.setValidationQueryTimeout(validationQueryTimeout);
} else {
target.setValidationQueryTimeout(sourceDatasource.getValidationQueryTimeout());
}
String connectionInitSqls = ptDataSourceConf.getProperty("connectionInitSqls");
if (StringUtils.isNotBlank(connectionInitSqls)) {
target.setConnectionInitSqls(Arrays.asList(StringUtils.split(connectionInitSqls, ';')));
} else {
target.setConnectionInitSqls(sourceDatasource.getConnectionInitSqls());
}
Boolean accessToUnderlyingConnectionAllowed = ptDataSourceConf.getBooleanProperty("accessToUnderlyingConnectionAllowed");
if (accessToUnderlyingConnectionAllowed != null) {
target.setAccessToUnderlyingConnectionAllowed(accessToUnderlyingConnectionAllowed);
} else {
target.setAccessToUnderlyingConnectionAllowed(sourceDatasource.isAccessToUnderlyingConnectionAllowed());
}
Integer removeAbandonedTimeout = ptDataSourceConf.getIntProperty("removeAbandonedTimeout");
if (removeAbandonedTimeout != null) {
target.setRemoveAbandonedTimeout(removeAbandonedTimeout);
} else {
target.setRemoveAbandonedTimeout(sourceDatasource.getRemoveAbandonedTimeout());
}
Boolean logAbandoned = ptDataSourceConf.getBooleanProperty("logAbandoned");
if (logAbandoned != null) {
target.setLogAbandoned(logAbandoned);
} else {
target.setLogAbandoned(sourceDatasource.getLogAbandoned());
}
return target;
}
use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class DataSourceWrapUtil method selectMatchPtDataSourceConfiguration.
@SuppressWarnings("unchecked")
private static ShadowDatabaseConfig selectMatchPtDataSourceConfiguration(BasicDataSource source, Map<String, ShadowDatabaseConfig> shadowDbConfigurations) {
BasicDataSource dataSource = source;
String key = DbUrlUtils.getKey(dataSource.getUrl(), dataSource.getUsername());
ShadowDatabaseConfig shadowDatabaseConfig = shadowDbConfigurations.get(key);
if (shadowDatabaseConfig == null) {
key = DbUrlUtils.getKey(dataSource.getUrl(), null);
shadowDatabaseConfig = shadowDbConfigurations.get(key);
}
return shadowDatabaseConfig;
}
use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class DataSourceWrapUtil method generate.
public static BasicDataSource generate(BasicDataSource sourceDatasource) {
Map<String, ShadowDatabaseConfig> conf = GlobalConfig.getInstance().getShadowDatasourceConfigs();
if (conf == null) {
return null;
}
ShadowDatabaseConfig ptDataSourceConf = selectMatchPtDataSourceConfiguration(sourceDatasource, conf);
if (ptDataSourceConf == null) {
return null;
}
String url = ptDataSourceConf.getShadowUrl();
String username = ptDataSourceConf.getShadowUsername();
String password = ptDataSourceConf.getShadowPassword();
if (StringUtils.isBlank(url) || StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
return null;
}
String driverClassName = ptDataSourceConf.getShadowDriverClassName();
if (StringUtils.isBlank(driverClassName)) {
driverClassName = sourceDatasource.getDriverClassName();
}
BasicDataSource target = new BasicDataSource();
target.setUrl(url);
target.setUsername(username);
target.setPassword(password);
target.setDriverClassName(driverClassName);
Boolean defaultAutoCommit = ptDataSourceConf.getBooleanProperty("defaultAutoCommit");
if (defaultAutoCommit != null) {
target.setDefaultAutoCommit(defaultAutoCommit);
} else {
target.setDefaultAutoCommit(sourceDatasource.getDefaultAutoCommit());
}
Boolean defaultReadOnly = ptDataSourceConf.getBooleanProperty("defaultReadOnly");
if (defaultReadOnly != null) {
target.setDefaultReadOnly(defaultReadOnly);
} else {
target.setDefaultReadOnly(sourceDatasource.getDefaultReadOnly());
}
Integer defaultTransactionIsolation = ptDataSourceConf.getIntProperty("defaultTransactionIsolation");
if (defaultTransactionIsolation != null) {
target.setDefaultTransactionIsolation(defaultTransactionIsolation);
} else {
target.setDefaultTransactionIsolation(sourceDatasource.getDefaultTransactionIsolation());
}
String defaultCatalog = ptDataSourceConf.getProperty("defaultCatalog");
if (StringUtils.isNotBlank(defaultCatalog)) {
target.setDefaultCatalog(defaultCatalog);
} else {
target.setDefaultCatalog(sourceDatasource.getDefaultCatalog());
}
String evictionPolicyClassName = ptDataSourceConf.getProperty("evictionPolicyClassName");
if (StringUtils.isNotBlank(evictionPolicyClassName)) {
target.setEvictionPolicyClassName(evictionPolicyClassName);
} else {
target.setEvictionPolicyClassName(sourceDatasource.getEvictionPolicyClassName());
}
Boolean fastFailValidation = ptDataSourceConf.getBooleanProperty("fastFailValidation");
if (fastFailValidation != null) {
target.setFastFailValidation(fastFailValidation);
} else {
target.setFastFailValidation(sourceDatasource.getFastFailValidation());
}
Integer maxIdle = ptDataSourceConf.getIntProperty("maxIdle");
if (maxIdle != null) {
target.setMaxIdle(maxIdle);
} else {
target.setMaxIdle(sourceDatasource.getMaxIdle());
}
Integer minIdle = ptDataSourceConf.getIntProperty("minIdle");
if (minIdle != null) {
target.setMinIdle(minIdle);
} else {
target.setMinIdle(sourceDatasource.getMinIdle());
}
Integer initialSize = ptDataSourceConf.getIntProperty("initialSize");
if (initialSize != null) {
target.setInitialSize(initialSize);
} else {
target.setInitialSize(sourceDatasource.getInitialSize());
}
Boolean poolPreparedStatements = ptDataSourceConf.getBooleanProperty("poolPreparedStatements");
if (poolPreparedStatements != null) {
target.setPoolPreparedStatements(poolPreparedStatements);
} else {
target.setPoolPreparedStatements(sourceDatasource.isPoolPreparedStatements());
}
Long softMinEvictableIdleTimeMillis = ptDataSourceConf.getLongProperty("softMinEvictableIdleTimeMillis");
if (softMinEvictableIdleTimeMillis != null) {
target.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
} else {
target.setSoftMinEvictableIdleTimeMillis(sourceDatasource.getSoftMinEvictableIdleTimeMillis());
}
Integer maxOpenPreparedStatements = ptDataSourceConf.getIntProperty("maxOpenPreparedStatements");
if (maxOpenPreparedStatements != null) {
target.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
} else {
target.setMaxOpenPreparedStatements(sourceDatasource.getMaxOpenPreparedStatements());
}
Boolean testOnBorrow = ptDataSourceConf.getBooleanProperty("testOnBorrow");
if (testOnBorrow != null) {
target.setTestOnBorrow(testOnBorrow);
} else {
target.setTestOnBorrow(sourceDatasource.getTestOnBorrow());
}
Integer maxTotal = ptDataSourceConf.getIntProperty("maxTotal");
if (maxTotal != null) {
target.setMaxTotal(maxTotal);
} else {
target.setMaxTotal(sourceDatasource.getMaxTotal());
}
Boolean testOnCreate = ptDataSourceConf.getBooleanProperty("testOnCreate");
if (testOnCreate != null) {
target.setTestOnCreate(testOnCreate);
} else {
target.setTestOnCreate(sourceDatasource.getTestOnCreate());
}
Boolean testOnReturn = ptDataSourceConf.getBooleanProperty("testOnReturn");
if (testOnReturn != null) {
target.setTestOnReturn(testOnReturn);
} else {
target.setTestOnReturn(sourceDatasource.getTestOnReturn());
}
Long timeBetweenEvictionRunsMillis = ptDataSourceConf.getLongProperty("timeBetweenEvictionRunsMillis");
if (timeBetweenEvictionRunsMillis != null) {
target.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
} else {
target.setTimeBetweenEvictionRunsMillis(sourceDatasource.getTimeBetweenEvictionRunsMillis());
}
Integer numTestsPerEvictionRun = ptDataSourceConf.getIntProperty("numTestsPerEvictionRun");
if (numTestsPerEvictionRun != null) {
target.setNumTestsPerEvictionRun(Integer.valueOf(numTestsPerEvictionRun));
} else {
target.setNumTestsPerEvictionRun(sourceDatasource.getNumTestsPerEvictionRun());
}
Long minEvictableIdleTimeMillis = ptDataSourceConf.getLongProperty("minEvictableIdleTimeMillis");
if (minEvictableIdleTimeMillis != null) {
target.setMinEvictableIdleTimeMillis(Long.valueOf(minEvictableIdleTimeMillis));
} else {
target.setMinEvictableIdleTimeMillis(sourceDatasource.getMinEvictableIdleTimeMillis());
}
Boolean testWhileIdle = ptDataSourceConf.getBooleanProperty("testWhileIdle");
if (testWhileIdle != null) {
target.setTestWhileIdle(testWhileIdle);
} else {
target.setTestWhileIdle(sourceDatasource.getTestWhileIdle());
}
String validationQuery = ptDataSourceConf.getProperty("validationQuery");
if (StringUtils.isNotBlank(validationQuery)) {
target.setValidationQuery(validationQuery);
} else {
target.setValidationQuery(sourceDatasource.getValidationQuery());
}
Integer validationQueryTimeout = ptDataSourceConf.getIntProperty("validationQueryTimeout");
if (validationQueryTimeout != null) {
target.setValidationQueryTimeout(validationQueryTimeout);
} else {
target.setValidationQueryTimeout(sourceDatasource.getValidationQueryTimeout());
}
String connectionInitSqls = ptDataSourceConf.getProperty("connectionInitSqls");
if (StringUtils.isNotBlank(connectionInitSqls)) {
target.setConnectionInitSqls(Arrays.asList(StringUtils.split(connectionInitSqls, ';')));
} else {
target.setConnectionInitSqls(sourceDatasource.getConnectionInitSqls());
}
Boolean accessToUnderlyingConnectionAllowed = ptDataSourceConf.getBooleanProperty("accessToUnderlyingConnectionAllowed");
if (accessToUnderlyingConnectionAllowed != null) {
target.setAccessToUnderlyingConnectionAllowed(accessToUnderlyingConnectionAllowed);
} else {
target.setAccessToUnderlyingConnectionAllowed(sourceDatasource.isAccessToUnderlyingConnectionAllowed());
}
Integer removeAbandonedTimeout = ptDataSourceConf.getIntProperty("removeAbandonedTimeout");
if (removeAbandonedTimeout != null) {
target.setRemoveAbandonedTimeout(removeAbandonedTimeout);
} else {
target.setRemoveAbandonedTimeout(sourceDatasource.getRemoveAbandonedTimeout());
}
Boolean logAbandoned = ptDataSourceConf.getBooleanProperty("logAbandoned");
if (logAbandoned != null) {
target.setLogAbandoned(logAbandoned);
} else {
target.setLogAbandoned(sourceDatasource.getLogAbandoned());
}
return target;
}
use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class ShadowDatabaseConfigs method compareIsChangeAndSet.
@Override
public Boolean compareIsChangeAndSet(ApplicationConfig applicationConfig, Map<String, ShadowDatabaseConfig> newValue) {
Set<ShadowDatabaseConfig> needCloseDataSource = new HashSet<ShadowDatabaseConfig>();
// 同名配置比对
for (Map.Entry<String, ShadowDatabaseConfig> old : GlobalConfig.getInstance().getShadowDatasourceConfigs().entrySet()) {
String key = old.getKey();
ShadowDatabaseConfig newConfig = newValue.get(key);
if (null == newConfig) {
// 删除的配置
needCloseDataSource.add(old.getValue());
if (logger.isInfoEnabled()) {
logger.info("deleted config:" + key);
}
// 不需要再比对
continue;
}
// 判断是否变更
if (!newConfig.equals(old.getValue())) {
// 修改的配置
needCloseDataSource.add(old.getValue());
if (logger.isInfoEnabled()) {
logger.info("modified config:" + key);
}
}
}
for (Map.Entry<String, ShadowDatabaseConfig> entry : newValue.entrySet()) {
// 判断是否是新增内容
if (null == GlobalConfig.getInstance().getShadowDatabaseConfig(entry.getKey())) {
// 新增的配置
needCloseDataSource.add(entry.getValue());
if (logger.isInfoEnabled()) {
logger.info("added config:" + entry.getKey());
}
}
}
if (needCloseDataSource.isEmpty() && newValue.size() == GlobalConfig.getInstance().getShadowDatasourceConfigs().size()) {
return Boolean.FALSE;
}
GlobalConfig.getInstance().setShadowDatabaseConfigs(newValue, true);
applicationConfig.setShadowDatabaseConfigs(GlobalConfig.getInstance().getShadowDatasourceConfigs());
ShadowDataSourceConfigModifyEvent shadowDataSourceConfigModifyEvent = new ShadowDataSourceConfigModifyEvent(needCloseDataSource);
EventRouter.router().publish(shadowDataSourceConfigModifyEvent);
// 清除影子表模式的sql表名替换缓存
SqlParser.clear();
PradarSwitcher.turnConfigSwitcherOn(ConfigNames.SHADOW_DATABASE_CONFIGS);
if (logger.isInfoEnabled()) {
logger.info("publish datasource config successful. new config: {}", newValue);
for (Map.Entry<String, ShadowDatabaseConfig> entry : newValue.entrySet()) {
logger.info(entry.getKey());
logger.info(entry.getValue().toString());
}
}
return Boolean.TRUE;
}
use of com.pamirs.pradar.internal.config.ShadowDatabaseConfig in project LinkAgent by shulieTech.
the class DataSourceWrapUtil method selectMatchPtDataSourceConfiguration.
@SuppressWarnings("unchecked")
private static ShadowDatabaseConfig selectMatchPtDataSourceConfiguration(ComboPooledDataSource source, Map<String, ShadowDatabaseConfig> shadowDbConfigurations) {
ComboPooledDataSource dataSource = source;
String key = DbUrlUtils.getKey(dataSource.getJdbcUrl(), dataSource.getUser());
ShadowDatabaseConfig shadowDatabaseConfig = shadowDbConfigurations.get(key);
if (shadowDatabaseConfig == null) {
key = DbUrlUtils.getKey(dataSource.getJdbcUrl(), null);
shadowDatabaseConfig = shadowDbConfigurations.get(key);
}
return shadowDatabaseConfig;
}
Aggregations