OrchestrationSpringMasterSlaveDataSource.java 3.2 KB
Newer Older
H
haocao 已提交
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>
 */

H
haocao 已提交
18
package io.shardingjdbc.orchestration.spring.datasource;
H
haocao 已提交
19

H
haocao 已提交
20
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;
H
haocao 已提交
21
import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
22
import io.shardingjdbc.core.rule.MasterSlaveRule;
H
haocao 已提交
23 24 25 26
import io.shardingjdbc.orchestration.api.config.OrchestrationMasterSlaveConfiguration;
import io.shardingjdbc.orchestration.internal.config.ConfigurationService;
import io.shardingjdbc.orchestration.internal.state.InstanceStateService;
import io.shardingjdbc.orchestration.reg.base.CoordinatorRegistryCenter;
H
haocao 已提交
27 28 29 30 31 32 33
import lombok.Setter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;
34
import java.util.Map.Entry;
H
haocao 已提交
35 36

/**
H
haocao 已提交
37
 * Orchestration master-slave datasource for spring namespace.
H
haocao 已提交
38
 *
H
haocao 已提交
39
 * @author caohao
H
haocao 已提交
40
 */
H
haocao 已提交
41
public class OrchestrationSpringMasterSlaveDataSource extends MasterSlaveDataSource implements ApplicationContextAware {
H
haocao 已提交
42
    
T
terrymanu 已提交
43 44 45 46
    private final ConfigurationService configurationService;
    
    private final InstanceStateService instanceStateService;
    
T
terrymanu 已提交
47
    private final OrchestrationMasterSlaveConfiguration config;
T
terrymanu 已提交
48
    
H
haocao 已提交
49 50 51
    @Setter
    private ApplicationContext applicationContext;
    
T
terrymanu 已提交
52 53 54 55 56 57
    public OrchestrationSpringMasterSlaveDataSource(final String name, final boolean overwrite, final CoordinatorRegistryCenter registryCenter, 
                                                    final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration config) throws SQLException {
        super(config.build(dataSourceMap));
        this.config = new OrchestrationMasterSlaveConfiguration(name, overwrite, registryCenter, dataSourceMap, config);
        configurationService = new ConfigurationService(name, registryCenter);
        instanceStateService = new InstanceStateService(name, registryCenter);
T
terrymanu 已提交
58 59 60 61 62 63
    }
    
    /**
     * initial orchestration spring master-slave data source.
     */
    public void init() {
T
terrymanu 已提交
64
        configurationService.addMasterSlaveConfiguration(config, this);
T
terrymanu 已提交
65
        instanceStateService.addMasterSlaveState(this);
H
haocao 已提交
66
    }
67 68 69 70 71 72 73 74 75
    
    @Override
    public void renew(final MasterSlaveRule masterSlaveRule) throws SQLException {
        DataSourceBeanUtil.createDataSourceBean(applicationContext, masterSlaveRule.getMasterDataSourceName(), masterSlaveRule.getMasterDataSource());
        for (Entry<String, DataSource> entry : masterSlaveRule.getSlaveDataSourceMap().entrySet()) {
            DataSourceBeanUtil.createDataSourceBean(applicationContext, entry.getKey(), entry.getValue());
        }
        super.renew(masterSlaveRule);
    }
H
haocao 已提交
76
}