Search in sources :

Example 1 with GhostResource

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();
        }
    }
}
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)

Aggregations

GhostResource (org.wso2.carbon.registry.api.GhostResource)1 Registry (org.wso2.carbon.registry.core.Registry)1 RegistryCacheKey (org.wso2.carbon.registry.core.caching.RegistryCacheKey)1 DataBaseConfiguration (org.wso2.carbon.registry.core.config.DataBaseConfiguration)1 Mount (org.wso2.carbon.registry.core.config.Mount)1 RemoteConfiguration (org.wso2.carbon.registry.core.config.RemoteConfiguration)1 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)1 UserStoreException (org.wso2.carbon.user.api.UserStoreException)1