OrchestrationShardingDataSourceFactory.java 10.8 KB
Newer Older
T
terrymanu 已提交
1 2 3 4 5 6 7
/*
 * 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
 *
T
terrymanu 已提交
8
 *     http://www.apache.org/licenses/LICENSE-2.0
T
terrymanu 已提交
9 10 11 12 13 14 15 16 17
 *
 * 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

马晓光 已提交
20
import com.google.common.base.Preconditions;
21 22
import io.shardingjdbc.core.api.MasterSlaveDataSourceFactory;
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;
T
terrymanu 已提交
23
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
H
haocao 已提交
24
import io.shardingjdbc.core.yaml.sharding.YamlShardingRuleConfiguration;
T
terrymanu 已提交
25
import io.shardingjdbc.orchestration.api.config.OrchestrationConfiguration;
26
import io.shardingjdbc.orchestration.internal.OrchestrationFacade;
H
haocao 已提交
27
import io.shardingjdbc.orchestration.internal.OrchestrationShardingDataSource;
28
import io.shardingjdbc.orchestration.internal.config.ConfigurationService;
H
haocao 已提交
29
import io.shardingjdbc.orchestration.yaml.YamlOrchestrationShardingRuleConfiguration;
T
terrymanu 已提交
30 31
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
H
haocao 已提交
32 33
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
T
terrymanu 已提交
34 35

import javax.sql.DataSource;
H
haocao 已提交
36 37 38 39 40
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
T
terrymanu 已提交
41
import java.sql.SQLException;
42
import java.util.Collections;
43
import java.util.LinkedHashMap;
T
terrymanu 已提交
44
import java.util.Map;
T
terrymanu 已提交
45 46 47 48
import java.util.Properties;

/**
 * Orchestration sharding data source factory.
H
haocao 已提交
49 50 51
 *
 * @author zhangliang
 * @author caohao
T
terrymanu 已提交
52 53 54
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class OrchestrationShardingDataSourceFactory {
M
maxiaoguang 已提交
55
    
56 57 58 59 60
    /**
     * Create sharding data source.
     *
     * @param dataSourceMap data source map
     * @param shardingRuleConfig sharding rule configuration
马晓光 已提交
61
     * @param orchestrationConfig orchestration configuration
62 63 64 65 66 67 68 69 70
     * @param configMap config map
     * @param props properties for data source
     * @return sharding data source
     * @throws SQLException SQL exception
     */
    public static DataSource createDataSource(
            final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig,
            final Map<String, Object> configMap, final Properties props, final OrchestrationConfiguration orchestrationConfig) throws SQLException {
        OrchestrationFacade orchestrationFacade = new OrchestrationFacade(orchestrationConfig);
马晓光 已提交
71
        if (null == shardingRuleConfig || shardingRuleConfig.getTableRuleConfigs().isEmpty()) {
72
            ConfigurationService configService = orchestrationFacade.getConfigService();
马晓光 已提交
73 74 75
            final ShardingRuleConfiguration cloudShardingRuleConfig = configService.loadShardingRuleConfiguration();
            Preconditions.checkState(null != cloudShardingRuleConfig, "Missing the sharding rule configuration on register center");
            return createDataSource(configService.loadDataSourceMap(), cloudShardingRuleConfig, configService.loadShardingConfigMap(), configService.loadShardingProperties(), orchestrationFacade);
76 77 78 79
        } else {
            return createDataSource(dataSourceMap, shardingRuleConfig, configMap, props, orchestrationFacade);
        }
    }
M
maxiaoguang 已提交
80
    
81 82 83 84 85
    /**
     * Create sharding data source.
     *
     * @param dataSourceMap data source map
     * @param yamlShardingRuleConfig yaml sharding rule configuration
马晓光 已提交
86
     * @param orchestrationConfig orchestration configuration
87 88 89 90 91 92
     * @return sharding data source
     * @throws SQLException SQL exception
     */
    public static DataSource createDataSource(
            final Map<String, DataSource> dataSourceMap, final YamlShardingRuleConfiguration yamlShardingRuleConfig, final OrchestrationConfiguration orchestrationConfig) throws SQLException {
        OrchestrationFacade orchestrationFacade = new OrchestrationFacade(orchestrationConfig);
马晓光 已提交
93
        if (null == yamlShardingRuleConfig) {
94
            ConfigurationService configService = orchestrationFacade.getConfigService();
马晓光 已提交
95 96 97
            final ShardingRuleConfiguration cloudShardingRuleConfig = configService.loadShardingRuleConfiguration();
            Preconditions.checkState(null != cloudShardingRuleConfig, "Missing the sharding rule configuration on register center");
            return createDataSource(configService.loadDataSourceMap(), cloudShardingRuleConfig, configService.loadShardingConfigMap(), configService.loadShardingProperties(), orchestrationFacade);
98 99 100 101
        } else {
            return createDataSource(dataSourceMap, yamlShardingRuleConfig.getShardingRuleConfiguration(), yamlShardingRuleConfig.getConfigMap(), yamlShardingRuleConfig.getProps(), orchestrationFacade);
        }
    }
