package com.collective.celos.server;

import com.collective.celos.servlet.*;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import java.io.File;
import java.util.Map;

public class CelosServer {

    private Server server;

    public Integer startServer(int port, Map<String, String> jsVariables, String workflowConfigurationPath, String defaultsConfigurationPath, String stateDatabasePath) throws Exception {
        if (port > 0) {
            server = new Server(port);
        } else {
            server = new Server();
        }

        setupContext(jsVariables, workflowConfigurationPath, defaultsConfigurationPath, stateDatabasePath);

        ServerConnector connector = new ServerConnector(server);
        server.setConnectors(new Connector[] { connector });
        server.start();

        return connector.getLocalPort();
    }

    public Integer startServer(Map<String, String> jsVariables, String workflowConfigurationPath, String defaultsConfigurationPath, String stateDatabasePath) throws Exception {
        return startServer(-1, jsVariables, workflowConfigurationPath, defaultsConfigurationPath, stateDatabasePath);
    }


    private void setupContext(Map<String, String> jsVariables, String workflowConfigurationPath, String defaultsConfigurationPath, String stateDatabasePath) {
        assureDirIsCreated(workflowConfigurationPath);
        assureDirIsCreated(defaultsConfigurationPath);
        assureDirIsCreated(stateDatabasePath);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);

        context.addServlet(new ServletHolder(new SchedulerServlet()), "/scheduler");
        context.addServlet(new ServletHolder(new JSONWorkflowListServlet()), "/workflow-list");
        context.addServlet(new ServletHolder(new JSONWorkflowServlet()), "/workflow");
        context.addServlet(new ServletHolder(new JSONSlotStateServlet()), "/slot-state");
        context.addServlet(new ServletHolder(new RerunServlet()), "/rerun");
        context.addServlet(new ServletHolder(new WorkflowJSConfigServlet()), "/workflow-file");
        context.addServlet(new ServletHolder(new ClearCacheServlet()), "/clear-cache");

        context.setInitParameter(AbstractServlet.WORKFLOW_CONFIGURATION_PATH_ATTR, workflowConfigurationPath);
        context.setInitParameter(AbstractServlet.DEFAULTS_CONFIGURATION_PATH_ATTR, defaultsConfigurationPath);
        context.setInitParameter(AbstractServlet.STATE_DATABASE_PATH_ATTR, stateDatabasePath);
        context.setAttribute(AbstractServlet.ADDITIONAL_JS_VARIABLES, jsVariables);
    }

    public void stopServer() throws Exception {
        server.stop();
        server.destroy();
        server.join();
    }

    private void assureDirIsCreated(String paramPath) {
        File path = new File(paramPath);
        path.mkdirs();
    }

}
@@ -305,16 +305,63 @@
 tHolder;
+%0Aimport org.eclipse.jetty.webapp.WebAppContext;
 %0A%0Aimport
@@ -455,1093 +455,203 @@
 rver
-;%0A%0A    public Integer startServer(int port, Map%3CString, String%3E jsVariables, String workflowConfigurationPath, String defaultsConfigurationPath, String stateDatabasePath) throws Exception %7B%0A        if (port %3E 0) %7B%0A            server = new Server(port);%0A        %7D else %7B%0A            server = new Server();%0A        %7D%0A%0A        setupContext(jsVariables, workflowConfigurationPath, defaultsConfigurationPath, stateDatabasePath);%0A%0A        ServerConnector connector = new ServerConnector(server);%0A        server.setConnectors(new Connector%5B%5D %7B connector %7D);%0A        server.start();%0A%0A        return connector.getLocalPort();%0A    %7D%0A%0A    public Integer startServer(Map%3CString, String%3E jsVariables, String workflowConfigurationPath, String defaultsConfigurationPath, String stateDatabasePath) throws Exception %7B%0A        return startServer(-1, jsVariables, workflowConfigurationPath, defaultsConfigurationPath, stateDatabasePath);%0A    %7D%0A%0A%0A    private void setupContext(Map%3CString, String%3E jsVariables, String workflowConfigurationPath, String defaultsConfigurationPath, String stateDatabasePath) %7B
+ = new Server();%0A%0A    public Integer startServer(Map%3CString, String%3E jsVariables, String workflowConfigurationPath, String defaultsConfigurationPath, String stateDatabasePath) throws Exception %7B%0A
 %0A   
@@ -818,138 +818,149 @@
    S
-ervletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);%0A        context.set
+tring rootPath = getClass().getClassLoader().getResource(%22.%22).toString();%0A        WebAppContext context = new WebApp
 Context
+(root
 Path
-(
+, 
 %22/%22);%0A
+%0A
     
@@ -1004,620 +1004,232 @@
     
-context.addServlet(new ServletHolder(new SchedulerServlet()), %22/scheduler%22);%0A        context.addServlet(new ServletHolder(new JSONWorkflowListServlet()), %22/workflow-list%22);%0A        context.addServlet(new ServletHolder(new JSONWorkflowServlet()), %22/workflow%22);%0A        context.addServlet(new ServletHolder(new JSONSlotStateServlet()), %22/slot-state%22);%0A        context.addServlet(new ServletHolder(new RerunServlet()), %22/rerun%22);%0A        context.addServlet(new ServletHolder(new WorkflowJSConfigServlet()), %22/workflow-file%22);%0A        context.addServlet(new ServletHolder(new ClearCacheServlet()), %22/clear-cache%22
+ServerConnector connector = new ServerConnector(server);%0A        server.setConnectors(new Connector%5B%5D%7Bconnector%7D);%0A        server.start();%0A%0A        context.setAttribute(AbstractServlet.ADDITIONAL_JS_VARIABLES, jsVariables
 );%0A
-%0A
     
@@ -1537,32 +1537,33 @@
 abasePath);%0A
+%0A
         
 context.setA
@@ -1554,84 +1554,42 @@
     
-context.setAttribute(AbstractServlet.ADDITIONAL_JS_VARIABLES, jsVariables
+return connector.getLocalPort(
 );%0A
+%0A
     

use web.xml in CelosServer