diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/api/MasterSlaveDataSourceFactory.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/api/MasterSlaveDataSourceFactory.java index b7dcd3dc24bf8dd8c21155ef4eb0cdcba1ead445..0d26a5bcf7c18544aae35c27fdedd8b323307a31 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/api/MasterSlaveDataSourceFactory.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/api/MasterSlaveDataSourceFactory.java @@ -19,14 +19,20 @@ package io.shardingjdbc.core.api; import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration; import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource; -import io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveDataSource; +import io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; import javax.sql.DataSource; +import java.io.ByteArrayInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStreamReader; import java.sql.SQLException; +import java.util.Collections; import java.util.Map; /** @@ -62,7 +68,8 @@ public final class MasterSlaveDataSourceFactory { * @throws IOException IO exception */ public static DataSource createDataSource(final File yamlFile) throws SQLException, IOException { - return new YamlMasterSlaveDataSource(yamlFile); + YamlMasterSlaveRuleConfiguration config = unmarshal(yamlFile); + return new MasterSlaveDataSource(config.getMasterSlaveRule(Collections.emptyMap())); } /** @@ -77,7 +84,8 @@ public final class MasterSlaveDataSourceFactory { * @throws IOException IO exception */ public static DataSource createDataSource(final Map dataSourceMap, final File yamlFile) throws SQLException, IOException { - return new YamlMasterSlaveDataSource(dataSourceMap, yamlFile); + YamlMasterSlaveRuleConfiguration config = unmarshal(yamlFile); + return new MasterSlaveDataSource(config.getMasterSlaveRule(dataSourceMap)); } /** @@ -91,7 +99,8 @@ public final class MasterSlaveDataSourceFactory { * @throws IOException IO exception */ public static DataSource createDataSource(final byte[] yamlByteArray) throws SQLException, IOException { - return new YamlMasterSlaveDataSource(yamlByteArray); + YamlMasterSlaveRuleConfiguration config = unmarshal(yamlByteArray); + return new MasterSlaveDataSource(config.getMasterSlaveRule(Collections.emptyMap())); } /** @@ -106,6 +115,20 @@ public final class MasterSlaveDataSourceFactory { * @throws IOException IO exception */ public static DataSource createDataSource(final Map dataSourceMap, final byte[] yamlByteArray) throws SQLException, IOException { - return new YamlMasterSlaveDataSource(dataSourceMap, yamlByteArray); + YamlMasterSlaveRuleConfiguration config = unmarshal(yamlByteArray); + return new MasterSlaveDataSource(config.getMasterSlaveRule(dataSourceMap)); + } + + private static YamlMasterSlaveRuleConfiguration unmarshal(final File yamlFile) throws IOException { + try ( + FileInputStream fileInputStream = new FileInputStream(yamlFile); + InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8") + ) { + return new Yaml(new Constructor(YamlMasterSlaveRuleConfiguration.class)).loadAs(inputStreamReader, YamlMasterSlaveRuleConfiguration.class); + } + } + + private static YamlMasterSlaveRuleConfiguration unmarshal(final byte[] yamlByteArray) throws IOException { + return new Yaml(new Constructor(YamlMasterSlaveRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlMasterSlaveRuleConfiguration.class); } } diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/api/ShardingDataSourceFactory.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/api/ShardingDataSourceFactory.java index 875721624346d48f1f47f0b716593f87533aaddd..42eaaa707476e5abf413f35578e24e845f8056fd 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/api/ShardingDataSourceFactory.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/api/ShardingDataSourceFactory.java @@ -19,14 +19,20 @@ package io.shardingjdbc.core.api; import io.shardingjdbc.core.api.config.ShardingRuleConfiguration; import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource; -import io.shardingjdbc.core.yaml.sharding.YamlShardingDataSource; +import io.shardingjdbc.core.yaml.sharding.YamlShardingRuleConfiguration; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; import javax.sql.DataSource; +import java.io.ByteArrayInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStreamReader; import java.sql.SQLException; +import java.util.Collections; import java.util.Map; import java.util.Properties; @@ -72,7 +78,8 @@ public final class ShardingDataSourceFactory { * @throws IOException IO exception */ public static DataSource createDataSource(final File yamlFile) throws SQLException, IOException { - return new YamlShardingDataSource(yamlFile); + YamlShardingRuleConfiguration config = unmarshal(yamlFile); + return new ShardingDataSource(config.getShardingRule(Collections.emptyMap()), config.getProps()); } /** @@ -85,7 +92,8 @@ public final class ShardingDataSourceFactory { * @throws SQLException IO exception */ public static DataSource createDataSource(final Map dataSourceMap, final File yamlFile) throws SQLException, IOException { - return new YamlShardingDataSource(dataSourceMap, yamlFile); + YamlShardingRuleConfiguration config = unmarshal(yamlFile); + return new ShardingDataSource(config.getShardingRule(dataSourceMap), config.getProps()); } /** @@ -97,7 +105,8 @@ public final class ShardingDataSourceFactory { * @throws SQLException IO exception */ public static DataSource createDataSource(final byte[] yamlByteArray) throws SQLException, IOException { - return new YamlShardingDataSource(yamlByteArray); + YamlShardingRuleConfiguration config = unmarshal(yamlByteArray); + return new ShardingDataSource(config.getShardingRule(Collections.emptyMap()), config.getProps()); } /** @@ -110,6 +119,20 @@ public final class ShardingDataSourceFactory { * @throws SQLException IO exception */ public static DataSource createDataSource(final Map dataSourceMap, final byte[] yamlByteArray) throws SQLException, IOException { - return new YamlShardingDataSource(dataSourceMap, yamlByteArray); + YamlShardingRuleConfiguration config = unmarshal(yamlByteArray); + return new ShardingDataSource(config.getShardingRule(dataSourceMap), config.getProps()); + } + + private static YamlShardingRuleConfiguration unmarshal(final File yamlFile) throws IOException { + try ( + FileInputStream fileInputStream = new FileInputStream(yamlFile); + InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8") + ) { + return new Yaml(new Constructor(YamlShardingRuleConfiguration.class)).loadAs(inputStreamReader, YamlShardingRuleConfiguration.class); + } + } + + private static YamlShardingRuleConfiguration unmarshal(final byte[] yamlByteArray) throws IOException { + return new Yaml(new Constructor(YamlShardingRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlShardingRuleConfiguration.class); } } diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/yaml/masterslave/YamlMasterSlaveDataSource.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/yaml/masterslave/YamlMasterSlaveDataSource.java deleted file mode 100644 index 80efaf9d7ec472a671c6dbf5b0138f631a23585a..0000000000000000000000000000000000000000 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/yaml/masterslave/YamlMasterSlaveDataSource.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 1999-2015 dangdang.com. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingjdbc.core.yaml.masterslave; - -import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.Constructor; - -import javax.sql.DataSource; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.sql.SQLException; -import java.util.Collections; -import java.util.Map; - -/** - * Master-slave datasource for yaml. - * - * @author caohao - */ -public class YamlMasterSlaveDataSource extends MasterSlaveDataSource { - - public YamlMasterSlaveDataSource(final File yamlFile) throws IOException, SQLException { - super(unmarshal(yamlFile).getMasterSlaveRule(Collections.emptyMap())); - } - - public YamlMasterSlaveDataSource(final Map dataSourceMap, final File yamlFile) throws IOException, SQLException { - super(unmarshal(yamlFile).getMasterSlaveRule(dataSourceMap)); - } - - public YamlMasterSlaveDataSource(final byte[] yamlByteArray) throws IOException, SQLException { - super(unmarshal(yamlByteArray).getMasterSlaveRule(Collections.emptyMap())); - } - - public YamlMasterSlaveDataSource(final Map dataSourceMap, final byte[] yamlByteArray) throws IOException, SQLException { - super(unmarshal(yamlByteArray).getMasterSlaveRule(dataSourceMap)); - } - - private static YamlMasterSlaveRuleConfiguration unmarshal(final File yamlFile) throws IOException { - try ( - FileInputStream fileInputStream = new FileInputStream(yamlFile); - InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8") - ) { - return new Yaml(new Constructor(YamlMasterSlaveRuleConfiguration.class)).loadAs(inputStreamReader, YamlMasterSlaveRuleConfiguration.class); - } - } - - private static YamlMasterSlaveRuleConfiguration unmarshal(final byte[] yamlByteArray) throws IOException { - return new Yaml(new Constructor(YamlMasterSlaveRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlMasterSlaveRuleConfiguration.class); - } -} diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/yaml/sharding/YamlShardingDataSource.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/yaml/sharding/YamlShardingDataSource.java deleted file mode 100644 index ae36d3aa2ee54a2976aeb4eabcde5b5b14d48733..0000000000000000000000000000000000000000 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/yaml/sharding/YamlShardingDataSource.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 1999-2015 dangdang.com. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - *

- */ - -package io.shardingjdbc.core.yaml.sharding; - -import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.constructor.Constructor; - -import javax.sql.DataSource; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.sql.SQLException; -import java.util.Collections; -import java.util.Map; - -/** - * Sharding datasource for yaml. - * - * @author zhangliang - */ -public class YamlShardingDataSource extends ShardingDataSource { - - public YamlShardingDataSource(final File yamlFile) throws IOException, SQLException { - super(unmarshal(yamlFile).getShardingRule(Collections.emptyMap()), unmarshal(yamlFile).getProps()); - } - - public YamlShardingDataSource(final Map dataSourceMap, final File yamlFile) throws IOException, SQLException { - super(unmarshal(yamlFile).getShardingRule(dataSourceMap), unmarshal(yamlFile).getProps()); - } - - public YamlShardingDataSource(final byte[] yamlByteArray) throws IOException, SQLException { - super(unmarshal(yamlByteArray).getShardingRule(Collections.emptyMap()), unmarshal(yamlByteArray).getProps()); - } - - public YamlShardingDataSource(final Map dataSourceMap, final byte[] yamlByteArray) throws IOException, SQLException { - super(unmarshal(yamlByteArray).getShardingRule(dataSourceMap), unmarshal(yamlByteArray).getProps()); - } - - private static YamlShardingRuleConfiguration unmarshal(final File yamlFile) throws IOException { - try ( - FileInputStream fileInputStream = new FileInputStream(yamlFile); - InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8") - ) { - return new Yaml(new Constructor(YamlShardingRuleConfiguration.class)).loadAs(inputStreamReader, YamlShardingRuleConfiguration.class); - } - } - - private static YamlShardingRuleConfiguration unmarshal(final byte[] yamlByteArray) throws IOException { - return new Yaml(new Constructor(YamlShardingRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlShardingRuleConfiguration.class); - } -} diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/masterslave/YamlMasterSlaveIntegrateTest.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/masterslave/YamlMasterSlaveIntegrateTest.java index 1549e4cb92f5f144073ef8d1524948ffd1b0d4fe..e3af6baec046e83295715fd48419ff53db71d219 100644 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/masterslave/YamlMasterSlaveIntegrateTest.java +++ b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/masterslave/YamlMasterSlaveIntegrateTest.java @@ -20,6 +20,7 @@ package io.shardingjdbc.core.yaml.masterslave; import com.google.common.base.Function; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import io.shardingjdbc.core.api.MasterSlaveDataSourceFactory; import io.shardingjdbc.core.yaml.AbstractYamlDataSourceTest; import lombok.RequiredArgsConstructor; import org.junit.Test; @@ -57,9 +58,9 @@ public class YamlMasterSlaveIntegrateTest extends AbstractYamlDataSourceTest { File yamlFile = new File(YamlMasterSlaveIntegrateTest.class.getResource(filePath).toURI()); DataSource dataSource; if (hasDataSource) { - dataSource = new YamlMasterSlaveDataSource(yamlFile); + dataSource = MasterSlaveDataSourceFactory.createDataSource(yamlFile); } else { - dataSource = new YamlMasterSlaveDataSource(Maps.asMap(Sets.newHashSet("db_master", "db_slave_0", "db_slave_1"), new Function() { + dataSource = MasterSlaveDataSourceFactory.createDataSource(Maps.asMap(Sets.newHashSet("db_master", "db_slave_0", "db_slave_1"), new Function() { @Override public DataSource apply(final String key) { return createDataSource(key); diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingDataSourceTest.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingDataSourceTest.java index dbc0f331d4eadf89943029d74e00bc5f5ee14f97..0a78fb8a977e9be45a9787a6a7f5958333ff3b43 100644 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingDataSourceTest.java +++ b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingDataSourceTest.java @@ -17,6 +17,7 @@ package io.shardingjdbc.core.yaml.sharding; +import io.shardingjdbc.core.api.ShardingDataSourceFactory; import io.shardingjdbc.core.exception.ShardingJdbcException; import io.shardingjdbc.core.jdbc.core.ShardingContext; import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource; @@ -75,14 +76,14 @@ public class YamlShardingDataSourceTest { } private ShardingRule getShardingRule(final String fileName) throws NoSuchFieldException, IllegalAccessException, URISyntaxException, IOException, SQLException { - return getShardingRule(new YamlShardingDataSource(new File(getClass().getResource(fileName).toURI()))); + return getShardingRule(ShardingDataSourceFactory.createDataSource(new File(getClass().getResource(fileName).toURI()))); } private ShardingRule getShardingRule(final Map dataSourceMap, final String fileName) throws ReflectiveOperationException, URISyntaxException, IOException, SQLException { - return getShardingRule(new YamlShardingDataSource(dataSourceMap, new File(getClass().getResource(fileName).toURI()))); + return getShardingRule(ShardingDataSourceFactory.createDataSource(dataSourceMap, new File(getClass().getResource(fileName).toURI()))); } - private ShardingRule getShardingRule(final ShardingDataSource shardingDataSource) throws NoSuchFieldException, IllegalAccessException { + private ShardingRule getShardingRule(final DataSource shardingDataSource) throws NoSuchFieldException, IllegalAccessException { Field field = ShardingDataSource.class.getDeclaredField("shardingContext"); field.setAccessible(true); ShardingContext shardingContext = (ShardingContext) field.get(shardingDataSource); diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingIntegrateTest.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingIntegrateTest.java index d6f741e750f84ece8bc2aeb34371dab227a0b705..129f50134506f5ad3f202986c775ef76457bff1e 100644 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingIntegrateTest.java +++ b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingIntegrateTest.java @@ -20,6 +20,7 @@ package io.shardingjdbc.core.yaml.sharding; import com.google.common.base.Function; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import io.shardingjdbc.core.api.ShardingDataSourceFactory; import io.shardingjdbc.core.yaml.AbstractYamlDataSourceTest; import lombok.RequiredArgsConstructor; import org.junit.Test; @@ -59,9 +60,9 @@ public class YamlShardingIntegrateTest extends AbstractYamlDataSourceTest { File yamlFile = new File(YamlShardingIntegrateTest.class.getResource(filePath).toURI()); DataSource dataSource; if (hasDataSource) { - dataSource = new YamlShardingDataSource(yamlFile); + dataSource = ShardingDataSourceFactory.createDataSource(yamlFile); } else { - dataSource = new YamlShardingDataSource(Maps.asMap(Sets.newHashSet("db0", "db1"), new Function() { + dataSource = ShardingDataSourceFactory.createDataSource(Maps.asMap(Sets.newHashSet("db0", "db1"), new Function() { @Override public DataSource apply(final String key) { return createDataSource(key); diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingWithMasterSlaveIntegrateTest.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingWithMasterSlaveIntegrateTest.java index 5ff03bfe91ab81bf43ff349d6f1a86a739d8ca03..5ef13de15bbde2c0281fe4e9a0fc21b3609bf096 100644 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingWithMasterSlaveIntegrateTest.java +++ b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingWithMasterSlaveIntegrateTest.java @@ -20,6 +20,7 @@ package io.shardingjdbc.core.yaml.sharding; import com.google.common.base.Function; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import io.shardingjdbc.core.api.ShardingDataSourceFactory; import io.shardingjdbc.core.yaml.AbstractYamlDataSourceTest; import lombok.RequiredArgsConstructor; import org.junit.Test; @@ -61,7 +62,7 @@ public class YamlShardingWithMasterSlaveIntegrateTest extends AbstractYamlDataSo File yamlFile = new File(YamlShardingWithMasterSlaveIntegrateTest.class.getResource(filePath).toURI()); DataSource dataSource; if (hasDataSource) { - dataSource = new YamlShardingDataSource(yamlFile); + dataSource = ShardingDataSourceFactory.createDataSource(yamlFile); } else { Map dataSourceMap = Maps.asMap(Sets.newHashSet("db0_master", "db0_slave", "db1_master", "db1_slave"), new Function() { @Override @@ -73,7 +74,7 @@ public class YamlShardingWithMasterSlaveIntegrateTest extends AbstractYamlDataSo for (Map.Entry each : dataSourceMap.entrySet()) { result.put(each.getKey(), each.getValue()); } - dataSource = new YamlShardingDataSource(result, yamlFile); + dataSource = ShardingDataSourceFactory.createDataSource(result, yamlFile); } try (Connection conn = dataSource.getConnection(); Statement stm = conn.createStatement()) {