// Path: src/main/java/it/unipi/di/acube/smaph/servlet/SmaphContextListener.java
// public class SmaphContextListener implements ServletContextListener {
// 	private final static Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
// 	public final static String WIKI_PAGES_DB = "it.unipi.di.acube.smaph.wiki-pages-db";
// 	public final static String FREEBASE_DIR = "it.unipi.di.acube.smaph.wiki-to-freebase-db";
// 	public final static String  ENTITY_TO_ANCHORS_DB = "it.unipi.di.acube.smaph.entity-to-anchors-db";
// 	public final static String  WAT_GCUBE_TOKEN = "it.unipi.di.acube.smaph.wat-gcube-token";
// 	@Override
// 	public void contextInitialized(ServletContextEvent e) {
// 		LOG.info("Creating Smaph context.");
// 		ServletContext context = e.getServletContext();
// 		context.setAttribute("nif-parser", new TurtleNIFDocumentParser());
// 		context.setAttribute("nif-creator", new TurtleNIFDocumentCreator());
// 		String watGcubeToken = context.getInitParameter(WAT_GCUBE_TOKEN);
// 		SmaphBuilder.initialize(watGcubeToken);
// 		try {
// 			context.setAttribute("wikipedia-api", new WikipediaInterfaceWAT.WikipediaInterfaceWATBuilder().gcubeToken(watGcubeToken).build());
// 		} catch (URISyntaxException e1) {
// 			e1.printStackTrace();
// 		}
// 		
// 		context.setAttribute("wiki-to-freebase", WikipediaToFreebase.open(context.getInitParameter(FREEBASE_DIR)));
// 		context.setAttribute("entity-to-anchors", EntityToAnchors.fromDB(context.getInitParameter(ENTITY_TO_ANCHORS_DB)));
// 	}
// 
// 	@Override
// 	public void contextDestroyed(ServletContextEvent arg0) {
// 		LOG.info("Destroying Smaph context.");
// 	}
// }


import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.ws.rs.ProcessingException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.StaticHttpHandler;
import org.glassfish.grizzly.servlet.ServletRegistration;
import org.glassfish.grizzly.servlet.WebappContext;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import it.unipi.di.acube.smaph.servlet.SmaphContextListener;
package it.unipi.di.acube.smaph.server;





public class ServerMain {
	private final static Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

