172 lines
5.3 KiB
Java
172 lines
5.3 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.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;
|
|
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 contextScope = null;
|
|
|
|
private static ApplicationProfileGenericResourceReader gisViewerProfile;
|
|
private static ApplicationProfileGenericResourceReader geoExplorerProfile;
|
|
|
|
/* (non-Javadoc)
|
|
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
|
|
*/
|
|
@Override
|
|
public void contextInitialized(ServletContextEvent event) {
|
|
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 GeonetworkInstanceCache();
|
|
new GisViewerApplicationURLCache();
|
|
|
|
logger.info("Context initialized with: ");
|
|
logger.info("Scope: "+contextScope);
|
|
logger.info("GisViewerProfile: "+gisViewerProfile);
|
|
logger.info("GeoExplorerProfile: "+geoExplorerProfile);
|
|
}
|
|
|
|
/* (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;
|
|
}
|
|
|
|
|
|
/**
|
|
* 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 ApplicationProfileGenericResourceReader getGisViewerProfile() {
|
|
|
|
return gisViewerProfile;
|
|
}
|
|
|
|
|
|
/**
|
|
* Gets the geo explorer profile.
|
|
*
|
|
* @return the geoExplorerProfile
|
|
*/
|
|
public static ApplicationProfileGenericResourceReader getGeoExplorerProfile() {
|
|
|
|
return geoExplorerProfile;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Gets the context scope.
|
|
*
|
|
* @return the contextScope
|
|
*/
|
|
public static String getContextScope() {
|
|
|
|
return contextScope;
|
|
}
|
|
|
|
|
|
|
|
} |