M
maxiaoguang 已提交
102
    
马晓光 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    /**
     * Create sharding data source.
     *
     * @param dataSourceMap data source map
     * @param shardingRuleConfig sharding rule configuration
     * @param orchestrationFacade orchestration facade
     * @param configMap config map
     * @param props properties for data source
     * @return sharding data source
     * @throws SQLException SQL exception
     */
    private static DataSource createDataSource(
            final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig,
            final Map<String, Object> configMap, final Properties props, final OrchestrationFacade orchestrationFacade) throws SQLException {
        processDataSourceMapWithMasterSlave(dataSourceMap, shardingRuleConfig);
        OrchestrationShardingDataSource result = new OrchestrationShardingDataSource(dataSourceMap, shardingRuleConfig, configMap, props, orchestrationFacade);
        result.init();
        return result;
    }
H
haocao 已提交
122 123 124 125 126 127 128 129 130 131
    
    /**
     * 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 已提交
132
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlFile);
H
haocao 已提交
133
        YamlShardingRuleConfiguration shardingRuleConfig = config.getShardingRule();
134
        return createDataSource(config.getDataSources(), shardingRuleConfig, config.getOrchestration().getOrchestrationConfiguration());
H
haocao 已提交
135 136 137 138 139 140 141 142 143 144 145 146
    }
    
    /**
     * 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 已提交
147
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlFile);
H
haocao 已提交
148
        YamlShardingRuleConfiguration shardingRuleConfig = config.getShardingRule();
149
        return createDataSource(dataSourceMap, shardingRuleConfig, config.getOrchestration().getOrchestrationConfiguration());
H
haocao 已提交
150 151 152 153 154 155 156 157 158
    }
    
    /**
     * 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
     */
159
    public static DataSource createDataSource(final byte[] yamlByteArray) throws SQLException {
H
haocao 已提交
160
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlByteArray);
H
haocao 已提交
161
        YamlShardingRuleConfiguration shardingRuleConfig = config.getShardingRule();
162
        return createDataSource(config.getDataSources(), shardingRuleConfig, config.getOrchestration().getOrchestrationConfiguration());
H
haocao 已提交
163 164 165 166 167 168 169 170 171 172
    }
    
    /**
     * 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
     */
173
    public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlByteArray) throws SQLException {
H
haocao 已提交
174
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlByteArray);
H
haocao 已提交
175
        YamlShardingRuleConfiguration shardingRuleConfig = config.getShardingRule();
176
        return createDataSource(dataSourceMap, shardingRuleConfig, config.getOrchestration().getOrchestrationConfiguration());
H
haocao 已提交
177 178
    }
    
H
haocao 已提交
179
    private static YamlOrchestrationShardingRuleConfiguration unmarshal(final File yamlFile) throws IOException {
H
haocao 已提交
180 181 182 183
        try (
                FileInputStream fileInputStream = new FileInputStream(yamlFile);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8")
        ) {
H
haocao 已提交
184
            return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(inputStreamReader, YamlOrchestrationShardingRuleConfiguration.class);
H
haocao 已提交
185 186 187
        }
    }
    
188
    private static YamlOrchestrationShardingRuleConfiguration unmarshal(final byte[] yamlByteArray) {
H
haocao 已提交
189
        return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlOrchestrationShardingRuleConfiguration.class);
H
haocao 已提交
190
    }
191 192 193
    
    private static void processDataSourceMapWithMasterSlave(final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfiguration) throws SQLException {
        for (MasterSlaveRuleConfiguration each : shardingRuleConfiguration.getMasterSlaveRuleConfigs()) {
194 195 196 197 198 199 200 201
            processDataSourceMapWithMasterSlave(dataSourceMap, each);
        }
    }
    
    private static void processDataSourceMapWithMasterSlave(final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig) throws SQLException {
        Map<String, DataSource> masterSlaveDataSourceMap = new LinkedHashMap<>(masterSlaveRuleConfig.getSlaveDataSourceNames().size() + 1, 1);
        for (String each : masterSlaveRuleConfig.getSlaveDataSourceNames()) {
            masterSlaveDataSourceMap.put(each, dataSourceMap.remove(each));
202
        }
203 204
        masterSlaveDataSourceMap.put(masterSlaveRuleConfig.getMasterDataSourceName(), dataSourceMap.remove(masterSlaveRuleConfig.getMasterDataSourceName()));
        dataSourceMap.put(masterSlaveRuleConfig.getName(), MasterSlaveDataSourceFactory.createDataSource(masterSlaveDataSourceMap, masterSlaveRuleConfig, Collections.<String, Object>emptyMap()));
205
    }
T
terrymanu 已提交
206
}