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

120 lines
3.4 KiB
Java

/**
*
*/
package org.gcube.datatransfer.resolver.caches;
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("cache expired");
//prePopulateCache();
}
};
catalogueApplicationProfiles = CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(
1, TimeUnit.DAYS).removalListener(removalListener).
build(loader);
//Populating the cache at init stage
populateTheCache();
logger.info("Pre-Loaded CatalogueApplicationProfiles cache with: "+catalogueApplicationProfiles.asMap().size()+" item/s");
}
/**
* Populate the cache.
*/
private static void populateTheCache(){
try{
//POPULATE THE CACHE READING THE RESOURCE "CATALOGUE-RESOLVER"
logger.info("Trying to pre-populate catalogue resolver cache");
ScopeProvider.instance.set(UriResolverStartupListener.getRootContextScope());
ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(UriResolverStartupListener.getRootContextScope(), true);
catalogueApplicationProfiles.asMap().putAll(appPrCatResolver.getHashVreNameScope());
logger.info("Cache populated with: "+catalogueApplicationProfiles.asMap().toString());
//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){
//THIS CHECK SHOULD BE NOT NEEDED
String fullScope = catalogueApplicationProfiles.getIfPresent(vreName);
if(fullScope==null){
populateTheCache();
fullScope = catalogueApplicationProfiles.getIfPresent(vreName);
}
return fullScope;
}
}