/*
 * 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;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import io.shardingsphere.core.orche.config.ProxyBasicRule;
import io.shardingsphere.jdbc.orchestration.internal.OrchestrationFacade;
import io.shardingsphere.jdbc.orchestration.internal.OrchestrationProxyConfiguration;
import io.shardingsphere.jdbc.orchestration.internal.config.ConfigurationService;
import io.shardingsphere.core.orche.eventbus.state.circuit.CircuitStateEventBusInstance;
import io.shardingsphere.core.orche.eventbus.state.disabled.DisabledStateEventBusInstance;
import io.shardingsphere.core.orche.eventbus.config.proxy.ProxyConfigurationEventBusInstance;
import io.shardingsphere.proxy.config.RuleRegistry;
import io.shardingsphere.proxy.config.YamlProxyConfiguration;
import io.shardingsphere.proxy.frontend.ShardingProxy;
import io.shardingsphere.proxy.listener.ProxyListenerRegister;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Sharding-Proxy Bootstrap.
 *
 * @author zhangliang
 * @author wangkai
 * @author panjuan
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class Bootstrap {
    
    private static final int DEFAULT_PORT = 3307;
    
    private static final String DEFAULT_CONFIG_PATH = "/conf/";
    
    private static final String DEFAULT_CONFIG_FILE = "config.yaml";
    
    private static final RuleRegistry RULE_REGISTRY = RuleRegistry.getInstance();
    
    /**
     * Main Entrance.
     * 
     * @param args startup arguments
     * @throws InterruptedException interrupted exception
     * @throws IOException IO exception
     */
    public static void main(final String[] args) throws InterruptedException, IOException {
        ProxyListenerRegister.register();
        YamlProxyConfiguration localConfig = loadLocalConfiguration(new File(Bootstrap.class.getResource(getConfig(args)).getFile()));
        int port = getPort(args);
        if (null == localConfig.getOrchestration()) {
            startWithoutRegistryCenter(localConfig, port);
        } else {
            startWithRegistryCenter(localConfig, port);
        }
    }
    
    private static OrchestrationProxyConfiguration getOrchestrationProxyConfiguration(final YamlProxyConfiguration localConfig) {
        ProxyBasicRule proxyBasicRule = new ProxyBasicRule(localConfig.getShardingRule(), localConfig.getMasterSlaveRule(), localConfig.getProxyAuthority());
        return new OrchestrationProxyConfiguration(localConfig.getDataSources(), proxyBasicRule);
    }
    
    private static YamlProxyConfiguration loadLocalConfiguration(final File yamlFile) throws IOException {
        try (
                FileInputStream fileInputStream = new FileInputStream(yamlFile);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8")
        ) {
            YamlProxyConfiguration result = new Yaml(new Constructor(YamlProxyConfiguration.class)).loadAs(inputStreamReader, YamlProxyConfiguration.class);
            Preconditions.checkNotNull(result, String.format("Configuration file `%s` is invalid.", yamlFile.getName()));
            Preconditions.checkState(!result.getDataSources().isEmpty() || null != result.getOrchestration(), "Data sources configuration can not be empty.");
            Preconditions.checkState(null != result.getShardingRule() || null != result.getMasterSlaveRule() || null != result.getOrchestration(),
                    "Configuration invalid, sharding rule, local and orchestration configuration can not be both null.");
            Preconditions.checkState(!Strings.isNullOrEmpty(result.getProxyAuthority().getUsername()) || null != result.getOrchestration(), "Authority configuration is invalid.");
            return result;
        }
    }
    
    private static int getPort(final String[] args) {
        if (0 == args.length) {
            return DEFAULT_PORT;
        }
        try {
            return Integer.parseInt(args[0]);
        } catch (final NumberFormatException ex) {
            return DEFAULT_PORT;
        }
    }
    
    private static String getConfig(final String[] args) {
        if (2 != args.length) {
            return DEFAULT_CONFIG_PATH + DEFAULT_CONFIG_FILE;
        }
        return DEFAULT_CONFIG_PATH + args[1];
    }
    
    private static void startWithoutRegistryCenter(final YamlProxyConfiguration localConfig, final int port) throws InterruptedException {
        OrchestrationProxyConfiguration orcheConfiguration = getOrchestrationProxyConfiguration(localConfig);
        RULE_REGISTRY.init(orcheConfiguration.getDataSources(), orcheConfiguration.getProxyBasicRule());
        new ShardingProxy().start(port);
    }
    
    private static void startWithRegistryCenter(final YamlProxyConfiguration localConfig, final int port) throws InterruptedException {
        try (OrchestrationFacade orchestrationFacade = new OrchestrationFacade(localConfig.getOrchestration().getOrchestrationConfiguration())) {
            if (null != localConfig.getShardingRule() || null != localConfig.getMasterSlaveRule()) {
                orchestrationFacade.init(getOrchestrationProxyConfiguration(localConfig));
            }
            initRuleRegistry(orchestrationFacade.getConfigService());
            new ShardingProxy().start(port);
        }
    }
    
    private static void initRuleRegistry(final ConfigurationService configService) {
        RULE_REGISTRY.init(configService.loadDataSources(), configService.loadProxyConfiguration());
        ProxyConfigurationEventBusInstance.getInstance().register(RULE_REGISTRY);
        DisabledStateEventBusInstance.getInstance().register(RULE_REGISTRY);
        CircuitStateEventBusInstance.getInstance().register(RULE_REGISTRY);
    }
}

@@ -2963,405 +2963,8 @@
    %0A
-    private static OrchestrationProxyConfiguration getOrchestrationProxyConfiguration(final YamlProxyConfiguration localConfig) %7B%0A        ProxyBasicRule proxyBasicRule = new ProxyBasicRule(localConfig.getShardingRule(), localConfig.getMasterSlaveRule(), localConfig.getProxyAuthority());%0A        return new OrchestrationProxyConfiguration(localConfig.getDataSources(), proxyBasicRule);%0A    %7D%0A    %0A
     
@@ -4903,22 +4903,17 @@
 uration 
-orcheC
+c
 onfigura
@@ -4927,37 +4927,32 @@
 getOrchestration
-Proxy
 Configuration(lo
@@ -4990,22 +4990,17 @@
 RY.init(
-orcheC
+c
 onfigura
@@ -5026,14 +5026,9 @@
 (), 
-orcheC
+c
 onfi
@@ -5542,37 +5542,32 @@
 getOrchestration
-Proxy
 Configuration(lo
@@ -5704,24 +5704,24 @@
 tart(port);%0A
-
         %7D%0A  
@@ -5721,32 +5721,424 @@
    %7D%0A    %7D%0A    %0A
+    private static OrchestrationProxyConfiguration getOrchestrationConfiguration(final YamlProxyConfiguration localConfig) %7B%0A        ProxyBasicRule proxyBasicRule = new ProxyBasicRule(localConfig.getShardingRule(), localConfig.getMasterSlaveRule(), localConfig.getProxyAuthority());%0A        return new OrchestrationProxyConfiguration(localConfig.getDataSources(), proxyBasicRule);%0A    %7D%0A    %0A
     private stat

refactor Bootstrap