提交 85fb33cf 编写于 作者: H haocao

Refactor YamlShardingRuleConfiguration and YamlMasterSlaveRuleConfiguration.

上级 f25de54a
......@@ -55,17 +55,28 @@ public class YamMasterSlaveRuleConfiguration {
private Properties props = new Properties();
/**
* Build master-slave rule from yaml.
* Get master-slave rule from yaml.
*
* @param dataSourceMap data source map
* @return master-slave rule from yaml
* @throws SQLException SQL exception
*/
public MasterSlaveRule build(final Map<String, DataSource> dataSourceMap) throws SQLException {
public MasterSlaveRule getMasterSlaveRule(final Map<String, DataSource> dataSourceMap) throws SQLException {
return getMasterSlaveRuleConfiguration().build(dataSourceMap.isEmpty() ? dataSources : dataSourceMap);
}
/**
* Get master-slave rule configuration from yaml.
*
* @return master-slave rule configuration from yaml
*/
public MasterSlaveRuleConfiguration getMasterSlaveRuleConfiguration() {
MasterSlaveRuleConfiguration result = new MasterSlaveRuleConfiguration();
result.setName(name);
result.setMasterDataSourceName(masterDataSourceName);
result.setSlaveDataSourceNames(slaveDataSourceNames);
result.setLoadBalanceAlgorithmType(loadBalanceAlgorithmType);
result.setLoadBalanceAlgorithmClassName(loadBalanceAlgorithmClassName);
return result.build(dataSourceMap.isEmpty() ? dataSources : dataSourceMap);
return result;
}
}
......@@ -39,19 +39,19 @@ import java.util.Map;
public class YamlMasterSlaveDataSource extends MasterSlaveDataSource {
public YamlMasterSlaveDataSource(final File yamlFile) throws IOException, SQLException {
super(unmarshal(yamlFile).build(Collections.<String, DataSource>emptyMap()));
super(unmarshal(yamlFile).getMasterSlaveRule(Collections.<String, DataSource>emptyMap()));
}
public YamlMasterSlaveDataSource(final Map<String, DataSource> dataSourceMap, final File yamlFile) throws IOException, SQLException {
super(unmarshal(yamlFile).build(dataSourceMap));
super(unmarshal(yamlFile).getMasterSlaveRule(dataSourceMap));
}
public YamlMasterSlaveDataSource(final byte[] yamlByteArray) throws IOException, SQLException {
super(unmarshal(yamlByteArray).build(Collections.<String, DataSource>emptyMap()));
super(unmarshal(yamlByteArray).getMasterSlaveRule(Collections.<String, DataSource>emptyMap()));
}
public YamlMasterSlaveDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlByteArray) throws IOException, SQLException {
super(unmarshal(yamlByteArray).build(dataSourceMap));
super(unmarshal(yamlByteArray).getMasterSlaveRule(dataSourceMap));
}
private static YamMasterSlaveRuleConfiguration unmarshal(final File yamlFile) throws IOException {
......
......@@ -39,19 +39,19 @@ import java.util.Map;
public class YamlShardingDataSource extends ShardingDataSource {
public YamlShardingDataSource(final File yamlFile) throws IOException, SQLException {
super(unmarshal(yamlFile).build(Collections.<String, DataSource>emptyMap()), unmarshal(yamlFile).getProps());
super(unmarshal(yamlFile).getShardingRule(Collections.<String, DataSource>emptyMap()), unmarshal(yamlFile).getProps());
}
public YamlShardingDataSource(final Map<String, DataSource> dataSourceMap, final File yamlFile) throws IOException, SQLException {
super(unmarshal(yamlFile).build(dataSourceMap), unmarshal(yamlFile).getProps());
super(unmarshal(yamlFile).getShardingRule(dataSourceMap), unmarshal(yamlFile).getProps());
}
public YamlShardingDataSource(final byte[] yamlByteArray) throws IOException, SQLException {
super(unmarshal(yamlByteArray).build(Collections.<String, DataSource>emptyMap()), unmarshal(yamlByteArray).getProps());
super(unmarshal(yamlByteArray).getShardingRule(Collections.<String, DataSource>emptyMap()), unmarshal(yamlByteArray).getProps());
}
public YamlShardingDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlByteArray) throws IOException, SQLException {
super(unmarshal(yamlByteArray).build(dataSourceMap), unmarshal(yamlByteArray).getProps());
super(unmarshal(yamlByteArray).getShardingRule(dataSourceMap), unmarshal(yamlByteArray).getProps());
}
private static YamlShardingRuleConfiguration unmarshal(final File yamlFile) throws IOException {
......
......@@ -62,13 +62,22 @@ public class YamlShardingRuleConfiguration {
private Properties props = new Properties();
/**
* Build sharding rule from yaml.
* Get sharding rule from yaml.
*
* @param dataSourceMap data source map
* @return sharding rule from yaml
* @throws SQLException SQL exception
*/
public ShardingRule build(final Map<String, DataSource> dataSourceMap) throws SQLException {
public ShardingRule getShardingRule(final Map<String, DataSource> dataSourceMap) throws SQLException {
return getShardingRuleConfiguration().build(dataSourceMap.isEmpty() ? dataSources : dataSourceMap);
}
/**
* Get sharding rule configuration from yaml.
*
* @return sharding rule configuration from yaml
*/
public ShardingRuleConfiguration getShardingRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.setDefaultDataSourceName(defaultDataSourceName);
for (Map.Entry<String, YamlTableRuleConfiguration> entry : tables.entrySet()) {
......@@ -95,6 +104,6 @@ public class YamlShardingRuleConfiguration {
masterSlaveRuleConfigs.add(config);
}
result.setMasterSlaveRuleConfigs(masterSlaveRuleConfigs);
return result.build(dataSourceMap.isEmpty() ? dataSources : dataSourceMap);
return result;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册