/*
 * 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 com.google.common.eventbus.Subscribe;
import io.shardingsphere.core.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.constant.properties.ShardingProperties;
import io.shardingsphere.jdbc.orchestration.internal.event.config.ShardingConfigurationEventBusEvent;
import io.shardingsphere.jdbc.orchestration.internal.event.state.CircuitStateEventBusEvent;
import io.shardingsphere.jdbc.orchestration.internal.event.state.DisabledStateEventBusEvent;
import io.shardingsphere.core.jdbc.adapter.AbstractDataSourceAdapter;
import io.shardingsphere.core.jdbc.core.connection.ShardingConnection;
import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingsphere.core.rule.ShardingRule;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

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

/**
 * Orchestration sharding datasource.
 *
 * @author caohao
 */
@Slf4j
public final class OrchestrationShardingDataSource extends AbstractDataSourceAdapter implements AutoCloseable {
    
    @Getter
    private final ShardingDataSource dataSource;
    
    private final OrchestrationFacade orchestrationFacade;
    
    public OrchestrationShardingDataSource(final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig,
                                           final Map<String, Object> configMap, final Properties props, final OrchestrationFacade orchestrationFacade) throws SQLException {
        super(dataSourceMap.values());
        this.dataSource = new ShardingDataSource(dataSourceMap, new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()), configMap, props);
        this.orchestrationFacade = orchestrationFacade;
        this.orchestrationFacade.init(dataSourceMap, shardingRuleConfig, configMap, props);
    }
    
    @Override
    public ShardingConnection getConnection() {
        return dataSource.getConnection();
    }
    
    @Override
    public void close() {
        dataSource.close();
        orchestrationFacade.close();
    }
    
    /**
     * Renew disable dataSource names.
     *
     * @param disabledStateEventBusEvent jdbc disabled event bus event
     */
    @Subscribe
    public void renewDisabledDataSourceNames(final DisabledStateEventBusEvent disabledStateEventBusEvent) {
        dataSource.renewDisabledDataSourceNames(disabledStateEventBusEvent.getDisabledDataSourceNames());
    }
    
    /**
     * Renew circuit breaker dataSource names.
     *
     * @param circuitStateEventBusEvent jdbc disabled event bus event
     */
    @Subscribe
    public void renewCircuitBreakerDataSourceNames(final CircuitStateEventBusEvent circuitStateEventBusEvent) {
        dataSource.renewCircuitBreakerDataSourceNames(circuitStateEventBusEvent.isCircuitBreak());
    }
    
    /**
     * Renew sharding data source.
     *
     * @param shardingEvent sharding configuration event bus event.
     */
    @Subscribe
    public void renew(final ShardingConfigurationEventBusEvent shardingEvent) {
        super.renew(shardingEvent.getDataSourceMap().values());
        ShardingProperties shardingProperties = new ShardingProperties(null == shardingEvent.getProps() ? new Properties() : shardingEvent.getProps());
        dataSource.renew(shardingEvent.getDataSourceMap(), shardingEvent.getShardingRule(), shardingProperties);
    }
}

@@ -881,83 +881,51 @@
 ere.
-jdbc.orchestration.internal.event.config.ShardingConfigurationEventBusEvent
+core.jdbc.adapter.AbstractDataSourceAdapter
 ;%0Aim
@@ -951,73 +951,52 @@
 ere.
+core.
 jdbc.
+c
 or
-chestration.internal.event.state.CircuitStateEventBusEvent
+e.connection.ShardingConnection
 ;%0Aim
@@ -1022,144 +1022,101 @@
 ere.
+core.
 jdbc.
+c
 or
-chestration.internal.event.state.DisabledStateEventBusEvent;%0Aimport io.shardingsphere.core.jdbc.adapter.AbstractDataSourceAdapter
+e.datasource.ShardingDataSource;%0Aimport io.shardingsphere.core.rule.ShardingRule
 ;%0Aim
@@ -1142,52 +1142,83 @@
 ere.
-core.
 jdbc.
-c
 or
-e.connection.ShardingConnection
+chestration.internal.event.config.ShardingConfigurationEventBusEvent
 ;%0Aim
@@ -1244,101 +1244,166 @@
 ere.
-core.
 jdbc.
-c
 or
-e.datasource.ShardingDataSource;%0Aimport io.shardingsphere.core.rule.ShardingRule
+chestration.internal.event.state.CircuitStateEventBusEvent;%0Aimport io.shardingsphere.jdbc.orchestration.internal.event.state.DisabledStateEventBusEvent
 ;%0Aim
@@ -1512,24 +1512,85 @@
 LException;%0A
+import java.util.Collection;%0Aimport java.util.LinkedHashMap;%0A
 import java.
@@ -1844,22 +1844,16 @@
 private 
-final 
 Sharding
@@ -1940,24 +1940,80 @@
 acade;%0A    %0A
+    private Map%3CString, DataSource%3E dataSourceMap;%0A    %0A
     public O
@@ -2641,24 +2641,68 @@
 ap, props);%0A
+        this.dataSourceMap = dataSourceMap;%0A
     %7D%0A    %0A 
@@ -3188,33 +3188,45 @@
 vent) %7B%0A        
-d
+Map%3CString, D
 ataSource.renewD
@@ -3222,22 +3222,41 @@
 urce
-.re
+%3E 
 newD
-is
+ataSourceMap = getAvail
 able
-d
 Data
@@ -3261,21 +3261,19 @@
 taSource
-Names
+Map
 (disable
@@ -3319,24 +3319,508 @@
 ceNames());%0A
+        dataSource = new ShardingDataSource(newDataSourceMap, dataSource.getShardingContext(), dataSource.getShardingProperties(), dataSource.getDatabaseType());%0A    %7D%0A    %0A    private Map%3CString, DataSource%3E getAvailableDataSourceMap(final Collection%3CString%3E disabledDataSourceNames) %7B%0A        Map%3CString, DataSource%3E result = new LinkedHashMap%3C%3E(dataSourceMap);%0A        for (String each : disabledDataSourceNames) %7B%0A            result.remove(each);%0A        %7D%0A        return result;%0A
     %7D%0A    %0A 

rewrite renewDisabledDataSourceNames