|
| 1 | +/* |
| 2 | + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. |
| 3 | + * <p> |
| 4 | + * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * http://www.gnu.org/licenses/lgpl.html |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package top.continew.starter.extension.tenant.util; |
| 18 | + |
| 19 | +import cn.hutool.extra.spring.SpringUtil; |
| 20 | +import top.continew.starter.extension.tenant.TenantDataSourceHandler; |
| 21 | +import top.continew.starter.extension.tenant.config.TenantProvider; |
| 22 | +import top.continew.starter.extension.tenant.context.TenantContext; |
| 23 | +import top.continew.starter.extension.tenant.context.TenantContextHolder; |
| 24 | +import top.continew.starter.extension.tenant.enums.TenantIsolationLevel; |
| 25 | + |
| 26 | +/** |
| 27 | + * 租户工具类 |
| 28 | + * |
| 29 | + * @author 小熊 |
| 30 | + * @author Charles7c |
| 31 | + * @since 2.13.1 |
| 32 | + */ |
| 33 | +public class TenantUtils { |
| 34 | + |
| 35 | + private TenantUtils() { |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * 使用指定租户执行业务逻辑 |
| 40 | + * |
| 41 | + * <p> |
| 42 | + * 强制设置 {@code TenantContextHolder.setIgnore(false)},执行完恢复原值。<br> |
| 43 | + * 适用于在非租户逻辑中执行有租户逻辑的业务逻辑。 |
| 44 | + * </p> |
| 45 | + * |
| 46 | + * @param tenantId 租户 ID |
| 47 | + * @param runnable 业务逻辑 |
| 48 | + */ |
| 49 | + public static void execute(Long tenantId, Runnable runnable) { |
| 50 | + // 未启用租户,直接执行业务逻辑 |
| 51 | + if (TenantContextHolder.isTenantDisabled()) { |
| 52 | + runnable.run(); |
| 53 | + return; |
| 54 | + } |
| 55 | + // 原租户上下文 |
| 56 | + TenantContext oldContext = TenantContextHolder.getContext(); |
| 57 | + boolean oldIgnore = TenantContextHolder.isIgnore(); |
| 58 | + boolean isPush = false; |
| 59 | + try { |
| 60 | + TenantContext newContext = SpringUtil.getBean(TenantProvider.class) |
| 61 | + .getByTenantId(tenantId.toString(), false); |
| 62 | + // 设置新租户上下文 |
| 63 | + TenantContextHolder.setContext(newContext); |
| 64 | + TenantContextHolder.setIgnore(false); |
| 65 | + // 数据源级隔离:切换数据源 |
| 66 | + if (TenantIsolationLevel.DATASOURCE.equals(newContext.getIsolationLevel())) { |
| 67 | + SpringUtil.getBean(TenantDataSourceHandler.class).changeDataSource(newContext.getDataSource()); |
| 68 | + isPush = true; |
| 69 | + } |
| 70 | + // 执行业务逻辑 |
| 71 | + runnable.run(); |
| 72 | + } finally { |
| 73 | + // 恢复原租户上下文 |
| 74 | + TenantContextHolder.setContext(oldContext); |
| 75 | + TenantContextHolder.setIgnore(oldIgnore); |
| 76 | + if (isPush) { |
| 77 | + SpringUtil.getBean(TenantDataSourceHandler.class).poll(); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * 忽略租户执行业务逻辑 |
| 84 | + * |
| 85 | + * <p> |
| 86 | + * 适用于在租户逻辑中执行非租户逻辑的业务逻辑。 |
| 87 | + * </p> |
| 88 | + * |
| 89 | + * @param runnable 业务逻辑 |
| 90 | + */ |
| 91 | + public void executeIgnore(Runnable runnable) { |
| 92 | + // 未启用租户,直接执行业务逻辑 |
| 93 | + if (TenantContextHolder.isTenantDisabled()) { |
| 94 | + runnable.run(); |
| 95 | + return; |
| 96 | + } |
| 97 | + boolean oldIgnore = TenantContextHolder.isIgnore(); |
| 98 | + try { |
| 99 | + TenantContextHolder.setIgnore(true); |
| 100 | + // 执行业务逻辑 |
| 101 | + runnable.run(); |
| 102 | + } finally { |
| 103 | + TenantContextHolder.setIgnore(oldIgnore); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments