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

import com.google.common.base.Preconditions;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.event.ShardingEventBusInstance;
import io.shardingsphere.core.exception.ShardingException;
import io.shardingsphere.core.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
import io.shardingsphere.core.listener.JDBCListenerRegister;
import lombok.Getter;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.logging.Logger;

/**
 * Adapter for {@code Datasource}.
 * 
 * @author zhangliang
 * @author panjuan
 */
public abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOperationDataSource {
    
    static {
        JDBCListenerRegister.register();
    }
    
    @Getter
    private DatabaseType databaseType;
    
    private PrintWriter logWriter = new PrintWriter(System.out);
    
    public AbstractDataSourceAdapter(final Collection<DataSource> dataSources) throws SQLException {
        databaseType = getDatabaseType(dataSources);
        ShardingEventBusInstance.getInstance().register(this);
    }
    
    protected final DatabaseType getDatabaseType(final Collection<DataSource> dataSources) throws SQLException {
        DatabaseType result = null;
        for (DataSource each : dataSources) {
            DatabaseType databaseType = getDatabaseType(each);
            Preconditions.checkState(null == result || result.equals(databaseType), String.format("Database type inconsistent with '%s' and '%s'", result, databaseType));
            result = databaseType;
        }
        return result;
    }
    
    private DatabaseType getDatabaseType(final DataSource dataSource) throws SQLException {
        if (dataSource instanceof AbstractDataSourceAdapter) {
            return ((AbstractDataSourceAdapter) dataSource).databaseType;
        }
        try (Connection connection = dataSource.getConnection()) {
            return DatabaseType.valueFrom(connection.getMetaData().getDatabaseProductName());
        }
    }
    
    /**
     * Renew abstract data source adapter.
     *
     * @param dataSources data sources
     */
    public void renew(final Collection<DataSource> dataSources) {
        try {
            databaseType = getDatabaseType(dataSources);
        } catch (final SQLException ex) {
            throw new ShardingException(ex);
        }
    }
    
    @Override
    public final PrintWriter getLogWriter() {
        return logWriter;
    }
    
    @Override
    public final void setLogWriter(final PrintWriter out) {
        this.logWriter = out;
    }
    
    @Override
    public final Logger getParentLogger() {
        return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    }
    
    @Override
    public final Connection getConnection(final String username, final String password) throws SQLException {
        return getConnection();
    }
}

@@ -823,67 +823,8 @@
 ce;%0A
-import io.shardingsphere.core.exception.ShardingException;%0A
 impo
@@ -902,24 +902,24 @@
 DataSource;%0A
+
 import io.sh
@@ -1773,24 +1773,212 @@
 %0A    %7D%0A    %0A
+    public AbstractDataSourceAdapter(final DatabaseType databaseType) %7B%0A        this.databaseType = databaseType;%0A        ShardingEventBusInstance.getInstance().register(this);%0A    %7D%0A    %0A
     protecte
@@ -2775,24 +2775,24 @@
 ection()) %7B%0A
+
             
@@ -2898,358 +2898,8 @@
    %0A
-    /**%0A     * Renew abstract data source adapter.%0A     *%0A     * @param dataSources data sources%0A     */%0A    public void renew(final Collection%3CDataSource%3E dataSources) %7B%0A        try %7B%0A            databaseType = getDatabaseType(dataSources);%0A        %7D catch (final SQLException ex) %7B%0A            throw new ShardingException(ex);%0A        %7D%0A    %7D%0A    %0A
     

add new construction