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

org.zodiac.datasource.DynamicDataSourceObjectHolder Maven / Gradle / Ivy

The newest version!
package org.zodiac.datasource;

import java.util.concurrent.atomic.AtomicReference;

import org.zodiac.datasource.config.DatabaseType;
import org.zodiac.datasource.exception.DataSourceNotFoundException;
import org.zodiac.datasource.jdbc.JdbcDynamicDataSourceObject;
import org.zodiac.datasource.jdbc.switcher.DefaultJdbcSwitcher;
import org.zodiac.datasource.jdbc.switcher.JdbcSwitcher;
import org.zodiac.datasource.r2dbc.R2dbcDynamicDataSourceObject;
import org.zodiac.datasource.r2dbc.switcher.DefaultR2dbcSwicher;
import org.zodiac.datasource.r2dbc.switcher.R2dbcSwitcher;

import reactor.core.publisher.Mono;

/**
 * 用于操作动态数据源,如获取当前使用的数据源,使用switcher切换数据源等。
 *
 */
public final class DynamicDataSourceObjectHolder {

    static volatile AtomicReference dynamicDataSourceServiceRef =
        new AtomicReference<>();

    static volatile JdbcSwitcher jdbcSwitcher = new DefaultJdbcSwitcher();
    static volatile R2dbcSwitcher r2dbcSwitcher = new DefaultR2dbcSwicher();

    public static boolean initDynamicDataSourceReady(DynamicDataSourceObjectService dynamicDataSourceObjectService) {
        return seDataSourceObjectService(dynamicDataSourceObjectService);
    }

    public static boolean isDynamicDataSourceReady() {
        return getDataSourceObjectService() != null;
    }

    public static void checkDynamicDataSourceReady() {
        if (getDataSourceObjectService() == null) {
            throw new UnsupportedOperationException("dataSourceService not ready");
        }
    }

    /**
     * @return 动态数据源切换器。
     */
    public static JdbcSwitcher switcher() {
        return jdbcSwitcher;
    }

    public static R2dbcSwitcher r2dbcSwitcher() {
        return r2dbcSwitcher;
    }

    /**
     * @return 默认数据源。
     */
    public static JdbcDynamicDataSourceObject defaultDataSource() {
        checkDynamicDataSourceReady();
        return (JdbcDynamicDataSourceObject)getDataSourceObjectService().getDefaultDataSource();
    }

    /**
     * 根据指定的数据源id获取动态数据源
     *
     * @param dataSourceId
     *            数据源id
     * @return 动态数据源
     * @throws DataSourceNotFoundException
     *             如果数据源不存在将抛出此异常
     */
    public static JdbcDynamicDataSourceObject dataSource(String dataSourceId) {
        checkDynamicDataSourceReady();
        return getDataSourceObjectService().getDataSource(dataSourceId);
    }

    /**
     * @return 当前使用的数据源
     */
    public static JdbcDynamicDataSourceObject currentDataSource() {
        return jdbcSwitcher.datasource().current().map(getDataSourceObjectService()::getDataSource)
            .orElseGet(DynamicDataSourceObjectHolder::defaultDataSource);
    }

    public static Mono currentR2dbc() {
        return r2dbcSwitcher.datasource().current().flatMap(getDataSourceObjectService()::getR2dbcDataSource)
            .switchIfEmpty(Mono.defer(() -> Mono.just(getDataSourceObjectService().getDefaultDataSource())
                .map(R2dbcDynamicDataSourceObject.class::cast)));
    }

    /**
     * @return 当前使用的数据源是否为默认数据源
     */
    public static boolean currentIsDefault() {
        return !jdbcSwitcher.datasource().current().isPresent();
    }

    /**
     * 判断指定id的数据源是否存在
     *
     * @param id
     *            数据源id {@link DynamicDataSourceObject#getId()}
     * @return 数据源是否存在
     */
    public static boolean existing(String id) {
        try {
            checkDynamicDataSourceReady();
            return getDataSourceObjectService().getDataSource(id) != null;
        } catch (DataSourceNotFoundException e) {
            return false;
        }
    }

    /**
     * @return 当前使用的数据源是否存在
     */
    public static boolean currentExisting() {
        if (currentIsDefault()) {
            return true;
        }
        try {
            return currentDataSource() != null;
        } catch (DataSourceNotFoundException e) {
            return false;
        }
    }

    /**
     * @return 当前数据库类型
     */
    public static DatabaseType currentDatabaseType() {
        return currentDataSource().getType();
    }

    /**
     * @return 默认的数据库类型
     */
    public static DatabaseType defaultDatabaseType() {
        return defaultDataSource().getType();
    }

    private static DynamicDataSourceObjectService getDataSourceObjectService() {
        return dynamicDataSourceServiceRef.get();
    }

    private static boolean seDataSourceObjectService(DynamicDataSourceObjectService dynamicDataSourceObjectService) {
        return dynamicDataSourceServiceRef.compareAndSet(null, dynamicDataSourceObjectService);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy