use of org.wso2.carbon.registry.api.GhostResource in project carbon-apimgt by wso2.
the class RegistryCacheInvalidationService method invalidateCache.
/**
* This method invalidates registry cache for given resource in given tenant domain
* @param path
* @param tenantDomain
* @throws APIManagementException
*/
public void invalidateCache(String path, String tenantDomain) throws APIManagementException {
Registry registry;
boolean isTenantFlowStarted = false;
try {
int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
isTenantFlowStarted = true;
registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
Cache<RegistryCacheKey, GhostResource> cache = RegistryUtils.getResourceCache(RegistryConstants.REGISTRY_CACHE_BACKED_ID);
RegistryCacheKey cacheKey = null;
// Is registry mounted
if (registry.getRegistryContext().getRemoteInstances().size() > 0) {
for (Mount mount : registry.getRegistryContext().getMounts()) {
for (RemoteConfiguration configuration : registry.getRegistryContext().getRemoteInstances()) {
if (path.startsWith(mount.getPath())) {
DataBaseConfiguration dataBaseConfiguration = registry.getRegistryContext().getDBConfig(configuration.getDbConfig());
String connectionId = (dataBaseConfiguration.getUserName() != null ? dataBaseConfiguration.getUserName().split("@")[0] : dataBaseConfiguration.getUserName()) + "@" + dataBaseConfiguration.getDbUrl();
cacheKey = RegistryUtils.buildRegistryCacheKey(connectionId, tenantId, path);
if (cacheKey != null && cache.containsKey(cacheKey)) {
cache.remove(cacheKey);
}
}
}
}
} else {
DataBaseConfiguration dataBaseConfiguration = registry.getRegistryContext().getDefaultDataBaseConfiguration();
String connectionId = (dataBaseConfiguration.getUserName() != null ? dataBaseConfiguration.getUserName().split("@")[0] : dataBaseConfiguration.getUserName()) + "@" + dataBaseConfiguration.getDbUrl();
cacheKey = RegistryUtils.buildRegistryCacheKey(connectionId, tenantId, path);
if (cacheKey != null && cache.containsKey(cacheKey)) {
cache.remove(cacheKey);
}
}
} catch (RegistryException e) {
APIUtil.handleException("Error in accessing governance registry while invalidating cache for " + path + "in tenant " + tenantDomain, e);
} catch (UserStoreException e) {
APIUtil.handleException("Error in retrieving Tenant Information while invalidating cache for " + path + "in tenant " + tenantDomain, e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
Aggregations