	/**
	 * Starts Grizzly HTTP server exposing SMAPH JAX-RS resources.
	 * 
	 * @throws URISyntaxException
	 * @throws ProcessingException
	 */
	public static void startServer(String serverUri, Path storageBase, String watGcubeToken) throws ProcessingException, URISyntaxException {
		LOG.info("Initializing SMAPH services.");
		LOG.info("Storage path: {}", storageBase.toAbsolutePath());

		HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(new URI(serverUri));
		httpServer.getServerConfiguration().addHttpHandler(new StaticHttpHandler("src/main/webapp/"), "/*");

		WebappContext context = new WebappContext("smaph");
		ResourceConfig rc = new ResourceConfig().packages("it.unipi.di.acube.smaph.servlet");
		ServletRegistration registration = context.addServlet("ServletContainer", new ServletContainer(rc));
		registration.addMapping("/smaph/*");
		context.addListener(SmaphContextListener.class);
		context.setInitParameter(SmaphContextListener.WIKI_PAGES_DB, storageBase.resolve("mapdb/wikipedia_pages.db").toString());
		context.setInitParameter(SmaphContextListener.FREEBASE_DIR, storageBase.resolve("mapdb/freebase.db").toString());
		context.setInitParameter(SmaphContextListener.ENTITY_TO_ANCHORS_DB, storageBase.resolve("mapdb/e2a.db").toString());
		context.setInitParameter(SmaphContextListener.WAT_GCUBE_TOKEN, watGcubeToken);
		context.deploy(httpServer);
		try {
			httpServer.start();
			LOG.info("Smaph started with WADL available at " + "{}application.wadl\nPress CTRL^C (SIGINT) to terminate.",
			        serverUri);
			Thread.currentThread().join();
			LOG.info("Shutting server down..");
		} catch (Exception e) {
// Path: src/main/java/it/unipi/di/acube/smaph/servlet/SmaphContextListener.java
// public class SmaphContextListener implements ServletContextListener {
// 	private final static Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
// 	public final static String WIKI_PAGES_DB = "it.unipi.di.acube.smaph.wiki-pages-db";
// 	public final static String FREEBASE_DIR = "it.unipi.di.acube.smaph.wiki-to-freebase-db";
// 	public final static String  ENTITY_TO_ANCHORS_DB = "it.unipi.di.acube.smaph.entity-to-anchors-db";
// 	public final static String  WAT_GCUBE_TOKEN = "it.unipi.di.acube.smaph.wat-gcube-token";
// 	@Override
// 	public void contextInitialized(ServletContextEvent e) {
// 		LOG.info("Creating Smaph context.");
// 		ServletContext context = e.getServletContext();
// 		context.setAttribute("nif-parser", new TurtleNIFDocumentParser());
// 		context.setAttribute("nif-creator", new TurtleNIFDocumentCreator());
// 		String watGcubeToken = context.getInitParameter(WAT_GCUBE_TOKEN);
// 		SmaphBuilder.initialize(watGcubeToken);
// 		try {
// 			context.setAttribute("wikipedia-api", new WikipediaInterfaceWAT.WikipediaInterfaceWATBuilder().gcubeToken(watGcubeToken).build());
// 		} catch (URISyntaxException e1) {
// 			e1.printStackTrace();
// 		}
// 		
// 		context.setAttribute("wiki-to-freebase", WikipediaToFreebase.open(context.getInitParameter(FREEBASE_DIR)));
// 		context.setAttribute("entity-to-anchors", EntityToAnchors.fromDB(context.getInitParameter(ENTITY_TO_ANCHORS_DB)));
// 	}
// 
// 	@Override
// 	public void contextDestroyed(ServletContextEvent arg0) {
// 		LOG.info("Destroying Smaph context.");
// 	}
// }


// Path: src/main/java/it/unipi/di/acube/smaph/server/ServerMain.java
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.ws.rs.ProcessingException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.StaticHttpHandler;
import org.glassfish.grizzly.servlet.ServletRegistration;
import org.glassfish.grizzly.servlet.WebappContext;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import it.unipi.di.acube.smaph.servlet.SmaphContextListener;

package it.unipi.di.acube.smaph.server;





public class ServerMain {
	private final static Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

	/**
	 * Starts Grizzly HTTP server exposing SMAPH JAX-RS resources.
	 * 
	 * @throws URISyntaxException
	 * @throws ProcessingException
	 */
	public static void startServer(String serverUri, Path storageBase, String watGcubeToken) throws ProcessingException, URISyntaxException {
		LOG.info("Initializing SMAPH services.");
		LOG.info("Storage path: {}", storageBase.toAbsolutePath());

		HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(new URI(serverUri));
		httpServer.getServerConfiguration().addHttpHandler(new StaticHttpHandler("src/main/webapp/"), "/*");

		WebappContext context = new WebappContext("smaph");
		ResourceConfig rc = new ResourceConfig().packages("it.unipi.di.acube.smaph.servlet");
		ServletRegistration registration = context.addServlet("ServletContainer", new ServletContainer(rc));
		registration.addMapping("/smaph/*");
		context.addListener(SmaphContextListener.class);
		context.setInitParameter(SmaphContextListener.WIKI_PAGES_DB, storageBase.resolve("mapdb/wikipedia_pages.db").toString());
		context.setInitParameter(SmaphContextListener.FREEBASE_DIR, storageBase.resolve("mapdb/freebase.db").toString());
		context.setInitParameter(SmaphContextListener.ENTITY_TO_ANCHORS_DB, storageBase.resolve("mapdb/e2a.db").toString());
		context.setInitParameter(SmaphContextListener.WAT_GCUBE_TOKEN, watGcubeToken);
		context.deploy(httpServer);
		try {
			httpServer.start();
			LOG.info("Smaph started with WADL available at " + "{}application.wadl\nPress CTRL^C (SIGINT) to terminate.",
			        serverUri);
			Thread.currentThread().join();
			LOG.info("Shutting server down..");
		} catch (Exception e) {
			LOG.error("There was an error while starting Grizzly HTTP server.", e);