/*
 * 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.shardingproxy.runtime;

import com.google.common.eventbus.Subscribe;
import io.shardingsphere.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.core.rule.DataSourceParameter;
import io.shardingsphere.core.rule.MasterSlaveRule;
import io.shardingsphere.orchestration.internal.event.config.MasterSlaveRuleChangedEvent;
import io.shardingsphere.orchestration.internal.rule.OrchestrationMasterSlaveRule;
import lombok.Getter;

import java.util.Map;

/**
 * Master-slave schema.
 *
 * @author panjuan
 */
@Getter
public final class MasterSlaveSchema extends LogicSchema {
    
    private MasterSlaveRule masterSlaveRule;
    
    public MasterSlaveSchema(final String name, final Map<String, DataSourceParameter> dataSources, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final boolean isUsingRegistry) {
        super(name, dataSources);
        masterSlaveRule = getMasterSlaveRule(masterSlaveRuleConfig, isUsingRegistry);
    }
    
    private MasterSlaveRule getMasterSlaveRule(final MasterSlaveRuleConfiguration masterSlaveRule, final boolean isUsingRegistry) {
        return isUsingRegistry ? new OrchestrationMasterSlaveRule(masterSlaveRule) : new MasterSlaveRule(masterSlaveRule);
    }
    
    /**
     * Renew master-slave rule.
     *
     * @param masterSlaveEvent master-slave event.
     */
    @Subscribe
    public void renew(final MasterSlaveRuleChangedEvent masterSlaveEvent) {
        if (!getName().equals(masterSlaveEvent.getShardingSchemaName())) {
            return;
        }
        masterSlaveRule = new MasterSlaveRule(masterSlaveEvent.getMasterSlaveRuleConfig());
    }
}

@@ -774,16 +774,73 @@
 ration;%0A
+import io.shardingsphere.core.constant.ShardingConstant;%0A
 import i
@@ -887,16 +887,16 @@
 ameter;%0A
-
 import i
@@ -1025,24 +1025,112 @@
 angedEvent;%0A
+import io.shardingsphere.orchestration.internal.event.state.DisabledStateEventBusEvent;%0A
 import io.sh
@@ -1223,16 +1223,75 @@
 etter;%0A%0A
+import java.util.Collection;%0Aimport java.util.Collections;%0A
 import j
@@ -2367,24 +2367,24 @@
 ;%0A        %7D%0A
-
         mast
@@ -2469,10 +2469,884 @@
 ;%0A    %7D%0A
+    %0A    /**%0A     * Renew disabled data source names.%0A     *%0A     * @param disabledStateEventBusEvent jdbc disabled event bus event%0A     */%0A    @Subscribe%0A    public void renew(final DisabledStateEventBusEvent disabledStateEventBusEvent) %7B%0A        Map%3CString, Collection%3CString%3E%3E disabledSchemaDataSourceMap = disabledStateEventBusEvent.getDisabledSchemaDataSourceMap();%0A        if (!disabledSchemaDataSourceMap.keySet().contains(getName())) %7B%0A            return;%0A        %7D%0A        renew(disabledSchemaDataSourceMap.get(getName()));%0A    %7D%0A    %0A    private void renew(final Collection%3CString%3E disabledDataSourceNames) %7B%0A        DisabledStateEventBusEvent eventBusEvent = new DisabledStateEventBusEvent(Collections.singletonMap(ShardingConstant.LOGIC_SCHEMA_NAME, disabledDataSourceNames));%0A        ((OrchestrationMasterSlaveRule) masterSlaveRule).renew(eventBusEvent);%0A    %7D%0A
 %7D%0A

add renew()