uri-resolver/src/main/java/org/gcube/datatransfer/resolver/listeners/UriResolverStartupListener....

179 lines
6.1 KiB
Java

/**
*
*/
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;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebListener;
//import org.gcube.datatransfer.resolver.caches.LoadingCatalogueApplicationProfilesCache;
import org.gcube.datatransfer.resolver.caches.LoadingGeonetworkInstanceCache;
import org.gcube.datatransfer.resolver.caches.LoadingGisViewerApplicationURLCache;
import org.gcube.datatransfer.resolver.caches.LoadingVREsScopeCache;
import org.gcube.datatransfer.resolver.gis.property.ApplicationProfilePropertyReader;
import org.gcube.datatransfer.resolver.gis.property.PropertyFileNotFoundException;
import org.gcube.smartgears.ContextProvider;
import org.gcube.smartgears.context.application.ApplicationContext;
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";
public static final String ENV_SCOPE = "SCOPE"; //Environment Variable
private static String rootContextScope = null;
private static ApplicationProfilePropertyReader gisViewerProfile;
private static ApplicationProfilePropertyReader geoExplorerProfile;
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent event) {
try {
rootContextScope = loadScopeFromEnvironment();
ApplicationContext ctx = ContextProvider.get();
String rootScope = ctx.container().configuration().infrastructure();
logger.info("The ContextProvider returned the infrastructure name: "+rootScope);
}
catch (Exception 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);
//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()+"]");
}
/* (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 ApplicationProfilePropertyReader 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 ApplicationProfilePropertyReader(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;
}
/**
* 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.
*
* @return the gis viewer profile
*/
public static ApplicationProfilePropertyReader getGisViewerProfile() {
return gisViewerProfile;
}
/**
* Gets the geo explorer profile.
*
* @return the geoExplorerProfile
*/
public static ApplicationProfilePropertyReader getGeoExplorerProfile() {
return geoExplorerProfile;
}
/**
* Gets the root context scope.
*
* @return the root context scope
*/
public static String getRootContextScope() {
return rootContextScope;
}
}