bc53366805a3750310783b8d23bf15fe1d89f7f1
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / ShellServlet.java
1 package org.ibex.xt;
2
3 import java.io.*;
4 import java.net.*;
5 import java.util.*;
6 import java.util.regex.*;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
9
10 import org.ibex.util.*;
11 import org.ibex.util.Collections;
12 import org.ibex.js.*;
13
14 import org.prevayler.*;
15
16
17 public class ShellServlet extends HttpServlet {
18     private ServletContext cx = null;
19     private Prevayler prevayler;
20     private JS prevalent;
21
22     // FIXME: destroy() counter with normal Servlet
23     public void init(ServletConfig sc) throws ServletException {
24         cx = sc.getServletContext();
25         prevayler = Prevalence.getPrevayler(cx);
26         prevalent = (JS)prevayler.prevalentSystem();
27     }
28
29     public void doPost(HttpServletRequest rq, HttpServletResponse rs) throws IOException {
30         Shell.Request r;
31         try { r = (Shell.Request)new ObjectInputStream(rq.getInputStream()).readObject(); }
32         catch (ClassNotFoundException e) {
33             e.printStackTrace();
34             throw new IOException("exception receiving request, class not found");
35         }
36
37         JSScope scope = (JSScope)rq.getSession().getAttribute("scope");
38         if (scope == null) {
39             System.out.println("creating new scope");
40             try {
41                 scope = new JSScope(null);
42                 scope.put("prevalent", prevalent);
43             } catch (JSExn e) {
44                 e.printStackTrace();
45                 throw new IOException("unexpected JSExn");
46             }
47             rq.getSession().setAttribute("scope", scope);
48         }
49         Object ret;
50         try { ret = r.process(scope); } catch (JSExn e) { ret = e; }
51         new ObjectOutputStream(rs.getOutputStream()).writeObject(ret);
52     }
53 }