OrchestrationMasterSlaveDataSourceFactory.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.MasterSlaveDataSourceFactory;
T
terrymanu 已提交
21
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;
T
terrymanu 已提交
22
import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
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.YamlOrchestrationMasterSlaveRuleConfiguration;
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

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