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

org.zodiac.autoconfigure.tenant.PlatformTenantDataSourceAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.tenant;

import javax.sql.DataSource;

import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Role;
import org.springframework.core.annotation.Order;
import org.zodiac.autoconfigure.tenant.condition.ConditionalOnPlatformTenantEnabled;
import org.zodiac.commons.util.Asserts;

@SpringBootConfiguration
@ConditionalOnPlatformTenantEnabled
@ConditionalOnProperty(value = org.zodiac.tenant.constants.PlatformTenantSystemPropetiesConstants.PLATFORM_TENANT_DYNAMIC_DATASOURCE, havingValue = "true")
@Import(value = {com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceCreatorAutoConfiguration.class})
@EnableConfigurationProperties(value = {DataSourceProperties.class, com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties.class})
@AutoConfigureBefore(value = {com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure.class, com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration.class})
@ConditionalOnClass(value = {com.baomidou.dynamic.datasource.DynamicRoutingDataSource.class, com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder.class, com.alibaba.druid.pool.DruidDataSource.class, org.springframework.jdbc.core.JdbcTemplate.class, org.zodiac.tenant.TenantId.class})
public class PlatformTenantDataSourceAutoConfiguration {

    private PlatformTenantProperties platformTenantProperties;
    private org.zodiac.security.SecurityAuthOperations2 securityAuthOperations;

    public PlatformTenantDataSourceAutoConfiguration(PlatformTenantProperties platformTenantProperties,
        org.zodiac.security.SecurityAuthOperations2 securityAuthOperations) {
        this.platformTenantProperties = platformTenantProperties;
        this.securityAuthOperations = securityAuthOperations;
    }

    @Bean
    @Primary
    protected com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider dynamicDataSourceProvider(DataSourceProperties dataSourceProperties,
        com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties dynamicDataSourceProperties) {
        String driverClassName = dataSourceProperties.getDriverClassName();
        String url = dataSourceProperties.getUrl();
        String username = dataSourceProperties.getUsername();
        String password = dataSourceProperties.getPassword();
        com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty master =
            dynamicDataSourceProperties.getDatasource().get(dynamicDataSourceProperties.getPrimary());
        if (master != null) {
            driverClassName = master.getDriverClassName();
            url = master.getUrl();
            username = master.getUsername();
            password = master.getPassword();
        }
        return new org.zodiac.tenant.dynamic.TenantDataSourceJdbcProvider(dynamicDataSourceProperties, platformTenantProperties.getDataSourceGroupSql(),
            driverClassName, url, username, password);
    }

    @Bean
    @Primary
    protected DataSource dataSource(com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider dynamicDataSourceProvider,
        com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties dynamicDataSourceProperties) {
        com.baomidou.dynamic.datasource.DynamicRoutingDataSource dataSource = new com.baomidou.dynamic.datasource.DynamicRoutingDataSource();
        dataSource.setPrimary(dynamicDataSourceProperties.getPrimary());
        dataSource.setStrict(dynamicDataSourceProperties.getStrict());
        dataSource.setStrategy(dynamicDataSourceProperties.getStrategy());
        dataSource.setProvider(dynamicDataSourceProvider);
        dataSource.setP6spy(dynamicDataSourceProperties.getP6spy());
        dataSource.setSeata(dynamicDataSourceProperties.getSeata());
        return dataSource;
    }

    @Bean
    @ConditionalOnMissingBean
    protected org.zodiac.tenant.dynamic.TenantDataSourceAnnotationInterceptor tenantDataSourceAnnotationInterceptor(com.baomidou.dynamic.datasource.processor.DsProcessor dsProcessor,
        com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties dynamicDataSourceProperties) {
        return new org.zodiac.tenant.dynamic.TenantDataSourceAnnotationInterceptor(dynamicDataSourceProperties.isAllowedPublicOnly(),
            dsProcessor, securityAuthOperations);
    }

