package lein.ring;

import clojure.lang.RT;
import clojure.lang.IFn;
import clojure.lang.Var;
import clojure.lang.Symbol;


import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class Servlet extends GenericServlet {

    public IFn handler;

    public static Var REQUIRE = RT.var("clojure.core", "require");
    public static Var SYMBOL = RT.var("clojure.core", "symbol");
    public static Var RESOLVE = RT.var("clojure.core", "resolve");
    public static Var DEREF = RT.var("clojure.core", "deref");
    public static Symbol SERVLET_NS =  Symbol.create("ring.util.servlet");
    public static Var MAKE_SERVICE_METHOD =
        RT.var("ring.util.servlet", "make-service-method");

    static {
        REQUIRE.invoke(SERVLET_NS);
    }

    // TODO: destroy

    public Servlet() {;}
    public void init() throws ServletException {
        ServletConfig config = this.getServletConfig();
        REQUIRE.invoke(SYMBOL.invoke(config.getInitParameter("ns-name")));
        handler=(IFn) MAKE_SERVICE_METHOD.invoke(DEREF.invoke(RT.var(config.getInitParameter("ns-name"),
                                                                     config.getInitParameter("handler-name"))));

        REQUIRE.invoke(SYMBOL.invoke(config.getInitParameter("init-ns-name")));
        RT.var(config.getInitParameter("init-ns-name"), 
               config.getInitParameter("init-name")).invoke();
    }

    public void service(ServletRequest request, ServletResponse response) {
        handler.invoke(this, request, response);
    }

}

@@ -922,16 +922,17 @@
 t() %7B;%7D%0A
+%0A
     publ
@@ -1028,24 +1028,25 @@
 nfig();%0A
+%0A
         
 REQUIRE.
@@ -1041,35 +1041,33 @@
     
-REQUIRE.invoke(SYMBOL.invok
+// Handler%0A        requir
 e(co
@@ -1091,33 +1091,32 @@
 eter(%22ns-name%22))
-)
 ;%0A        handle
@@ -1120,61 +1120,30 @@
 dler
-=(IFn) MAKE_SERVICE_METHOD.invoke(DEREF.invoke(RT.var
+ = createServiceMethod
 (con
@@ -1217,39 +1217,8 @@
     
-                 
-              
 conf
@@ -1253,18 +1253,16 @@
 -name%22))
-))
 ;%0A%0A     
@@ -1268,35 +1268,30 @@
     
-REQUIRE.invoke(SYMBOL.invok
+// Init%0A        requir
 e(co
@@ -1328,17 +1328,16 @@
 -name%22))
-)
 ;%0A      
@@ -1338,22 +1338,22 @@
         
-RT.var
+invoke
 (config.
@@ -1385,17 +1385,16 @@
 -name%22),
- 
 %0A       
@@ -1442,17 +1442,8 @@
 e%22))
-.invoke()
 ;%0A  
@@ -1579,10 +1579,386 @@
 %0A    %7D%0A%0A
+    private void require(String namespace) %7B%0A        REQUIRE.invoke(SYMBOL.invoke(namespace));%0A    %7D%0A%0A    private IFn createServiceMethod(String namespace, String handler) %7B%0A        return (IFn) MAKE_SERVICE_METHOD.invoke(DEREF.invoke(RT.var(namespace, handler)));%0A    %7D%0A%0A    private void invoke(String namespace, String var) %7B%0A        RT.var(namespace, var).invoke();%0A    %7D%0A%0A
 %7D%0A

Clean up Servlet.java