/*
 * 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.shardingproxy.backend.jdbc.datasource;

import io.shardingsphere.core.constant.ConnectionMode;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.rule.DataSourceParameter;
import io.shardingsphere.shardingproxy.backend.BackendDataSource;
import io.shardingsphere.shardingproxy.runtime.GlobalRegistry;
import lombok.Getter;

import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Backend data source for JDBC.
 *
 * @author zhaojun
 * @author zhangliang
 * @author panjuan
 */
public final class JDBCBackendDataSource implements BackendDataSource, AutoCloseable {
    
    @Getter
    private final Map<String, DataSource> dataSources;
    
    private final Map<String, DataSource> availableDataSources;
    
    public JDBCBackendDataSource(final Map<String, DataSourceParameter> dataSourceParameters) {
        dataSources = createDataSourceMap(dataSourceParameters);
        availableDataSources = new LinkedHashMap<>(dataSources);
    }
    
    private Map<String, DataSource> createDataSourceMap(final Map<String, DataSourceParameter> dataSourceParameters) {
        // TODO getCircuitDataSourceMap if getCircuitBreakerDataSourceNames() is not empty
        return getNormalDataSourceMap(dataSourceParameters);
    }
    
    private Map<String, DataSource> getNormalDataSourceMap(final Map<String, DataSourceParameter> dataSourceParameters) {
        Map<String, DataSource> result = new LinkedHashMap<>(dataSourceParameters.size(), 1);
        for (Entry<String, DataSourceParameter> entry : dataSourceParameters.entrySet()) {
            try {
                result.put(entry.getKey(), getBackendDataSourceFactory().build(entry.getKey(), entry.getValue()));
            // CHECKSTYLE:OFF
            } catch (final Exception ex) {
                // CHECKSTYLE:ON
                throw new ShardingException(String.format("Can not build data source, name is `%s`.", entry.getKey()), ex);
            }
        }
        return result;
    }
    
    private JDBCBackendDataSourceFactory getBackendDataSourceFactory() {
        switch (GlobalRegistry.getInstance().getTransactionType()) {
            case XA:
                return new JDBCXABackendDataSourceFactory();
            default:
                return new JDBCRawBackendDataSourceFactory();
        }
    }
    
    /**
     * Set available data sources.
     *
     * @param disabledDataSourceNames disabled data source names
     */
    public void setAvailableDataSources(final Collection<String> disabledDataSourceNames) {
        synchronized (availableDataSources) {
            availableDataSources.clear();
            for (Entry<String, DataSource> entry : dataSources.entrySet()) {
                if (!disabledDataSourceNames.contains(entry.getKey())) {
                    availableDataSources.put(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    
    /**
     * Get connection.
     *
     * @param dataSourceName data source name
     * @return connection
     * @throws SQLException SQL exception
     */
    public Connection getConnection(final String dataSourceName) throws SQLException {
        return getConnections(ConnectionMode.MEMORY_STRICTLY, dataSourceName, 1).get(0);
    }
    
    /**
     * Get connections.
     *
     * @param connectionMode connection mode 
     * @param dataSourceName data source name
     * @param connectionSize size of connections to be get
     * @return connections
     * @throws SQLException SQL exception
     */
    @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
    public List<Connection> getConnections(final ConnectionMode connectionMode, final String dataSourceName, final int connectionSize) throws SQLException {
        DataSource dataSource = availableDataSources.get(dataSourceName);
        if (1 == connectionSize) {
            return Collections.singletonList(dataSource.getConnection());
        }
        if (ConnectionMode.CONNECTION_STRICTLY == connectionMode) {
            return createConnections(dataSource, connectionSize);
        }
        synchronized (dataSource) {
            return createConnections(dataSource, connectionSize);
        }
    }
    
    private List<Connection> createConnections(final DataSource dataSource, final int connectionSize) throws SQLException {
        List<Connection> result = new ArrayList<>(connectionSize);
        for (int i = 0; i < connectionSize; i++) {
            result.add(dataSource.getConnection());
        }
        return result;
    }
    
    @Override
    public void close() {
        closeOriginalDataSources();
    }
    
    private void closeOriginalDataSources() {
        for (DataSource each : dataSources.values()) {
            try {
                Method method = each.getClass().getDeclaredMethod("close");
                method.invoke(each);
            } catch (final ReflectiveOperationException ignored) {
            }
        }
    }
}

@@ -1586,77 +1586,8 @@
    %0A
-    private final Map%3CString, DataSource%3E availableDataSources;%0A    %0A
     
@@ -1747,73 +1747,8 @@
 s);%0A
-        availableDataSources = new LinkedHashMap%3C%3E(dataSources);%0A
     
@@ -3104,594 +3104,8 @@
 /**%0A
-     * Set available data sources.%0A     *%0A     * @param disabledDataSourceNames disabled data source names%0A     */%0A    public void setAvailableDataSources(final Collection%3CString%3E disabledDataSourceNames) %7B%0A        synchronized (availableDataSources) %7B%0A            availableDataSources.clear();%0A            for (Entry%3CString, DataSource%3E entry : dataSources.entrySet()) %7B%0A                if (!disabledDataSourceNames.contains(entry.getKey())) %7B%0A                    availableDataSources.put(entry.getKey(), entry.getValue());%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A    %0A    /**%0A
     

delete setAvailableDataSources