2018-11-02 15:58:38 +01:00
/ * *
*
* /
package org.gcube.datatransfer.resolver.caches ;
2018-12-17 11:48:18 +01:00
import java.util.concurrent.ExecutionException ;
2018-11-02 15:58:38 +01:00
import java.util.concurrent.TimeUnit ;
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileReader ;
2018-11-15 15:35:54 +01:00
import org.gcube.datatransfer.resolver.init.UriResolverSmartGearManagerInit ;
2018-11-02 15:58:38 +01:00
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 ;
2018-11-05 12:55:05 +01:00
import com.google.common.cache.RemovalListener ;
import com.google.common.cache.RemovalNotification ;
2018-11-02 15:58:38 +01:00
2018-11-05 15:45:31 +01:00
2018-11-05 15:58:49 +01:00
2018-11-02 15:58:38 +01:00
/ * *
2018-11-05 15:58:49 +01:00
* The Class LoadingGisViewerApplicationURLCache .
2018-11-02 15:58:38 +01:00
*
* @author Francesco Mangiacrapa at ISTI - CNR ( francesco . mangiacrapa @isti.cnr.it )
2018-11-05 15:45:31 +01:00
* Nov 5 , 2018
2018-11-02 15:58:38 +01:00
* /
2018-11-05 15:58:49 +01:00
public class LoadingGisViewerApplicationURLCache {
2018-11-02 15:58:38 +01:00
2018-11-05 15:58:49 +01:00
private static Logger logger = LoggerFactory . getLogger ( LoadingGisViewerApplicationURLCache . class ) ;
2018-11-05 12:55:05 +01:00
//A cache (Scope, GisViewerApplication-URL)
2018-11-02 15:58:38 +01:00
private static LoadingCache < String , String > gisViewerApplicationURLCache ;
static {
2018-11-05 12:55:05 +01:00
CacheLoader < String , String > loader = new CacheLoader < String , String > ( ) {
@Override
public String load ( String scope )
throws Exception {
2018-11-05 17:52:43 +01:00
logger . info ( " Loading the cache for scope: " + scope ) ;
2018-11-05 12:55:05 +01:00
return loadGisViewerApplicationURL ( scope ) ;
}
} ;
RemovalListener < String , String > removalListener = new RemovalListener < String , String > ( ) {
public void onRemoval ( RemovalNotification < String , String > removal ) {
2020-06-15 17:46:56 +02:00
logger . info ( " cache expired " ) ;
2018-11-05 12:55:05 +01:00
}
} ;
2018-11-02 15:58:38 +01:00
2018-11-05 12:55:05 +01:00
gisViewerApplicationURLCache =
CacheBuilder . newBuilder ( ) . maximumSize ( 100 ) . expireAfterWrite (
1 , TimeUnit . DAYS ) . removalListener ( removalListener ) .
build ( loader ) ;
2018-11-02 15:58:38 +01:00
2018-11-06 15:20:05 +01:00
logger . info ( " cache instancied " ) ;
2018-11-02 15:58:38 +01:00
}
/ * *
2018-12-17 11:48:18 +01:00
* Gets the .
2018-11-02 15:58:38 +01:00
*
2018-12-17 11:48:18 +01:00
* @param scope the scope
* @return the string
* @throws ExecutionException the execution exception
2018-11-02 15:58:38 +01:00
* /
2018-12-17 11:48:18 +01:00
public static String get ( String scope ) throws ExecutionException {
2018-11-02 15:58:38 +01:00
2018-12-17 11:48:18 +01:00
return gisViewerApplicationURLCache . get ( scope ) ;
2018-11-02 15:58:38 +01:00
}
2018-12-17 11:48:18 +01:00
2018-11-02 15:58:38 +01:00
/ * *
* Load gis viewer application url .
*
* @param scope the scope
* @return the string
* /
2018-12-17 11:48:18 +01:00
protected static String loadGisViewerApplicationURL ( String scope ) {
2018-11-02 15:58:38 +01:00
if ( scope = = null | | scope . isEmpty ( ) )
logger . warn ( " Scope is null or ermpty, skipping loadGisViewerApplicationURL " ) ;
2018-11-15 15:35:54 +01:00
ApplicationProfileReader reader = new ApplicationProfileReader ( scope , UriResolverSmartGearManagerInit . getGisViewerProfile ( ) . getGenericResource ( ) , UriResolverSmartGearManagerInit . getGisViewerProfile ( ) . getAppId ( ) , false ) ;
2018-11-05 17:52:43 +01:00
if ( reader . getApplicationProfile ( ) = = null ) {
2018-11-15 15:35:54 +01:00
logger . error ( " NO Appllication Profile " + UriResolverSmartGearManagerInit . getGisViewerProfile ( ) . getAppId ( ) + " found in the scope: " + scope + " , returning null! " ) ;
2018-11-05 17:52:43 +01:00
return null ;
}
2018-11-02 15:58:38 +01:00
String url = reader . getApplicationProfile ( ) . getUrl ( ) ;
logger . info ( " With scope " + scope + " loaded the GisViewer Application URL " + url ) ;
return url ;
}
}