use of org.wso2.carbon.registry.core.caching.RegistryCacheKey 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();
}
}
}
use of org.wso2.carbon.registry.core.caching.RegistryCacheKey in project carbon-apimgt by wso2.
the class RegistryCacheInvalidationServiceTestCase method testInvalidateCacheWhenRegistryUnmountedAndCached.
@Test
public void testInvalidateCacheWhenRegistryUnmountedAndCached() throws APIManagementException {
List<RemoteConfiguration> remoteConfigurationList = new ArrayList<RemoteConfiguration>();
Mockito.when(registryContext.getRemoteInstances()).thenReturn(remoteConfigurationList);
DataBaseConfiguration dbConfiguration = Mockito.mock(DataBaseConfiguration.class);
Mockito.when(registryContext.getDefaultDataBaseConfiguration()).thenReturn(dbConfiguration);
PowerMockito.when(RegistryUtils.getResourceCache(RegistryConstants.REGISTRY_CACHE_BACKED_ID)).thenReturn(cache);
Mockito.when(dbConfiguration.getUserName()).thenReturn("john@foo.com");
Mockito.when(dbConfiguration.getDbUrl()).thenReturn("xyz.com/db");
RegistryCacheKey registryCacheKey = Mockito.mock(RegistryCacheKey.class);
PowerMockito.when(RegistryUtils.buildRegistryCacheKey("john@xyz.com/db", 6543, path)).thenReturn(registryCacheKey);
Mockito.when(cache.containsKey(registryCacheKey)).thenReturn(true);
RegistryCacheInvalidationService registryCacheInvalidationService = new RegistryCacheInvalidationService();
registryCacheInvalidationService.invalidateCache(path, tenantDomain);
Mockito.verify(cache, Mockito.times(1)).remove(Matchers.any());
}
use of org.wso2.carbon.registry.core.caching.RegistryCacheKey in project carbon-apimgt by wso2.
the class RegistryCacheInvalidationServiceTestCase method testInvalidateCacheWhenRegistryMountedAndCached.
@Test
public void testInvalidateCacheWhenRegistryMountedAndCached() throws APIManagementException {
List<RemoteConfiguration> remoteConfigurationList = new ArrayList<RemoteConfiguration>();
RemoteConfiguration remoteConfiguration = new RemoteConfiguration();
remoteConfiguration.setDbConfig("");
remoteConfigurationList.add(remoteConfiguration);
List<Mount> mountList = new ArrayList<Mount>();
Mount mount = new Mount();
mount.setPath("/_system/config");
mountList.add(mount);
DataBaseConfiguration dataBaseConfiguration = Mockito.mock(DataBaseConfiguration.class);
Mockito.when(registryContext.getMounts()).thenReturn(mountList);
Mockito.when(registryContext.getDBConfig(remoteConfiguration.getDbConfig())).thenReturn(dataBaseConfiguration);
Mockito.when(registryContext.getRemoteInstances()).thenReturn(remoteConfigurationList);
Mockito.when(registryContext.getDefaultDataBaseConfiguration()).thenReturn(dataBaseConfiguration);
PowerMockito.when(RegistryUtils.getResourceCache(RegistryConstants.REGISTRY_CACHE_BACKED_ID)).thenReturn(cache);
Mockito.when(dataBaseConfiguration.getUserName()).thenReturn("john@foo.com");
Mockito.when(dataBaseConfiguration.getDbUrl()).thenReturn("xyz.com/db");
RegistryCacheKey registryCacheKey = Mockito.mock(RegistryCacheKey.class);
PowerMockito.when(RegistryUtils.buildRegistryCacheKey("john@xyz.com/db", 6543, path)).thenReturn(registryCacheKey);
Mockito.when(cache.containsKey(registryCacheKey)).thenReturn(true);
RegistryCacheInvalidationService registryCacheInvalidationService = new RegistryCacheInvalidationService();
registryCacheInvalidationService.invalidateCache(path, tenantDomain);
Mockito.verify(cache, Mockito.times(1)).remove(Matchers.any());
}
Aggregations