yangys
2024-05-18 040976de6f9934b99f30268a28e2ecf42260e217
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.qianwen.core.tenant.dynamic;
 
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import java.util.Set;
import javax.sql.DataSource;
import com.qianwen.core.cache.utils.CacheUtil;
import com.qianwen.core.tenant.constant.TenantBaseConstant;
import com.qianwen.core.tool.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
 
 
public class TenantDataSourceHolder {
    private final DataSource dataSource;
    private final DataSourceCreator dataSourceCreator;
    private final JdbcTemplate jdbcTemplate;
 
    public TenantDataSourceHolder(final DataSource dataSource, final DataSourceCreator dataSourceCreator, final JdbcTemplate jdbcTemplate) {
        this.dataSource = dataSource;
        this.dataSourceCreator = dataSourceCreator;
        this.jdbcTemplate = jdbcTemplate;
    }
 
    public void handleDataSource(String tenantId) {
        DynamicRoutingDataSource ds = (DynamicRoutingDataSource)this.dataSource;
        Set<String> keys = ds.getCurrentDataSources().keySet();
        if (!keys.contains(tenantId)) {
          TenantDataSource tenantDataSource = getDataSource(tenantId);
          if (tenantDataSource != null) {
            DataSourceProperty dataSourceProperty = new DataSourceProperty();
            BeanUtils.copyProperties(tenantDataSource, dataSourceProperty);
            dataSourceProperty.setLazy(Boolean.FALSE);
            DataSource dataSource = this.dataSourceCreator.createDataSource(dataSourceProperty);
            ds.addDataSource(tenantId, dataSource);
          } 
        } 
    }
 
    private Boolean existDataSource(String tenantId) {
        Boolean exist = (Boolean) CacheUtil.get(TenantBaseConstant.TENANT_DATASOURCE_CACHE, TenantBaseConstant.TENANT_DATASOURCE_EXIST_KEY, tenantId, Boolean.class, Boolean.FALSE);
        if (exist == null) {
            TenantDataSource tenantDataSource = (TenantDataSource) this.jdbcTemplate.queryForObject(TenantBaseConstant.TENANT_DATASOURCE_EXIST_STATEMENT, new String[]{tenantId}, new BeanPropertyRowMapper(TenantDataSource.class));
            if (tenantDataSource != null && StringUtil.isNotBlank(tenantDataSource.getDatasourceId())) {
                exist = Boolean.TRUE;
            } else {
                exist = Boolean.FALSE;
            }
            CacheUtil.put(TenantBaseConstant.TENANT_DATASOURCE_CACHE, TenantBaseConstant.TENANT_DATASOURCE_EXIST_KEY, tenantId, exist, Boolean.FALSE);
        }
        return exist;
    }
 
    private TenantDataSource getDataSource(String tenantId) {
        if (!existDataSource(tenantId).booleanValue()) {
            return null;
        }
        TenantDataSource tenantDataSource = (TenantDataSource) CacheUtil.get(TenantBaseConstant.TENANT_DATASOURCE_CACHE, TenantBaseConstant.TENANT_DATASOURCE_KEY, tenantId, TenantDataSource.class, Boolean.FALSE);
        if (tenantDataSource == null) {
            tenantDataSource = (TenantDataSource) this.jdbcTemplate.queryForObject(TenantBaseConstant.TENANT_DATASOURCE_SINGLE_STATEMENT, new String[]{tenantId}, new BeanPropertyRowMapper(TenantDataSource.class));
            if (tenantDataSource != null && StringUtil.isNoneBlank(new CharSequence[]{tenantDataSource.getTenantId(), tenantDataSource.getDriverClass(), tenantDataSource.getUrl(), tenantDataSource.getUsername(), tenantDataSource.getPassword()})) {
                CacheUtil.put(TenantBaseConstant.TENANT_DATASOURCE_CACHE, TenantBaseConstant.TENANT_DATASOURCE_KEY, tenantId, tenantDataSource, Boolean.FALSE);
            } else {
                tenantDataSource = null;
            }
        }
        return tenantDataSource;
    }
}