completed caches and refactored

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@173959 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-11-05 14:45:31 +00:00
parent 37d0816954
commit 04d897ee7c
10 changed files with 760 additions and 687 deletions

View File

@ -0,0 +1,107 @@
/**
*
*/
package org.gcube.datatransfer.resolver.caches;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
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 CatalogueApplicationProfilesCache.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 5, 2018
*/
public class CatalogueApplicationProfilesCache {
private static Logger logger = LoggerFactory.getLogger(CatalogueApplicationProfilesCache.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(CatalogueApplicationProfilesCache.class.getSimpleName() +" loaded");
return loadApplicationProfiles(vreName);
}
};
RemovalListener<String, String> removalListener = new RemovalListener<String, String>() {
@Override
public void onRemoval(RemovalNotification<String, String> arg0) {
logger.info(CatalogueApplicationProfilesCache.class.getSimpleName() +" cache expired");
}
};
catalogueApplicationProfiles = CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(
1, TimeUnit.HOURS).removalListener(removalListener).
build(loader);
//PRE-POPULATE CACHE
ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(UriResolverStartupListener.getContextScope(), true);
catalogueApplicationProfiles.asMap().putAll(appPrCatResolver.getHashVreNameScope());
logger.info(CatalogueApplicationProfilesCache.class.getSimpleName() +" instancied "+catalogueApplicationProfiles);
logger.info("Pre-Loaded cache is: "+catalogueApplicationProfiles.toString());
}
/**
* 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 loadApplicationProfiles(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;
}
}

View File

@ -1,60 +0,0 @@
/**
*
*/
package org.gcube.datatransfer.resolver.caches;
import java.util.concurrent.TimeUnit;
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;
/**
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Nov 5, 2018
*/
public class CatalogueApplicationProfilesGuavaCache {
private static Logger logger = LoggerFactory.getLogger(CatalogueApplicationProfilesGuavaCache.class);
private static LoadingCache<String, String> catalogueApplicationProfiles;
static{
CacheLoader<String, String> loader = new CacheLoader<String, String>(){
@Override
public String load(String arg0)
throws Exception {
logger.info(CatalogueApplicationProfilesGuavaCache.class.getSimpleName() +" loaded");
//ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(scopeToEncDecr, true);
return null;
}
};
RemovalListener<String, String> removalListener = new RemovalListener<String, String>() {
@Override
public void onRemoval(RemovalNotification<String, String> arg0) {
logger.info(CatalogueApplicationProfilesGuavaCache.class.getSimpleName() +" cache expired");
}
};
catalogueApplicationProfiles = CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(
1, TimeUnit.HOURS).removalListener(removalListener).
build(loader);
}
}

View File

