OrchestrationShardingDataSourceFactory.java 6.9 KB
Newer Older
T
terrymanu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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>
 */

18
package io.shardingjdbc.orchestration.api;
T
terrymanu 已提交
19

T
terrymanu 已提交
20
import io.shardingjdbc.core.api.ShardingDataSourceFactory;
T
terrymanu 已提交
21
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
T
terrymanu 已提交
22
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
H
haocao 已提交
23
import io.shardingjdbc.core.yaml.sharding.YamlShardingRuleConfiguration;
T
terrymanu 已提交
24
import io.shardingjdbc.orchestration.api.config.OrchestrationConfiguration;
T
terrymanu 已提交
25
import io.shardingjdbc.orchestration.internal.OrchestrationFacade;
H
haocao 已提交
26
import io.shardingjdbc.orchestration.yaml.YamlOrchestrationShardingRuleConfiguration;
T
terrymanu 已提交
27 28
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
H
haocao 已提交
29 30
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
T
terrymanu 已提交
31 32

import javax.sql.DataSource;
H
haocao 已提交
33 34 35 36 37
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
T
terrymanu 已提交
38
import java.sql.SQLException;
T
terrymanu 已提交
39
import java.util.Map;
T
terrymanu 已提交
40 41 42 43
import java.util.Properties;

/**
 * Orchestration sharding data source factory.
H
haocao 已提交
44 45 46
 *
 * @author zhangliang
 * @author caohao
T
terrymanu 已提交
47 48 49 50 51 52
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class OrchestrationShardingDataSourceFactory {
    
    /**
     * Create sharding data source.
53
     *
T
terrymanu 已提交
54 55
     * @param dataSourceMap data source map
     * @param shardingRuleConfig sharding rule configuration
T
terrymanu 已提交
56
     * @param orchestrationConfig orchestration master-slave configuration
T
terrymanu 已提交
57
     * @param props properties for data source
H
haocao 已提交
58
     *
T
terrymanu 已提交
59 60 61
     * @return sharding data source
     * @throws SQLException SQL exception
     */
H
haocao 已提交
62 63
    public static DataSource createDataSource(
            final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig, 
H
haocao 已提交
64 65
            final OrchestrationConfiguration orchestrationConfig, final Map<String, Object> configMap, final Properties props) throws SQLException {
        ShardingDataSource result = (ShardingDataSource) ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, configMap, props);
T
terrymanu 已提交
66
        new OrchestrationFacade(orchestrationConfig).initShardingOrchestration(dataSourceMap, shardingRuleConfig, props, result);
T
terrymanu 已提交
67
        return result;
68
    }
H
haocao 已提交
69 70 71 72 73 74 75 76 77 78
    
    /**
     * Create sharding data source.
     *
     * @param yamlFile yaml file for rule configuration of databases and tables sharding with data sources
     * @return sharding data source
     * @throws SQLException SQL exception
     * @throws IOException IO exception
     */
    public static DataSource createDataSource(final File yamlFile) throws SQLException, IOException {
H
haocao 已提交
79
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlFile);
H
haocao 已提交
80 81 82
        YamlShardingRuleConfiguration shardingRuleConfig = config.getShardingRule();
        return createDataSource(config.getDataSources(), shardingRuleConfig.getShardingRuleConfiguration(), config.getOrchestration().getOrchestrationConfiguration(), 
                shardingRuleConfig.getData(), shardingRuleConfig.getProps());
H
haocao 已提交
83 84 85 86 87 88 89 90 91 92 93 94
    }
    
    /**
     * Create sharding data source.
     *
     * @param dataSourceMap data source map
     * @param yamlFile yaml file for rule configuration of databases and tables sharding without data sources
     * @return sharding data source
     * @throws SQLException SQL exception
     * @throws IOException IO exception
     */
    public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final File yamlFile) throws SQLException, IOException {
H
haocao 已提交
95
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlFile);
H
haocao 已提交
96 97 98
        YamlShardingRuleConfiguration shardingRuleConfig = config.getShardingRule();
        return createDataSource(dataSourceMap, shardingRuleConfig.getShardingRuleConfiguration(), config.getOrchestration().getOrchestrationConfiguration(), 
                shardingRuleConfig.getData(), shardingRuleConfig.getProps());
H
haocao 已提交
99 100 101 102 103 104 105 106 107 108 109
    }
    
    /**
     * Create sharding data source.
     *
     * @param yamlByteArray yaml byte array for rule configuration of databases and tables sharding with data sources
     * @return sharding data source
     * @throws SQLException SQL exception
     * @throws IOException IO exception
     */
    public static DataSource createDataSource(final byte[] yamlByteArray) throws SQLException, IOException {
H
haocao 已提交
110
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlByteArray);
H
haocao 已提交
111 112 113
        YamlShardingRuleConfiguration shardingRuleConfig = config.getShardingRule();
        return createDataSource(config.getDataSources(), shardingRuleConfig.getShardingRuleConfiguration(), config.getOrchestration().getOrchestrationConfiguration(), 
                shardingRuleConfig.getData(), shardingRuleConfig.getProps());
H
haocao 已提交
114 115 116 117 118 119 120 121 122 123 124 125
    }
    
    /**
     * Create sharding data source.
     *
     * @param dataSourceMap data source map
     * @param yamlByteArray yaml byte array for rule configuration of databases and tables sharding without data sources
     * @return sharding data source
     * @throws SQLException SQL exception
     * @throws IOException IO exception
     */
    public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlByteArray) throws SQLException, IOException {
H
haocao 已提交
126
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlByteArray);
H
haocao 已提交
127 128 129
        YamlShardingRuleConfiguration shardingRuleConfig = config.getShardingRule();
        return createDataSource(dataSourceMap, shardingRuleConfig.getShardingRuleConfiguration(), config.getOrchestration().getOrchestrationConfiguration(), 
                shardingRuleConfig.getData(), shardingRuleConfig.getProps());
H
haocao 已提交
130 131
    }
    
H
haocao 已提交
132
    private static YamlOrchestrationShardingRuleConfiguration unmarshal(final File yamlFile) throws IOException {
H
haocao 已提交
133 134 135 136
        try (
                FileInputStream fileInputStream = new FileInputStream(yamlFile);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8")
        ) {
H
haocao 已提交
137
            return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(inputStreamReader, YamlOrchestrationShardingRuleConfiguration.class);
H
haocao 已提交
138 139 140
        }
    }
    
H
haocao 已提交
141 142
    private static YamlOrchestrationShardingRuleConfiguration unmarshal(final byte[] yamlByteArray) throws IOException {
        return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlOrchestrationShardingRuleConfiguration.class);
H
haocao 已提交
143
    }
T
terrymanu 已提交
144
}