OrchestrationShardingDataSourceFactory.java 6.8 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;
T
terrymanu 已提交
23
import io.shardingjdbc.orchestration.api.config.OrchestrationConfiguration;
T
terrymanu 已提交
24
import io.shardingjdbc.orchestration.internal.OrchestrationFacade;
H
haocao 已提交
25
import io.shardingjdbc.orchestration.yaml.YamlOrchestrationShardingRuleConfiguration;
T
terrymanu 已提交
26 27
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
H
haocao 已提交
28 29
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
T
terrymanu 已提交
30 31

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

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