Commit 1ef429b7 authored by terrymanu's avatar terrymanu
Browse files

For #647: ShardingRuleException => ShardingConfigurationException

parent 242acb80
Loading
Loading
Loading
Loading
+8 −17
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithm;
import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithmType;
import io.shardingjdbc.core.exception.ShardingJdbcException;
import io.shardingjdbc.core.exception.ShardingConfigurationException;
import io.shardingjdbc.core.rule.MasterSlaveRule;
import lombok.Getter;
import lombok.Setter;
@@ -51,7 +51,7 @@ public class MasterSlaveRuleConfiguration {
    /**
     * Build master-slave rule.
     *
     * @return sharding rule
     * @return master-slave rule
     */
    public MasterSlaveRule build() {
        Preconditions.checkNotNull(name, "name cannot be null.");
@@ -62,30 +62,21 @@ public class MasterSlaveRuleConfiguration {
    }
    
    private MasterSlaveLoadBalanceAlgorithm getLoadBalanceAlgorithm() {
        MasterSlaveLoadBalanceAlgorithm result;
        if (null != loadBalanceAlgorithmType) {
            result = loadBalanceAlgorithmType.getAlgorithm();
        } else {
            result = Strings.isNullOrEmpty(loadBalanceAlgorithmClassName) ? null : newInstance(loadBalanceAlgorithmClassName);
        if (null == loadBalanceAlgorithmType) {
            return Strings.isNullOrEmpty(loadBalanceAlgorithmClassName) ? null : newInstance(loadBalanceAlgorithmClassName);
        }
        return result;
        return loadBalanceAlgorithmType.getAlgorithm();
    }
    
    /**
     * New instance.
     * 
     * @param masterSlaveLoadBalanceAlgorithmClassName master-slave load balance algorithm class name
     * @return master-slave load balance algorithm
     */
    public MasterSlaveLoadBalanceAlgorithm newInstance(final String masterSlaveLoadBalanceAlgorithmClassName) {
    private MasterSlaveLoadBalanceAlgorithm newInstance(final String masterSlaveLoadBalanceAlgorithmClassName) {
        try {
            Class<?> result = Class.forName(masterSlaveLoadBalanceAlgorithmClassName);
            if (!MasterSlaveLoadBalanceAlgorithm.class.isAssignableFrom(result)) {
                throw new ShardingJdbcException("Class %s should be implement %s", masterSlaveLoadBalanceAlgorithmClassName, MasterSlaveLoadBalanceAlgorithm.class.getName());
                throw new ShardingConfigurationException("Class %s should be implement %s", masterSlaveLoadBalanceAlgorithmClassName, MasterSlaveLoadBalanceAlgorithm.class.getName());
            }
            return (MasterSlaveLoadBalanceAlgorithm) result.newInstance();
        } catch (final ReflectiveOperationException ex) {
            throw new ShardingJdbcException("Class %s should have public privilege and no argument constructor", masterSlaveLoadBalanceAlgorithmClassName);
            throw new ShardingConfigurationException("Class %s should have public privilege and no argument constructor", masterSlaveLoadBalanceAlgorithmClassName);
        }
    }
}
+3 −3
Original line number Diff line number Diff line
@@ -15,14 +15,14 @@
 * </p>
 */

package io.shardingjdbc.core.rule;
package io.shardingjdbc.core.exception;

/**
 * Sharding rule exception.
 *
 * @author zhangliang
 */
public final class ShardingRuleException extends RuntimeException {
public final class ShardingConfigurationException extends RuntimeException {
    
    private static final long serialVersionUID = -1360264079938958332L;
    
@@ -32,7 +32,7 @@ public final class ShardingRuleException extends RuntimeException {
     * @param errorMessage formatted error message
     * @param args arguments of error message
     */
    public ShardingRuleException(final String errorMessage, final Object... args) {
    public ShardingConfigurationException(final String errorMessage, final Object... args) {
        super(String.format(errorMessage, args));
    }
}
+3 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package io.shardingjdbc.core.rule;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import io.shardingjdbc.core.exception.ShardingConfigurationException;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@@ -70,14 +71,14 @@ public final class BindingTableRule {
            }
        }
        if (-1 == index) {
            throw new ShardingRuleException("Actual table [%s].[%s] is not in table config", dataSource, otherActualTable);
            throw new ShardingConfigurationException("Actual table [%s].[%s] is not in table config", dataSource, otherActualTable);
        }
        for (TableRule each : tableRules) {
            if (each.getLogicTable().equals(logicTable.toLowerCase())) {
                return each.getActualDataNodes().get(index).getTableName().toLowerCase();
            }
        }
        throw new ShardingRuleException("Cannot find binding actual table, data source: %s, logic table: %s, other actual table: %s", dataSource, logicTable, otherActualTable);
        throw new ShardingConfigurationException("Cannot find binding actual table, data source: %s, logic table: %s, other actual table: %s", dataSource, logicTable, otherActualTable);
    }
    
    Collection<String> getAllLogicTables() {
+2 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
package io.shardingjdbc.core.rule;

import com.google.common.base.Splitter;
import io.shardingjdbc.core.exception.ShardingConfigurationException;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@@ -49,7 +50,7 @@ public class DataNode {
     */
    public DataNode(final String dataNode) {
        if (!isValidDataNode(dataNode)) {
            throw new ShardingRuleException("Invalid format for actual data nodes: '%s'", dataNode);
            throw new ShardingConfigurationException("Invalid format for actual data nodes: '%s'", dataNode);
        }
        List<String> segments = Splitter.on(DELIMITER).splitToList(dataNode);
        dataSourceName = segments.get(0);
+5 −4
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package io.shardingjdbc.core.rule;

import com.google.common.base.Optional;
import com.google.common.base.Strings;
import io.shardingjdbc.core.exception.ShardingConfigurationException;
import io.shardingjdbc.core.keygen.KeyGenerator;
import io.shardingjdbc.core.parsing.parser.context.condition.Column;
import io.shardingjdbc.core.routing.strategy.ShardingStrategy;
@@ -127,7 +128,7 @@ public final class ShardingRule {
        if (null != defaultDataSourceName) {
            return createTableRuleWithDefaultDataSource(logicTableName.toLowerCase());
        }
        throw new ShardingRuleException("Cannot find table rule and default data source with logic table: '%s'", logicTableName);
        throw new ShardingConfigurationException("Cannot find table rule and default data source with logic table: '%s'", logicTableName);
    }
    
    private TableRule createTableRuleWithDefaultDataSource(final String logicTableName) {
@@ -269,7 +270,7 @@ public final class ShardingRule {
    public Number generateKey(final String logicTableName) {
        Optional<TableRule> tableRule = tryFindTableRuleByLogicTable(logicTableName);
        if (!tableRule.isPresent()) {
            throw new ShardingRuleException("Cannot find strategy for generate keys.");
            throw new ShardingConfigurationException("Cannot find strategy for generate keys.");
        }
        if (null != tableRule.get().getKeyGenerator()) {
            return tableRule.get().getKeyGenerator().generateKey();
@@ -289,7 +290,7 @@ public final class ShardingRule {
                return each.getLogicTable();
            }
        }
        throw new ShardingRuleException("Cannot find logic table name with logic index name: '%s'", logicIndexName);
        throw new ShardingConfigurationException("Cannot find logic table name with logic index name: '%s'", logicIndexName);
    }
    
    /**
@@ -305,6 +306,6 @@ public final class ShardingRule {
                return each;
            }
        }
        throw new ShardingRuleException("Cannot find actual data node for logic table name: '%s'", tableRule.getLogicTable());
        throw new ShardingConfigurationException("Cannot find actual data node for logic table name: '%s'", tableRule.getLogicTable());
    }
}
Loading