/*
 * 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.shardingjdbc.jdbc.core;

import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.constant.properties.ShardingProperties;
import io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import io.shardingsphere.core.executor.ShardingExecuteEngine;
import io.shardingsphere.core.metadata.ShardingMetaData;
import io.shardingsphere.core.rule.ShardingRule;
import io.shardingsphere.shardingjdbc.jdbc.metadata.JDBCTableMetaDataConnectionManager;
import lombok.AccessLevel;
import lombok.Getter;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * Sharding runtime context.
 * 
 * @author gaohongtao
 * @author panjuan
 */
@Getter
public final class ShardingContext implements AutoCloseable {
    
    private final ShardingRule shardingRule;
    
    private final DatabaseType databaseType;
    
    private final ShardingExecuteEngine executeEngine;
    
    private final ShardingProperties shardingProperties;
    
    private final ShardingMetaData metaData;
    
    public ShardingContext(final Map<String, DataSource> dataSourceMap, final ShardingRule shardingRule,
                           final DatabaseType databaseType, final Properties props) throws SQLException {
        this.shardingRule = shardingRule;
        this.databaseType = databaseType;
        shardingProperties = new ShardingProperties(null == props ? new Properties() : props);
        int executorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
        executeEngine = new ShardingExecuteEngine(executorSize);
        metaData = new ShardingMetaData(
                getDataSourceURLs(dataSourceMap), shardingRule, databaseType, executeEngine, new JDBCTableMetaDataConnectionManager(dataSourceMap), getMaxConnectionsSizePerQuery());
    }
    
    private Map<String, String> getDataSourceURLs(final Map<String, DataSource> dataSourceMap) throws SQLException {
        Map<String, String> result = new LinkedHashMap<>(dataSourceMap.size(), 1);
        for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
            result.put(entry.getKey(), getDataSourceURL(entry.getValue()));
        }
        return result;
    }
    
    private String getDataSourceURL(final DataSource dataSource) throws SQLException {
        try (Connection connection = dataSource.getConnection()) {
            return connection.getMetaData().getURL();
        }
    }
    
    /**
     * Is show sql or not.
     *
     * @return show or not
     */
    public boolean isShowSQL() {
        return shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW);
    }
    
    /**
     * Get max connections size per query.
     *
     * @return max connections size per query
     */
    public int getMaxConnectionsSizePerQuery() {
        return shardingProperties.getValue(ShardingPropertiesConstant.MAX_CONNECTIONS_SIZE_PER_QUERY);
    }
    
    @Override
    public void close() {
        executeEngine.close();
    }
}

@@ -721,156 +721,8 @@
 pe;%0A
-import io.shardingsphere.core.constant.properties.ShardingProperties;%0Aimport io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;%0A
 impo
@@ -977,35 +977,8 @@
 er;%0A
-import lombok.AccessLevel;%0A
 impo
@@ -1168,37 +1168,8 @@
 try;
-%0Aimport java.util.Properties;
 %0A%0A/*
@@ -1487,70 +1487,8 @@
    %0A
-    private final ShardingProperties shardingProperties;%0A    %0A
     
@@ -1708,24 +1708,61 @@
 nal 
-Properties props
+int executorSize, final int maxConnectionsSizePerQuer
 ) th
@@ -1869,201 +1869,8 @@
 pe;%0A
-        shardingProperties = new ShardingProperties(null == props ? new Properties() : props);%0A        int executorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);%0A
     
@@ -2119,20 +2119,17 @@
 ceMap), 
-getM
+m
 axConnec
@@ -2144,19 +2144,16 @@
 ePerQuer
-y()
 );%0A    %7D
@@ -2786,485 +2786,8 @@
    %0A
-    /**%0A     * Is show sql or not.%0A     *%0A     * @return show or not%0A     */%0A    public boolean isShowSQL() %7B%0A        return shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW);%0A    %7D%0A    %0A    /**%0A     * Get max connections size per query.%0A     *%0A     * @return max connections size per query%0A     */%0A    public int getMaxConnectionsSizePerQuery() %7B%0A        return shardingProperties.getValue(ShardingPropertiesConstant.MAX_CONNECTIONS_SIZE_PER_QUERY);%0A    %7D%0A    %0A
     

use maxConnectionsSizePerQuery