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

import io.shardingsphere.core.constant.ConnectionMode;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.executor.ShardingExecuteEngine;
import io.shardingsphere.core.jdbc.metadata.JDBCTableMetaDataConnectionManager;
import io.shardingsphere.core.metadata.ShardingMetaData;
import io.shardingsphere.core.rule.ShardingRule;
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 ShardingRule shardingRule;
    
    private DatabaseType databaseType;
    
    private ShardingExecuteEngine executeEngine;
    
    private ShardingMetaData metaData;
    
    private ConnectionMode connectionMode;
    
    private int maxConnectionsSizePerQuery;
    
    private boolean showSQL;
    
    public ShardingContext(final Map<String, DataSource> dataSourceMap, final ShardingRule shardingRule, final DatabaseType databaseType, 
                           final ShardingExecuteEngine executeEngine, final ConnectionMode connectionMode, final int maxConnectionsSizePerQuery, final boolean showSQL) {
        init(dataSourceMap, shardingRule, databaseType, executeEngine, connectionMode, maxConnectionsSizePerQuery, showSQL);
    }
    
    private void init(final Map<String, DataSource> dataSourceMap, final ShardingRule shardingRule, final DatabaseType databaseType, 
                      final ShardingExecuteEngine executeEngine, final ConnectionMode connectionMode, final int maxConnectionsSizePerQuery, final boolean showSQL) {
        this.shardingRule = shardingRule;
        this.executeEngine = executeEngine;
        this.databaseType = databaseType;
        this.connectionMode = connectionMode;
        this.maxConnectionsSizePerQuery = maxConnectionsSizePerQuery;
        this.showSQL = showSQL;
        metaData = new ShardingMetaData(getDataSourceURLs(dataSourceMap), shardingRule, databaseType, executeEngine, new JDBCTableMetaDataConnectionManager(dataSourceMap), maxConnectionsSizePerQuery);
    }
    
    private static Map<String, String> getDataSourceURLs(final Map<String, DataSource> dataSourceMap) {
        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 static String getDataSourceURL(final DataSource dataSource) {
        try (Connection connection = dataSource.getConnection()) {
            return connection.getMetaData().getURL();
        } catch (final SQLException ex) {
            throw new ShardingException(ex);
        }
    }
    
    @Override
    public void close() {
        executeEngine.close();
    }
}

@@ -1425,32 +1425,38 @@
    %0A    private 
+final 
 ShardingRule sha
@@ -1483,16 +1483,22 @@
 private 
+final 
 Database
@@ -1525,32 +1525,38 @@
    %0A    private 
+final 
 ShardingExecuteE
@@ -1597,51 +1597,13 @@
 ate 
-ShardingMetaData metaData;%0A    %0A    private
+final
  Con
@@ -1647,16 +1647,22 @@
 private 
+final 
 int maxC
@@ -1702,16 +1702,22 @@
 private 
+final 
 boolean 
@@ -1739,468 +1739,78 @@
    p
-ublic ShardingContext(final Map%3CString, DataSource%3E dataSourceMap, final ShardingRule shardingRule, final DatabaseType databaseType, %0A                           final ShardingExecuteEngine executeEngine, final ConnectionMode connectionMode, final int maxConnectionsSizePerQuery, final boolean showSQL) %7B%0A        init(dataSourceMap, shardingRule, databaseType, executeEngine, connectionMode, maxConnectionsSizePerQuery, showSQL);%0A    %7D%0A    %0A    private void ini
+rivate final ShardingMetaData metaData;%0A    %0A    public ShardingContex
 t(fi
@@ -1919,16 +1919,21 @@
 eType, %0A
+     
         
@@ -2148,78 +2148,78 @@
 his.
-executeEngine = executeEngine;%0A        this.databaseType = databaseTyp
+databaseType = databaseType;%0A        this.executeEngine = executeEngin
 e;%0A 
@@ -2589,23 +2589,16 @@
 private 
-static 
 Map%3CStri
@@ -2968,15 +2968,8 @@
 ate 
-static 
 Stri

remove ShardingContext.init