/*
 * Copyright 2016-2018 shardingsphere.io.
 * <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>
 */

package io.shardingsphere.jdbc.orchestration.internal;

import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;

/**
 * Orchestration master-slave datasource.
 *
 * @author caohao
 * @author panjuan
 */
@Slf4j
public final class OrchestrationMasterSlaveDataSource extends MasterSlaveDataSource implements AutoCloseable {
    
    private final OrchestrationFacade orchestrationFacade;
    
    private final Map<String, DataSource> dataSourceMap;
    
    private final MasterSlaveRuleConfiguration masterSlaveRuleConfig;
    
    private final Map<String, Object> configMap;
    
    private final Properties props;
    
    public OrchestrationMasterSlaveDataSource(final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig,
                                              final Map<String, Object> configMap, final Properties props, final OrchestrationFacade orchestrationFacade) throws SQLException {
        super(dataSourceMap, masterSlaveRuleConfig, configMap, props);
        this.orchestrationFacade = orchestrationFacade;
        this.dataSourceMap = dataSourceMap;
        this.masterSlaveRuleConfig = masterSlaveRuleConfig;
        this.configMap = configMap;
        this.props = props;
    }
    
    /**
     * Initialize for master-slave orchestration.
     */
    public void init() {
        orchestrationFacade.init(dataSourceMap, masterSlaveRuleConfig, configMap, props);
    }
    
    @Override
    public void close() {
        orchestrationFacade.close();
    }
}

@@ -707,39 +707,235 @@
 api.
-config.MasterSlaveRuleConfigura
+ConfigMapContext;%0Aimport io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration;%0Aimport io.shardingsphere.core.jdbc.adapter.AbstractDataSourceAdapter;%0Aimport io.shardingsphere.core.jdbc.core.connection.MasterSlaveConnec
 tion
@@ -1100,24 +1100,82 @@
 LException;%0A
+import java.util.Collection;%0Aimport java.util.LinkedList;%0A
 import java.
@@ -1373,27 +1373,24 @@
 extends 
-MasterSlave
+Abstract
 DataSour
@@ -1391,16 +1391,23 @@
 taSource
+Adapter
  impleme
@@ -1511,34 +1511,33 @@
 ate final Ma
-p%3CString, 
+sterSlave
 DataSource%3E 
@@ -1526,33 +1526,32 @@
 rSlaveDataSource
-%3E
  dataSourceMap;%0A
@@ -1549,181 +1549,8 @@
 urce
-Map;%0A    %0A    private final MasterSlaveRuleConfiguration masterSlaveRuleConfig;%0A    %0A    private final Map%3CString, Object%3E configMap;%0A    %0A    private final Properties props
 ;%0A  
@@ -1891,16 +1891,201 @@
   super(
+getAllDataSources(dataSourceMap, masterSlaveRuleConfig.getMasterDataSourceName(), masterSlaveRuleConfig.getSlaveDataSourceNames()));%0A        this.dataSource = new MasterSlaveDataSource(
 dataSour
@@ -2197,170 +2197,456 @@
     
+%7D%0A
     
-this.dataSourceMap = dataSourceMap;%0A        this.masterSlaveRuleConfig = masterSlaveRuleConfig;%0A        this.configMap = configMap;%0A        this.props = props
+%0A    private static Collection%3CDataSource%3E getAllDataSources(final Map%3CString, DataSource%3E dataSourceMap, final String masterDataSourceName, final Collection%3CString%3E slaveDataSourceNames) %7B%0A        Collection%3CDataSource%3E result = new LinkedList%3C%3E();%0A        result.add(dataSourceMap.get(masterDataSourceName));%0A        for (String each : slaveDataSourceNames) %7B%0A            result.add(dataSourceMap.get(each));%0A        %7D%0A        return result
 ;%0A  
@@ -2741,24 +2741,52 @@
 id init() %7B%0A
+        dataSource.close();%0A
         orch
@@ -2820,45 +2820,105 @@
 urce
-Map, masterSlaveRuleConfig, c
+.getDataSourceMap(), new MasterSlaveRuleConfiguration(dataSource.getMasterSlaveRule()), C
 onfigMap
 , pr
@@ -2909,31 +2909,234 @@
 ), ConfigMap
-, props
+Context.getInstance().getMasterSlaveConfig(), dataSource.getShardingProperties().getProps());%0A    %7D%0A    %0A    @Override%0A    public MasterSlaveConnection getConnection() %7B%0A        return dataSource.getConnection(
 );%0A    %7D%0A   

remove extends