package jp.hasc.loggerserver;
import org.eclipse.core.resources.IFolder;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class HascLoggerServer {
	public static Server server = null;
	public static int portNum = 8080;
	
	private static IFolder outputBaseFolder__;
	static public void toggleServer(IFolder f) throws Exception{
		outputBaseFolder__ = f;
		//try {
			if (server == null) {
				if( f == null)return; // f が null ならサーバ稼働させず。
				server = new Server(portNum);
				System.out.println("HascLogger Server Started on port "+portNum);

				ServletContextHandler context = new ServletContextHandler(
						ServletContextHandler.SESSIONS);
				context.setContextPath("/");
				server.setHandler(context);
				context.addServlet(
						new ServletHolder(new HascUploaderServlet()), "/*");
				context.addServlet(
						new ServletHolder(new HascUploaderServletOld()), "/old/*");

				server.start();
//				server.join();
			} else { // stop server
				server.stop();
				System.out.println("HascLogger Server Stopped.");
				server = null;
			}
		/*
		} catch (Exception e) {
			e.printStackTrace();
		}
		*/
	}
	public static IFolder getOutputBaseFolder() {
		return outputBaseFolder__;
	}
}
