initial framwork for xt shell
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / ShellServlet.java
diff --git a/src/java/org/ibex/xt/ShellServlet.java b/src/java/org/ibex/xt/ShellServlet.java
new file mode 100644 (file)
index 0000000..bc53366
--- /dev/null
@@ -0,0 +1,53 @@
+package org.ibex.xt;
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import java.util.regex.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+import org.ibex.util.*;
+import org.ibex.util.Collections;
+import org.ibex.js.*;
+
+import org.prevayler.*;
+
+
+public class ShellServlet extends HttpServlet {
+    private ServletContext cx = null;
+    private Prevayler prevayler;
+    private JS prevalent;
+
+    // FIXME: destroy() counter with normal Servlet
+    public void init(ServletConfig sc) throws ServletException {
+        cx = sc.getServletContext();
+        prevayler = Prevalence.getPrevayler(cx);
+        prevalent = (JS)prevayler.prevalentSystem();
+    }
+
+    public void doPost(HttpServletRequest rq, HttpServletResponse rs) throws IOException {
+        Shell.Request r;
+        try { r = (Shell.Request)new ObjectInputStream(rq.getInputStream()).readObject(); }
+        catch (ClassNotFoundException e) {
+            e.printStackTrace();
+            throw new IOException("exception receiving request, class not found");
+        }
+
+        JSScope scope = (JSScope)rq.getSession().getAttribute("scope");
+        if (scope == null) {
+            System.out.println("creating new scope");
+            try {
+                scope = new JSScope(null);
+                scope.put("prevalent", prevalent);
+            } catch (JSExn e) {
+                e.printStackTrace();
+                throw new IOException("unexpected JSExn");
+            }
+            rq.getSession().setAttribute("scope", scope);
+        }
+        Object ret;
+        try { ret = r.process(scope); } catch (JSExn e) { ret = e; }
+        new ObjectOutputStream(rs.getOutputStream()).writeObject(ret);
+    }
+}