|
17 | 17 |
|
18 | 18 | package io.shardingjdbc.core.api.config;
|
19 | 19 |
|
20 |
| -import com.google.common.base.Preconditions; |
21 |
| -import com.google.common.base.Strings; |
22 | 20 | import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithm;
|
23 |
| -import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithmType; |
24 |
| -import io.shardingjdbc.core.exception.ShardingJdbcException; |
25 |
| -import io.shardingjdbc.core.rule.MasterSlaveRule; |
| 21 | +import lombok.AllArgsConstructor; |
26 | 22 | import lombok.Getter;
|
27 |
| -import lombok.Setter; |
| 23 | +import lombok.RequiredArgsConstructor; |
28 | 24 |
|
29 | 25 | import java.util.Collection;
|
30 |
| -import java.util.LinkedList; |
31 | 26 |
|
32 | 27 | /**
|
33 | 28 | * Master-slave rule configuration.
|
34 | 29 | *
|
35 | 30 | * @author zhangliang
|
36 | 31 | */
|
| 32 | +@AllArgsConstructor |
| 33 | +@RequiredArgsConstructor |
37 | 34 | @Getter
|
38 |
| -@Setter |
39 |
| -public class MasterSlaveRuleConfiguration { |
| 35 | +public final class MasterSlaveRuleConfiguration { |
40 | 36 |
|
41 |
| - private String name; |
| 37 | + private final String name; |
42 | 38 |
|
43 |
| - private String masterDataSourceName; |
| 39 | + private final String masterDataSourceName; |
44 | 40 |
|
45 |
| - private Collection<String> slaveDataSourceNames = new LinkedList<>(); |
| 41 | + private final Collection<String> slaveDataSourceNames; |
46 | 42 |
|
47 |
| - private MasterSlaveLoadBalanceAlgorithmType loadBalanceAlgorithmType; |
48 |
| - |
49 |
| - private String loadBalanceAlgorithmClassName; |
50 |
| - |
51 |
| - /** |
52 |
| - * Build master-slave rule. |
53 |
| - * |
54 |
| - * @return sharding rule |
55 |
| - */ |
56 |
| - public MasterSlaveRule build() { |
57 |
| - Preconditions.checkNotNull(name, "name cannot be null."); |
58 |
| - Preconditions.checkNotNull(masterDataSourceName, "masterDataSourceName cannot be null."); |
59 |
| - Preconditions.checkNotNull(slaveDataSourceNames, "slaveDataSourceNames cannot be null."); |
60 |
| - Preconditions.checkArgument(!slaveDataSourceNames.isEmpty(), "slaveDataSourceNames cannot be empty."); |
61 |
| - return new MasterSlaveRule(name, masterDataSourceName, slaveDataSourceNames, getLoadBalanceAlgorithm()); |
62 |
| - } |
63 |
| - |
64 |
| - private MasterSlaveLoadBalanceAlgorithm getLoadBalanceAlgorithm() { |
65 |
| - MasterSlaveLoadBalanceAlgorithm result; |
66 |
| - if (null != loadBalanceAlgorithmType) { |
67 |
| - result = loadBalanceAlgorithmType.getAlgorithm(); |
68 |
| - } else { |
69 |
| - result = Strings.isNullOrEmpty(loadBalanceAlgorithmClassName) ? null : newInstance(loadBalanceAlgorithmClassName); |
70 |
| - } |
71 |
| - return result; |
72 |
| - } |
73 |
| - |
74 |
| - /** |
75 |
| - * New instance. |
76 |
| - * |
77 |
| - * @param masterSlaveLoadBalanceAlgorithmClassName master-slave load balance algorithm class name |
78 |
| - * @return master-slave load balance algorithm |
79 |
| - */ |
80 |
| - public MasterSlaveLoadBalanceAlgorithm newInstance(final String masterSlaveLoadBalanceAlgorithmClassName) { |
81 |
| - try { |
82 |
| - Class<?> result = Class.forName(masterSlaveLoadBalanceAlgorithmClassName); |
83 |
| - if (!MasterSlaveLoadBalanceAlgorithm.class.isAssignableFrom(result)) { |
84 |
| - throw new ShardingJdbcException("Class %s should be implement %s", masterSlaveLoadBalanceAlgorithmClassName, MasterSlaveLoadBalanceAlgorithm.class.getName()); |
85 |
| - } |
86 |
| - return (MasterSlaveLoadBalanceAlgorithm) result.newInstance(); |
87 |
| - } catch (final ReflectiveOperationException ex) { |
88 |
| - throw new ShardingJdbcException("Class %s should have public privilege and no argument constructor", masterSlaveLoadBalanceAlgorithmClassName); |
89 |
| - } |
90 |
| - } |
| 43 | + private MasterSlaveLoadBalanceAlgorithm loadBalanceAlgorithm; |
91 | 44 | }
|
0 commit comments