uri-resolver/src/main/java/org/gcube/datatransfer/resolver/init/UriResolverSmartGearInit.java

187 lines
5.8 KiB
Java

/**
*
*/
package org.gcube.datatransfer.resolver.init;
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.ServletException;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type;
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.ApplicationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class UriResolverSmartGearInit.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Nov 15, 2018
*/
public class UriResolverSmartGearInit implements ApplicationManager {
private static Logger logger = LoggerFactory.getLogger(UriResolverSmartGearInit.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 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()+"]");
}
}
catch (Exception e) {
//
logger.error(e.getMessage(), e);
}
}
/* (non-Javadoc)
* @see org.gcube.smartgears.ApplicationManager#onShutdown()
*/
@Override
public void onShutdown() {
// TODO Auto-generated method stub
}
/**
* 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(UriResolverServletContextListener.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;
}
}