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);            }        }