    @Bean
    @ConditionalOnMissingBean
    @Role(value = BeanDefinition.ROLE_INFRASTRUCTURE)
    protected com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationAdvisor dynamicDatasourceAnnotationAdvisor(
        org.zodiac.tenant.dynamic.TenantDataSourceAnnotationInterceptor tenantDataSourceAnnotationInterceptor,
        com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties dynamicDataSourceProperties) {
        com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationAdvisor advisor =
            new com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationAdvisor(tenantDataSourceAnnotationInterceptor);
        advisor.setOrder(dynamicDataSourceProperties.getOrder());
        return advisor;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(value = org.zodiac.tenant.constants.PlatformTenantSystemPropetiesConstants.PLATFORM_TENANT_DYNAMIC_GLOBAL, havingValue = "true")
    protected org.zodiac.tenant.dynamic.TenantDataSourceGlobalInterceptor tenantDataSourceGlobalInterceptor() {
        return new org.zodiac.tenant.dynamic.TenantDataSourceGlobalInterceptor(securityAuthOperations);
    }

    @Bean
    @ConditionalOnMissingBean
    @Role(value = BeanDefinition.ROLE_INFRASTRUCTURE)
    @ConditionalOnProperty(value = org.zodiac.tenant.constants.PlatformTenantSystemPropetiesConstants.PLATFORM_TENANT_DYNAMIC_GLOBAL, havingValue = "true")
    protected org.zodiac.tenant.dynamic.TenantDataSourceGlobalAdvisor tenantDataSourceGlobalAdvisor(
        org.zodiac.tenant.dynamic.TenantDataSourceGlobalInterceptor tenantDataSourceGlobalInterceptor,
        com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties dynamicDataSourceProperties) {
        org.zodiac.tenant.dynamic.TenantDataSourceGlobalAdvisor advisor = new org.zodiac.tenant.dynamic.TenantDataSourceGlobalAdvisor(tenantDataSourceGlobalInterceptor,
            platformTenantProperties.getBasePackages());
        advisor.setOrder(dynamicDataSourceProperties.getOrder() + 1);
        return advisor;
    }

    @Bean
    @ConditionalOnMissingBean
    protected com.baomidou.dynamic.datasource.processor.DsProcessor dsProcessor() {
        com.baomidou.dynamic.datasource.processor.DsHeaderProcessor headerProcessor = new com.baomidou.dynamic.datasource.processor.DsHeaderProcessor();
        com.baomidou.dynamic.datasource.processor.DsSessionProcessor sessionProcessor = new com.baomidou.dynamic.datasource.processor.DsSessionProcessor();
        org.zodiac.tenant.dynamic.DsTenantIdProcessor tenantIdProcessor = new org.zodiac.tenant.dynamic.DsTenantIdProcessor(securityAuthOperations);
        com.baomidou.dynamic.datasource.processor.DsSpelExpressionProcessor spelExpressionProcessor = new com.baomidou.dynamic.datasource.processor.DsSpelExpressionProcessor();
        headerProcessor.setNextProcessor(sessionProcessor);
        sessionProcessor.setNextProcessor(tenantIdProcessor);
        tenantIdProcessor.setNextProcessor(spelExpressionProcessor);
        return headerProcessor;
    }

    @Order
    @SpringBootConfiguration
    @ConditionalOnPlatformTenantEnabled
    @ConditionalOnProperty(value = org.zodiac.tenant.constants.PlatformTenantSystemPropetiesConstants.PLATFORM_TENANT_DYNAMIC_DATASOURCE, havingValue = "true")
    @ConditionalOnClass(value = {com.baomidou.dynamic.datasource.DynamicRoutingDataSource.class, org.springframework.jdbc.core.JdbcTemplate.class, org.zodiac.tenant.TenantId.class})
    protected static class TenantDataSourceAnnotationConfiguration implements SmartInitializingSingleton {

        private final org.zodiac.tenant.dynamic.TenantDataSourceAnnotationInterceptor tenantDataSourceAnnotationInterceptor;

        private final com.baomidou.dynamic.datasource.DynamicRoutingDataSource dataSource;
        private final com.baomidou.dynamic.datasource.creator.DataSourceCreator dataSourceCreator;
        private final org.springframework.jdbc.core.JdbcTemplate jdbcTemplate;
        private final org.zodiac.security.SecurityCacheOperations securityCacheOperations;

        public TenantDataSourceAnnotationConfiguration(
            org.zodiac.tenant.dynamic.TenantDataSourceAnnotationInterceptor tenantDataSourceAnnotationInterceptor, DataSource dataSource,
            com.baomidou.dynamic.datasource.creator.DataSourceCreator dataSourceCreator, org.springframework.jdbc.core.JdbcTemplate jdbcTemplate,
            org.zodiac.security.SecurityCacheOperations securityCacheOperations) {
            Asserts.isInstanceOf(com.baomidou.dynamic.datasource.DynamicRoutingDataSource.class, dataSource);
            this.tenantDataSourceAnnotationInterceptor = tenantDataSourceAnnotationInterceptor;
            this.dataSource = (com.baomidou.dynamic.datasource.DynamicRoutingDataSource)dataSource;
            this.dataSourceCreator = dataSourceCreator;
            this.jdbcTemplate = jdbcTemplate;
            this.securityCacheOperations = securityCacheOperations;
        }

        @Override
        public void afterSingletonsInstantiated() {
            org.zodiac.tenant.dynamic.TenantDataSourceHolder tenantDataSourceHolder =
                new org.zodiac.tenant.dynamic.TenantDataSourceHolder(dataSource, dataSourceCreator, jdbcTemplate, securityCacheOperations);
            tenantDataSourceAnnotationInterceptor.setHolder(tenantDataSourceHolder);
        }
    }

    @Order
    @SpringBootConfiguration
    @ConditionalOnPlatformTenantEnabled
    @ConditionalOnProperty(value = org.zodiac.tenant.constants.PlatformTenantSystemPropetiesConstants.PLATFORM_TENANT_DYNAMIC_GLOBAL, havingValue = "true")
    @ConditionalOnClass(value = {com.baomidou.dynamic.datasource.DynamicRoutingDataSource.class, org.springframework.jdbc.core.JdbcTemplate.class, org.zodiac.tenant.TenantId.class})
    protected static class TenantDataSourceGlobalConfiguration implements SmartInitializingSingleton {

        private final org.zodiac.tenant.dynamic.TenantDataSourceGlobalInterceptor tenantDataSourceGlobalInterceptor;

        private final com.baomidou.dynamic.datasource.DynamicRoutingDataSource dataSource;
        private final com.baomidou.dynamic.datasource.creator.DataSourceCreator dataSourceCreator;
        private final org.springframework.jdbc.core.JdbcTemplate jdbcTemplate;
        private final org.zodiac.security.SecurityCacheOperations securityCacheOperations;

        public TenantDataSourceGlobalConfiguration(org.zodiac.tenant.dynamic.TenantDataSourceGlobalInterceptor tenantDataSourceGlobalInterceptor,
            DataSource dataSource, com.baomidou.dynamic.datasource.creator.DataSourceCreator dataSourceCreator, org.springframework.jdbc.core.JdbcTemplate jdbcTemplate,
            org.zodiac.security.SecurityCacheOperations securityCacheOperations) {
            Asserts.isInstanceOf(com.baomidou.dynamic.datasource.DynamicRoutingDataSource.class, dataSource);
            this.tenantDataSourceGlobalInterceptor = tenantDataSourceGlobalInterceptor;
            this.dataSource = (com.baomidou.dynamic.datasource.DynamicRoutingDataSource)dataSource;
            this.dataSourceCreator = dataSourceCreator;
            this.jdbcTemplate = jdbcTemplate;
            this.securityCacheOperations = securityCacheOperations;
        }

        @Override
        public void afterSingletonsInstantiated() {
            org.zodiac.tenant.dynamic.TenantDataSourceHolder tenantDataSourceHolder =
                new org.zodiac.tenant.dynamic.TenantDataSourceHolder(dataSource, dataSourceCreator, jdbcTemplate, securityCacheOperations);
            tenantDataSourceGlobalInterceptor.setHolder(tenantDataSourceHolder);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy