package name.felixbecker.jarlotte.jettyinitializer;

import name.felixbecker.jarlotte.api.JarlotteInitializer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.Jetty;
import org.eclipse.jetty.webapp.WebAppContext;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

public class Initializer implements JarlotteInitializer {

    public void initialize(File webAppDir) {

        System.out.println("I am the classloader " + getClass().getClassLoader());
        URLClassLoader u = (URLClassLoader) getClass().getClassLoader();
        for(URL url : u.getURLs()){
            System.out.println("Initializer: " + url);
        }

        System.out.println("Webapp root dir: " + webAppDir.getAbsolutePath());

        try {
            /*
            Server server = new Server(8080);

            WebAppContext webapp = new WebAppContext();
            webapp.setContextPath("/");
            webapp.setTempDirectory(webAppDir);
            webapp.setResourceBase(".");
            server.setHandler(webapp);
            server.start();
            server.join();
            */
            Server server = new Server(8080);


            WebAppContext wac = new WebAppContext();
            wac.setResourceBase(webAppDir.getAbsolutePath());
            wac.setDescriptor(webAppDir.getAbsolutePath() + "WEB-INF/web.xml");
            wac.setContextPath("/");
            wac.setParentLoaderPriority(true);

            server.setHandler(wac);

            server.start();
            server.join();
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

@@ -181,16 +181,67 @@
 .Jetty;%0A
+import org.eclipse.jetty.webapp.WebAppClassLoader;%0A
 import o
@@ -279,16 +279,16 @@
 ontext;%0A
-
 %0Aimport 
@@ -567,16 +567,29 @@
 Loader u
+rlClassLoader
  = (URLC
@@ -652,16 +652,29 @@
  url : u
+rlClassLoader
 .getURLs
@@ -842,23 +842,8 @@
 y %7B%0A
-            /*%0A
     
@@ -889,350 +889,8 @@
 );%0A%0A
-            WebAppContext webapp = new WebAppContext();%0A            webapp.setContextPath(%22/%22);%0A            webapp.setTempDirectory(webAppDir);%0A            webapp.setResourceBase(%22.%22);%0A            server.setHandler(webapp);%0A            server.start();%0A            server.join();%0A            */%0A            Server server = new Server(8080);%0A%0A%0A
     
@@ -1140,33 +1140,62 @@
 .set
-ParentLoaderPriority(true
+ClassLoader(new WebAppClassLoader(urlClassLoader, wac)
 );%0A%0A

use custom classloader to fix classloading issues