@ -18,15 +18,16 @@ import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
/**
* The Class GeoExplorerApplicationHostnameGuavaCache.
* The Class GeoExplorerApplicationURLCache.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 2, 2018
* Nov 5, 2018
*/
public class GeoExplorerApplicationURLGuavaCache {
public class GeoExplorerApplicationURLCache {
private static Logger logger = LoggerFactory.getLogger(GeoExplorerApplicationURLGuavaCache.class);
private static Logger logger = LoggerFactory.getLogger(GeoExplorerApplicationURLCache.class);
//A cache (Scope, GeoExplorer-URL)
private static LoadingCache<String, String> geoExplorerApplicationURLCache;
@ -37,14 +38,14 @@ public class GeoExplorerApplicationURLGuavaCache {
@Override
public String load(String scope)
throws Exception {
logger.info(GeoExplorerApplicationURLGuavaCache.class.getSimpleName() +" loaded");
logger.info(GeoExplorerApplicationURLCache.class.getSimpleName() +" loaded");
return loadGeoExplorerApplicationURL(scope);
}
};
RemovalListener<String, String> removalListener = new RemovalListener<String, String>() {
public void onRemoval(RemovalNotification<String, String> removal) {
logger.info(GeoExplorerApplicationURLGuavaCache.class.getSimpleName() +" cache expired");
logger.info(GeoExplorerApplicationURLCache.class.getSimpleName() +" cache expired");
}
};
@ -53,7 +54,7 @@ public class GeoExplorerApplicationURLGuavaCache {
1, TimeUnit.DAYS).removalListener(removalListener).
build(loader);
logger.info(GeoExplorerApplicationURLGuavaCache.class.getSimpleName() +" instancied");
logger.info(GeoExplorerApplicationURLCache.class.getSimpleName() +" instancied");
}
/**
@ -83,6 +84,5 @@ public class GeoExplorerApplicationURLGuavaCache {
String url = reader.getApplicationProfile().getUrl();
logger.info("With scope "+scope+" loaded the GeoExplorer Application URL "+url);
return url;
}
}

View File

@ -20,15 +20,16 @@ import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
/**
* The Class GeoentworkInstanceGuavaCache.
* The Class GeonetworkInstanceCache.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 2, 2018
* Nov 5, 2018
*/
public class GeonetworkInstanceGuavaCache {
public class GeonetworkInstanceCache {
private static Logger logger = LoggerFactory.getLogger(GeonetworkInstanceGuavaCache.class);
private static Logger logger = LoggerFactory.getLogger(GeonetworkInstanceCache.class);
//A cache (Scope, GeonetworkInstance)
private static LoadingCache<String, GeonetworkInstance> geonetworkInstancesCache;
@ -39,14 +40,14 @@ public class GeonetworkInstanceGuavaCache {
@Override
public GeonetworkInstance load(String scope)
throws Exception {
logger.info(GeonetworkInstanceGuavaCache.class.getSimpleName() +" loaded");
logger.info(GeonetworkInstanceCache.class.getSimpleName() +" loaded");
return loadGeonetworkInstance(scope);
}
};
RemovalListener<String, GeonetworkInstance> removalListener = new RemovalListener<String, GeonetworkInstance>() {
public void onRemoval(RemovalNotification<String, GeonetworkInstance> removal) {
logger.info(GeonetworkInstanceGuavaCache.class.getSimpleName() +" cache expired");
logger.info(GeonetworkInstanceCache.class.getSimpleName() +" cache expired");
}
};
@ -55,7 +56,7 @@ public class GeonetworkInstanceGuavaCache {
1, TimeUnit.DAYS).removalListener(removalListener).
build(loader);
logger.info(GeonetworkInstanceGuavaCache.class.getSimpleName() +" instancied");
logger.info(GeonetworkInstanceCache.class.getSimpleName() +" instancied");
}

View File

@ -17,15 +17,16 @@ import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
/**
* The Class GisViewerApplicationHostnameGuavaCache.
* The Class GisViewerApplicationURLCache.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 2, 2018
* Nov 5, 2018
*/
public class GisViewerApplicationURLGuavaCache {
public class GisViewerApplicationURLCache {
private static Logger logger = LoggerFactory.getLogger(GisViewerApplicationURLGuavaCache.class);
private static Logger logger = LoggerFactory.getLogger(GisViewerApplicationURLCache.class);
//A cache (Scope, GisViewerApplication-URL)
private static LoadingCache<String, String> gisViewerApplicationURLCache;
@ -36,14 +37,14 @@ public class GisViewerApplicationURLGuavaCache {
@Override
public String load(String scope)
throws Exception {
logger.info(GisViewerApplicationURLGuavaCache.class.getSimpleName() +" loaded");
logger.info(GisViewerApplicationURLCache.class.getSimpleName() +" loaded");
return loadGisViewerApplicationURL(scope);
}
};
RemovalListener<String, String> removalListener = new RemovalListener<String, String>() {
public void onRemoval(RemovalNotification<String, String> removal) {
logger.info(GisViewerApplicationURLGuavaCache.class.getSimpleName() +" cache expired");
logger.info(GisViewerApplicationURLCache.class.getSimpleName() +" cache expired");
}
};
@ -52,7 +53,7 @@ public class GisViewerApplicationURLGuavaCache {
1, TimeUnit.DAYS).removalListener(removalListener).
build(loader);
logger.info(GisViewerApplicationURLGuavaCache.class.getSimpleName() +" instancied");
logger.info(GisViewerApplicationURLCache.class.getSimpleName() +" instancied");
}

View File

@ -12,10 +12,11 @@ import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebListener;
import org.gcube.datatransfer.resolver.caches.GeonetworkInstanceGuavaCache;
import org.gcube.datatransfer.resolver.caches.GisViewerApplicationURLGuavaCache;
import org.gcube.datatransfer.resolver.caches.GeonetworkInstanceCache;
import org.gcube.datatransfer.resolver.caches.GisViewerApplicationURLCache;
import org.gcube.datatransfer.resolver.gis.property.ApplicationProfileGenericResourceReader;
import org.gcube.datatransfer.resolver.gis.property.PropertyFileNotFoundException;
import org.slf4j.Logger;
@ -45,6 +46,11 @@ public class UriResolverStartupListener implements ServletContextListener {
protected static final String SECONDARY_TYPE = "SECONDARY_TYPE";
protected static final String APP_ID = "APP_ID";
public static final String ENV_SCOPE = "SCOPE"; //Environment Variable
private static String contextScope = null;
private static ApplicationProfileGenericResourceReader gisViewerProfile;
private static ApplicationProfileGenericResourceReader geoExplorerProfile;
@ -53,11 +59,24 @@ public class UriResolverStartupListener implements ServletContextListener {
*/
@Override
public void contextInitialized(ServletContextEvent event) {
logger.info("Context initialized!");
try {
contextScope = loadScopeFromEnvironment();
}
catch (ServletException e) {
//
logger.error(e.getMessage(), e);
}
gisViewerProfile = loadApplicationProfile(event.getServletContext(), GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES);
geoExplorerProfile = loadApplicationProfile(event.getServletContext(), GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES);
new GeonetworkInstanceGuavaCache();
new GisViewerApplicationURLGuavaCache();
new GeonetworkInstanceCache();
new GisViewerApplicationURLCache();
logger.info("Context initialized with: ");
logger.info("Scope: "+contextScope);
logger.info("GisViewerProfile: "+gisViewerProfile);
logger.info("GeoExplorerProfile: "+geoExplorerProfile);
}
/* (non-Javadoc)
@ -94,6 +113,26 @@ public class UriResolverStartupListener implements ServletContextListener {
}
/**
* Load scope from environment.
*
* @return the scope read from Environment Variable
* @throws ServletException the servlet exception
*/
public static String loadScopeFromEnvironment() throws ServletException{
logger.info("Reading Environment Variable "+ENV_SCOPE);
String scopeFromEnv = System.getenv(ENV_SCOPE);
if(scopeFromEnv == null || scopeFromEnv.isEmpty())
throw new ServletException(UriResolverStartupListener.class.getName() +" cannot read scope from Environment Variable: "+ENV_SCOPE+", It is null or empty");
logger.info("Read scope: "+scopeFromEnv+" from Environment Variable: "+ENV_SCOPE);
return scopeFromEnv;
}
/**
* Gets the gis viewer profile.
@ -118,4 +157,16 @@ public class UriResolverStartupListener implements ServletContextListener {
/**
* Gets the context scope.
*
* @return the contextScope
*/
public static String getContextScope() {
return contextScope;
}
}

View File

@ -3,46 +3,17 @@
*/
package org.gcube.datatransfer.resolver.scope;
import javax.servlet.ServletException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class ScopeUtil.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Mar 22, 2017
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 5, 2018
*/
public class ScopeUtil {
public static final String ENV_SCOPE = "SCOPE"; //Environment Variable
private static final Logger logger = LoggerFactory.getLogger(ScopeUtil.class);
public static final String SCOPE_SEPARATOR = "|";
/**
* Gets the scope from environment.
*
* @return the scope from environment
* @throws ServletException the servlet exception
*/
public static String getScopeFromEnvironment() throws ServletException{
logger.info("Reading Environment Variable "+ENV_SCOPE);
String scopeFromEnv = System.getenv(ENV_SCOPE);
if(scopeFromEnv == null || scopeFromEnv.isEmpty())
throw new ServletException(ScopeUtil.class.getName() +" cannot read scope from Environment Variable: "+ENV_SCOPE+", It is null or empty");
logger.info("Read scope: "+scopeFromEnv+" from Environment Variable: "+ENV_SCOPE);
return scopeFromEnv;
}
/**
* Normalize scope.
* Add the '/' as prefix and remove all '|'

View File

@ -18,11 +18,13 @@ import javax.ws.rs.core.Response.Status;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.ResourceCatalogueCodes;
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileNotFoundException;
import org.gcube.datatransfer.resolver.caches.CatalogueApplicationProfilesCache;
import org.gcube.datatransfer.resolver.catalogue.CatalogueRequest;
import org.gcube.datatransfer.resolver.catalogue.resource.ApplicationProfileReaderForCatalogueResolver;
import org.gcube.datatransfer.resolver.catalogue.resource.CkanCatalogueConfigurationsReader;
import org.gcube.datatransfer.resolver.catalogue.resource.GatewayCKANCatalogueReference;
import org.gcube.datatransfer.resolver.catalogue.resource.UpdateApplicationProfileCatalogueResolver;
import org.gcube.datatransfer.resolver.listeners.UriResolverStartupListener;
import org.gcube.smartgears.utils.InnerMethodName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -34,12 +36,10 @@ public class CatalogueResolver {
private static Logger logger = LoggerFactory.getLogger(CatalogueResolver.class);
public static final String ENV_SCOPE = "SCOPE"; //Environment Variable
/** The scope to enc decr. */
private String scopeToEncDecr = null;
//private String scopeToEncDecr = null;
ApplicationProfileReaderForCatalogueResolver appPrCatResolver;
//ApplicationProfileReaderForCatalogueResolver appPrCatResolver;
@GET
@Path("{entityContext:ctlg(-(o|g|p|d))?}/{vreName}/{entityName}")
@ -49,15 +49,16 @@ public class CatalogueResolver {
try {
String entityContextValue = ResourceCatalogueCodes.valueOfCodeId(entityContext).getValue();
ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(vreName, true);
//ApplicationProfileReaderForCatalogueResolver appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(vreName, true);
String fullScope = appPrCatResolver.getHashVreNameScope().get(vreName);
String fullScope = CatalogueApplicationProfilesCache.getCache().get(vreName);
logger.debug("Read fullScope: "+fullScope + " for VRE_NAME: "+vreName +" into Application Profile "+ApplicationProfileReaderForCatalogueResolver.RESOURCE_NAME);
//String fullScope = appPrCatResolver.getHashVreNameScope().get(vreName);
ScopeProvider.instance.set(fullScope);
GatewayCKANCatalogueReference ckanCatalogueReference = CkanCatalogueConfigurationsReader.loadCatalogueEndPoints();
logger.debug("Read fullScope: "+fullScope + " for VRE_NAME: "+vreName +" into Application Profile "+ApplicationProfileReaderForCatalogueResolver.RESOURCE_NAME);
//IS THE PRODUCT PLUBLIC OR PRIVATE?
//USING ACCESS TO PUBLIC PORTLET IF THE ITEM IS PUBLIC, OTHERWISE ACCESS TO PRIVATE PORTLET
String ckanPorltetUrl = ckanCatalogueReference.getPrivatePortletURL();
@ -121,9 +122,10 @@ public class CatalogueResolver {
new Thread(){
public void run() {
try {
boolean endPointUpdated = UpdateApplicationProfileCatalogueResolver.validateEndPoint(scopeToEncDecr, vreName, fullscope);
if(endPointUpdated)
appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(fullscope, true);
boolean endPointUpdated = UpdateApplicationProfileCatalogueResolver.validateEndPoint(UriResolverStartupListener.getContextScope(), vreName, fullscope);
logger.info("Is the Application profile for Catalogue Resolver updated? "+endPointUpdated);
// if(endPointUpdated)
// appPrCatResolver = new ApplicationProfileReaderForCatalogueResolver(fullscope, true);
}
catch (ApplicationProfileNotFoundException e) {
logger.error("Error during validating Application Profile", e);

View File

@ -15,9 +15,9 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datatransfer.resolver.caches.GeoExplorerApplicationURLGuavaCache;
import org.gcube.datatransfer.resolver.caches.GeonetworkInstanceGuavaCache;
import org.gcube.datatransfer.resolver.caches.GisViewerApplicationURLGuavaCache;
import org.gcube.datatransfer.resolver.caches.GeoExplorerApplicationURLCache;
import org.gcube.datatransfer.resolver.caches.GeonetworkInstanceCache;
import org.gcube.datatransfer.resolver.caches.GisViewerApplicationURLCache;
import org.gcube.datatransfer.resolver.gis.GeonetworkInstance;
import org.gcube.datatransfer.resolver.gis.MetadataConverter;
import org.gcube.datatransfer.resolver.gis.entity.GisLayerItem;
@ -155,11 +155,11 @@ public class GisResolver {
protected String getGisViewerApplicationURL(String scope) throws Exception{
logger.info("Tentative of recovering gis viewer application hostname from cache for scope: "+scope);
String gisViewerAppHostname = GisViewerApplicationURLGuavaCache.getCache().get(scope);
String gisViewerAppHostname = GisViewerApplicationURLCache.getCache().get(scope);
if(gisViewerAppHostname==null){
logger.info("Gis viewer application hostname is null, reading from application profile..");
String url = GisViewerApplicationURLGuavaCache.loadGisViewerApplicationURL(scope);
GisViewerApplicationURLGuavaCache.getCache().put(scope, url);
String url = GisViewerApplicationURLCache.loadGisViewerApplicationURL(scope);
GisViewerApplicationURLCache.getCache().put(scope, url);
logger.info("Updated GisViewerApplication cache! Scope "+scope+" linking "+url);
return url;
}else
@ -203,13 +203,13 @@ public class GisResolver {
protected GeonetworkInstance getCachedGeonetworkInstance(String scope) throws Exception{
logger.info("Attempt to get the GeonetworkInstance from cache by scope: "+scope);
GeonetworkInstance geonInstance = GeonetworkInstanceGuavaCache.getCache().get(scope);
GeonetworkInstance geonInstance = GeonetworkInstanceCache.getCache().get(scope);
if(geonInstance==null){
logger.info("GeonetworkInstance is null in cache, reading from library...");
try {
geonInstance = GeonetworkInstanceGuavaCache.loadGeonetworkInstance(scope);
GeonetworkInstanceGuavaCache.getCache().put(scope, geonInstance);
geonInstance = GeonetworkInstanceCache.loadGeonetworkInstance(scope);
GeonetworkInstanceCache.getCache().put(scope, geonInstance);
logger.info("Updated GeonetworkInstance cache! Scope "+scope+" linking "+geonInstance);
} catch (Exception e) {
logger.error("An error occurred on getting GeonetworkInstance for scope: "+scope, e);
@ -234,11 +234,11 @@ public class GisResolver {
protected String getGeoExplorerApplicationURL(String scope) throws Exception{
logger.info("Tentative of recovering geo explorer application hostname from cache for scope: "+scope);
String geoExplorerApplicationHostname = GeoExplorerApplicationURLGuavaCache.getCache().get(scope);
String geoExplorerApplicationHostname = GeoExplorerApplicationURLCache.getCache().get(scope);
if(geoExplorerApplicationHostname==null){
logger.info("GeoExplorer application hostname is null, reading from application profile..");
String url = GeoExplorerApplicationURLGuavaCache.loadGeoExplorerApplicationURL(scope);
GeoExplorerApplicationURLGuavaCache.getCache().put(scope, url);
String url = GeoExplorerApplicationURLCache.loadGeoExplorerApplicationURL(scope);
GeoExplorerApplicationURLCache.getCache().put(scope, url);
logger.info("Updated GeoExplorerApplication cache! Scope "+scope+" linking "+url);
return url;
}else