2018-11-02 15:58:38 +01:00
/ * *
*
* /
package org.gcube.datatransfer.resolver.listeners ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileNotFoundException ;
import java.io.IOException ;
import java.util.Properties ;
import javax.servlet.ServletContext ;
import javax.servlet.ServletContextEvent ;
import javax.servlet.ServletContextListener ;
2018-11-05 15:45:31 +01:00
import javax.servlet.ServletException ;
2018-11-02 15:58:38 +01:00
import javax.servlet.annotation.WebListener ;
2018-11-05 15:58:49 +01:00
import org.gcube.datatransfer.resolver.caches.LoadingCatalogueApplicationProfilesCache ;
import org.gcube.datatransfer.resolver.caches.LoadingGeonetworkInstanceCache ;
import org.gcube.datatransfer.resolver.caches.LoadingGisViewerApplicationURLCache ;
2018-11-02 15:58:38 +01:00
import org.gcube.datatransfer.resolver.gis.property.ApplicationProfileGenericResourceReader ;
import org.gcube.datatransfer.resolver.gis.property.PropertyFileNotFoundException ;
2018-11-05 17:52:43 +01: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 ;
/ * *
* The listener interface for receiving startup events .
* The class that is interested in processing a startup
* event implements this interface , and the object created
* with that class is registered with a component using the
* component ' s < code > addStartupListener < code > method . When
* the startup event occurs , that object ' s appropriate
* method is invoked .
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
* Nov 2 , 2018
* /
@WebListener
public class UriResolverStartupListener implements ServletContextListener {
private static Logger logger = LoggerFactory . getLogger ( UriResolverStartupListener . class ) ;
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
private static String contextScope = null ;
2018-11-02 15:58:38 +01:00
private static ApplicationProfileGenericResourceReader gisViewerProfile ;
private static ApplicationProfileGenericResourceReader geoExplorerProfile ;
/ * ( non - Javadoc )
* @see javax . servlet . ServletContextListener # contextInitialized ( javax . servlet . ServletContextEvent )
* /
@Override
public void contextInitialized ( ServletContextEvent event ) {
2018-11-05 15:45:31 +01:00
2018-11-05 17:52:43 +01:00
try {
2018-11-05 15:45:31 +01:00
contextScope = loadScopeFromEnvironment ( ) ;
2018-11-05 17:52:43 +01:00
ApplicationContext ctx = ContextProvider . get ( ) ;
String rootScope = ctx . container ( ) . configuration ( ) . infrastructure ( ) ;
2018-11-06 15:20:05 +01:00
logger . info ( " The ContextProvider returned the infrastructure name: " + rootScope ) ;
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
gisViewerProfile = loadApplicationProfile ( event . getServletContext ( ) , GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES ) ;
geoExplorerProfile = loadApplicationProfile ( event . getServletContext ( ) , GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES ) ;
2018-11-05 17:52:43 +01:00
//init the caches
2018-11-05 15:58:49 +01:00
new LoadingGeonetworkInstanceCache ( ) ;
new LoadingGisViewerApplicationURLCache ( ) ;
new LoadingCatalogueApplicationProfilesCache ( ) ;
2018-11-05 15:45:31 +01:00
logger . info ( " Context initialized with: " ) ;
logger . info ( " Scope: " + contextScope ) ;
2018-11-05 17:52:43 +01:00
logger . info ( " GisViewerProfile [ID: " + gisViewerProfile . getAppId ( ) + " , Generic Resource Type: " + gisViewerProfile . getGenericResource ( ) + " ] " ) ;
logger . info ( " GeoExplorerProfile [ID: " + geoExplorerProfile . getAppId ( ) + " , Generic Resource Type: " + geoExplorerProfile . getGenericResource ( ) + " ] " ) ;
2018-11-02 15:58:38 +01:00
}
/ * ( non - Javadoc )
* @see javax . servlet . ServletContextListener # contextDestroyed ( javax . servlet . ServletContextEvent )
* /
@Override
public void contextDestroyed ( ServletContextEvent event ) {
// Perform action during application's shutdown
}
/ * *
* Gets the currency .
*
* @param context the context
* @param propertyFileName the property file name
* @return the currency
* /
private static ApplicationProfileGenericResourceReader loadApplicationProfile ( 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 ) ) ) ;
return new ApplicationProfileGenericResourceReader ( new FileInputStream ( new File ( realPath ) ) ) ;
} 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 ( ) )
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 ;
}
2018-11-02 15:58:38 +01:00
/ * *
* Gets the gis viewer profile .
*
* @return the gis viewer profile
* /
public static ApplicationProfileGenericResourceReader getGisViewerProfile ( ) {
return gisViewerProfile ;
}
/ * *
* Gets the geo explorer profile .
*
* @return the geoExplorerProfile
* /
public static ApplicationProfileGenericResourceReader getGeoExplorerProfile ( ) {
return geoExplorerProfile ;
}
2018-11-05 15:45:31 +01:00
/ * *
* Gets the context scope .
*
* @return the contextScope
* /
public static String getContextScope ( ) {
return contextScope ;
}
2018-11-02 15:58:38 +01:00
}