/* Copyright Dassault Systemes, 1999, 2018 */
package examples.development.applications.customwebtop;

import java.io.File;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.engineous.sdk.pse.SysFiper;

public class SampleServletContextHandler implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		//Perform one-time application initialization.
		try 
		{
			// Lookup the location of the Fiper installation
			// in the environment settings, as defined
			// in the web.xml deployment descriptor.
			Context env = (Context)new InitialContext().lookup ("java:comp/env");
			String installPath = (String)env.lookup("esihome");
			String configPath = installPath + File.separatorChar + ".." + File.separatorChar + "config";
			// Init the Fiper system environment, disallow native code in server
			SysFiper.setFiperEnv(installPath, configPath, null, false);
	         
			String cprFile = (String)env.lookup("cprfile");
			// Init the Fiper ACS connection
			SysFiper.initConnection (new File(cprFile));
		}
		catch (Throwable t) {
		// The ServletContextListener interface defines no THROWS clause on
		// this method, so we can only throw runtime exceptions. Most web
		// containers will report this in their log or console file.
		throw new RuntimeException ("Failed to initialize Fiper API.", t);
		}
	}

}
