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