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

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

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