package org.gcube.gcat.configuration; import java.util.HashMap; import java.util.Map; import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.gcat.configuration.isproxies.GCoreISConfigurationProxy; import org.gcube.gcat.configuration.isproxies.GCoreISConfigurationProxyFactory; import org.gcube.gcat.configuration.isproxies.FacetBasedISConfigurationProxyFactory; import org.gcube.gcat.configuration.isproxies.FacetBasedISConfigurationProxy; import org.gcube.gcat.configuration.service.ServiceCatalogueConfiguration; import org.gcube.gcat.persistence.ckan.CKANUserCache; /** * @author Luca Frosini (ISTI - CNR) */ public class CatalogueConfigurationFactory { private static final Map catalogueConfigurations; static { catalogueConfigurations = new HashMap<>(); } private static ServiceCatalogueConfiguration load(String context) { ServiceCatalogueConfiguration serviceCatalogueConfiguration = null; try { FacetBasedISConfigurationProxy facetBasedISConfigurationProxy = FacetBasedISConfigurationProxyFactory.getInstance(context); serviceCatalogueConfiguration = facetBasedISConfigurationProxy.getCatalogueConfiguration(); } catch(Exception e){ GCoreISConfigurationProxy gCoreISConfigurationProxy = GCoreISConfigurationProxyFactory.getInstance(); serviceCatalogueConfiguration = gCoreISConfigurationProxy.getCatalogueConfiguration(); } return serviceCatalogueConfiguration; } private static void purgeFromIS(String context) { FacetBasedISConfigurationProxy restISConfigurationProxy = FacetBasedISConfigurationProxyFactory.getInstance(context); restISConfigurationProxy.delete(); GCoreISConfigurationProxy gCoreISConfigurationProxy = GCoreISConfigurationProxyFactory.getInstance(); gCoreISConfigurationProxy.delete(); } private static void createOrUpdateOnIS(String context, ServiceCatalogueConfiguration catalogueConfiguration) throws Exception { FacetBasedISConfigurationProxy restISConfigurationProxy = FacetBasedISConfigurationProxyFactory.getInstance(context); restISConfigurationProxy.setCatalogueConfiguration(catalogueConfiguration); restISConfigurationProxy.createOrUpdateOnIS(); GCoreISConfigurationProxy gCoreISConfigurationProxy = GCoreISConfigurationProxyFactory.getInstance(); gCoreISConfigurationProxy.setCatalogueConfiguration(catalogueConfiguration); gCoreISConfigurationProxy.createOrUpdateOnIS(); } public synchronized static ServiceCatalogueConfiguration getInstance() { String context = SecretManagerProvider.instance.get().getContext(); ServiceCatalogueConfiguration catalogueConfiguration = catalogueConfigurations.get(context); if(catalogueConfiguration == null) { catalogueConfiguration = load(context); catalogueConfigurations.put(context, catalogueConfiguration); } return catalogueConfiguration; } public synchronized static void renew() { String context = SecretManagerProvider.instance.get().getContext(); catalogueConfigurations.remove(context); ServiceCatalogueConfiguration catalogueConfiguration = load(context); catalogueConfigurations.put(context, catalogueConfiguration); } public synchronized static void purge() { // Remove the resource from IS String context = SecretManagerProvider.instance.get().getContext(); catalogueConfigurations.remove(context); purgeFromIS(context); } public synchronized static ServiceCatalogueConfiguration createOrUpdate(ServiceCatalogueConfiguration catalogueConfiguration) throws Exception { String context = SecretManagerProvider.instance.get().getContext(); catalogueConfigurations.remove(context); createOrUpdateOnIS(context, catalogueConfiguration); catalogueConfigurations.put(context, catalogueConfiguration); // The supported organizations could be changed we need to empty the user cache for the context // to avoid to miss to add an user in an organization which has been added. CKANUserCache.emptyUserCache(); return catalogueConfiguration; } }