This documentation is for WSO2 Carbon 4.2.0. View documentation for the latest release.
Page Comparison - CarbonContext API (v.19 vs v.20) - Carbon 4.2.0 - WSO2 Documentation

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

You should have an idea, under which thread you are executing. It is better to resolve the CarbonContext outside that util method, as opposed to resolving the CarbonContext within the method. If calling CarbonContext.getCurrentContext returns a CarbonContext with tenant ID tenantId=-1 or tenant domain tenantDomain=null, that means, some upstream code has not populated the data needed for the CarbonContext; therefore, you have to first look into fixing this issue.

...

During an execution flow, sometimes you will need to switch from super tenant mode to tenant mode, do some work as that tenant, and then get back to super tenant mode. In such a scenario, you will do the following;

...

startTenantFlow

...

:

  1. Start the Tenant Flow.

  2. Set the tenant ID and the tenant domain in the new CarbonContext data holder that gets created when you start the tenant flow.

  3. Do somethingCarryout the respective action.

  4. endTenantFlow End the tenant flow.

Info

Always follow the following template when you carryout the latter mentioned steps:

Code Block
try{
    PrivilegedCarbonContext.startTenantFlow();
    PrivilegedCarbonContext privilegedCarbonContext =
               PrivilegedCarbonContext.getCurrentContext()
   privilegedCarbonContext.setTenantId(tenantId);
   privilegedCarbonContext.setTenantDomain(tenantDomain);
   // set other stuff like registry etc. if needed

  doSomething();
} finally {
   PrivilegedCarbonContext.endTenantFlow();
}

...

public int getTenantId()

This method retrieves the tenant ID.

String getUsername()

This method retrieves the tenant's username.

String getTenantDomain()

This method retrieves the tenant domain.

Registry getRegistry(RegistryType type)

...

  • USER_CONFIGURATION - The configuration registry of the currently logged in user.
  • USER_GOVERNANCE - The governance registry of the currently logged in user.
  • SYSTEM_CONFIGURATION - The system configuration registry of the tenant.
UserRealm getUserRealm()

This method retrieves the user realm of the current tenant.

CarbonQueue<?> getQueue(String name)

...