ofbiz实体引擎(九) 多租户
来源:互联网 发布:单片机控制蓝牙模块 编辑:程序博客网 时间:2024/05/17 02:30
多租户在平台中是根据delegator不同操作不同的数据库
/** * @author 郑小康 * 设置完整的delegator 其可能形式有 default 或者defalut#tenantDelegatorName * 针对于第一种情况 delegatorBaseName =="default" delegatorTenantId=null * 针对第二种情况 delegatorBaseName =="default" delegatorTenantId="tenantDelegatorName" * 为什么存在第二种情况,是因为在多租户中要实现数据独立,所以获取基础delagtor 和租户delegator,注意这时并未创建实例更没有建立数据库连接 * 其再获取了默认的delegator中的信息之后,如果存在delegatorBaseName 则将 uri username password进行覆盖 * */ protected void setDelegatorNames(String delegatorFullName) { this.delegatorFullName = delegatorFullName; int hashSymbolIndex = delegatorFullName.indexOf('#'); if (hashSymbolIndex == -1) { this.delegatorBaseName = delegatorFullName; } else { this.delegatorBaseName = delegatorFullName.substring(0, hashSymbolIndex); this.delegatorTenantId = delegatorFullName.substring(hashSymbolIndex + 1); } }
//多租户 根据默认baseDelegator获取域名对应TenantId 拼接DelegatorName获取其实例if (useMultitenant) { // get tenant delegator by domain name,获取服务名 String serverName = httpRequest.getServerName(); try { // if tenant was specified, replace delegator with the new per-tenant delegator and set tenantId to session attribute Delegator delegator = getDelegator(config.getServletContext()); //Use base delegator for fetching data from entity of entityGroup com.hanlin.fadp.tenant Delegator baseDelegator = DelegatorFactory.getDelegator(delegator.getDelegatorBaseName()); GenericValue tenantDomainName = EntityQuery.use(baseDelegator).from("TenantDomainName").where("domainName", serverName).queryOne(); String tenantId = null; if(UtilValidate.isNotEmpty(tenantDomainName)) { tenantId = tenantDomainName.getString("tenantId"); } if(UtilValidate.isEmpty(tenantId)) { tenantId = (String) httpRequest.getAttribute("userTenantId"); } if(UtilValidate.isEmpty(tenantId)) { tenantId = (String) httpRequest.getParameter("userTenantId"); } if (UtilValidate.isNotEmpty(tenantId)) { // if the request path is a root mount then redirect to the initial path if (UtilValidate.isNotEmpty(requestPath) && requestPath.equals(contextUri)) { GenericValue tenant = EntityQuery.use(baseDelegator).from("Tenant").where("tenantId", tenantId).queryOne(); String initialPath = tenant.getString("initialPath"); if (UtilValidate.isNotEmpty(initialPath) && !"/".equals(initialPath)) { ((HttpServletResponse)response).sendRedirect(initialPath); return; } } // make that tenant active, setup a new delegator and a new dispatcher String tenantDelegatorName = delegator.getDelegatorBaseName() + "#" + tenantId; httpRequest.getSession().setAttribute("delegatorName", tenantDelegatorName); // after this line the delegator is replaced with the new per-tenant delegator delegator = DelegatorFactory.getDelegator(tenantDelegatorName); config.getServletContext().setAttribute("delegator", delegator); // clear web context objects config.getServletContext().setAttribute("security", null); config.getServletContext().setAttribute("dispatcher", null); /** * 初始化security,根据delegatorName先从缓存中获取,如果缓存中不存在对应的security,则实例化一个 * 由于该过滤器是每次请求都会经过,所以根据域名不同,获取的security就有所不同,这样就可以实现共用一套用户表在不同租户中权限不同 */ Security security = getSecurity(); // initialize the services dispatcher LocalDispatcher dispatcher = getDispatcher(config.getServletContext()); // set web context objects request.setAttribute("dispatcher", dispatcher); request.setAttribute("security", security); request.setAttribute("userTenantId", tenantId); } // NOTE DEJ20101130: do NOT always put the delegator name in the user's session because the user may // have logged in and specified a tenant, and even if no Tenant record with a matching domainName field // is found this will change the user's delegator back to the base one instead of the one for the // tenant specified on login // httpRequest.getSession().setAttribute("delegatorName", delegator.getDelegatorName()); } catch (GenericEntityException e) { Debug.logWarning(e, "Unable to get Tenant没有获取这个租户", module); } }
阅读全文
0 0
- ofbiz实体引擎(九) 多租户
- ofbiz的实体引擎
- OFbiz实体引擎指南
- OFbiz实体引擎
- Ofbiz引擎之实体引擎
- ofbiz实体引擎(五) ModelGroupReader
- Eclipse中OFBiz实体引擎剥离机制
- OFBiz实体引擎开发烹调书
- ofbiz实体引擎常用查询API实例
- Apache OFBiz 学习笔记 之 实体引擎
- ofbiz实体引擎(一) 获取Delegator
- ofbiz实体引擎(四) ModelReader的作用
- ofbiz实体引擎(七) 检查数据源
- ofbiz实体引擎(八) 创建表
- ofbiz的实体引擎orm的性能优化分析
- ofbiz实体引擎(二) delegator实例化具体方式
- ofbiz实体引擎(三) GenericDelegator实例化的具体过程
- ofbiz实体引擎(六) GenericHelper的初始化创建
- C++学习(61)
- Linux运维学习笔记之三:组合命令及命令分类、符号、管道 、工具集
- CSS3落叶动画
- 图的深度遍历----dfs
- opencl基础概念之并行
- ofbiz实体引擎(九) 多租户
- ReentrantLock实现原理及源码分析
- 5、volley 官方教程-自定义请求
- Linux运维学习笔记之四:安装后的基本调优及安全设置(系统基础优化)
- ES6中的类
- 《数据库SQL实战》获取所有部门当前manager的当前薪水情况
- seq2seq学习笔记
- ECMAScript 6 学习(二)async函数
- 6、volley 源码解析之工作流程综述