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 ;
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 ;
2018-11-09 12:05:53 +01:00
import org.gcube.datatransfer.resolver.caches.LoadingVREsScopeCache ;
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 ;
2018-11-02 15:58:38 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/ * *
2018-11-15 12:18:03 +01:00
* The Class UriResolverSmartGearInit .
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 12:18:03 +01:00
public class UriResolverSmartGearInit implements ApplicationManager {
2018-11-02 15:58:38 +01:00
2018-11-15 12:18:03 +01:00
private static Logger logger = LoggerFactory . getLogger ( UriResolverSmartGearInit . class ) ;
2018-11-02 15:58:38 +01:00
public static final String GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES = " gisviewerappgenericresource.properties " ;
public static final String GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES = " geoexplorerappgenericresource.properties " ;
protected static final String SECONDARY_TYPE = " SECONDARY_TYPE " ;
protected static final String APP_ID = " APP_ID " ;
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-02 15:58:38 +01:00
2018-11-14 17:00:22 +01:00
2018-11-15 12:18:03 +01:00
/ * ( non - Javadoc )
* @see org . gcube . smartgears . ApplicationManager # onInit ( )
* /
@Override
public void onInit ( ) {
try {
if ( rootContextScope = = null ) {
logger . info ( " The RootContextScope is null, getting it from ScopeProvider " ) ;
String scope = ScopeProvider . instance . get ( ) ;
ScopeBean theScopeBean = new ScopeBean ( scope ) ;
logger . info ( " The ScopeBean is: " + theScopeBean . toString ( ) ) ;
if ( theScopeBean . is ( Type . INFRASTRUCTURE ) ) {
rootContextScope = theScopeBean . name ( ) ;
rootContextScope = rootContextScope . startsWith ( " / " ) ? rootContextScope : " / " + rootContextScope ;
logger . info ( " The RootContextScope has value: " + rootContextScope ) ;
}
} else {
//THE ROOT SCOPE has been initialized
gisViewerProfile = loadApplicationProfile ( UriResolverServletContextListener . getServletContext ( ) , GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES ) ;
geoExplorerProfile = loadApplicationProfile ( UriResolverServletContextListener . getServletContext ( ) , GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES ) ;
//init the caches
new LoadingGeonetworkInstanceCache ( ) ;
new LoadingGisViewerApplicationURLCache ( ) ;
new LoadingVREsScopeCache ( ) ;
//new LoadingCatalogueApplicationProfilesCache();
logger . info ( " Context initialized with: " ) ;
logger . info ( " Scope: " + rootContextScope ) ;
logger . info ( " GisViewerProfile [ID: " + gisViewerProfile . getAppId ( ) + " , Generic Resource Type: " + gisViewerProfile . getGenericResource ( ) + " ] " ) ;
logger . info ( " GeoExplorerProfile [ID: " + geoExplorerProfile . getAppId ( ) + " , Generic Resource Type: " + geoExplorerProfile . getGenericResource ( ) + " ] " ) ;
}
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
//
logger . error ( e . getMessage ( ) , e ) ;
}
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 {
Properties props = new Properties ( ) ;
props . load ( new FileInputStream ( new File ( realPath ) ) ) ;
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 ) {
logger . error ( " PropertyFileNotFoundException: " + contextPath , ex ) ;
} catch ( IOException e ) {
logger . error ( " Error on loading property from: " + contextPath , e ) ;
}
return null ;
}
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 {
logger . info ( " Reading Environment Variable " + ENV_SCOPE ) ;
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
logger . info ( " Read scope: " + scopeFromEnv + " from Environment Variable: " + ENV_SCOPE ) ;
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-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
}
2018-11-15 12:18:03 +01:00
}