Tenant context


First, the tenant module (OSGi bundle) is located in jonas/modules/libraries/tenant, its structure is :

Tenant folder structure

Tenant identifier

The tenant identifier is specified in the web deployment descriptor (jonas-web.xml) as follows:
<tenant-id> VALUE </tenant-id>
NOTE : A new xml schema was created related to the 5.3 jonas version to support this new item. Think to update the used schema in the web app.



This tenant identifier is bound during the deployment of the application. The linked name is java:comp/tenantId.

Tenant Context

The tenant context is composed of :
  • TenantId : tenant identifier

    To access the context of the current tenant, use :

TenantCurrent.getCurrent().getTenantContext();

HTTP Tenant Filter

To set the context on a thread, a HTTP filter is used. In fact, before answering the request, we have to save the context of the current tenant and set the new context :
// Save the current context
old = TenantCurrent.getCurrent().getTenantContext();
TenantCurrent.getCurrent().setTenantContext(this.ctx);
Next, execute the request. And finally, restore the old context :
// Restore the old context
TenantCurrent.getCurrent().setTenantContext(old);


This filter is created by calling the method getTenantIdFilter() set in BaseWebContainerService. The valve is set in Tomcat7Service as follows :

// For the tenantId
Filter tenantIdHttpFilter = null;
// get an instance of the filtre
tenantIdHttpFilter = getTenantIdFilter(webDD.getTenantId());
// needs to add this filter on the context
jStdCtx.addValve(new FilterValveWrapper(tenantIdHttpFilter));