package com.usoftchina.bi.core.jdbc;

import java.util.HashSet;
import java.util.Set;

/**
 * @Description 动态数据源上下文管理
 * @Date 2018-06-23
 */
public class DynamicDataSourceContextHolder {

    //存放当前线程使用的数据源类型信息
    private static final ThreadLocal<Object> contextHolder = new ThreadLocal<Object>();
    //存放数据源id
    private static Set<Object> dataSourceIds = new HashSet<Object>();

    public static Set<Object> getDataSourceIds() {
        return dataSourceIds;
    }

    public static void setDataSourceIds(Set<Object> dataSourceIds) {
        DynamicDataSourceContextHolder.dataSourceIds = dataSourceIds;
    }

    //设置数据源
    public static void setDataSourceType(Object dataSourceType) {
        contextHolder.set(dataSourceType);
    }

    //获取数据源
    public static Object getDataSourceType() {
        return contextHolder.get();
    }

    //清除数据源
    public static void clearDataSourceType() {
        contextHolder.remove();
    }

    //判断当前数据源是否存在
    public static boolean isContainsDataSource(Object dataSourceId) {
        return dataSourceIds.contains(dataSourceId);
    }
}