多租戶在平臺中是根據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);
}
}