2018-11-02 15:58:38 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.gcube.datatransfer.resolver.caches;
|
|
|
|
|
2018-12-17 11:48:18 +01:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2018-11-02 15:58:38 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import org.gcube.datatransfer.resolver.gis.GeonetworkAccessParameter;
|
2019-05-21 16:27:58 +02:00
|
|
|
import org.gcube.datatransfer.resolver.gis.GeonetworkAccessParameter.AccountType;
|
2018-11-02 15:58:38 +01:00
|
|
|
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.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.google.common.cache.CacheBuilder;
|
|
|
|
import com.google.common.cache.CacheLoader;
|
|
|
|
import com.google.common.cache.LoadingCache;
|
2018-11-05 12:55:05 +01:00
|
|
|
import com.google.common.cache.RemovalListener;
|
|
|
|
import com.google.common.cache.RemovalNotification;
|
2018-11-02 15:58:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-11-05 15:58:49 +01:00
|
|
|
* The Class LoadingGeonetworkInstanceCache.
|
2018-11-02 15:58:38 +01:00
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
2018-11-05 15:45:31 +01:00
|
|
|
* Nov 5, 2018
|
2018-11-02 15:58:38 +01:00
|
|
|
*/
|
2018-11-05 15:58:49 +01:00
|
|
|
public class LoadingGeonetworkInstanceCache {
|
2018-11-02 15:58:38 +01:00
|
|
|
|
2018-11-05 15:58:49 +01:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(LoadingGeonetworkInstanceCache.class);
|
2018-11-02 15:58:38 +01:00
|
|
|
|
2018-11-05 12:55:05 +01:00
|
|
|
//A cache (Scope, GeonetworkInstance)
|
2018-11-02 15:58:38 +01:00
|
|
|
private static LoadingCache<String, GeonetworkInstance> geonetworkInstancesCache;
|
2018-11-05 12:55:05 +01:00
|
|
|
|
2018-11-02 15:58:38 +01:00
|
|
|
static {
|
|
|
|
|
2018-11-05 12:55:05 +01:00
|
|
|
CacheLoader<String, GeonetworkInstance> loader = new CacheLoader<String, GeonetworkInstance> () {
|
|
|
|
@Override
|
|
|
|
public GeonetworkInstance load(String scope)
|
|
|
|
throws Exception {
|
2018-11-06 14:24:33 +01:00
|
|
|
logger.info("Loading the cache for scope: "+scope);
|
2018-11-05 12:55:05 +01:00
|
|
|
return loadGeonetworkInstance(scope);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
RemovalListener<String, GeonetworkInstance> removalListener = new RemovalListener<String, GeonetworkInstance>() {
|
|
|
|
public void onRemoval(RemovalNotification<String, GeonetworkInstance> removal) {
|
2020-06-18 15:48:47 +02:00
|
|
|
logger.debug("cache expired");
|
2018-11-05 12:55:05 +01:00
|
|
|
}
|
|
|
|
};
|
2018-11-02 15:58:38 +01:00
|
|
|
|
2018-11-05 12:55:05 +01:00
|
|
|
geonetworkInstancesCache =
|
2019-05-21 16:27:58 +02:00
|
|
|
CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(
|
2018-11-05 12:55:05 +01:00
|
|
|
1, TimeUnit.DAYS).removalListener(removalListener).
|
|
|
|
build(loader);
|
2018-11-02 15:58:38 +01:00
|
|
|
|
2018-11-06 15:20:05 +01:00
|
|
|
logger.info("cache instancied");
|
2018-11-02 15:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-12-17 11:48:18 +01:00
|
|
|
* Gets the.
|
2018-11-02 15:58:38 +01:00
|
|
|
*
|
2018-12-17 11:48:18 +01:00
|
|
|
* @param scope the scope
|
|
|
|
* @return the geonetwork instance
|
|
|
|
* @throws ExecutionException the execution exception
|
2018-11-02 15:58:38 +01:00
|
|
|
*/
|
2018-12-17 11:48:18 +01:00
|
|
|
public static GeonetworkInstance get(String scope) throws ExecutionException{
|
2018-11-02 15:58:38 +01:00
|
|
|
|
2018-12-17 11:48:18 +01:00
|
|
|
return geonetworkInstancesCache.get(scope);
|
2018-11-02 15:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load geonetwork instance.
|
|
|
|
*
|
|
|
|
* @param scope the scope
|
|
|
|
* @return the geonetwork instance
|
|
|
|
* @throws GeonetworkInstanceException the geonetwork instance exception
|
|
|
|
*/
|
2018-12-17 11:48:18 +01:00
|
|
|
protected static GeonetworkInstance loadGeonetworkInstance(String scope)
|
2018-11-02 15:58:38 +01:00
|
|
|
throws GeonetworkInstanceException {
|
|
|
|
|
2018-11-06 14:24:33 +01:00
|
|
|
if (scope == null || scope.isEmpty()){
|
|
|
|
logger.warn("Scope is null or empty, returning GeonetworkInstance as null");
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-02 15:58:38 +01:00
|
|
|
|
|
|
|
GeonetworkAccessParameter gntwAccess = new GeonetworkAccessParameter(scope);
|
2019-05-22 15:32:25 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* !!!!!!!DOES NOT AUTHENTICATE NEVER!!!!!!
|
|
|
|
*
|
|
|
|
*/
|
2019-05-21 16:27:58 +02:00
|
|
|
GeonetworkInstance geoInstance = gntwAccess.getGeonetworkInstance(false, GeonetworkLoginLevel.CKAN, AccountType.CKAN);
|
|
|
|
logger.info("Loaded "+geoInstance+" for scope: " + scope);
|
|
|
|
return geoInstance;
|
2018-11-02 15:58:38 +01:00
|
|
|
}
|
|
|
|
}
|