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

import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;
import com.google.common.collect.Sets;
import io.shardingsphere.core.exception.ShardingConfigurationException;
import io.shardingsphere.core.rule.DataSourceParameter;
import lombok.Getter;
import lombok.Setter;

import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Data source configuration.
 *
 * @author zhangliang
 */
@Getter
@Setter
public final class DataSourceConfiguration {
    
    private static final Collection<Class<?>> GENERAL_CLASS_TYPE;
    
    private static final Collection<String> SKIPPED_PROPERTY_NAMES;
    
    static {
        GENERAL_CLASS_TYPE = Sets.<Class<?>>newHashSet(boolean.class, Boolean.class, int.class, Integer.class, long.class, Long.class, String.class);
        SKIPPED_PROPERTY_NAMES = Sets.newHashSet("loginTimeout");
    }
    
    private String dataSourceClassName;
    
    private Map<String, Object> properties;
    
    /**
     * Get data source configuration.
     * 
     * @param dataSource data source
     * @return data source configuration
     */
    public static DataSourceConfiguration getDataSourceConfiguration(final DataSource dataSource) {
        Map<String, Object> properties = new LinkedHashMap<>();
        try {
            for (Method each : findAllGetterMethods(dataSource)) {
                String propertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, each.getName().substring(3));
                if (GENERAL_CLASS_TYPE.contains(each.getReturnType()) && !SKIPPED_PROPERTY_NAMES.contains(propertyName)) {
                    properties.put(propertyName, each.invoke(dataSource));
                }
            }
        } catch (final ReflectiveOperationException ex) {
            throw new ShardingConfigurationException(ex);
        }
        DataSourceConfiguration result = new DataSourceConfiguration();
        result.setDataSourceClassName(dataSource.getClass().getName());
        result.setProperties(properties);
        return result;
    }
    
    /**
     * Get data source configuration.
     *
     * @param dataSourceParameter data source parameter
     * @return data source configuration
     */
    public static DataSourceConfiguration getDataSourceConfiguration(final DataSourceParameter dataSourceParameter) {
        DataSourceConfiguration result = new DataSourceConfiguration();
        result.setDataSourceClassName("HikariDataSource");
        for (Field each : dataSourceParameter.getClass().getDeclaredFields()) {
            try {
                each.setAccessible(true);
                result.getProperties().put(each.getName(), each.get(dataSourceParameter));
            } catch (final ReflectiveOperationException ignored) {
            }
        }
        return result;
    }
    
    private static Collection<Method> findAllGetterMethods(final DataSource dataSource) {<commit_msg>result.setProperties(new LinkedHashMap<String, Object>());<commit_after>/*
 * 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.config;

import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;
import com.google.common.collect.Sets;
import io.shardingsphere.core.exception.ShardingConfigurationException;
import io.shardingsphere.core.rule.DataSourceParameter;
import lombok.Getter;
import lombok.Setter;

import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Data source configuration.
 *
 * @author zhangliang
 */
@Getter
@Setter
public final class DataSourceConfiguration {
    
    private static final Collection<Class<?>> GENERAL_CLASS_TYPE;
    
    private static final Collection<String> SKIPPED_PROPERTY_NAMES;
    
    static {
        GENERAL_CLASS_TYPE = Sets.<Class<?>>newHashSet(boolean.class, Boolean.class, int.class, Integer.class, long.class, Long.class, String.class);
        SKIPPED_PROPERTY_NAMES = Sets.newHashSet("loginTimeout");
    }
    
    private String dataSourceClassName;
    
    private Map<String, Object> properties;
    
    /**
     * Get data source configuration.
     * 
     * @param dataSource data source
     * @return data source configuration
     */
    public static DataSourceConfiguration getDataSourceConfiguration(final DataSource dataSource) {
        Map<String, Object> properties = new LinkedHashMap<>();
        try {
            for (Method each : findAllGetterMethods(dataSource)) {
                String propertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, each.getName().substring(3));
                if (GENERAL_CLASS_TYPE.contains(each.getReturnType()) && !SKIPPED_PROPERTY_NAMES.contains(propertyName)) {
                    properties.put(propertyName, each.invoke(dataSource));
                }
            }
        } catch (final ReflectiveOperationException ex) {
            throw new ShardingConfigurationException(ex);
        }
        DataSourceConfiguration result = new DataSourceConfiguration();
        result.setDataSourceClassName(dataSource.getClass().getName());
        result.setProperties(properties);
        return result;
    }
    
    /**
     * Get data source configuration.
     *
     * @param dataSourceParameter data source parameter
     * @return data source configuration
     */
    public static DataSourceConfiguration getDataSourceConfiguration(final DataSourceParameter dataSourceParameter) {
        DataSourceConfiguration result = new DataSourceConfiguration();
        result.setDataSourceClassName("HikariDataSource");
        result.setProperties(new LinkedHashMap<String, Object>());
        for (Field each : dataSourceParameter.getClass().getDeclaredFields()) {
            try {
                each.setAccessible(true);
                result.getProperties().put(each.getName(), each.get(dataSourceParameter));
            } catch (final ReflectiveOperationException ignored) {
            }
        }
        return result;
    }
    
    private static Collection<Method> findAllGetterMethods(final DataSource dataSource) {
{'fraction_non_alphanumeric': 0.09093298291721419, 'fraction_numerical': 0.003416557161629435, 'mean_word_length': 3.3, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '986975', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1959, 'n_words': 507}