All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.base4j.mybatis.config.datasource.AtomikosDataSourceBuilder Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package com.base4j.mybatis.config.datasource;

import com.base4j.mybatis.config.exception.ConfigErrorException;
import com.base4j.mybatis.config.exception.DataSourceBuildException;
import com.base4j.mybatis.tool.NullUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by USER on 2017/5/1.
 */
public class AtomikosDataSourceBuilder extends DataSourceBuilder {

    /**
     * 初始连接池信息
     *
     * @throws ClassNotFoundException
     */
    @Override
    protected void initPoolInfo() {
        String dataSourcePoolType = applicationProperties.getDataSourcePoolType();
        if (NullUtil.isNotEmpty(dataSourcePoolType)) {
            if (dataSourcePoolType.startsWith("atomikos")) {
                try {
                    poolType = DataSourcePoolType.valueOf(dataSourcePoolType);
                } catch (Exception e) {
                    throw new ConfigErrorException("不支持的连接池类型:" + dataSourcePoolType);
                }
            } else {
                poolType = DataSourcePoolType.atomikos_noxa;
            }
        } else {
            poolType = DataSourcePoolType.atomikos_noxa;
        }
        try {
            poolTypeClass = Class.forName(poolType.getPoolClass());
        } catch (ClassNotFoundException e) {
            throw new DataSourceBuildException(poolType.getPoolClass() + " is not found!");
        }
    }

    @Override
    public String getUrl(Map conf) {
        if (conf == null) {
            throw new IllegalArgumentException("datasource config info can not be null");
        } else {
            if (poolType == DataSourcePoolType.atomikos_xa) {
                Map xaProperties = (Map) conf.get("xaProperties");
                if (!(xaProperties).containsKey("url")) {
                    throw new IllegalArgumentException("datasource config must contains the key: url");
                } else {
                    return (String) xaProperties.get("url");
                }
            } else {
                return (String) conf.get("url");
            }

        }
    }

    @Override
    public Map getDataSourceConf(String prefix) {
        Map dataSourceConf = super.getDataSourceConf(prefix);
        dataSourceConf.put("uniqueResourceName", getDbName(prefix));
        if (poolType == DataSourcePoolType.atomikos_xa) {
            replaceConfig(dataSourceConf, "driverClassName", "xaDataSourceClassName");
            dataSourceConf.put("xaProperties", getXaProperties(dataSourceConf));
            configAdapterXa(dataSourceConf);
        } else {
            replaceConfig(dataSourceConf, "username", "user");
            configAdapterNoXa(dataSourceConf);
        }
        return dataSourceConf;
    }

    private String getDbName(String prefix) {
        if (NullUtil.isNotEmpty(prefix)) {
            if (prefix.startsWith(DEFAULT_DATASOURCE_PREFIX)) {
                return DATASOURCE_BEAN_PREFIX + MAIN_DATASOURCE_SUFFIX;
            } else {
                return DATASOURCE_BEAN_PREFIX + prefix.split("\\.")[2];
            }
        } else {
            throw new IllegalArgumentException("prefix can not be null");
        }
    }

    /**
     * 设置数据库连接信息
     * @param dataSourceConf 默认配置
     * @return 适配后的配置
     */
    private Map getXaProperties(Map dataSourceConf) {
        Map xaProperties = new HashMap();

        xaProperties.put("url", dataSourceConf.get("url"));
        dataSourceConf.remove("url");

        xaProperties.put("user", dataSourceConf.get("username"));
        dataSourceConf.remove("username");

        xaProperties.put("password", dataSourceConf.get("password"));
        dataSourceConf.remove("password");

        return xaProperties;
    }

    /**
     * 配置适配
     * @param dataSourceConf
     */
    private void configAdapterNoXa(Map dataSourceConf) {
        replaceConfig(dataSourceConf, "minIdle", "minPoolSize");
        replaceConfig(dataSourceConf, "maxIdle", "maxPoolSize");
        replaceConfig(dataSourceConf, "maxWait", "borrowConnectionTimeout");
        replaceConfig(dataSourceConf, "validationQuery", "testQuery");
        replaceConfig(dataSourceConf, "timeBetweenEvictionRunsMillis", "maintenanceInterval");
        dataSourceConf.remove("testOnBorrow");
        dataSourceConf.remove("testWhileIdle");
        dataSourceConf.remove("initialSize");
    }

    /**
     * 配置适配
     * @param dataSourceConf
     */
    private void configAdapterXa(Map dataSourceConf) {
        replaceConfig(dataSourceConf, "minIdle", "minPoolSize");
        replaceConfig(dataSourceConf, "maxIdle", "maxPoolSize");
        replaceConfig(dataSourceConf, "maxWait", "borrowConnectionTimeout");
        replaceConfig(dataSourceConf, "validationQuery", "testQuery");
        replaceConfig(dataSourceConf, "timeBetweenEvictionRunsMillis", "maintenanceInterval");
        dataSourceConf.remove("testOnBorrow");
        dataSourceConf.remove("testWhileIdle");
        dataSourceConf.remove("driverClassName");
        dataSourceConf.remove("initialSize");
    }

    /**
     * 属性替换
     * @param defaultDataSourceConf 配置项
     * @param source 原属性名称
     * @param target 现在属性名称
     */
    private void replaceConfig(Map defaultDataSourceConf, String source, String target) {
        Object sourceValue = defaultDataSourceConf.get(source);
        defaultDataSourceConf.put(target, sourceValue);
        defaultDataSourceConf.remove(source);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy