Search in sources :

Example 1 with DataBaseConfiguration

use of org.wso2.carbon.registry.core.config.DataBaseConfiguration 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();
        }
    }
}
Also used : RemoteConfiguration(org.wso2.carbon.registry.core.config.RemoteConfiguration) RegistryCacheKey(org.wso2.carbon.registry.core.caching.RegistryCacheKey) Mount(org.wso2.carbon.registry.core.config.Mount) UserStoreException(org.wso2.carbon.user.api.UserStoreException) DataBaseConfiguration(org.wso2.carbon.registry.core.config.DataBaseConfiguration) Registry(org.wso2.carbon.registry.core.Registry) GhostResource(org.wso2.carbon.registry.api.GhostResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 2 with DataBaseConfiguration

use of org.wso2.carbon.registry.core.config.DataBaseConfiguration 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());
}
Also used : RemoteConfiguration(org.wso2.carbon.registry.core.config.RemoteConfiguration) ArrayList(java.util.ArrayList) RegistryCacheKey(org.wso2.carbon.registry.core.caching.RegistryCacheKey) DataBaseConfiguration(org.wso2.carbon.registry.core.config.DataBaseConfiguration) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with DataBaseConfiguration

use of org.wso2.carbon.registry.core.config.DataBaseConfiguration in project carbon-apimgt by wso2.

the class RegistryCacheInvalidationServiceTestCase method testInvalidateCacheWhenRegistryUnmountedAndNotCached.

@Test
public void testInvalidateCacheWhenRegistryUnmountedAndNotCached() 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);
    RegistryCacheInvalidationService registryCacheInvalidationService = new RegistryCacheInvalidationService();
    registryCacheInvalidationService.invalidateCache(path, tenantDomain);
    Mockito.verify(cache, Mockito.times(0)).remove(Matchers.any());
}
Also used : RemoteConfiguration(org.wso2.carbon.registry.core.config.RemoteConfiguration) ArrayList(java.util.ArrayList) DataBaseConfiguration(org.wso2.carbon.registry.core.config.DataBaseConfiguration) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with DataBaseConfiguration

use of org.wso2.carbon.registry.core.config.DataBaseConfiguration 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());
}
Also used : RemoteConfiguration(org.wso2.carbon.registry.core.config.RemoteConfiguration) ArrayList(java.util.ArrayList) Mount(org.wso2.carbon.registry.core.config.Mount) RegistryCacheKey(org.wso2.carbon.registry.core.caching.RegistryCacheKey) DataBaseConfiguration(org.wso2.carbon.registry.core.config.DataBaseConfiguration) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

DataBaseConfiguration (org.wso2.carbon.registry.core.config.DataBaseConfiguration)4 RemoteConfiguration (org.wso2.carbon.registry.core.config.RemoteConfiguration)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 RegistryCacheKey (org.wso2.carbon.registry.core.caching.RegistryCacheKey)3 Mount (org.wso2.carbon.registry.core.config.Mount)2 GhostResource (org.wso2.carbon.registry.api.GhostResource)1 Registry (org.wso2.carbon.registry.core.Registry)1 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)1 UserStoreException (org.wso2.carbon.user.api.UserStoreException)1