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 org.eclipse.jetty.webapp.WebAppContext;

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

public class CelosServer {

    private Server server = new Server();

    public Integer startServer(Map<String, String> jsVariables, File workflowConfigurationPath, File defaultsConfigurationPath, File stateDatabasePath) throws Exception {

        workflowConfigurationPath.mkdirs();
        defaultsConfigurationPath.mkdirs();
        stateDatabasePath.mkdirs();

        String rootPath = new File(Thread.currentThread().getContextClassLoader().getResource("WEB-INF").toURI()).getParent();
        WebAppContext context = new WebAppContext(rootPath, "/");

        server.setHandler(context);

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

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

        return connector.getLocalPort();

    }

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

}
@@ -70,16 +70,109 @@
 vlet.*;%0A
+import org.apache.commons.httpclient.URI;%0Aimport org.apache.commons.httpclient.URIException;%0A
 import o
@@ -299,112 +299,8 @@
 or;%0A
-import org.eclipse.jetty.servlet.ServletContextHandler;%0Aimport org.eclipse.jetty.servlet.ServletHolder;%0A
 impo
@@ -364,16 +364,71 @@
 o.File;%0A
+import java.nio.file.Path;%0Aimport java.nio.file.Paths;%0A
 import j
@@ -829,27 +829,39 @@
 ing 
-rootPath = new File
+webAppParent = getUriParentPath
 (Thr
@@ -938,20 +938,19 @@
 RI()
-).getParent(
+.toString()
 );%0A 
@@ -1002,16 +1002,31 @@
 ext(
-rootPath
+webAppParent.toString()
 , %22/
@@ -1720,16 +1720,320 @@
 %0A    %7D%0A%0A
+    private static String getUriParentPath(String oldUri) throws URIException %7B%0A        URI uri = new org.apache.commons.httpclient.URI(oldUri, false);%0A        Path parentPath = Paths.get(uri.getPath()).getParent();%0A        uri.setPath(parentPath.toString() + %22/%22);%0A        return uri.toString();%0A    %7D%0A%0A
     publ

fix to work with jar's root dir