/*
 * 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.proxy.backend.netty.client;

import io.shardingsphere.proxy.config.ProxyContext;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * backend netty client manager.
 *
 * @author chenqingyang
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class BackendNettyClientManager {
    
    private static final BackendNettyClientManager INSTANCE = new BackendNettyClientManager();
    
    private static final ProxyContext PROXY_CONTEXT = ProxyContext.getInstance();
    
    private final Map<String, BackendNettyClient> clientMap = new HashMap<>();
    
    /**
     * Get instance of backend netty client manager.
     *
     * @return instance of backend netty client manager.
     */
    public static BackendNettyClientManager getInstance() {
        return INSTANCE;
    }
    
    /**
     * Initialize backend netty client map.
     */
    public void init() {
        for (String schema : PROXY_CONTEXT.getSchemaNames()) {
            clientMap.put(schema, new BackendNettyClient(PROXY_CONTEXT.getRuleRegistry(schema)));
        }
    }
    
    /**
     * get backend netty client of schema.
     *
     * @param schema schema
     * @return backend netty client of schema
     */
    public BackendNettyClient getBackendNettyClient(final String schema) {
        return clientMap.get(schema);
    }
    
    /**
     * Start all backend connection client for netty.
     *
     * @throws InterruptedException interrupted exception
     */
    public void start() throws InterruptedException {
        for (BackendNettyClient backendNettyClient : clientMap.values()) {
            backendNettyClient.start();
        }
    }
    
    /**
     * Stop all backend connection client for netty.
     */
    public void stop() {
        for (BackendNettyClient backendNettyClient : clientMap.values()) {
            backendNettyClient.stop();
        }
    }
}

@@ -1511,275 +1511,8 @@
 /**%0A
-     * Initialize backend netty client map.%0A     */%0A    public void init() %7B%0A        for (String schema : PROXY_CONTEXT.getSchemaNames()) %7B%0A            clientMap.put(schema, new BackendNettyClient(PROXY_CONTEXT.getRuleRegistry(schema)));%0A        %7D%0A    %7D%0A    %0A    /**%0A
     
@@ -1969,69 +1969,227 @@
 or (
-BackendNettyClient backendNettyClient : clientMap.values()) %7B
+String schema : PROXY_CONTEXT.getSchemaNames()) %7B%0A            BackendNettyClient backendNettyClient = new BackendNettyClient(PROXY_CONTEXT.getRuleRegistry(schema));%0A            clientMap.put(schema, backendNettyClient);
 %0A   

fix bug that netty client is null