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

/**
 * 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 ShardingExecuteEngine executeEngine, final int maxConnectionsSizePerQuery, final boolean showSQL) throws SQLException {
        this.shardingRule = shardingRule;
        this.databaseType = databaseType;
        this.executeEngine = executeEngine;
        this.maxConnectionsSizePerQuery = maxConnectionsSizePerQuery;
        metaData = new ShardingMetaData(
                getDataSourceURLs(dataSourceMap), shardingRule, databaseType, executeEngine, new JDBCTableMetaDataConnectionManager(dataSourceMap), maxConnectionsSizePerQuery);
    }
    
    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,65 +721,8 @@
 pe;%0A
-import io.shardingsphere.core.constant.ShardingConstant;%0A
 impo
@@ -1121,16 +1121,43 @@
 anager;%0A
+import lombok.AccessLevel;%0A
 import l
@@ -1339,16 +1339,45 @@
 p.Entry;
+%0Aimport java.util.Properties;
 %0A%0A/**%0A *
@@ -1679,32 +1679,65 @@
 uteEngine;%0A    %0A
+    @Getter(AccessLevel.PRIVATE)%0A
     private fina
@@ -1936,17 +1936,16 @@
 ingRule,
- 
 %0A       
@@ -2007,104 +2007,24 @@
 nal 
-ShardingExecuteEngine executeEngine, final int maxConnectionsSizePerQuery, final boolean showSQL
+Properties props
 ) th
@@ -2139,112 +2139,256 @@
     
-this.executeEngine = executeEngine;%0A        this.maxConnectionsSizePerQuery = maxConnectionsSizePerQuery
+shardingProperties = new ShardingProperties(null == props ? new Properties() : props);%0A        int executorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);%0A        executeEngine = new ShardingExecuteEngine(executorSize)
 ;%0A  
@@ -2574,17 +2574,20 @@
 ceMap), 
-m
+getM
 axConnec
@@ -2603,16 +2603,18 @@
 PerQuery
+()
 );%0A    %7D

modify ShardingContext()