/*
 * 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.orchestration.internal.listener;

import io.shardingsphere.orchestration.internal.config.AuthenticationListenerManager;
import io.shardingsphere.orchestration.internal.config.ConfigMapListenerManager;
import io.shardingsphere.orchestration.internal.config.RuleListenerManager;
import io.shardingsphere.orchestration.internal.config.DataSourceListenerManager;
import io.shardingsphere.orchestration.internal.config.PropertiesListenerManager;
import io.shardingsphere.orchestration.internal.state.datasource.DataSourceStateListenerManager;
import io.shardingsphere.orchestration.internal.state.instance.InstanceStateListenerManager;
import io.shardingsphere.orchestration.reg.api.RegistryCenter;

import java.util.Collection;
import java.util.LinkedList;

/**
 * Registry center's listener factory.
 *
 * @author caohao
 * @author panjuan
 */
public final class ListenerFactory {
    
    private final Collection<RuleListenerManager> ruleListenerManagers = new LinkedList<>();
    
    private final Collection<DataSourceListenerManager> dataSourceListenerManagers = new LinkedList<>();
    
    private final PropertiesListenerManager propertiesListenerManager;
    
    private final AuthenticationListenerManager authenticationListenerManager;
    
    private final ConfigMapListenerManager configMapListenerManager;
    
    private final InstanceStateListenerManager instanceStateListenerManager;
    
    private final DataSourceStateListenerManager dataSourceStateListenerManager;
    
    public ListenerFactory(final String name, final RegistryCenter regCenter, final Collection<String> shardingSchemaNames) {
        for (String each : shardingSchemaNames) {
            dataSourceListenerManagers.add(new DataSourceListenerManager(name, regCenter, each));
            ruleListenerManagers.add(new RuleListenerManager(name, regCenter, each));
        }
        propertiesListenerManager = new PropertiesListenerManager(name, regCenter);
        authenticationListenerManager = new AuthenticationListenerManager(name, regCenter);
        instanceStateListenerManager = new InstanceStateListenerManager(name, regCenter);
        configMapListenerManager = new ConfigMapListenerManager(name, regCenter);
        dataSourceStateListenerManager = new DataSourceStateListenerManager(name, regCenter);
    }
    
    /**
     * Initialize listeners.
     *
     */
    public void initListeners() {
        for (RuleListenerManager each : ruleListenerManagers) {
            each.watch();
        }
        for (DataSourceListenerManager each : dataSourceListenerManagers) {
            each.watch();
        }
        propertiesListenerManager.watch();
        authenticationListenerManager.watch();
        instanceStateListenerManager.watch();
        dataSourceStateListenerManager.watch();
        configMapListenerManager.watch();
    }
}

@@ -3049,21 +3049,20 @@
         
-for (
+init
 RuleList
@@ -3076,19 +3076,34 @@
 ager
- each : rul
+s();%0A        initDataSourc
 eLis
@@ -3107,35 +3107,35 @@
 ListenerManagers
-) %7B
+();
 %0A            eac
@@ -3131,81 +3131,143 @@
     
-    each.watch();%0A        %7D%0A        for (DataSourceListenerManager each :
+propertiesListenerManager.watch();%0A        authenticationListenerManager.watch();%0A        instanceStateListenerManager.watch();%0A       
  dat
@@ -3265,32 +3265,37 @@
       dataSource
+State
 ListenerManagers
@@ -3297,67 +3297,35 @@
 ager
-s) %7B%0A            each.watch();%0A        %7D%0A        properties
+.watch();%0A        configMap
 List
@@ -3353,186 +3353,335 @@
     
+%7D%0A
     
-authenticationListenerManager.watch();%0A        instanceStateListenerManager.watch();%0A        dataSourceStateListenerManager.watch();%0A        configMapListenerManager.watch();
+%0A    private void initDataSourceListenerManagers() %7B%0A        for (DataSourceListenerManager each : dataSourceListenerManagers) %7B%0A            each.watch();%0A        %7D%0A    %7D%0A    %0A    private void initRuleListenerManagers() %7B%0A        for (RuleListenerManager each : ruleListenerManagers) %7B%0A            each.watch();%0A        %7D
 %0A   

add initRuleListenerManagers()