/** * */ package org.gcube.datatransfer.resolver.caches; import java.util.concurrent.TimeUnit; import org.gcube.datatransfer.resolver.gis.GeonetworkAccessParameter; import org.gcube.datatransfer.resolver.gis.GeonetworkAccessParameter.GeonetworkLoginLevel; import org.gcube.datatransfer.resolver.gis.GeonetworkInstance; import org.gcube.datatransfer.resolver.gis.exception.GeonetworkInstanceException; import org.gcube.datatransfer.resolver.listeners.UriResolverStartupListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; /** * The Class GeoentworkInstanceGuavaCache. * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) * Nov 2, 2018 */ public class GeoentworkInstanceGuavaCache { private static Logger logger = LoggerFactory.getLogger(UriResolverStartupListener.class); private static LoadingCache geonetworkInstancesCache; static { geonetworkInstancesCache = CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite( 10, TimeUnit.MINUTES).build( new CacheLoader() { @Override public GeonetworkInstance load(String scope) throws Exception { return loadGeonetworkInstance(scope); } }); logger.info(GeoentworkInstanceGuavaCache.class.getSimpleName() +" instancied"); } /** * Gets the cache. * * @return the cache */ public static LoadingCache getCache() { return geonetworkInstancesCache; } /** * Load geonetwork instance. * * @param scope the scope * @return the geonetwork instance * @throws GeonetworkInstanceException the geonetwork instance exception */ public static GeonetworkInstance loadGeonetworkInstance(String scope) throws GeonetworkInstanceException { if (scope == null || scope.isEmpty()) logger.warn("Scope is null or ermpty, skipping loadGeonetworkInstance"); GeonetworkAccessParameter gntwAccess = new GeonetworkAccessParameter(scope); GeonetworkInstance gnInstance = gntwAccess.getGeonetworkInstance(true, GeonetworkLoginLevel.ADMIN); logger.info("Loaded "+gnInstance+" for scope: " + scope); return gnInstance; } }