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

121 lines
4.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.annotation.WebListener;
import org.gcube.datatransfer.resolver.caches.GeoentworkInstanceGuavaCache;
import org.gcube.datatransfer.resolver.caches.GisViewerApplicationHostnameGuavaCache;
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";
private static ApplicationProfileGenericResourceReader gisViewerProfile;
private static ApplicationProfileGenericResourceReader geoExplorerProfile;
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent event) {
logger.info("Context initialized!");
gisViewerProfile = loadApplicationProfile(event.getServletContext(), GIS_VIEWER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES);
geoExplorerProfile = loadApplicationProfile(event.getServletContext(), GEO_EXPLORER_GENERIC_RESOURCE_GCUBE_APPS_PROPERTIES);
new GeoentworkInstanceGuavaCache();
new GisViewerApplicationHostnameGuavaCache();
}
/* (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;
}
/**
* 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;
}
}