提交 99626b56 编写于 作者: T terrymanu

fix gson adapter for config

上级 f96c088e
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* 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.
* </p>
*/
package io.shardingjdbc.orchestration.internal.json;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.shardingjdbc.core.keygen.KeyGenerator;
import io.shardingjdbc.core.keygen.KeyGeneratorFactory;
import java.io.IOException;
/**
* Key generator gson type adapter.
*
* @author zhangliang
*/
public final class KeyGeneratorGsonTypeAdapter extends TypeAdapter<KeyGenerator> {
@Override
public KeyGenerator read(final JsonReader in) throws IOException {
String keyGeneratorClassName = null;
in.beginObject();
while (in.hasNext()) {
Preconditions.checkArgument("keyGenerator".equals(in.nextName()));
keyGeneratorClassName = in.nextString();
}
in.endObject();
return Strings.isNullOrEmpty(keyGeneratorClassName) ? null : KeyGeneratorFactory.newInstance(keyGeneratorClassName);
}
@Override
public void write(final JsonWriter out, final KeyGenerator value) throws IOException {
out.beginObject();
if (null != value) {
out.name("keyGenerator").value(value.getClass().getName());
}
out.endObject();
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* 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.
* </p>
*/
package io.shardingjdbc.orchestration.internal.json;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithm;
import io.shardingjdbc.core.exception.ShardingJdbcException;
import java.io.IOException;
/**
* Master-slave load balance algorithm gson type adapter.
*
* @author zhangliang
*/
public final class MasterSlaveLoadBalanceAlgorithmGsonTypeAdapter extends TypeAdapter<MasterSlaveLoadBalanceAlgorithm> {
@Override
public MasterSlaveLoadBalanceAlgorithm read(final JsonReader in) throws IOException {
String masterSlaveLoadBalanceAlgorithmClassName = null;
in.beginObject();
while (in.hasNext()) {
Preconditions.checkArgument("masterSlaveLoadBalanceAlgorithm".equals(in.nextName()));
masterSlaveLoadBalanceAlgorithmClassName = in.nextString();
}
in.endObject();
try {
return Strings.isNullOrEmpty(masterSlaveLoadBalanceAlgorithmClassName) ? null : (MasterSlaveLoadBalanceAlgorithm) Class.forName(masterSlaveLoadBalanceAlgorithmClassName).newInstance();
} catch (ReflectiveOperationException ex) {
throw new ShardingJdbcException(ex);
}
}
@Override
public void write(final JsonWriter out, final MasterSlaveLoadBalanceAlgorithm value) throws IOException {
out.beginObject();
if (null != value) {
out.name("masterSlaveLoadBalanceAlgorithm").value(value.getClass().getName());
}
out.endObject();
}
}
......@@ -17,8 +17,10 @@
package io.shardingjdbc.orchestration.internal.json;
import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithm;
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.api.config.strategy.ShardingStrategyConfiguration;
import io.shardingjdbc.core.keygen.KeyGenerator;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
......@@ -32,6 +34,8 @@ public final class ShardingRuleConfigurationConverter {
static {
GsonFactory.registerTypeAdapter(ShardingStrategyConfiguration.class, new ShardingStrategyConfigurationGsonTypeAdapter());
GsonFactory.registerTypeAdapter(KeyGenerator.class, new KeyGeneratorGsonTypeAdapter());
GsonFactory.registerTypeAdapter(MasterSlaveLoadBalanceAlgorithm.class, new MasterSlaveLoadBalanceAlgorithmGsonTypeAdapter());
}
/**
......
......@@ -17,6 +17,7 @@
package io.shardingjdbc.orchestration.internal.json;
import com.google.common.base.Strings;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
......@@ -88,9 +89,12 @@ public final class ShardingStrategyConfigurationGsonTypeAdapter extends TypeAdap
final String algorithmClassName, final String preciseAlgorithmClassName, final String rangeAlgorithmClassName,
final String algorithmInlineExpression) {
if (type.equals(ShardingStrategyType.STANDARD.name())) {
return new StandardShardingStrategyConfiguration(shardingColumn,
ShardingAlgorithmFactory.newInstance(preciseAlgorithmClassName, PreciseShardingAlgorithm.class),
ShardingAlgorithmFactory.newInstance(rangeAlgorithmClassName, RangeShardingAlgorithm.class));
if (Strings.isNullOrEmpty(rangeAlgorithmClassName)) {
return new StandardShardingStrategyConfiguration(shardingColumn, ShardingAlgorithmFactory.newInstance(preciseAlgorithmClassName, PreciseShardingAlgorithm.class));
} else {
return new StandardShardingStrategyConfiguration(shardingColumn, ShardingAlgorithmFactory.newInstance(preciseAlgorithmClassName, PreciseShardingAlgorithm.class),
ShardingAlgorithmFactory.newInstance(rangeAlgorithmClassName, RangeShardingAlgorithm.class));
}
}
if (type.equals(ShardingStrategyType.COMPLEX.name())) {
return new ComplexShardingStrategyConfiguration(shardingColumns, ShardingAlgorithmFactory.newInstance(algorithmClassName, ComplexKeysShardingAlgorithm.class));
......
......@@ -41,11 +41,13 @@ import static org.junit.Assert.assertThat;
public final class ShardingRuleConfigurationConverterTest {
private final String commonShardingRuleConfigJson = "{\"tableRuleConfigs\":[{\"logicTable\":\"t_order\",\"actualDataNodes\":\"t_order_${[0, 1]}\","
+ "\"databaseShardingStrategyConfig\":{},\"tableShardingStrategyConfig\":{}},"
+ "\"databaseShardingStrategyConfig\":{},\"tableShardingStrategyConfig\":{},\"keyGenerator\":{}},"
+ "{\"logicTable\":\"t_order_item\",\"actualDataNodes\":\"t_order_item_${[0, 1]}\","
+ "\"databaseShardingStrategyConfig\":{},\"tableShardingStrategyConfig\":{}}"
+ "\"databaseShardingStrategyConfig\":{},\"tableShardingStrategyConfig\":{},\"keyGenerator\":{}}"
+ "],\"bindingTableGroups\":[\"t_order, t_order_item\"],\"defaultDatabaseShardingStrategyConfig\":{},";
private final String defaultKeyGenerator = ",\"defaultKeyGenerator\":{}";
private final String masterSlaveRuleConfigJson = ",\"masterSlaveRuleConfigs\":[]}";
@Test
......@@ -149,27 +151,27 @@ public final class ShardingRuleConfigurationConverterTest {
+ "\"defaultTableShardingStrategyConfig\":{\"type\":\"STANDARD\",\"shardingColumn\":\"order_id\","
+ "\"preciseAlgorithmClassName\":\"io.shardingjdbc.orchestration.internal.json.fixture.TestPreciseShardingAlgorithm\","
+ "\"rangeAlgorithmClassName\":\"io.shardingjdbc.orchestration.internal.json.fixture.TestRangeShardingAlgorithm\"}"
+ masterSlaveRuleConfigJson;
+ defaultKeyGenerator + masterSlaveRuleConfigJson;
}
private String getJsonForComplexStrategy() {
return commonShardingRuleConfigJson + "\"defaultTableShardingStrategyConfig\":{\"type\":\"COMPLEX\",\"shardingColumns\":\"order_id,item_id\","
+ "\"algorithmClassName\":\"io.shardingjdbc.orchestration.internal.json.fixture.TestComplexKeysShardingAlgorithm\"}"
+ masterSlaveRuleConfigJson;
+ defaultKeyGenerator + masterSlaveRuleConfigJson;
}
private String getJsonForInlineStrategy() {
return commonShardingRuleConfigJson + "\"defaultTableShardingStrategyConfig\":{\"type\":\"INLINE\",\"shardingColumn\":\"order_id\",\"algorithmExpression\":\"order_${user_id % 2}\"}"
+ masterSlaveRuleConfigJson;
+ defaultKeyGenerator + masterSlaveRuleConfigJson;
}
private String getJsonForHintStrategy() {
return commonShardingRuleConfigJson + "\"defaultTableShardingStrategyConfig\":{\"type\":\"HINT\","
+ "\"algorithmClassName\":\"io.shardingjdbc.orchestration.internal.json.fixture.TestHintShardingAlgorithm\"}" + masterSlaveRuleConfigJson;
+ "\"algorithmClassName\":\"io.shardingjdbc.orchestration.internal.json.fixture.TestHintShardingAlgorithm\"}" + defaultKeyGenerator + masterSlaveRuleConfigJson;
}
private String getJsonForNoneStrategy() {
return commonShardingRuleConfigJson + "\"defaultTableShardingStrategyConfig\":{\"type\":\"NONE\"}" + masterSlaveRuleConfigJson;
return commonShardingRuleConfigJson + "\"defaultTableShardingStrategyConfig\":{\"type\":\"NONE\"}" + defaultKeyGenerator + masterSlaveRuleConfigJson;
}
private void assertCommon(final ShardingRuleConfiguration actual) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册