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

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

/**
 * Orchestration sharding data source factory.
H
haocao 已提交
48 49 50
 *
 * @author zhangliang
 * @author caohao
T
terrymanu 已提交
51 52 53 54 55 56
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class OrchestrationShardingDataSourceFactory {
    
    /**
     * Create sharding data source.
57
     *
T
terrymanu 已提交
58 59
     * @param dataSourceMap data source map
     * @param shardingRuleConfig sharding rule configuration
60
     * @param orchestrationFacade orchestration facade
H
haocao 已提交
61
     * @param configMap config map
T
terrymanu 已提交
62 63 64 65
     * @param props properties for data source
     * @return sharding data source
     * @throws SQLException SQL exception
     */
H
haocao 已提交
66 67
    public static DataSource createDataSource(
            final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig, 
68
            final Map<String, Object> configMap, final Properties props, final OrchestrationFacade orchestrationFacade) throws SQLException {
69
        processDataSourceMapWithMasterSlave(dataSourceMap, shardingRuleConfig);
70
        OrchestrationShardingDataSource result = new OrchestrationShardingDataSource(dataSourceMap, shardingRuleConfig, configMap, props, orchestrationFacade);
H
haocao 已提交
71 72
        result.init();
        return result;
73
    }
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

    /**
     * Create sharding data source.
     *
     * @param dataSourceMap data source map
     * @param shardingRuleConfig sharding rule configuration
     * @param orchestrationConfig orchestration master-slave configuration
     * @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);
        if (dataSourceMap == null || dataSourceMap.isEmpty()) {
            ConfigurationService configService = orchestrationFacade.getConfigService();
            return createDataSource(configService.loadDataSourceMap(), configService.loadShardingRuleConfiguration(), configService.loadShardingConfigMap(), configService.loadShardingProperties(), orchestrationFacade);
        } else {
            return createDataSource(dataSourceMap, shardingRuleConfig, configMap, props, orchestrationFacade);
        }
    }

    /**
     * Create sharding data source.
     *
     * @param dataSourceMap data source map
     * @param yamlShardingRuleConfig yaml sharding rule configuration
     * @param orchestrationConfig orchestration master-slave configuration
     * @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);
        if (dataSourceMap == null || dataSourceMap.isEmpty()) {
            ConfigurationService configService = orchestrationFacade.getConfigService();
            return createDataSource(configService.loadDataSourceMap(), configService.loadShardingRuleConfiguration(), configService.loadShardingConfigMap(), configService.loadShardingProperties(), orchestrationFacade);
        } else {
            return createDataSource(dataSourceMap, yamlShardingRuleConfig.getShardingRuleConfiguration(), yamlShardingRuleConfig.getConfigMap(), yamlShardingRuleConfig.getProps(), orchestrationFacade);
        }
    }
H
haocao 已提交
117 118 119 120 121 122 123 124 125 126
    
    /**
     * 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 已提交
127
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlFile);
H
haocao 已提交
128
        YamlShardingRuleConfiguration shardingRuleConfig = config.getShardingRule();
129
        return createDataSource(config.getDataSources(), shardingRuleConfig, config.getOrchestration().getOrchestrationConfiguration());
H
haocao 已提交
130 131 132 133 134 135 136 137 138 139 140 141
    }
    
    /**
     * 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 已提交
142
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlFile);
H
haocao 已提交
143
        YamlShardingRuleConfiguration shardingRuleConfig = config.getShardingRule();
144
        return createDataSource(dataSourceMap, shardingRuleConfig, config.getOrchestration().getOrchestrationConfiguration());
H
haocao 已提交
145 146 147 148 149 150 151 152 153
    }
    
    /**
     * 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
     */
154
    public static DataSource createDataSource(final byte[] yamlByteArray) throws SQLException {
H
haocao 已提交
155
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlByteArray);
H
haocao 已提交
156
        YamlShardingRuleConfiguration shardingRuleConfig = config.getShardingRule();
157
        return createDataSource(config.getDataSources(), shardingRuleConfig, config.getOrchestration().getOrchestrationConfiguration());
H
haocao 已提交
158 159 160 161 162 163 164 165 166 167
    }
    
    /**
     * 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
     */
168
    public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlByteArray) throws SQLException {
H
haocao 已提交
169
        YamlOrchestrationShardingRuleConfiguration config = unmarshal(yamlByteArray);
H
haocao 已提交
170
        YamlShardingRuleConfiguration shardingRuleConfig = config.getShardingRule();
171
        return createDataSource(dataSourceMap, shardingRuleConfig, config.getOrchestration().getOrchestrationConfiguration());
H
haocao 已提交
172 173
    }
    
H
haocao 已提交
174
    private static YamlOrchestrationShardingRuleConfiguration unmarshal(final File yamlFile) throws IOException {
H
haocao 已提交
175 176 177 178
        try (
                FileInputStream fileInputStream = new FileInputStream(yamlFile);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8")
        ) {
H
haocao 已提交
179
            return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(inputStreamReader, YamlOrchestrationShardingRuleConfiguration.class);
H
haocao 已提交
180 181 182
        }
    }
    
183
    private static YamlOrchestrationShardingRuleConfiguration unmarshal(final byte[] yamlByteArray) {
H
haocao 已提交
184
        return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlOrchestrationShardingRuleConfiguration.class);
H
haocao 已提交
185
    }
186 187 188
    
    private static void processDataSourceMapWithMasterSlave(final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfiguration) throws SQLException {
        for (MasterSlaveRuleConfiguration each : shardingRuleConfiguration.getMasterSlaveRuleConfigs()) {
189 190 191 192 193 194 195 196
            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));
197
        }
198 199
        masterSlaveDataSourceMap.put(masterSlaveRuleConfig.getMasterDataSourceName(), dataSourceMap.remove(masterSlaveRuleConfig.getMasterDataSourceName()));
        dataSourceMap.put(masterSlaveRuleConfig.getName(), MasterSlaveDataSourceFactory.createDataSource(masterSlaveDataSourceMap, masterSlaveRuleConfig, Collections.<String, Object>emptyMap()));
200
    }
T
terrymanu 已提交
201
}