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

118 lines
3.5 KiB
Java

/**
*
*/
package org.gcube.datatransfer.resolver.caches;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.catalogue.resource.ApplicationProfileReaderForCatalogueResolver;
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 LoadingCatalogueApplicationProfilesCache.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 5, 2018
*/
public class LoadingCatalogueApplicationProfilesCache {
private static Logger logger = LoggerFactory.getLogger(LoadingCatalogueApplicationProfilesCache.class);
private static LoadingCache<String, String> catalogueApplicationProfiles;
static{
CacheLoader<String, String> loader = new CacheLoader<String, String>(){
@Override
public String load(String vreName)
throws Exception {
logger.info("Loading the cache for vreName: "+vreName);
String fullScope = loadFullScopeByApplicationProfile(vreName);
logger.info("Returning fullScope: "+fullScope+ " for the VRE name: "+vreName);
return fullScope;
}
};
RemovalListener<String, String> removalListener = new RemovalListener<String, String>() {
@Override
public void onRemoval(RemovalNotification<String, String> arg0) {
logger.info(LoadingCatalogueApplicationProfilesCache.class.getSimpleName() +" cache expired");
}
};
catalogueApplicationProfiles = CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(
1, TimeUnit.HOURS).removalListener(removalListener).
build(loader);
try{
//PRE-POPULATE CACHE
logger.info("Trying to pre-poluate catalogue resolver cache");
ScopeProvider.instance.set(UriResolverStartupListener.getContextScope());
ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(UriResolverStartupListener.getContextScope(), true);
catalogueApplicationProfiles.asMap().putAll(appPrCatResolver.getHashVreNameScope());
logger.info(LoadingCatalogueApplicationProfilesCache.class.getSimpleName() +" instancied");
logger.info("Pre-Loaded CatalogueApplicationProfiles cache is: "+catalogueApplicationProfiles.asMap().toString());
}catch(Exception e){
}finally{
}
}
/**
* Gets the cache.
*
* @return the cache
*/
public static LoadingCache<String, String> getCache(){
return catalogueApplicationProfiles;
}
/**
* Load application profiles.
*
* @param vreName the vre name
* @return the string
*/
public static String loadFullScopeByApplicationProfile(String vreName){
String fullScope = null;
try {
fullScope = catalogueApplicationProfiles.get(vreName);
}
catch (ExecutionException e) {
logger.warn("ExecutionException: ",e);
}
if(fullScope==null){
logger.debug("FullScope is null for VRE_NAME: "+vreName+" into Application Profile: "+ApplicationProfileReaderForCatalogueResolver.RESOURCE_NAME+", reading profile again");
ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(UriResolverStartupListener.getContextScope(), true);
catalogueApplicationProfiles.asMap().putAll(appPrCatResolver.getHashVreNameScope());
}
return fullScope;
}
}