Thursday, October 9, 2014

Load Tenant Registry in a Carbon Component

Load tenant registry in a Carbon component can be useful when you have to access registries across tenants when tenant unloading is enabled (This is enabled by default). For example in API Manager you are allowed to access APIs across tenants which saved in the tenant specific registry.

Let's see how this can be done.
In order to load tenant registry we need Tenant Registry Loader OSGi service. To access this service you can use Apache Felix Maven SCR annotations where you can use the following annotations to get the service reference in BundleActivator class.

*@scr.reference name="tenant.registryloader"
* interface="org.wso2.carbon.registry.core.service.TenantRegistryLoader"
* cardinality="1..1" policy="dynamic"
* bind="setTenantRegistryLoader"
* unbind="unsetTenantRegistryLoader"
As we defined in the annotations setTenantRegistryLoader() method will invoked when bind the OSGi service and unsetTenantRegistryLoader() method will invoke when unbind OSGi service. Following are the method implementations.
protected void setTenantRegistryLoader(TenantRegistryLoader tenantRegistryLoader) {
    this.tenantRegistryLoader = tenantRegistryLoader;
}

protected void unsetTenantRegistryLoader(TenantRegistryLoader tenantRegistryLoader) {
    this.tenantRegistryLoader = null;
}
Now we can use this service reference to load tenant registry inside a Carbon Component as follows.
tenantRegistryLoader.loadTenantRegistry(tenantId);
Hope you find it useful when you need to load tenant registry manually.

No comments:

Post a Comment