reenable XML snapshots
[org.ibex.xt.git] / src / org / ibex / xt / Prevalence.java
1 package org.ibex.xt;
2 import org.ibex.js.*;
3 import org.ibex.util.*;
4 import org.ibex.io.*;
5 import java.io.*;
6 import java.net.*;
7 import java.util.*;
8 import javax.servlet.*;
9 import javax.servlet.http.*;
10 import com.thoughtworks.xstream.*;
11 import org.prevayler.*;
12 import org.prevayler.implementation.snapshot.*;
13
14 public class Prevalence {
15
16     static final Hashtable prevaylers = new Hashtable();
17
18     public static void destroy(ServletContext cx, Prevayler prevayler) { try {
19         prevaylers.remove(cx);
20         prevayler.takeSnapshot();
21         prevayler.close();
22     } catch (Exception e) { e.printStackTrace(); } }
23
24     private static class SnapshotThread extends Thread {
25         ServletContext cx;
26         public SnapshotThread(ServletContext cx) { this.cx = cx; }
27         public void run() {
28             try {
29                 Thread.sleep(10000);
30                 Prevayler privatePrevayler = (Prevayler)prevaylers.get(cx);
31                 if (privatePrevayler == null) return;
32                 privatePrevayler.takeSnapshot();
33             } catch (Exception e) {
34                 e.printStackTrace();
35             }
36         }
37     }
38
39     public static Prevayler getPrevayler(ServletContext cx) {
40         try {
41             Prevayler prevayler;
42             synchronized(cx) {
43                 prevayler = (Prevayler)prevaylers.get(cx);
44                 if (prevayler == null) {
45                     PrevaylerFactory pf = new PrevaylerFactory();
46                     String base = cx.getRealPath("/") + "WEB-INF" + File.separatorChar + "prevalent";
47                     System.err.println("prevayling to " + base);
48                     pf.configurePrevalenceBase(base);
49                     XStreamSnapshotManager manager = new XStreamSnapshotManager(new JS(), base, null) {
50                             protected XStream createXStream() {
51                                 XStream xstream = new XStream();
52                                 xstream.alias("js", JS.class);
53                                 xstream.alias("jsdate", JSDate.class);
54                                 return xstream;
55                             }
56                         };
57                     System.err.println("configuring with " + manager);
58                     pf.configureSnapshotManager(manager);
59                     //pf.configureSnapshotManager(new SnapshotManager(new JS(), base));
60                     //pf.configureClassLoader(JSTransaction.class.getClassLoader());
61                     prevayler = pf.create();
62                     prevaylers.put(cx, prevayler);
63                     new SnapshotThread(cx).start();
64                 }
65             }
66             return prevayler;
67         } catch (Exception e) { throw new RuntimeException(e); } }
68
69     public static class JSTransaction implements Transaction {
70         public static final long serialVersionUid = 0xfb2aa281;
71         private JS js;
72         JSScope newscope;
73         Vec v;
74         public JSTransaction(JS js) throws JSExn {
75             newscope = new JSScope(null);
76             this.js = JS.cloneWithNewParentScope(js, newscope);
77             v = JS.getFormalArgs(this.js);
78             for(int i=0; i<v.size(); i++) {
79                 if ("prevalent".equals(v.elementAt(i))) continue;
80                 newscope.put(v.elementAt(i), JS.getParentScope(js).get(v.elementAt(i)));
81             }
82         }
83         public void executeOn(Object o, Date now) {
84             try {
85                 newscope.put("prevalent", o);
86                 newscope.put("now", new JSDate(now.getTime()));
87                 Object a = v.size() <= 0 ? null : newscope.get(v.elementAt(0));
88                 Object b = v.size() <= 1 ? null : newscope.get(v.elementAt(1));
89                 Object c = v.size() <= 2 ? null : newscope.get(v.elementAt(2));
90                 Object[] rest = v.size() <= 3 ? null : new Object[v.size() - 3];
91                 for(int i=3; i<v.size(); i++) rest[i-3] = v.elementAt(i);
92                 js.call(a, b, c, rest, v.size());
93             } catch (Exception e) { throw new RuntimeException(e); }
94         }
95     }
96
97     public static class JSQuery implements Query {
98         public static final long serialVersionUid = 0xfb2aa282;
99         private JS js;
100         private Object a;
101         public JSQuery(JS js, Object a) { this.js = JS.cloneWithNewParentScope(js, null); this.a = a; }
102         public Object query(Object o, Date now) {
103             try {
104                 return js.call(o, a, new JSDate(now.getTime()), null, 3);
105             } catch (Exception e) { throw new RuntimeException(e); }
106         }
107     }
108 }