2018-11-05 15:45:31 +01:00
/ * *
*
* /
package org.gcube.datatransfer.resolver.caches ;
import java.util.concurrent.ExecutionException ;
import java.util.concurrent.TimeUnit ;
2018-11-05 17:52:43 +01:00
import org.gcube.common.scope.api.ScopeProvider ;
2018-11-05 15:45:31 +01:00
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 ;
/ * *
2018-11-05 15:58:49 +01:00
* The Class LoadingCatalogueApplicationProfilesCache .
2018-11-05 15:45:31 +01:00
*
* @author Francesco Mangiacrapa at ISTI - CNR ( francesco . mangiacrapa @isti.cnr.it )
* Nov 5 , 2018
* /
2018-11-05 15:58:49 +01:00
public class LoadingCatalogueApplicationProfilesCache {
2018-11-05 15:45:31 +01:00
2018-11-05 15:58:49 +01:00
private static Logger logger = LoggerFactory . getLogger ( LoadingCatalogueApplicationProfilesCache . class ) ;
2018-11-05 15:45:31 +01:00
private static LoadingCache < String , String > catalogueApplicationProfiles ;
static {
CacheLoader < String , String > loader = new CacheLoader < String , String > ( ) {
@Override
public String load ( String vreName )
throws Exception {
2018-11-05 17:52:43 +01:00
logger . info ( " Loading the cache for vreName: " + vreName ) ;
String fullScope = loadFullScopeByApplicationProfile ( vreName ) ;
logger . info ( " Returning fullScope: " + fullScope + " for the VRE name: " + vreName ) ;
return fullScope ;
2018-11-05 15:45:31 +01:00
}
} ;
RemovalListener < String , String > removalListener = new RemovalListener < String , String > ( ) {
@Override
public void onRemoval ( RemovalNotification < String , String > arg0 ) {
2018-11-05 15:58:49 +01:00
logger . info ( LoadingCatalogueApplicationProfilesCache . class . getSimpleName ( ) + " cache expired " ) ;
2018-11-05 15:45:31 +01:00
}
} ;
catalogueApplicationProfiles = CacheBuilder . newBuilder ( ) . maximumSize ( 100 ) . expireAfterWrite (
1 , TimeUnit . HOURS ) . removalListener ( removalListener ) .
build ( loader ) ;
2018-11-05 17:52:43 +01:00
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 ) {
2018-11-05 15:45:31 +01:00
2018-11-05 17:52:43 +01:00
} finally {
}
2018-11-05 15:45:31 +01:00
}
/ * *
* 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
* /
2018-11-05 17:52:43 +01:00
public static String loadFullScopeByApplicationProfile ( String vreName ) {
2018-11-05 15:45:31 +01:00
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 ;
}
}