2018-11-02 15:58:38 +01:00
/ * *
*
* /
2018-11-15 12:18:03 +01:00
package org.gcube.datatransfer.resolver.init ;
2018-11-02 15:58:38 +01:00
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
import java.util.Properties ;
2020-06-18 15:48:47 +02:00
import java.util.Set ;
2018-11-02 15:58:38 +01:00
import javax.servlet.ServletContext ;
2018-11-05 15:45:31 +01:00
import javax.servlet.ServletException ;
2018-11-02 15:58:38 +01:00
2018-11-15 12:18:03 +01:00
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.common.scope.impl.ScopeBean.Type ;
2018-11-05 15:58:49 +01:00
import org.gcube.datatransfer.resolver.caches.LoadingGeonetworkInstanceCache ;
import org.gcube.datatransfer.resolver.caches.LoadingGisViewerApplicationURLCache ;
2020-06-18 15:48:47 +02:00
import org.gcube.datatransfer.resolver.caches.LoadingMapOfDetachedVRE ;
2019-05-16 12:54:20 +02:00
import org.gcube.datatransfer.resolver.caches.LoadingMapOfScopeCache ;
2018-11-06 17:26:44 +01:00
import org.gcube.datatransfer.resolver.gis.property.ApplicationProfilePropertyReader ;
2018-11-02 15:58:38 +01:00
import org.gcube.datatransfer.resolver.gis.property.PropertyFileNotFoundException ;
2018-11-15 12:18:03 +01:00
import org.gcube.smartgears.ApplicationManager ;
2020-06-18 15:48:47 +02:00
import org.gcube.smartgears.ContextProvider ;
import org.gcube.smartgears.context.application.ApplicationContext ;
2018-11-02 15:58:38 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/ * *
2018-11-15 15:35:54 +01:00
* The Class UriResolverSmartGearManagerInit .
2018-11-02 15:58:38 +01:00
*
2018-11-15 12:18:03 +01:00
* @author Francesco Mangiacrapa at ISTI - CNR ( francesco . mangiacrapa @isti.cnr.it )
* Nov 15 , 2018
2018-11-02 15:58:38 +01:00
* /
2018-11-15 15:35:54 +01:00
public class UriResolverSmartGearManagerInit implements ApplicationManager {
2018-11-02 15:58:38 +01:00
2018-11-15 15:35:54 +01:00
private static Logger log = LoggerFactory . getLogger ( UriResolverSmartGearManagerInit . class ) ;
2018-11-02 15:58:38 +01:00
2018-11-26 12:56:51 +01:00
public static final String GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES_FILENAME = " gisviewerappgenericresource.properties " ;
public static final String GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES_FILENAME = " geoexplorerappgenericresource.properties " ;
public static final String PARTHENOS_VRE_PROPERTIES_FILENAME = " parthenosvre.properties " ;
2018-11-02 15:58:38 +01:00
protected static final String SECONDARY_TYPE = " SECONDARY_TYPE " ;
protected static final String APP_ID = " APP_ID " ;
2018-11-26 12:56:51 +01:00
protected static final String VRE_NAME = " VRE_NAME " ;
2018-11-02 15:58:38 +01:00
2018-11-05 15:45:31 +01:00
public static final String ENV_SCOPE = " SCOPE " ; //Environment Variable
2018-11-06 17:09:38 +01:00
private static String rootContextScope = null ;
2018-11-05 15:45:31 +01:00
2018-11-06 17:26:44 +01:00
private static ApplicationProfilePropertyReader gisViewerProfile ;
private static ApplicationProfilePropertyReader geoExplorerProfile ;
2018-11-26 12:56:51 +01:00
private static String parthenosVREName ;
2018-11-02 15:58:38 +01:00
2018-11-15 12:18:03 +01:00
/ * ( non - Javadoc )
* @see org . gcube . smartgears . ApplicationManager # onInit ( )
* /
@Override
public void onInit ( ) {
2018-11-15 16:21:31 +01:00
log . info ( " init called with scope: " + ScopeProvider . instance . get ( ) ) ;
2018-11-15 12:18:03 +01:00
try {
2018-11-15 16:21:31 +01:00
boolean initRootContextPerformed = false ;
2018-11-15 15:35:54 +01:00
2018-11-15 12:18:03 +01:00
if ( rootContextScope = = null ) {
2018-11-15 16:21:31 +01:00
log . debug ( " The rootContextScope is null, getting it from ScopeProvider " ) ;
2018-11-15 12:18:03 +01:00
String scope = ScopeProvider . instance . get ( ) ;
ScopeBean theScopeBean = new ScopeBean ( scope ) ;
2018-11-15 16:21:31 +01:00
log . debug ( " The ScopeBean is: " + theScopeBean . toString ( ) ) ;
2018-11-15 12:18:03 +01:00
if ( theScopeBean . is ( Type . INFRASTRUCTURE ) ) {
rootContextScope = theScopeBean . name ( ) ;
rootContextScope = rootContextScope . startsWith ( " / " ) ? rootContextScope : " / " + rootContextScope ;
2018-11-15 16:21:31 +01:00
log . info ( " The rootContextScope has value: " + rootContextScope ) ;
2018-11-15 15:35:54 +01:00
//THE ROOT SCOPE has been initialized
2018-11-15 16:21:31 +01:00
initRootContextPerformed = true ;
2018-11-15 12:18:03 +01:00
}
2018-11-15 15:35:54 +01:00
}
2018-11-15 12:18:03 +01:00
2018-11-15 16:21:31 +01:00
//JUST ONCE AND TO BE SURE WITH THE ROOT SCOPE INITIALIZED
if ( initRootContextPerformed & & ( gisViewerProfile = = null | | geoExplorerProfile = = null ) ) {
log . info ( " init Profiles... " ) ;
2018-11-26 12:56:51 +01:00
gisViewerProfile = loadApplicationProfile ( UriResolverServletContextListener . getServletContext ( ) , GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES_FILENAME ) ;
geoExplorerProfile = loadApplicationProfile ( UriResolverServletContextListener . getServletContext ( ) , GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES_FILENAME ) ;
parthenosVREName = loadPartheosVREName ( UriResolverServletContextListener . getServletContext ( ) , PARTHENOS_VRE_PROPERTIES_FILENAME ) ;
log . info ( " GisViewerProfile [ID: " + gisViewerProfile . getAppId ( ) + " , Generic Resource Type: " + gisViewerProfile . getGenericResource ( ) + " ] loaded from " + GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES_FILENAME ) ;
log . info ( " GeoExplorerProfile [ID: " + geoExplorerProfile . getAppId ( ) + " , Generic Resource Type: " + geoExplorerProfile . getGenericResource ( ) + " ] loaded from " + GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES_FILENAME ) ;
log . info ( " PARTHENOS " + VRE_NAME + " is ' " + parthenosVREName + " ' loaded from " + PARTHENOS_VRE_PROPERTIES_FILENAME ) ;
2018-11-15 15:35:54 +01:00
}
2018-11-15 16:21:31 +01:00
//JUST ONCE AND TO BE SURE WITH THE ROOT SCOPE INITIALIZED
if ( initRootContextPerformed & & gisViewerProfile ! = null & & geoExplorerProfile ! = null ) {
log . info ( " Pre-Loading caches... using rootContextScope: " + rootContextScope ) ;
2020-06-18 15:48:47 +02:00
2018-11-15 12:18:03 +01:00
//init the caches
new LoadingGeonetworkInstanceCache ( ) ;
new LoadingGisViewerApplicationURLCache ( ) ;
2019-05-16 12:54:20 +02:00
new LoadingMapOfScopeCache ( ) ;
2020-06-18 15:48:47 +02:00
new LoadingMapOfDetachedVRE ( ) ;
2018-11-15 12:18:03 +01:00
}
2018-11-05 15:45:31 +01:00
}
2018-11-05 17:52:43 +01:00
catch ( Exception e ) {
2018-11-05 15:45:31 +01:00
//
2018-11-15 15:35:54 +01:00
log . error ( e . getMessage ( ) , e ) ;
2018-11-05 15:45:31 +01:00
}
2018-11-02 15:58:38 +01:00
}
2018-11-15 12:18:03 +01:00
/ * ( non - Javadoc )
* @see org . gcube . smartgears . ApplicationManager # onShutdown ( )
* /
@Override
public void onShutdown ( ) {
// TODO Auto-generated method stub
}
2018-11-02 15:58:38 +01:00
/ * *
* Gets the currency .
*
* @param context the context
* @param propertyFileName the property file name
* @return the currency
* /
2018-11-06 17:26:44 +01:00
private static ApplicationProfilePropertyReader loadApplicationProfile ( ServletContext context , String propertyFileName ) {
2018-11-02 15:58:38 +01:00
String contextPath = " /WEB-INF/property/ " + propertyFileName ;
String realPath = context . getRealPath ( contextPath ) ;
try {
2018-11-06 17:26:44 +01:00
return new ApplicationProfilePropertyReader ( new FileInputStream ( new File ( realPath ) ) ) ;
2018-11-02 15:58:38 +01:00
} catch ( PropertyFileNotFoundException | FileNotFoundException ex ) {
2018-11-15 15:35:54 +01:00
log . error ( " PropertyFileNotFoundException: " + contextPath , ex ) ;
2018-11-26 12:56:51 +01:00
}
return null ;
}
/ * *
* Load partheos vre name .
*
* @param context the context
* @param propertyFileName the property file name
* /
private static String loadPartheosVREName ( ServletContext context , String propertyFileName ) {
String contextPath = " /WEB-INF/property/ " + propertyFileName ;
String realPath = context . getRealPath ( contextPath ) ;
try {
Properties props = new Properties ( ) ;
props . load ( new FileInputStream ( new File ( realPath ) ) ) ;
parthenosVREName = props . getProperty ( VRE_NAME ) ;
return parthenosVREName ;
} catch ( FileNotFoundException ex ) {
log . error ( " PropertyFileNotFoundException: " + contextPath , ex ) ;
2018-11-02 15:58:38 +01:00
} catch ( IOException e ) {
2018-11-15 15:35:54 +01:00
log . error ( " Error on loading property from: " + contextPath , e ) ;
2018-11-02 15:58:38 +01:00
}
return null ;
2018-11-26 12:56:51 +01:00
2018-11-02 15:58:38 +01:00
}
2018-11-05 15:45:31 +01:00
/ * *
* Load scope from environment .
*
* @return the scope read from Environment Variable
* @throws ServletException the servlet exception
* /
public static String loadScopeFromEnvironment ( ) throws ServletException {
2018-11-15 15:35:54 +01:00
log . info ( " Reading Environment Variable " + ENV_SCOPE ) ;
2018-11-05 15:45:31 +01:00
String scopeFromEnv = System . getenv ( ENV_SCOPE ) ;
if ( scopeFromEnv = = null | | scopeFromEnv . isEmpty ( ) )
2018-11-15 12:18:03 +01:00
throw new ServletException ( UriResolverServletContextListener . class . getName ( ) + " cannot read scope from Environment Variable: " + ENV_SCOPE + " , It is null or empty " ) ;
2018-11-05 15:45:31 +01:00
2018-11-15 15:35:54 +01:00
log . info ( " Read scope: " + scopeFromEnv + " from Environment Variable: " + ENV_SCOPE ) ;
2018-11-05 15:45:31 +01:00
return scopeFromEnv ;
}
2018-11-02 15:58:38 +01:00
/ * *
* Gets the gis viewer profile .
*
* @return the gis viewer profile
* /
2018-11-06 17:26:44 +01:00
public static ApplicationProfilePropertyReader getGisViewerProfile ( ) {
2018-11-02 15:58:38 +01:00
return gisViewerProfile ;
}
/ * *
* Gets the geo explorer profile .
*
* @return the geoExplorerProfile
* /
2018-11-06 17:26:44 +01:00
public static ApplicationProfilePropertyReader getGeoExplorerProfile ( ) {
2018-11-02 15:58:38 +01:00
return geoExplorerProfile ;
}
2018-11-26 12:56:51 +01:00
/ * *
* Gets the parthenos vre name .
*
* @return the parthenosVREName
* /
public static String getParthenosVREName ( ) {
return parthenosVREName ;
}
2018-11-05 15:45:31 +01:00
/ * *
2018-11-06 17:09:38 +01:00
* Gets the root context scope .
2018-11-05 15:45:31 +01:00
*
2018-11-06 17:09:38 +01:00
* @return the root context scope
2018-11-05 15:45:31 +01:00
* /
2018-11-06 17:09:38 +01:00
public static String getRootContextScope ( ) {
2018-11-05 15:45:31 +01:00
2018-11-06 17:09:38 +01:00
return rootContextScope ;
2018-11-05 15:45:31 +01:00
}
2019-05-16 12:54:20 +02:00
public static void setRootContextScope ( String rootContextScope ) {
UriResolverSmartGearManagerInit . rootContextScope = rootContextScope ;
}
2020-06-18 15:48:47 +02:00
/ * *
* Gets the root token from SG configurations .
*
* @return the root token from SG configurations
* /
public static String getRootTokenFromSGConfiguration ( ) {
try {
ApplicationContext ctx = ContextProvider . get ( ) ;
Set < String > tokens = ctx . configuration ( ) . startTokens ( ) ;
for ( String token : tokens ) {
//I'm using the first token,
//it should be a token of root because of URI-Resolver works at root level
log . info ( " Token read from SG configuration is {}-MASKED-TOKEN " , token . substring ( 0 , 10 ) ) ;
return token ;
}
log . warn ( " Token not found by reading the SG configurations!!! Returning null " ) ;
return null ;
} catch ( Exception e ) {
log . error ( " Error on reading the start tokens from SG configuration " , e ) ;
return null ;
}
}
2018-11-15 12:18:03 +01:00
}