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

192 lines
5.4 KiB
Java

/**
*
*/
package org.gcube.datatransfer.resolver.caches;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.init.UriResolverSmartGearManagerInit;
import org.gcube.infrastructure.detachedres.detachedreslibrary.server.DetachedREsClient;
import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.DetachedREs;
import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.Gateway;
import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.VO;
import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.VRE;
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 LoadingMapOfDetachedVRE.
*
* @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy)
* Jun 8, 2020
*/
public class LoadingMapOfDetachedVRE {
private static Logger logger = LoggerFactory.getLogger(LoadingMapOfDetachedVRE.class);
private static LoadingCache<String, VRE> vreNameToVRE;
static{
CacheLoader<String, VRE> loader = new CacheLoader<String, VRE>(){
@Override
public VRE load(String vreName)
throws Exception {
logger.info("Loading the cache for VRE name: {}",vreName);
VRE theVRE = loadFullScopeforScopeName(vreName);
logger.info("Returning {} with scope {} for the VRE name: {}", VRE.class.getSimpleName(), theVRE.getScope(), vreName);
return theVRE;
}
};
RemovalListener<String, VRE> removalListener = new RemovalListener<String, VRE>() {
@Override
public void onRemoval(RemovalNotification<String, VRE> arg0) {
logger.info("cache expired");
//prePopulateCache();
}
};
vreNameToVRE = CacheBuilder.newBuilder().maximumSize(300).expireAfterWrite(
1, TimeUnit.DAYS).removalListener(removalListener).
build(loader);
//Populating the cache at init stage
populateTheCache();
logger.info("Pre-Loaded detached VRE Name to VRE Obj cache with: "+vreNameToVRE.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 the detached VREs cache with mapping (VRE Name, VRE Obj)");
ScopeProvider.instance.set(UriResolverSmartGearManagerInit.getRootContextScope());
DetachedREsClient detachedREsClient = new DetachedREsClient();
DetachedREs detachedREs = detachedREsClient.getDetachedREs();
int totalVREDimissed = 0;
for (Gateway gateway : detachedREs.getGateways().values()) {
logger.trace("Gateway: " + gateway.getName());
int vreDismissedPerGatew = 0;
for (VO vo : gateway.getVos().values()) {
logger.trace("VO: " + vo.getName());
for (VRE vre : vo.getVres().values()) {
if(logger.isTraceEnabled()) {
logger.trace("VRE name: " + vre.getName() +
" scope: "+vre.getScope() +
" VRE catalogue url: " + vre.getCatalogUrl() +
" VRE catalog Portlet URL: "+vre.getCatalogPortletURL());
}
try {
if(vre.getScope()!=null && !vre.getScope().isEmpty()) {
String vreName = vre.getScope().split("/")[3];
vreNameToVRE.put(vreName, vre);
vreDismissedPerGatew++;
}
}catch (Exception e) {
logger.warn("Error on parsing the scope: "+vre.getScope()+ " skipping it");
}
}
}
logger.debug("\nVREs dismissed loaded and cached per " + gateway.getName() + " are: "+vreDismissedPerGatew);
totalVREDimissed+=vreDismissedPerGatew;
}
logger.debug("\nTotal detached VREs are: "+totalVREDimissed);
logger.info("Cache populated with: ");
ConcurrentMap<String, VRE> mapOfVreNames = vreNameToVRE.asMap();
for (String key : mapOfVreNames.keySet()) {
VRE theDetachedVRE = mapOfVreNames.get(key);
logger.info("detached VRE with scope {}, catalogueURL {}, cataloguePortletURL {}",
theDetachedVRE.getScope(), theDetachedVRE.getCatalogUrl(),
theDetachedVRE.getCatalogPortletURL());
}
}catch(Exception e){
//SILENT
}finally{
}
}
/**
* Gets the.
*
* @param vreName the vre name
* @return the vre
* @throws ExecutionException the execution exception
*/
public static VRE get(String vreName) throws ExecutionException{
boolean cacheNotLoaded = true;
final int MAX_ATTEMPTS = 3;
int retry = 1;
while (cacheNotLoaded) {
cacheNotLoaded = vreNameToVRE == null;
if(retry<MAX_ATTEMPTS) {
try {
Thread.sleep(retry * 1000);
} catch (InterruptedException e) {
}
}
retry++;
logger.debug("is cache loaded? {}",!cacheNotLoaded);
}
return vreNameToVRE.get(vreName);
}
/**
* Load full scopefor scope name.
*
* @param vreName the scope name
* @return the scope bean
*/
protected static VRE loadFullScopeforScopeName(String vreName){
//THIS CHECK SHOULD BE NOT NEEDED
VRE theVRE = vreNameToVRE.getIfPresent(vreName);
if(theVRE==null){
populateTheCache();
theVRE = vreNameToVRE.getIfPresent(vreName);
}
return theVRE;
}
/**
* Gets the cache.
*
* @return the cache
*/
public LoadingCache<String, VRE> getCache(){
return vreNameToVRE;
}
}