uri-resolver/src/main/java/org/gcube/datatransfer/resolver/caches/LoadingGisViewerApplication...

95 lines
2.8 KiB
Java

/**
*
*/
package org.gcube.datatransfer.resolver.caches;
import java.util.concurrent.TimeUnit;
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileReader;
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;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
/**
* The Class LoadingGisViewerApplicationURLCache.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 5, 2018
*/
public class LoadingGisViewerApplicationURLCache {
private static Logger logger = LoggerFactory.getLogger(LoadingGisViewerApplicationURLCache.class);
//A cache (Scope, GisViewerApplication-URL)
private static LoadingCache<String, String> gisViewerApplicationURLCache;
static {
CacheLoader<String, String> loader = new CacheLoader<String, String> () {
@Override
public String load(String scope)
throws Exception {
logger.info("Loading the cache for scope: "+scope);
return loadGisViewerApplicationURL(scope);
}
};
RemovalListener<String, String> removalListener = new RemovalListener<String, String>() {
public void onRemoval(RemovalNotification<String, String> removal) {
logger.info(LoadingGisViewerApplicationURLCache.class.getSimpleName() +" cache expired");
}
};
gisViewerApplicationURLCache =
CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(
1, TimeUnit.DAYS).removalListener(removalListener).
build(loader);
logger.info(LoadingGisViewerApplicationURLCache.class.getSimpleName() +" instancied");
}
/**
* Gets the cache.
*
* @return the cache
*/
public static LoadingCache<String, String> getCache() {
return gisViewerApplicationURLCache;
}
/**
* Load gis viewer application url.
*
* @param scope the scope
* @return the string
*/
public static String loadGisViewerApplicationURL(String scope){
if (scope == null || scope.isEmpty())
logger.warn("Scope is null or ermpty, skipping loadGisViewerApplicationURL");
ApplicationProfileReader reader = new ApplicationProfileReader(scope, UriResolverStartupListener.getGisViewerProfile().getGenericResource(), UriResolverStartupListener.getGisViewerProfile().getAppId(), false);
if(reader.getApplicationProfile()==null){
logger.error("NO Appllication Profile "+UriResolverStartupListener.getGisViewerProfile().getAppId()+" found in the scope: "+scope+", returning null!");
return null;
}
String url = reader.getApplicationProfile().getUrl();
logger.info("With scope "+scope+" loaded the GisViewer Application URL "+url);
return url;
}
}