Commit 7921481a authored by haocao's avatar haocao
Browse files

Upgrade to 1.1.0-SNAPSHOT, support Spring Namespace and Yaml file configuration.

parent 3c05bb3f
Loading
Loading
Loading
Loading
+27 −3
Original line number Diff line number Diff line
@@ -2,12 +2,13 @@
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dangdang</groupId>
    <artifactId>sharding-jdbc</artifactId>
    <version>1.0.2-SNAPSHOT</version>
    <version>1.1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>${project.artifactId}</name>

    <modules>
        <module>sharding-jdbc-core</module>
        <module>sharding-jdbc-config-parent</module>
    </modules>

    <properties>
@@ -35,12 +36,15 @@
        <slf4j.version>1.7.7</slf4j.version>
        <metrics.version>3.1.0</metrics.version>
        <groovy.version>2.4.5</groovy.version>
        <snakeyaml.version>1.16</snakeyaml.version>
        <springframework.version>4.1.1.RELEASE</springframework.version>
        <mysql-connector-java.version>5.1.30</mysql-connector-java.version>
        <h2.version>1.4.184</h2.version>
        <junit.version>4.12</junit.version>
        <dbunit.version>2.5.0</dbunit.version>
        <unitils.core.version>3.4.2</unitils.core.version>
        <mockito.version>1.10.19</mockito.version>
        <hamcrest.version>1.3</hamcrest.version>
        <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
        <maven-resources-plugin.version>2.7</maven-resources-plugin.version>
        <maven-jar-plugin.version>2.6</maven-jar-plugin.version>
@@ -179,14 +183,22 @@
                <version>${groovy.version}</version>
                <classifier>indy</classifier>
            </dependency>

            <dependency>
                <groupId>org.yaml</groupId>
                <artifactId>snakeyaml</artifactId>
                <version>${snakeyaml.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-connector-java.version}</version>
                <scope>runtime</scope>
            </dependency>

            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
@@ -229,6 +241,18 @@
                <version>${h2.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-library</artifactId>
                <version>${hamcrest.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${springframework.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
+69 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.dangdang</groupId>
        <artifactId>sharding-jdbc</artifactId>
        <version>1.1.0-SNAPSHOT</version>
    </parent>
    <artifactId>sharding-jdbc-config-parent</artifactId>
    <packaging>pom</packaging>
    
    <modules>
        <module>sharding-jdbc-config-common</module>
        <module>sharding-jdbc-config-yaml</module>
        <module>sharding-jdbc-config-spring</module>
    </modules>
    
    <dependencies>
        <dependency>
            <groupId>com.dangdang</groupId>
            <artifactId>sharding-jdbc-core</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.unitils</groupId>
            <artifactId>unitils-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.dangdang</groupId>
                <artifactId>sharding-jdbc</artifactId>
                <version>${project.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
 No newline at end of file
+24 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>sharding-jdbc-config-parent</artifactId>
        <groupId>com.dangdang</groupId>
        <version>1.1.0-SNAPSHOT</version>
    </parent>
    <artifactId>sharding-jdbc-config-common</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy</artifactId>
            <classifier>indy</classifier>
        </dependency>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
 No newline at end of file
+138 −0
Original line number Diff line number Diff line
/**
 * 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 com.dangdang.ddframe.rdb.sharding.config.common.api;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import javax.sql.DataSource;

import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.AbstractShardingRuleConfigFileDelegate;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import lombok.Getter;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.slf4j.LoggerFactory;

/**
 * 分片规则构建器.
 * 
 * @author gaohongtao
 */
public class ShardingRuleBuilder {
    
    private Map<String, DataSource> dataSourceMap;
    
    @Getter
    private AbstractShardingRuleConfigFileDelegate ruleConfigDelegate;
    
    private DataSourceRule dataSourceRule;
    
    /**
     * 设置数据源映射.
     * 
     * @param dataSourceMap 数据源映射
     * @return 构建器对象
     */
    public ShardingRuleBuilder setDataSourceMap(final Map<String, DataSource> dataSourceMap) {
        this.dataSourceMap = dataSourceMap;
        return this;
    }
    
    /**
     * 解析规则配置对象中的规则配置.
     * 
     * @param config 规则配置对象
     * @return 构建器对象
     */
    public ShardingRuleBuilder parse(final ShardingRuleConfig config) {
        return parse("default", config);
    }
    
    /**
     * 解析规则配置对象中的规则配置.
     * 
     * @param logRoot 规则名称
     * @param config 规则配置对象
     * @return 构建器对象
     */
    public ShardingRuleBuilder parse(final String logRoot, final ShardingRuleConfig config) {
        if (null == dataSourceMap && null != config.getDataSource()) {
            dataSourceMap = config.getDataSource();
        }
        Binding binding = new Binding();
        binding.setProperty("shardingRuleConfig", config);
        GroovyShell shell = new GroovyShell(binding);
        URL templateUrl = this.getClass().getClassLoader().getResource("shardingJDBC_config_template.groovy");
        Preconditions.checkNotNull(templateUrl);
        Object result;
        try {
            result = shell.run(templateUrl.toURI(), new String[]{});
        } catch (final IOException | URISyntaxException ignored) {
            throw new UnsupportedOperationException("can not load template");
        }
        ruleConfigDelegate = (AbstractShardingRuleConfigFileDelegate) getShell(logRoot).parse(result.toString());
        initDelegate();
        return this;
    }
    
    private GroovyShell getShell(final String logRoot) {
        CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
        compilerConfiguration.setScriptBaseClass(AbstractShardingRuleConfigFileDelegate.class.getName());
        Binding binding = new Binding();
        binding.setVariable("log", LoggerFactory.getLogger(Joiner.on(".").join("com.dangdang.ddframe.rdb.sharding.configFile", logRoot)));
        return new GroovyShell(binding, compilerConfiguration);
    }
    
    private void initDelegate() {
        if (null != dataSourceMap) {
            dataSourceRule = new DataSourceRule(dataSourceMap);
            ruleConfigDelegate.setDataSourceRule(dataSourceRule);
        }
    }
    
    /**
     * 构建分片规则.
     * 
     * @return 分片规则对象
     */
    public ShardingRule build() {
        ruleConfigDelegate.run();
        Preconditions.checkNotNull(ruleConfigDelegate.getTableRules(), "Sharding JDBC: Config file must contains table config");
        
        return new ShardingRule(ruleConfigDelegate.getDataSourceRule(), ruleConfigDelegate.getTableRules(),
                null == ruleConfigDelegate.getBindingTableRules() ? Collections.<BindingTableRule>emptyList() : ruleConfigDelegate.getBindingTableRules(),
                null == ruleConfigDelegate.getDefaultDatabaseShardingStrategy() ? new DatabaseShardingStrategy(Collections.<String>emptyList(),
                        new NoneDatabaseShardingAlgorithm()) : ruleConfigDelegate.getDefaultDatabaseShardingStrategy(),
                null == ruleConfigDelegate.getDefaultTableShardingStrategy() ? new TableShardingStrategy(Collections.<String>emptyList(),
                        new NoneTableShardingAlgorithm()) : ruleConfigDelegate.getDefaultTableShardingStrategy());
    }
    
}
+33 −0
Original line number Diff line number Diff line
/**
 * 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 com.dangdang.ddframe.rdb.sharding.config.common.api.config;

import lombok.Getter;
import lombok.Setter;

/**
 * 绑定表配置.
 * 
 * @author gaohongtao
 */
@Getter
@Setter
public class BindingTableRuleConfig {
    
    private String tableNames;
}
Loading